This repository was archived by the owner on Nov 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathkvdk_sorted.cpp
134 lines (112 loc) · 4.59 KB
/
kvdk_sorted.cpp
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
/* SPDX-License-Identifier: BSD-3-Clause
* Copyright(c) 2021-2022 Intel Corporation
*/
#include "kvdk_c.hpp"
extern "C" {
KVDKSortedCollectionConfigs* KVDKCreateSortedCollectionConfigs() {
return new KVDKSortedCollectionConfigs;
}
void KVDKSetSortedCollectionConfigs(KVDKSortedCollectionConfigs* configs,
const char* comp_func_name,
size_t comp_func_len,
int index_with_hashtable) {
configs->rep.comparator_name = std::string(comp_func_name, comp_func_len);
configs->rep.index_with_hashtable = index_with_hashtable;
}
void KVDKDestroySortedCollectionConfigs(KVDKSortedCollectionConfigs* configs) {
delete configs;
}
KVDKStatus KVDKSortedCreate(KVDKEngine* engine, const char* collection_name,
size_t collection_len,
KVDKSortedCollectionConfigs* configs) {
KVDKStatus s = engine->rep->SortedCreate(
StringView(collection_name, collection_len), configs->rep);
return s;
}
KVDKStatus KVDKSortedDestroy(KVDKEngine* engine, const char* collection_name,
size_t collection_len) {
return engine->rep->SortedDestroy(
StringView(collection_name, collection_len));
}
KVDKStatus KVDKSortedSize(KVDKEngine* engine, const char* collection,
size_t collection_len, size_t* size) {
return engine->rep->SortedSize(StringView(collection, collection_len), size);
}
KVDKStatus KVDKSortedPut(KVDKEngine* engine, const char* collection,
size_t collection_len, const char* key, size_t key_len,
const char* val, size_t val_len) {
return engine->rep->SortedPut(StringView(collection, collection_len),
StringView(key, key_len),
StringView(val, val_len));
}
KVDKStatus KVDKSortedGet(KVDKEngine* engine, const char* collection,
size_t collection_len, const char* key, size_t key_len,
size_t* val_len, char** val) {
std::string val_str;
*val = nullptr;
KVDKStatus s = engine->rep->SortedGet(StringView(collection, collection_len),
StringView(key, key_len), &val_str);
if (s != KVDKStatus::Ok) {
*val_len = 0;
return s;
}
*val_len = val_str.size();
*val = CopyStringToChar(val_str);
return s;
}
KVDKStatus KVDKSortedDelete(KVDKEngine* engine, const char* collection,
size_t collection_len, const char* key,
size_t key_len) {
return engine->rep->SortedDelete(StringView(collection, collection_len),
StringView(key, key_len));
}
KVDKSortedIterator* KVDKSortedIteratorCreate(KVDKEngine* engine,
const char* collection,
size_t collection_len,
KVDKSnapshot* snapshot,
KVDKStatus* s) {
KVDKSortedIterator* result = new KVDKSortedIterator;
result->rep = (engine->rep->SortedIteratorCreate(
StringView{collection, collection_len},
snapshot ? snapshot->rep : nullptr, s));
if (!result->rep) {
delete result;
return nullptr;
}
return result;
}
void KVDKSortedIteratorDestroy(KVDKEngine* engine,
KVDKSortedIterator* iterator) {
if (iterator != nullptr) {
engine->rep->SortedIteratorRelease(iterator->rep);
}
delete iterator;
}
void KVDKSortedIteratorSeekToFirst(KVDKSortedIterator* iter) {
iter->rep->SeekToFirst();
}
void KVDKSortedIteratorSeekToLast(KVDKSortedIterator* iter) {
iter->rep->SeekToLast();
}
void KVDKSortedIteratorSeek(KVDKSortedIterator* iter, const char* str,
size_t str_len) {
iter->rep->Seek(std::string(str, str_len));
}
unsigned char KVDKSortedIteratorValid(KVDKSortedIterator* iter) {
return iter->rep->Valid();
}
void KVDKSortedIteratorNext(KVDKSortedIterator* iter) { iter->rep->Next(); }
void KVDKSortedIteratorPrev(KVDKSortedIterator* iter) { iter->rep->Prev(); }
void KVDKSortedIteratorKey(KVDKSortedIterator* iter, char** key,
size_t* key_len) {
std::string key_str = iter->rep->Key();
*key_len = key_str.size();
*key = CopyStringToChar(key_str);
}
void KVDKSortedIteratorValue(KVDKSortedIterator* iter, char** value,
size_t* val_len) {
std::string val_str = iter->rep->Value();
*val_len = val_str.size();
*value = CopyStringToChar(val_str);
}
} // extern "C"