Utils.cpp 4.4 KB
Newer Older
1
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
J
jinhai 已提交
2
//
3 4
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
J
jinhai 已提交
5
//
6 7 8 9 10
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
// or implied. See the License for the specific language governing permissions and limitations under the License.
J
jinhai 已提交
11

S
starlord 已提交
12
#include "db/Utils.h"
X
Xu Peng 已提交
13

S
shengjh 已提交
14
#include <fiu-local.h>
15

G
groot 已提交
16
#include <unistd.h>
S
starlord 已提交
17
#include <boost/filesystem.hpp>
X
Xu Peng 已提交
18
#include <chrono>
G
groot 已提交
19
#include <memory>
S
starlord 已提交
20
#include <mutex>
21
#include <regex>
S
starlord 已提交
22
#include <vector>
X
Xu Peng 已提交
23

C
Cai Yudong 已提交
24
#include "cache/CpuCacheMgr.h"
G
groot 已提交
25 26
#include "db/Types.h"

C
Cai Yudong 已提交
27 28 29
#ifdef MILVUS_GPU_VERSION
#include "cache/GpuCacheMgr.h"
#endif
W
Wang XiangYu 已提交
30
#include "config/ServerConfig.h"
C
Cai Yudong 已提交
31
//#include "storage/s3/S3ClientWrapper.h"
G
groot 已提交
32
#include "knowhere/index/vector_index/helpers/IndexParameter.h"
33 34 35
#include "utils/CommonUtil.h"
#include "utils/Log.h"

36 37
#include <map>

J
jinhai 已提交
38
namespace milvus {
X
Xu Peng 已提交
39 40 41
namespace engine {
namespace utils {

S
starlord 已提交
42 43
int64_t
GetMicroSecTimeStamp() {
X
Xu Peng 已提交
44
    auto now = std::chrono::system_clock::now();
S
starlord 已提交
45
    auto micros = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()).count();
X
Xu Peng 已提交
46 47 48 49

    return micros;
}

S
starlord 已提交
50
bool
51
IsSameIndex(const CollectionIndex& index1, const CollectionIndex& index2) {
G
groot 已提交
52
    return index1.index_type_ == index2.index_type_ && index1.extra_params_ == index2.extra_params_ &&
G
groot 已提交
53
           index1.metric_name_ == index2.metric_name_;
54 55
}

56
bool
G
groot 已提交
57 58 59 60
IsBinaryMetricType(const std::string& metric_type) {
    return (metric_type == knowhere::Metric::HAMMING) || (metric_type == knowhere::Metric::JACCARD) ||
           (metric_type == knowhere::Metric::SUBSTRUCTURE) || (metric_type == knowhere::Metric::SUPERSTRUCTURE) ||
           (metric_type == knowhere::Metric::TANIMOTO);
61 62
}

G
groot 已提交
63
engine::DateT
S
starlord 已提交
64
GetDate(const std::time_t& t, int day_delta) {
65 66 67 68 69 70
    struct tm ltm;
    localtime_r(&t, &ltm);
    if (day_delta > 0) {
        do {
            ++ltm.tm_mday;
            --day_delta;
S
starlord 已提交
71
        } while (day_delta > 0);
72 73 74 75 76
        mktime(&ltm);
    } else if (day_delta < 0) {
        do {
            --ltm.tm_mday;
            ++day_delta;
S
starlord 已提交
77
        } while (day_delta < 0);
78 79
        mktime(&ltm);
    }
S
starlord 已提交
80
    return ltm.tm_year * 10000 + ltm.tm_mon * 100 + ltm.tm_mday;
81 82
}

G
groot 已提交
83
engine::DateT
S
starlord 已提交
84
GetDateWithDelta(int day_delta) {
85 86 87
    return GetDate(std::time(nullptr), day_delta);
}

G
groot 已提交
88
engine::DateT
S
starlord 已提交
89
GetDate() {
90 91 92
    return GetDate(std::time(nullptr), 0);
}

93
// URI format: dialect://username:password@host:port/database
S
starlord 已提交
94
Status
S
starlord 已提交
95
ParseMetaUri(const std::string& uri, MetaUriInfo& info) {
96 97 98 99 100 101
    std::string dialect_regex = "(.*)";
    std::string username_tegex = "(.*)";
    std::string password_regex = "(.*)";
    std::string host_regex = "(.*)";
    std::string port_regex = "(.*)";
    std::string db_name_regex = "(.*)";
S
starlord 已提交
102 103
    std::string uri_regex_str = dialect_regex + "\\:\\/\\/" + username_tegex + "\\:" + password_regex + "\\@" +
                                host_regex + "\\:" + port_regex + "\\/" + db_name_regex;
104 105 106 107 108 109 110 111 112 113 114 115

    std::regex uri_regex(uri_regex_str);
    std::smatch pieces_match;

    if (std::regex_match(uri, pieces_match, uri_regex)) {
        info.dialect_ = pieces_match[1].str();
        info.username_ = pieces_match[2].str();
        info.password_ = pieces_match[3].str();
        info.host_ = pieces_match[4].str();
        info.port_ = pieces_match[5].str();
        info.db_name_ = pieces_match[6].str();

S
starlord 已提交
116
        // TODO(myh): verify host, port...
117 118 119 120 121 122 123
    } else {
        return Status(DB_INVALID_META_URI, "Invalid meta uri: " + uri);
    }

    return Status::OK();
}

G
groot 已提交
124 125 126 127 128 129 130
void
SendExitSignal() {
    LOG_SERVER_INFO_ << "Send SIGUSR2 signal to exit";
    pid_t pid = getpid();
    kill(pid, SIGUSR2);
}

G
groot 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
void
GetIDFromChunk(const engine::DataChunkPtr& chunk, engine::IDNumbers& ids) {
    ids.clear();
    if (chunk == nullptr) {
        return;
    }

    auto pair = chunk->fixed_fields_.find(engine::DEFAULT_UID_NAME);
    if (pair == chunk->fixed_fields_.end() || pair->second == nullptr) {
        return;
    }

    if (!pair->second->data_.empty()) {
        ids.resize(pair->second->data_.size() / sizeof(engine::IDNumber));
        memcpy((void*)(ids.data()), pair->second->data_.data(), pair->second->data_.size());
    }
}

S
starlord 已提交
149 150 151
}  // namespace utils
}  // namespace engine
}  // namespace milvus