VecIdMapper.cpp 5.4 KB
Newer Older
G
groot 已提交
1 2 3 4 5 6 7
/*******************************************************************************
 * Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
 * Unauthorized copying of this file, via any medium is strictly prohibited.
 * Proprietary and confidential.
 ******************************************************************************/

#include "VecIdMapper.h"
G
groot 已提交
8
#include "ServerConfig.h"
G
groot 已提交
9
#include "utils/Log.h"
G
groot 已提交
10 11 12 13 14
#include "utils/CommonUtil.h"

#include "rocksdb/db.h"
#include "rocksdb/slice.h"
#include "rocksdb/options.h"
G
groot 已提交
15

G
groot 已提交
16 17 18 19
namespace zilliz {
namespace vecwise {
namespace server {

G
groot 已提交
20
IVecIdMapper* IVecIdMapper::GetInstance() {
G
groot 已提交
21
#if 0
G
groot 已提交
22 23
    static SimpleIdMapper s_mapper;
    return &s_mapper;
G
groot 已提交
24
#else
G
groot 已提交
25
    static RocksIdMapper s_mapper;
G
groot 已提交
26 27
    return &s_mapper;
#endif
G
groot 已提交
28 29
}

G
groot 已提交
30
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
G
groot 已提交
31 32 33 34 35 36 37 38
SimpleIdMapper::SimpleIdMapper() {

}

SimpleIdMapper::~SimpleIdMapper() {

}

G
groot 已提交
39
ServerError SimpleIdMapper::Put(const std::string& nid, const std::string& sid) {
G
groot 已提交
40 41 42 43
    ids_[nid] = sid;
    return SERVER_SUCCESS;
}

G
groot 已提交
44 45
ServerError SimpleIdMapper::Put(const std::vector<std::string>& nid, const std::vector<std::string>& sid) {
    if(nid.size() != sid.size()) {
G
groot 已提交
46 47 48
        return SERVER_INVALID_ARGUMENT;
    }

G
groot 已提交
49
    for(size_t i = 0; i < nid.size(); i++) {
G
groot 已提交
50 51 52 53 54 55
        ids_[nid[i]] = sid[i];
    }

    return SERVER_SUCCESS;
}

G
groot 已提交
56
ServerError SimpleIdMapper::Get(const std::string& nid, std::string& sid) const {
G
groot 已提交
57 58 59 60 61 62
    auto iter = ids_.find(nid);
    if(iter == ids_.end()) {
        return SERVER_INVALID_ARGUMENT;
    }

    sid = iter->second;
G
groot 已提交
63

G
groot 已提交
64
    return SERVER_SUCCESS;
G
groot 已提交
65 66
}

G
groot 已提交
67
ServerError SimpleIdMapper::Get(const std::vector<std::string>& nid, std::vector<std::string>& sid) const {
G
groot 已提交
68 69 70
    sid.clear();

    ServerError err = SERVER_SUCCESS;
G
groot 已提交
71
    for(size_t i = 0; i < nid.size(); i++) {
G
groot 已提交
72 73 74
        auto iter = ids_.find(nid[i]);
        if(iter == ids_.end()) {
            sid.push_back("");
G
groot 已提交
75
            SERVER_LOG_ERROR << "ID mapper failed to find id: " << nid[i];
G
groot 已提交
76 77 78 79 80 81 82 83 84
            err = SERVER_INVALID_ARGUMENT;
            continue;
        }

        sid.push_back(iter->second);
    }

    return err;
}
G
groot 已提交
85

G
groot 已提交
86
ServerError SimpleIdMapper::Delete(const std::string& nid) {
G
groot 已提交
87 88
    ids_.erase(nid);
    return SERVER_SUCCESS;
G
groot 已提交
89 90
}

G
groot 已提交
91 92

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
G
groot 已提交
93
RocksIdMapper::RocksIdMapper() {
G
groot 已提交
94 95
    ConfigNode& config = ServerConfig::GetInstance().GetConfig(CONFIG_DB);
    std::string db_path = config.GetValue(CONFIG_DB_PATH);
G
groot 已提交
96 97 98
    db_path += "/id_mapping";
    CommonUtil::CreateDirectory(db_path);

G
groot 已提交
99 100 101 102 103 104
    rocksdb::Options options;
    // Optimize RocksDB. This is the easiest way to get RocksDB to perform well
    options.IncreaseParallelism();
    options.OptimizeLevelStyleCompaction();
    // create the DB if it's not already present
    options.create_if_missing = true;
G
groot 已提交
105
    options.max_open_files = config.GetInt32Value(CONFIG_DB_IDMAPPER_MAX_FILE, 128);
G
groot 已提交
106 107

    // open DB
G
groot 已提交
108
    rocksdb::Status s = rocksdb::DB::Open(options, db_path, &db_);
G
groot 已提交
109 110 111 112
    if(!s.ok()) {
        SERVER_LOG_ERROR << "ID mapper failed to initialize:" << s.ToString();
        db_ = nullptr;
    }
G
groot 已提交
113 114
}
RocksIdMapper::~RocksIdMapper() {
G
groot 已提交
115 116 117 118
    if(db_) {
        db_->Close();
        delete db_;
    }
G
groot 已提交
119 120
}

G
groot 已提交
121
ServerError RocksIdMapper::Put(const std::string& nid, const std::string& sid) {
G
groot 已提交
122 123 124 125
    if(db_ == nullptr) {
        return SERVER_NULL_POINTER;
    }

G
groot 已提交
126
    rocksdb::Slice key(nid);
G
groot 已提交
127 128 129 130 131 132 133
    rocksdb::Slice value(sid);
    rocksdb::Status s = db_->Put(rocksdb::WriteOptions(), key, value);
    if(!s.ok()) {
        SERVER_LOG_ERROR << "ID mapper failed to put:" << s.ToString();
        return SERVER_UNEXPECTED_ERROR;
    }

G
groot 已提交
134 135 136
    return SERVER_SUCCESS;
}

G
groot 已提交
137 138
ServerError RocksIdMapper::Put(const std::vector<std::string>& nid, const std::vector<std::string>& sid) {
    if(nid.size() != sid.size()) {
G
groot 已提交
139 140 141 142
        return SERVER_INVALID_ARGUMENT;
    }

    ServerError err = SERVER_SUCCESS;
G
groot 已提交
143
    for(size_t i = 0; i < nid.size(); i++) {
G
groot 已提交
144 145 146 147 148 149
        err = Put(nid[i], sid[i]);
        if(err != SERVER_SUCCESS) {
            return err;
        }
    }

G
groot 已提交
150
    return err;
G
groot 已提交
151 152
}

G
groot 已提交
153
ServerError RocksIdMapper::Get(const std::string& nid, std::string& sid) const {
G
groot 已提交
154 155 156 157
    if(db_ == nullptr) {
        return SERVER_NULL_POINTER;
    }

G
groot 已提交
158
    rocksdb::Slice key(nid);
G
groot 已提交
159 160 161 162 163 164
    rocksdb::Status s = db_->Get(rocksdb::ReadOptions(), key, &sid);
    if(!s.ok()) {
        SERVER_LOG_ERROR << "ID mapper failed to get:" << s.ToString();
        return SERVER_UNEXPECTED_ERROR;
    }

G
groot 已提交
165 166 167
    return SERVER_SUCCESS;
}

G
groot 已提交
168
ServerError RocksIdMapper::Get(const std::vector<std::string>& nid, std::vector<std::string>& sid) const {
G
groot 已提交
169 170 171
    sid.clear();

    ServerError err = SERVER_SUCCESS;
G
groot 已提交
172
    for(size_t i = 0; i < nid.size(); i++) {
G
groot 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185
        std::string str_id;
        ServerError temp_err = Get(nid[i], str_id);
        if(temp_err != SERVER_SUCCESS) {
            sid.push_back("");
            SERVER_LOG_ERROR << "ID mapper failed to get id: " << nid[i];
            err = temp_err;
            continue;
        }

        sid.push_back(str_id);
    }

    return err;
G
groot 已提交
186 187
}

G
groot 已提交
188
ServerError RocksIdMapper::Delete(const std::string& nid) {
G
groot 已提交
189 190 191 192
    if(db_ == nullptr) {
        return SERVER_NULL_POINTER;
    }

G
groot 已提交
193
    rocksdb::Slice key(nid);
G
groot 已提交
194 195 196 197 198 199
    rocksdb::Status s = db_->Delete(rocksdb::WriteOptions(), key);
    if(!s.ok()) {
        SERVER_LOG_ERROR << "ID mapper failed to delete:" << s.ToString();
        return SERVER_UNEXPECTED_ERROR;
    }

G
groot 已提交
200 201 202
    return SERVER_SUCCESS;
}

G
groot 已提交
203 204 205
}
}
}