forked from cloudflarearchive/kyotocabinet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
overview
327 lines (260 loc) · 11.8 KB
/
overview
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/**
@mainpage Kyoto Cabinet: a straightforward implementation of DBM
@section Introduction
Kyoto Cabinet is a library of routines for managing a database. The database is a simple data file containing records, each is a pair of a key and a value. Every key and value is serial bytes with variable length. Both binary data and character string can be used as a key and a value. Each key must be unique within a database. There is neither concept of data tables nor data types. Records are organized in hash table or B+ tree.
The following access methods are provided to the database: storing a record with a key and a value, deleting a record by a key, retrieving a record by a key. Moreover, traversal access to every key are provided. These access methods are similar to ones of the original DBM (and its followers: NDBM and GDBM) library defined in the UNIX standard. Kyoto Cabinet is an alternative for the DBM because of its higher performance.
Each operation of the hash database has the time complexity of "O(1)". Therefore, in theory, the performance is constant regardless of the scale of the database. In practice, the performance is determined by the speed of the main memory or the storage device. If the size of the database is less than the capacity of the main memory, the performance will seem on-memory speed, which is faster than std::map of STL. Of course, the database size can be greater than the capacity of the main memory and the upper limit is 8 exabytes. Even in that case, each operation needs only one or two seeking of the storage device.
Each operation of the B+ tree database has the time complexity of "O(log N)". Therefore, in theory, the performance is logarithmic to the scale of the database. Although the performance of random access of the B+ tree database is slower than that of the hash database, the B+ tree database supports sequential access in order of the keys, which realizes forward matching search for strings and range search for integers. The performance of sequential access is much faster than that of random access.
As the API is based on object-oriented design, the hash database and the the B+ tree database have same methods which inherited from the upper abstract class. Beside them, seven kinds of databases are provided under the same base class. The prototype hash database is powered by the standard container of std::unordered_map. The prototype tree database is powered by the standard container of std::map. The stash database is powered by the original implementation of naive hash map saving memory. The cache hash database is powered by the original implementation of doubly-linked hash map with LRU deletion algorithm. The cache tree database is powered by the cache hash database and provides B+ tree mechanism. The directory hash database is powered by the directory mechanism of the file system and stores records as respective files in a directory. The directory tree database is powered by the directory hash database and provides B+ tree mechanism. All databases have practical utility methods related to transaction and cursor. Programs for command line interface are also included in the package.
All databases have practical utility methods related to transaction and cursor. Programs for command line interface are also included in the package.
The following classes are most important. If you are new to Kyoto Cabinet, learn the polymorphic database first.
@li kyotocabinet::BasicDB -- common interface of concrete databases
@li kyotocabinet::ProtoHashDB -- on-memory hash database based on std::unordered_map
@li kyotocabinet::ProtoTreeDB -- on-memory hash database based on std::map
@li kyotocabinet::StashDB -- economical on-memory hash database for cache.
@li kyotocabinet::CacheDB -- on-memory hash database for cache with LRU deletion
@li kyotocabinet::GrassDB -- on-memory tree database for cache in order
@li kyotocabinet::HashDB -- file hash database, which implements hash table on a file
@li kyotocabinet::TreeDB -- file tree database, which implements B+ tree on a file
@li kyotocabinet::DirDB -- directory hash database, which handles respective files in a directory
@li kyotocabinet::ForestDB -- directory tree database, which implements B+ tree on a directory
@li kyotocabinet::TextDB -- plain text database, which treats a text file as a database
@li kyotocabinet::PolyDB -- polymorphic database, dynamic binding of the above databases
@li kyotocabinet::MapReduce -- MapReduce framework to process records in each database
@li kyotocabinet::IndexDB -- wrapper for efficient appending operations to each database
@li kclangc.h -- C language binding of the polymorphic database
See the project homepage ( http://fallabs.com/kyotocabinet/ ) for details.
@section Example
The following code is an example to use a polymorphic database.
@code
#include <kcpolydb.h>
using namespace std;
using namespace kyotocabinet;
// main routine
int main(int argc, char** argv) {
// create the database object
PolyDB db;
// open the database
if (!db.open("casket.kch", PolyDB::OWRITER | PolyDB::OCREATE)) {
cerr << "open error: " << db.error().name() << endl;
}
// store records
if (!db.set("foo", "hop") ||
!db.set("bar", "step") ||
!db.set("baz", "jump")) {
cerr << "set error: " << db.error().name() << endl;
}
// retrieve a record
string value;
if (db.get("foo", &value)) {
cout << value << endl;
} else {
cerr << "get error: " << db.error().name() << endl;
}
// traverse records
DB::Cursor* cur = db.cursor();
cur->jump();
string ckey, cvalue;
while (cur->get(&ckey, &cvalue, true)) {
cout << ckey << ":" << cvalue << endl;
}
delete cur;
// close the database
if (!db.close()) {
cerr << "close error: " << db.error().name() << endl;
}
return 0;
}
@endcode
The following code is a more complex example, which uses the Visitor pattern.
@code
#include <kcpolydb.h>
using namespace std;
using namespace kyotocabinet;
// main routine
int main(int argc, char** argv) {
// create the database object
PolyDB db;
// open the database
if (!db.open("casket.kch", PolyDB::OREADER)) {
cerr << "open error: " << db.error().name() << endl;
}
// define the visitor
class VisitorImpl : public DB::Visitor {
// call back function for an existing record
const char* visit_full(const char* kbuf, size_t ksiz,
const char* vbuf, size_t vsiz, size_t *sp) {
cout << string(kbuf, ksiz) << ":" << string(vbuf, vsiz) << endl;
return NOP;
}
// call back function for an empty record space
const char* visit_empty(const char* kbuf, size_t ksiz, size_t *sp) {
cerr << string(kbuf, ksiz) << " is missing" << endl;
return NOP;
}
} visitor;
// retrieve a record with visitor
if (!db.accept("foo", 3, &visitor, false) ||
!db.accept("dummy", 5, &visitor, false)) {
cerr << "accept error: " << db.error().name() << endl;
}
// traverse records with visitor
if (!db.iterate(&visitor, false)) {
cerr << "iterate error: " << db.error().name() << endl;
}
// close the database
if (!db.close()) {
cerr << "close error: " << db.error().name() << endl;
}
return 0;
}
@endcode
The following code is an example of word counting with the MapReduce framework.
@code
#include <kcpolydb.h>
#include <kcdbext.h>
using namespace std;
using namespace kyotocabinet;
// main routine
int main(int argc, char** argv) {
// create the database object
PolyDB db;
// open the database
if (!db.open()) {
cerr << "open error: " << db.error().name() << endl;
}
// store records
db.set("1", "this is a pen");
db.set("2", "what a beautiful pen this is");
db.set("3", "she is beautiful");
// define the mapper and the reducer
class MapReduceImpl : public MapReduce {
// call back function of the mapper
bool map(const char* kbuf, size_t ksiz, const char* vbuf, size_t vsiz) {
vector<string> words;
strsplit(string(vbuf, vsiz), ' ', &words);
for (vector<string>::iterator it = words.begin();
it != words.end(); it++) {
emit(it->data(), it->size(), "", 0);
}
return true;
}
// call back function of the reducer
bool reduce(const char* kbuf, size_t ksiz, ValueIterator* iter) {
size_t count = 0;
const char* vbuf;
size_t vsiz;
while ((vbuf = iter->next(&vsiz)) != NULL) {
count++;
}
cout << string(kbuf, ksiz) << ": " << count << endl;
return true;
}
} mr;
// execute the MapReduce process
if (!mr.execute(&db)) {
cerr << "MapReduce error: " << db.error().name() << endl;
}
// close the database
if (!db.close()) {
cerr << "close error: " << db.error().name() << endl;
}
return 0;
}
@endcode
The C language binding is also provided as a wrapper of the polymorphic database API. The following code is an example.
@code
#include <kclangc.h>
/* call back function for an existing record */
const char* visitfull(const char* kbuf, size_t ksiz,
const char* vbuf, size_t vsiz, size_t *sp, void* opq) {
fwrite(kbuf, 1, ksiz, stdout);
printf(":");
fwrite(vbuf, 1, vsiz, stdout);
printf("\n");
return KCVISNOP;
}
/* call back function for an empty record space */
const char* visitempty(const char* kbuf, size_t ksiz, size_t *sp, void* opq) {
fwrite(kbuf, 1, ksiz, stdout);
printf(" is missing\n");
return KCVISNOP;
}
/* main routine */
int main(int argc, char** argv) {
KCDB* db;
KCCUR* cur;
char *kbuf, *vbuf;
size_t ksiz, vsiz;
const char *cvbuf;
/* create the database object */
db = kcdbnew();
/* open the database */
if (!kcdbopen(db, "casket.kch", KCOWRITER | KCOCREATE)) {
fprintf(stderr, "open error: %s\n", kcecodename(kcdbecode(db)));
}
/* store records */
if (!kcdbset(db, "foo", 3, "hop", 3) ||
!kcdbset(db, "bar", 3, "step", 4) ||
!kcdbset(db, "baz", 3, "jump", 4)) {
fprintf(stderr, "set error: %s\n", kcecodename(kcdbecode(db)));
}
/* retrieve a record */
vbuf = kcdbget(db, "foo", 3, &vsiz);
if (vbuf) {
printf("%s\n", vbuf);
kcfree(vbuf);
} else {
fprintf(stderr, "get error: %s\n", kcecodename(kcdbecode(db)));
}
/* traverse records */
cur = kcdbcursor(db);
kccurjump(cur);
while ((kbuf = kccurget(cur, &ksiz, &cvbuf, &vsiz, 1)) != NULL) {
printf("%s:%s\n", kbuf, cvbuf);
kcfree(kbuf);
}
kccurdel(cur);
/* retrieve a record with visitor */
if (!kcdbaccept(db, "foo", 3, visitfull, visitempty, NULL, 0) ||
!kcdbaccept(db, "dummy", 5, visitfull, visitempty, NULL, 0)) {
fprintf(stderr, "accept error: %s\n", kcecodename(kcdbecode(db)));
}
/* traverse records with visitor */
if (!kcdbiterate(db, visitfull, NULL, 0)) {
fprintf(stderr, "iterate error: %s\n", kcecodename(kcdbecode(db)));
}
/* close the database */
if (!kcdbclose(db)) {
fprintf(stderr, "close error: %s\n", kcecodename(kcdbecode(db)));
}
/* delete the database object */
kcdbdel(db);
return 0;
}
@endcode
*/
/**
* Common namespace of Kyoto Cabinet.
*/
namespace kyotocabinet {}
/**
* @file kccommon.h common symbols for the library
* @file kcutil.h utility functions
* @file kcdb.h database interface
* @file kcthread.h threading devices
* @file kcfile.h filesystem abstraction
* @file kccompress.h data compressor and decompressor
* @file kccompare.h comparator functions
* @file kcmap.h data mapping structures
* @file kcregex.h regular expression
* @file kcplantdb.h plant database
* @file kcprotodb.h prototype database
* @file kccachedb.h cache hash database
* @file kchashdb.h file hash database
* @file kcdirdb.h directory hash database
* @file kctextdb.h plain text database
* @file kcpolydb.h polymorphic database
* @file kcdbext.h database extension
* @file kclangc.h C language binding
*/