-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataindex.cc
More file actions
137 lines (115 loc) · 4.07 KB
/
Copy pathdataindex.cc
File metadata and controls
137 lines (115 loc) · 4.07 KB
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
#include "dataindex.h"
namespace pmhashtable {
uint64_t DataIndex::Hash(const std::string str) {
uint64_t hashresult[2];
MurmurHash3_x64_128(str.c_str(), str.length(), SEED, hashresult);
return hashresult[0];
}
DataIndex::DataIndex(Options& opt, DataPool *dp) : arena_(opt.pm_path + opt.index_name + ".index", opt.index_size),
datapool_(dp),
index_size_(opt.index_size) {}
DataIndex::~DataIndex() {}
bool DataIndex::Write(const std::string key, const std::string value) {
uint64_t hashresult = Hash(key);
IndexNode *in = (IndexNode*)arena_.Allocate(hashresult);
int counter = 16; // align with 16B
while (in->is_valid) {
PoolNode *pn = (PoolNode*)datapool_->Translate(in->poolnode);
std::string k((char*)datapool_->Translate(pn->key), pn->key_len);
if (k == key) {
break;
} else {
// linear probe
in = (IndexNode*)arena_.Allocate(hashresult + counter);
counter += 16;
}
}
int64_t pos_in_pool = datapool_->NewNode(key, value);
in->poolnode = pos_in_pool;
in->is_valid = true;
arena_.Sync(in, sizeof(IndexNode));
return true;
}
bool DataIndex::Read(const std::string key, std::string* value) {
uint64_t hashresult = Hash(key);
IndexNode *in = (IndexNode*)arena_.Allocate(hashresult);
int counter = 16; // align with 16B
while (in->is_valid) {
PoolNode *pn = (PoolNode*)datapool_->Translate(in->poolnode);
std::string k((char*)datapool_->Translate(pn->key), pn->key_len);
if (k == key) {
*value = std::string((char*)datapool_->Translate(pn->value), pn->value_len);
return true;
} else {
// linear probe
in = (IndexNode*)arena_.Allocate(hashresult + counter);
counter += 16;
}
}
return false;
}
bool DataIndex::Delete(const std::string key) {
uint64_t hashresult = Hash(key);
IndexNode *in = (IndexNode*)arena_.Allocate(hashresult);
int counter = 16; // align with 16B
while (in->is_valid) {
PoolNode *pn = (PoolNode*)datapool_->Translate(in->poolnode);
std::string k((char*)datapool_->Translate(pn->key), pn->key_len);
if (k == key) {
in->is_valid = false;
arena_.Sync(in, sizeof(IndexNode));
return true;
} else {
in = (IndexNode*)arena_.Allocate(hashresult + counter);
counter += 16;
}
}
return false;
}
bool DataIndex::Scan(const std::string key, const int n) {
print_pos_ = 0;
key_in_scan_.clear();
value_in_scan_.clear();
uint64_t pos = 0;
while (pos < index_size_) {
IndexNode *in = (IndexNode*)arena_.Allocate(pos);
if (in->is_valid) {
PoolNode *pn = (PoolNode*)datapool_->Translate(in->poolnode);
std::string k((char*)datapool_->Translate(pn->key), pn->key_len);
if (k >= key) {
int insert_pos = std::upper_bound(key_in_scan_.begin(), key_in_scan_.end(), k) - key_in_scan_.begin();
if (insert_pos < n) {
key_in_scan_.insert(key_in_scan_.begin() + insert_pos, k);
std::string v((char*)datapool_->Translate(pn->value), pn->value_len);
value_in_scan_.insert(value_in_scan_.begin() + insert_pos, v);
if (key_in_scan_.size() > n) {
key_in_scan_.pop_back();
value_in_scan_.pop_back();
}
}
}
}
pos += 16;
}
if (key_in_scan_.size() == 0) {
return false;
} else {
return true;
}
}
std::string DataIndex::Key() {
assert(Valid());
return key_in_scan_[print_pos_];
}
std::string DataIndex::Value() {
assert(Valid());
return value_in_scan_[print_pos_];
}
bool DataIndex::Valid() {
return print_pos_ < key_in_scan_.size();
}
void DataIndex::Next() {
assert(Valid());
print_pos_++;
}
} // pmhashtable