Skip to content

Commit 339526d

Browse files
committed
Change Interface To Key Value
1 parent 80b9159 commit 339526d

File tree

3 files changed

+56
-53
lines changed

3 files changed

+56
-53
lines changed

demo/cpp_demo.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424

2525
int main(int argc, const char * argv[]) {
2626

27-
Wolf<int> wolf_home("/Users/wingerted/test_shared_memory_dat", 10000000, true);
28-
wolf_home.Add(1);
27+
Wolf<int, int> wolf_home("/Users/wingerted/test_shared_memory_dat", 10000000, true);
28+
wolf_home.Add(1, 1);
2929

3030

3131
// // insert code here...
@@ -92,10 +92,10 @@ int main(int argc, const char * argv[]) {
9292
//
9393
auto first = std::chrono::system_clock::now();
9494

95-
container::LockFreeSkipList<int> list(shm_manager2, true);
95+
container::LockFreeSkipList<int, int> list(shm_manager2, true);
9696
for (int i=0; i<20000; ++i) {
9797
//auto first = std::chrono::system_clock::now();
98-
list.Add(i);
98+
list.Add(i, i);
9999
//auto second = std::chrono::system_clock::now();
100100
// std::cout
101101
// << "Index: " << i

src/core/container/lockfree_skiplist.h

+40-37
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
namespace container{
2121

22-
template<typename Key, class Comparator = DefaultComparator<Key>>
22+
template<typename Key, typename Value, class Comparator = DefaultComparator<Key>>
2323
class LockFreeSkipList {
2424
public:
2525
const static int kMaxHeight {12};
@@ -34,13 +34,13 @@ class LockFreeSkipList {
3434
bool reset = false,
3535
Comparator cmp = DefaultComparator<Key>());
3636

37-
bool Add(Key key);
37+
bool Add(Key key, Value value);
3838
bool Remove(Key key);
39-
Node* FindLessThan(Key key);
39+
Value FindLessThan(Key key);
4040

4141
private:
4242
int RandomHeight();
43-
Node* NewNode(Key key, int height);
43+
Node* NewNode(Key key, Value value, int height);
4444
Node* GetNodeByOffset(uint32_t offset);
4545
bool FindWindow(Key key,
4646
Node** first_nodes,
@@ -55,11 +55,11 @@ class LockFreeSkipList {
5555
Random rnd_;
5656
Comparator compare_;
5757
ShmManager* shm_manager_;
58-
LockFreeSkipList<Key, Comparator>::Node* head_;
58+
LockFreeSkipList<Key, Value, Comparator>::Node* head_;
5959
};
6060

61-
template<typename Key, class Comparator>
62-
struct LockFreeSkipList<Key, Comparator>::Ref {
61+
template<typename Key, typename Value, class Comparator>
62+
struct LockFreeSkipList<Key, Value, Comparator>::Ref {
6363
Ref() = default;
6464
Ref(uint32_t offset, bool mark): next_offset(offset), mark_removed(mark) {}
6565
Ref(Node* node) {
@@ -72,11 +72,13 @@ struct LockFreeSkipList<Key, Comparator>::Ref {
7272
char padding[3] {0}; //这里需要手工初始化字节对齐的字节
7373
};
7474

75-
template<typename Key, class Comparator>
76-
struct LockFreeSkipList<Key, Comparator>::Node {
77-
Node(int in_key, uint32_t in_offset, int in_height): height(in_height),
78-
key(in_key),
79-
self_offset(in_offset) {
75+
template<typename Key, typename Value, class Comparator>
76+
struct LockFreeSkipList<Key, Value, Comparator>::Node {
77+
Node(Key in_key, Value in_value,
78+
uint32_t in_offset, int in_height): height(in_height),
79+
key(in_key),
80+
value(in_value),
81+
self_offset(in_offset) {
8082
for (int i=0; i<kMaxHeight; ++i) {
8183
this->ref[i] = {0, false};
8284
}
@@ -92,21 +94,22 @@ struct LockFreeSkipList<Key, Comparator>::Node {
9294

9395
int height;
9496
Key key;
97+
Value value;
9598
uint32_t self_offset;
9699
std::atomic<Ref> ref[kMaxHeight];
97100
};
98101

99-
template<typename Key, class Comparator>
100-
LockFreeSkipList<Key, Comparator>::LockFreeSkipList(ShmManager* shm_manager,
101-
bool reset,
102-
Comparator cmp):
102+
template<typename Key, typename Value, class Comparator>
103+
LockFreeSkipList<Key, Value, Comparator>::LockFreeSkipList(ShmManager* shm_manager,
104+
bool reset,
105+
Comparator cmp):
103106
//rnd_(static_cast<unsigned int>(std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()))),
104107
rnd_(0xdeadbeef),
105108
compare_(cmp),
106109
shm_manager_(shm_manager) {
107110

108111
if (reset) {
109-
this->head_ = this->NewNode(0, kMaxHeight);
112+
this->head_ = this->NewNode(Key(), Value(), kMaxHeight);
110113
for (int i = 0; i < kMaxHeight; i++) {
111114
this->head_->SetRef(i, {0, false});
112115
}
@@ -116,25 +119,25 @@ LockFreeSkipList<Key, Comparator>::LockFreeSkipList(ShmManager* shm_manager,
116119

117120
}
118121

119-
template<typename Key, class Comparator>
120-
auto LockFreeSkipList<Key, Comparator>::GetNodeByOffset(uint32_t offset) -> Node*{
122+
template<typename Key, typename Value, class Comparator>
123+
auto LockFreeSkipList<Key, Value, Comparator>::GetNodeByOffset(uint32_t offset) -> Node*{
121124
if (offset == 0) {
122125
return nullptr;
123126
} else {
124127
return (Node*) this->shm_manager_->GetBufferByOffset(offset);
125128
}
126129
}
127130

128-
template<typename Key, class Comparator>
129-
auto LockFreeSkipList<Key, Comparator>::NewNode(Key key, int height) -> Node* {
131+
template<typename Key, typename Value, class Comparator>
132+
auto LockFreeSkipList<Key, Value, Comparator>::NewNode(Key key, Value value, int height) -> Node* {
130133
uint32_t node_offset;
131134
char* node_buffer;
132135
std::tie(node_offset, node_buffer) = this->shm_manager_->Allocate(sizeof(Node));
133-
return new (node_buffer) Node(key, node_offset, height);
136+
return new (node_buffer) Node(key, value, node_offset, height);
134137
}
135138

136-
template<typename Key, class Comparator>
137-
bool LockFreeSkipList<Key, Comparator>::FindWindow(Key key,
139+
template<typename Key, typename Value, class Comparator>
140+
bool LockFreeSkipList<Key, Value, Comparator>::FindWindow(Key key,
138141
Node** first_nodes,
139142
Node** second_nodes) {
140143
while (true) {
@@ -148,8 +151,8 @@ bool LockFreeSkipList<Key, Comparator>::FindWindow(Key key,
148151
}
149152
}
150153

151-
template<typename Key, class Comparator>
152-
auto LockFreeSkipList<Key, Comparator>::FindLessThan(Key key) -> Node* {
154+
template<typename Key, typename Value, class Comparator>
155+
Value LockFreeSkipList<Key, Value, Comparator>::FindLessThan(Key key) {
153156
Node* pred = this->head_;
154157
for (int i=kMaxHeight-1; i>=0; --i) {
155158
Node* curr = this->GetNodeByOffset(pred->GetRef(i)->load().next_offset);
@@ -175,12 +178,12 @@ auto LockFreeSkipList<Key, Comparator>::FindLessThan(Key key) -> Node* {
175178
}
176179
}
177180
}
178-
179-
return pred;
181+
182+
return pred->value;
180183
}
181184

182-
template<typename Key, class Comparator>
183-
bool LockFreeSkipList<Key, Comparator>::FindWindowInner(Node* start_node,
185+
template<typename Key, typename Value, class Comparator>
186+
bool LockFreeSkipList<Key, Value, Comparator>::FindWindowInner(Node* start_node,
184187
Key key,
185188
Node** first_nodes,
186189
Node** second_nodes,
@@ -227,8 +230,8 @@ bool LockFreeSkipList<Key, Comparator>::FindWindowInner(Node* start_node,
227230
return curr == nullptr ? false : (curr->key == key); // 这里已经进行到最底层, 如果找到key则返回true
228231
}
229232

230-
template<typename Key, class Comparator>
231-
int LockFreeSkipList<Key, Comparator>::RandomHeight() {
233+
template<typename Key, typename Value, class Comparator>
234+
int LockFreeSkipList<Key, Value, Comparator>::RandomHeight() {
232235

233236
static const unsigned int kBranching = 4;
234237
int height = 1;
@@ -243,8 +246,8 @@ int LockFreeSkipList<Key, Comparator>::RandomHeight() {
243246
// return u(this->rnd_);
244247
}
245248

246-
template<typename Key, class Comparator>
247-
bool LockFreeSkipList<Key, Comparator>::Add(Key key) {
249+
template<typename Key, typename Value, class Comparator>
250+
bool LockFreeSkipList<Key, Value, Comparator>::Add(Key key, Value value) {
248251
int height = this->RandomHeight();
249252

250253
Node* first_nodes[kMaxHeight];
@@ -261,7 +264,7 @@ bool LockFreeSkipList<Key, Comparator>::Add(Key key) {
261264
// 不支持重复的Key
262265
return false;
263266
} else {
264-
Node* new_node = this->NewNode(key, height);
267+
Node* new_node = this->NewNode(key, value, height);
265268
for (int i=0; i<height; ++i) {
266269
Ref ref_to_second = {second_nodes[i]};
267270
new_node->SetRef(i, ref_to_second);
@@ -292,8 +295,8 @@ bool LockFreeSkipList<Key, Comparator>::Add(Key key) {
292295
}
293296
}
294297

295-
template<typename Key, class Comparator>
296-
bool LockFreeSkipList<Key, Comparator>::Remove(Key key) {
298+
template<typename Key, typename Value, class Comparator>
299+
bool LockFreeSkipList<Key, Value, Comparator>::Remove(Key key) {
297300
Node* first_nodes[kMaxHeight];
298301
Node* second_nodes[kMaxHeight];
299302
while (true) {

src/wolf.h

+12-12
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "core/container/lockfree_linklist.h"
1212
#include "core/container/shm_manager.h"
1313

14-
template <typename Data>
14+
template <typename Index, typename Data>
1515
class Wolf{
1616
public:
1717
Wolf(const std::string& index_path,
@@ -29,22 +29,22 @@ class Wolf{
2929
auto* shm_manager = new container::ShmManager(allocator);
3030

3131
if (data_path.empty()) {
32-
this->inner_data_ = new container::LockFreeSkipList<Data>(shm_manager, index_reset_flag);
32+
this->inner_data_ = new container::LockFreeSkipList<Index, Data>(shm_manager, index_reset_flag);
3333
} else {
34-
this->index_ = new container::LockFreeSkipList<uint32_t>(shm_manager, index_reset_flag);
34+
this->index_ = new container::LockFreeSkipList<Index, uint32_t>(shm_manager, index_reset_flag);
3535
this->data_area_ = new area::ShmArea(data_path, data_max_size, data_reset_flag);
3636
}
3737
}
3838

39-
void Add(const Data& data) {
39+
void Add(const Index& index, const Data& data) {
4040
if (this->inner_data_ != nullptr) {
41-
this->inner_data_->Add(data);
41+
this->inner_data_->Add(index, data);
4242
} else {
43-
// auto* buffer = this->data_area_->Allocate(sizeof(Data));
44-
// memcpy(buffer, &data, sizeof(Data));
45-
//
46-
// auto offset = (uint32_t)(buffer - this->data_area_->MemoryStart());
47-
//this->index_->Add(offset);
43+
auto* buffer = this->data_area_->Allocate(sizeof(Data));
44+
memcpy(buffer, &data, sizeof(Data));
45+
46+
auto offset = (uint32_t)(buffer - this->data_area_->MemoryStart());
47+
this->index_->Add(index, offset);
4848
}
4949
}
5050

@@ -59,7 +59,7 @@ class Wolf{
5959
const std::string data_path_;
6060
uint32_t data_max_size_;
6161
bool data_reset_flag_;
62-
container::LockFreeSkipList<Data>* inner_data_ = nullptr;
63-
container::LockFreeSkipList<uint32_t>* index_ = nullptr;
62+
container::LockFreeSkipList<Index, Data>* inner_data_ = nullptr;
63+
container::LockFreeSkipList<Index, uint32_t>* index_ = nullptr;
6464
area::ShmArea* data_area_ = nullptr;
6565
};

0 commit comments

Comments
 (0)