-
Notifications
You must be signed in to change notification settings - Fork 315
/
rocksdb_raw_vector.cc
175 lines (152 loc) · 4.64 KB
/
rocksdb_raw_vector.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* Copyright 2019 The Gamma Authors.
*
* This source code is licensed under the Apache License, Version 2.0 license
* found in the LICENSE file in the root directory of this source tree.
*/
#ifdef WITH_ROCKSDB
#include "rocksdb_raw_vector.h"
#include "log.h"
#include "rocksdb/table.h"
#include "utils.h"
#include <stdio.h>
using namespace std;
using namespace rocksdb;
namespace tig_gamma {
RocksDBRawVector::RocksDBRawVector(const std::string &name, int dimension,
int max_vector_size,
const std::string &root_path,
const StoreParams &store_params)
: RawVector(name, dimension, max_vector_size, root_path),
AsyncFlusher(name) {
root_path_ = root_path;
db_ = nullptr;
raw_vector_io_ = nullptr;
store_params_ = new StoreParams(store_params);
}
RocksDBRawVector::~RocksDBRawVector() {
if (db_) {
delete db_;
}
if (raw_vector_io_)
delete raw_vector_io_;
if (store_params_)
delete store_params_;
}
int RocksDBRawVector::Init() {
block_cache_size_ = store_params_->cache_size_;
raw_vector_io_ = new RawVectorIO(this);
if (raw_vector_io_->Init(true)) {
LOG(ERROR) << "init raw vector io error";
return -1;
}
std::shared_ptr<Cache> cache = NewLRUCache(block_cache_size_);
// BlockBasedTableOptions table_options_;
table_options_.block_cache = cache;
Options options;
options.table_factory.reset(NewBlockBasedTableFactory(table_options_));
options.IncreaseParallelism();
// options.OptimizeLevelStyleCompaction();
// create the DB if it's not already present
options.create_if_missing = true;
string db_path = root_path_ + "/" + vector_name_;
if (!utils::isFolderExist(db_path.c_str())) {
mkdir(db_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
}
// open DB
Status s = DB::Open(options, db_path, &db_);
if (!s.ok()) {
LOG(ERROR) << "open rocks db error: " << s.ToString();
return -1;
}
nflushed_ = ntotal_;
LOG(INFO) << "rocks raw vector init success! name=" << vector_name_
<< ", flushed num=" << nflushed_
<< ", block cache size=" << block_cache_size_ << "m";
return 0;
}
const float *RocksDBRawVector::GetVector(long vid) const {
if (vid >= ntotal_ || vid < 0) {
return nullptr;
}
string key, value;
ToRowKey((int)vid, key);
Status s = db_->Get(ReadOptions(), Slice(key), &value);
if (!s.ok()) {
LOG(ERROR) << "rocksdb get error:" << s.ToString() << ", key=" << key;
return nullptr;
}
float *vector = new float[dimension_];
assert((size_t)vector_byte_size_ == value.size());
memcpy((void *)vector, value.data(), vector_byte_size_);
return vector;
}
int RocksDBRawVector::FlushOnce() {
int num = ntotal_ - nflushed_;
if (num > 0) {
raw_vector_io_->Dump(nflushed_, num);
}
return num;
}
int RocksDBRawVector::AddToStore(float *v, int len) {
if (v == nullptr || len != dimension_)
return -1;
string key;
ToRowKey(ntotal_, key);
Status s =
db_->Put(WriteOptions(), Slice(key), Slice((char *)v, vector_byte_size_));
if (!s.ok()) {
LOG(ERROR) << "rocksdb put error:" << s.ToString() << ", key=" << key;
return -2;
}
return 0;
}
const float *RocksDBRawVector::GetVectorHeader(int start, int end) {
if (start < 0 || start >= ntotal_ || start >= end) {
return nullptr;
}
rocksdb::Iterator *it = db_->NewIterator(rocksdb::ReadOptions());
string start_key, end_key;
ToRowKey(start, start_key);
ToRowKey(end, end_key);
it->Seek(Slice(start_key));
int num = end - start;
float *vectors = new float[(uint64_t)dimension_ * num];
for (int c = 0; c < num; c++, it->Next()) {
if (!it->Valid()) {
LOG(ERROR) << "rocksdb iterator error, count=" << c;
delete it;
return nullptr;
}
Slice value = it->value();
assert(value.size_ == (size_t)vector_byte_size_);
memcpy((void *)(vectors + (uint64_t)c * dimension_), value.data_,
vector_byte_size_);
#ifdef DEBUG
string expect_key;
ToRowKey(c + start, expect_key);
string key = it->key().ToString();
if (key != expect_key) {
LOG(ERROR) << "vid=" << c + start << ", invalid key=" << key
<< ", expect=" << expect_key;
}
#endif
}
delete it;
return vectors;
}
void RocksDBRawVector::Destroy(std::vector<const float *> &results) {
for (const float *p : results) {
delete[] p;
}
}
void RocksDBRawVector::Destroy(const float *result, bool header) {
delete[] result;
}
void RocksDBRawVector::ToRowKey(int vid, string &key) const {
char data[11];
snprintf(data, 11, "%010d", vid);
key.assign(data, 10);
}
} // namespace tig_gamma
#endif // WITH_ROCKSDB