1 /*************************************************************************************************
2 * The B+ tree database API of Tokyo Cabinet
3 * Copyright (C) 2006-2008 Mikio Hirabayashi
4 * This file is part of Tokyo Cabinet.
5 * Tokyo Cabinet is free software; you can redistribute it and/or modify it under the terms of
6 * the GNU Lesser General Public License as published by the Free Software Foundation; either
7 * version 2.1 of the License or any later version. Tokyo Cabinet is distributed in the hope
8 * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10 * License for more details.
11 * You should have received a copy of the GNU Lesser General Public License along with Tokyo
12 * Cabinet; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
13 * Boston, MA 02111-1307 USA.
14 *************************************************************************************************/
17 #ifndef _TCBDB_H /* duplication check */
20 #if defined(__cplusplus)
21 #define __TCBDB_CLINKAGEBEGIN extern "C" {
22 #define __TCBDB_CLINKAGEEND }
24 #define __TCBDB_CLINKAGEBEGIN
25 #define __TCBDB_CLINKAGEEND
39 /*************************************************************************************************
41 *************************************************************************************************/
44 /* type of the pointer to a comparison function.
45 `aptr' specifies the pointer to the region of one key.
46 `asiz' specifies the size of the region of one key.
47 `bptr' specifies the pointer to the region of the other key.
48 `bsiz' specifies the size of the region of the other key.
49 `op' specifies the pointer to the optional opaque object.
50 The return value is positive if the former is big, negative if the latter is big, 0 if both
52 typedef int (*BDBCMP)(const char *aptr, int asiz, const char *bptr, int bsiz, void *op);
54 typedef struct { /* type of structure for a B+ tree database */
55 void *mmtx; /* mutex for method */
56 void *cmtx; /* mutex for cache */
57 void *tmtx; /* mutex for transaction */
58 TCHDB *hdb; /* internal database object */
59 char *opaque; /* opaque buffer */
60 bool open; /* whether the internal database is opened */
61 bool wmode; /* whether to be writable */
62 uint32_t lmemb; /* number of members in each leaf */
63 uint32_t nmemb; /* number of members in each node */
64 uint8_t opts; /* options */
65 uint64_t root; /* ID number of the root page */
66 uint64_t first; /* ID number of the first leaf */
67 uint64_t last; /* ID number of the last leaf */
68 uint64_t lnum; /* number of leaves */
69 uint64_t nnum; /* number of nodes */
70 uint64_t rnum; /* number of records */
71 TCMAP *leafc; /* cache for leaves */
72 TCMAP *nodec; /* cache for nodes */
73 BDBCMP cmp; /* pointer to the comparison function */
74 void *cmpop; /* opaque object for the comparison function */
75 uint32_t lcnum; /* max number of cached leaves */
76 uint32_t ncnum; /* max number of cached nodes */
77 uint32_t lsmax; /* max size of each leaf */
78 uint32_t lschk; /* counter for leaf size checking */
79 uint64_t capnum; /* capacity number of records */
80 uint64_t *hist; /* history array of visited nodes */
81 int hnum; /* number of element of the history array */
82 uint64_t hleaf; /* ID number of the leaf referred by the history */
83 uint64_t lleaf; /* ID number of the last visited leaf */
84 bool tran; /* whether in the transaction */
85 char *rbopaque; /* opaque for rollback */
86 int64_t cnt_saveleaf; /* tesing counter for leaf save times */
87 int64_t cnt_loadleaf; /* tesing counter for leaf load times */
88 int64_t cnt_killleaf; /* tesing counter for leaf kill times */
89 int64_t cnt_adjleafc; /* tesing counter for node cache adjust times */
90 int64_t cnt_savenode; /* tesing counter for node save times */
91 int64_t cnt_loadnode; /* tesing counter for node load times */
92 int64_t cnt_adjnodec; /* tesing counter for node cache adjust times */
95 enum { /* enumeration for additional flags */
96 BDBFOPEN = HDBFOPEN, /* whether opened */
97 BDBFFATAL = HDBFFATAL /* whetehr with fatal error */
100 enum { /* enumeration for tuning options */
101 BDBTLARGE = 1 << 0, /* use 64-bit bucket array */
102 BDBTDEFLATE = 1 << 1, /* compress each page with Deflate */
103 BDBTTCBS = 1 << 2 /* compress each page with TCBS */
106 enum { /* enumeration for open modes */
107 BDBOREADER = 1 << 0, /* open as a reader */
108 BDBOWRITER = 1 << 1, /* open as a writer */
109 BDBOCREAT = 1 << 2, /* writer creating */
110 BDBOTRUNC = 1 << 3, /* writer truncating */
111 BDBONOLCK = 1 << 4, /* open without locking */
112 BDBOLCKNB = 1 << 5 /* lock without blocking */
115 typedef struct { /* type of structure for a B+ tree cursor */
116 TCBDB *bdb; /* database object */
117 uint64_t id; /* ID number of the leaf */
118 int32_t kidx; /* number of the key */
119 int32_t vidx; /* number of the value */
122 enum { /* enumeration for cursor put mode */
123 BDBCPCURRENT, /* current */
124 BDBCPBEFORE, /* before */
125 BDBCPAFTER /* after */
129 /* Get the message string corresponding to an error code.
130 `ecode' specifies the error code.
131 The return value is the message string of the error code. */
132 const char *tcbdberrmsg(int ecode);
135 /* Create a B+ tree database object.
136 The return value is the new B+ tree database object. */
137 TCBDB *tcbdbnew(void);
140 /* Delete a B+ tree database object.
141 `bdb' specifies the B+ tree database object.
142 If the database is not closed, it is closed implicitly. Note that the deleted object and its
143 derivatives can not be used anymore. */
144 void tcbdbdel(TCBDB *bdb);
147 /* Get the last happened error code of a B+ tree database object.
148 `bdb' specifies the B+ tree database object.
149 The return value is the last happened error code.
150 The following error code is defined: `TCESUCCESS' for success, `TCETHREAD' for threading
151 error, `TCEINVALID' for invalid operation, `TCENOFILE' for file not found, `TCENOPERM' for no
152 permission, `TCEMETA' for invalid meta data, `TCERHEAD' for invalid record header, `TCEOPEN'
153 for open error, `TCECLOSE' for close error, `TCETRUNC' for trunc error, `TCESYNC' for sync
154 error, `TCESTAT' for stat error, `TCESEEK' for seek error, `TCEREAD' for read error,
155 `TCEWRITE' for write error, `TCEMMAP' for mmap error, `TCELOCK' for lock error, `TCEUNLINK'
156 for unlink error, `TCERENAME' for rename error, `TCEMKDIR' for mkdir error, `TCERMDIR' for
157 rmdir error, `TCEKEEP' for existing record, `TCENOREC' for no record found, and `TCEMISC' for
158 miscellaneous error. */
159 int tcbdbecode(TCBDB *bdb);
162 /* Set mutual exclusion control of a B+ tree database object for threading.
163 `bdb' specifies the B+ tree database object which is not opened.
164 If successful, the return value is true, else, it is false.
165 Note that the mutual exclusion control is needed if the object is shared by plural threads and
166 this function should should be called before the database is opened. */
167 bool tcbdbsetmutex(TCBDB *bdb);
170 /* Set the custom comparison function of a B+ tree database object.
171 `bdb' specifies the B+ tree database object which is not opened.
172 `cmp' specifies the pointer to the custom comparison function.
173 `cmpop' specifies an arbitrary pointer to be given as a parameter of the comparison function.
174 If it is not needed, `NULL' can be specified.
175 If successful, the return value is true, else, it is false.
176 The default comparison function compares keys of two records by lexical order. The functions
177 `tcbdbcmplexical' (dafault), `tcbdbcmpdecimal', `tcbdbcmpint32', and `tcbdbcmpint64' are
178 built-in. Note that the comparison function should be set before the database is opened.
179 Moreover, user-defined comparison functions should be set every time the database is being
181 bool tcbdbsetcmpfunc(TCBDB *bdb, BDBCMP cmp, void *cmpop);
184 /* Set the tuning parameters of a B+ tree database object.
185 `bdb' specifies the B+ tree database object which is not opened.
186 `lmemb' specifies the number of members in each leaf page. If it is not more than 0, the
187 default value is specified. The default value is 128.
188 `nmemb' specifies the number of members in each non-leaf page. If it is not more than 0, the
189 default value is specified. The default value is 256.
190 `bnum' specifies the number of elements of the bucket array. If it is not more than 0, the
191 default value is specified. The default value is 16381. Suggested size of the bucket array
192 is about from 1 to 4 times of the number of all pages to be stored.
193 `apow' specifies the size of record alignment by power of 2. If it is negative, the default
194 value is specified. The default value is 8 standing for 2^8=256.
195 `fpow' specifies the maximum number of elements of the free block pool by power of 2. If it
196 is negative, the default value is specified. The default value is 10 standing for 2^10=1024.
197 `opts' specifies options by bitwise or: `BDBTLARGE' specifies that the size of the database
198 can be larger than 2GB by using 64-bit bucket array, `BDBTDEFLATE' specifies that each page
199 is compressed with Deflate encoding, `BDBTTCBS' specifies that each page is compressed with
201 If successful, the return value is true, else, it is false.
202 Note that the tuning parameters should be set before the database is opened. */
203 bool tcbdbtune(TCBDB *bdb, int32_t lmemb, int32_t nmemb,
204 int64_t bnum, int8_t apow, int8_t fpow, uint8_t opts);
207 /* Set the caching parameters of a B+ tree database object.
208 `bdb' specifies the B+ tree database object which is not opened.
209 `lcnum' specifies the maximum number of leaf nodes to be cached. If it is not more than 0,
210 the default value is specified. The default value is 1024.
211 `ncnum' specifies the maximum number of non-leaf nodes to be cached. If it is not more than 0,
212 the default value is specified. The default value is 512.
213 If successful, the return value is true, else, it is false.
214 Note that the caching parameters should be set before the database is opened. */
215 bool tcbdbsetcache(TCBDB *bdb, int32_t lcnum, int32_t ncnum);
218 /* Open a database file and connect a B+ tree database object.
219 `bdb' specifies the B+ tree database object which is not opened.
220 `path' specifies the path of the database file.
221 `omode' specifies the connection mode: `BDBOWRITER' as a writer, `BDBOREADER' as a reader.
222 If the mode is `BDBOWRITER', the following may be added by bitwise or: `BDBOCREAT', which
223 means it creates a new database if not exist, `BDBOTRUNC', which means it creates a new database
224 regardless if one exists. Both of `BDBOREADER' and `BDBOWRITER' can be added to by
225 bitwise or: `BDBONOLCK', which means it opens the database file without file locking, or
226 `BDBOLCKNB', which means locking is performed without blocking.
227 If successful, the return value is true, else, it is false. */
228 bool tcbdbopen(TCBDB *bdb, const char *path, int omode);
231 /* Close a B+ tree database object.
232 `bdb' specifies the B+ tree database object.
233 If successful, the return value is true, else, it is false.
234 Update of a database is assured to be written when the database is closed. If a writer opens
235 a database but does not close it appropriately, the database will be broken. */
236 bool tcbdbclose(TCBDB *bdb);
239 /* Store a record into a B+ tree database object.
240 `bdb' specifies the B+ tree database object connected as a writer.
241 `kbuf' specifies the pointer to the region of the key.
242 `ksiz' specifies the size of the region of the key.
243 `vbuf' specifies the pointer to the region of the value.
244 `vsiz' specifies the size of the region of the value.
245 If successful, the return value is true, else, it is false.
246 If a record with the same key exists in the database, it is overwritten. */
247 bool tcbdbput(TCBDB *bdb, const void *kbuf, int ksiz, const void *vbuf, int vsiz);
250 /* Store a string record into a B+ tree database object.
251 `bdb' specifies the B+ tree database object connected as a writer.
252 `kstr' specifies the string of the key.
253 `vstr' specifies the string of the value.
254 If successful, the return value is true, else, it is false.
255 If a record with the same key exists in the database, it is overwritten. */
256 bool tcbdbput2(TCBDB *bdb, const char *kstr, const char *vstr);
259 /* Store a new record into a B+ tree database object.
260 `bdb' specifies the B+ tree database object connected as a writer.
261 `kbuf' specifies the pointer to the region of the key.
262 `ksiz' specifies the size of the region of the key.
263 `vbuf' specifies the pointer to the region of the value.
264 `vsiz' specifies the size of the region of the value.
265 If successful, the return value is true, else, it is false.
266 If a record with the same key exists in the database, this function has no effect. */
267 bool tcbdbputkeep(TCBDB *bdb, const void *kbuf, int ksiz, const void *vbuf, int vsiz);
270 /* Store a new string record into a B+ tree database object.
271 `bdb' specifies the B+ tree database object connected as a writer.
272 `kstr' specifies the string of the key.
273 `vstr' specifies the string of the value.
274 If successful, the return value is true, else, it is false.
275 If a record with the same key exists in the database, this function has no effect. */
276 bool tcbdbputkeep2(TCBDB *bdb, const char *kstr, const char *vstr);
279 /* Concatenate a value at the end of the existing record in a B+ tree database object.
280 `bdb' specifies the B+ tree database object connected as a writer.
281 `kbuf' specifies the pointer to the region of the key.
282 `ksiz' specifies the size of the region of the key.
283 `vbuf' specifies the pointer to the region of the value.
284 `vsiz' specifies the size of the region of the value.
285 If successful, the return value is true, else, it is false.
286 If there is no corresponding record, a new record is created. */
287 bool tcbdbputcat(TCBDB *bdb, const void *kbuf, int ksiz, const void *vbuf, int vsiz);
290 /* Concatenate a string value at the end of the existing record in a B+ tree database object.
291 `bdb' specifies the B+ tree database object connected as a writer.
292 `kstr' specifies the string of the key.
293 `vstr' specifies the string of the value.
294 If successful, the return value is true, else, it is false.
295 If there is no corresponding record, a new record is created. */
296 bool tcbdbputcat2(TCBDB *bdb, const char *kstr, const char *vstr);
299 /* Store a record into a B+ tree database object with allowing duplication of keys.
300 `bdb' specifies the B+ tree database object connected as a writer.
301 `kbuf' specifies the pointer to the region of the key.
302 `ksiz' specifies the size of the region of the key.
303 `vbuf' specifies the pointer to the region of the value.
304 `vsiz' specifies the size of the region of the value.
305 If successful, the return value is true, else, it is false.
306 If a record with the same key exists in the database, the new record is placed after the
308 bool tcbdbputdup(TCBDB *bdb, const void *kbuf, int ksiz, const void *vbuf, int vsiz);
311 /* Store a string record into a B+ tree database object with allowing duplication of keys.
312 `bdb' specifies the B+ tree database object connected as a writer.
313 `kstr' specifies the string of the key.
314 `vstr' specifies the string of the value.
315 If successful, the return value is true, else, it is false.
316 If a record with the same key exists in the database, the new record is placed after the
318 bool tcbdbputdup2(TCBDB *bdb, const char *kstr, const char *vstr);
321 /* Store records into a B+ tree database object with allowing duplication of keys.
322 `bdb' specifies the B+ tree database object connected as a writer.
323 `kbuf' specifies the pointer to the region of the common key.
324 `ksiz' specifies the size of the region of the common key.
325 `vals' specifies a list object containing values.
326 If successful, the return value is true, else, it is false.
327 If a record with the same key exists in the database, the new records are placed after the
329 bool tcbdbputdup3(TCBDB *bdb, const void *kbuf, int ksiz, const TCLIST *vals);
332 /* Remove a record of a B+ tree database object.
333 `bdb' specifies the B+ tree database object connected as a writer.
334 `kbuf' specifies the pointer to the region of the key.
335 `ksiz' specifies the size of the region of the key.
336 If successful, the return value is true, else, it is false.
337 If the key of duplicated records is specified, the first one is selected. */
338 bool tcbdbout(TCBDB *bdb, const void *kbuf, int ksiz);
341 /* Remove a string record of a B+ tree database object.
342 `bdb' specifies the B+ tree database object connected as a writer.
343 `kstr' specifies the string of the key.
344 If successful, the return value is true, else, it is false.
345 If the key of duplicated records is specified, the first one is selected. */
346 bool tcbdbout2(TCBDB *bdb, const char *kstr);
349 /* Remove records of a B+ tree database object.
350 `bdb' specifies the B+ tree database object connected as a writer.
351 `kbuf' specifies the pointer to the region of the key.
352 `ksiz' specifies the size of the region of the key.
353 If successful, the return value is true, else, it is false.
354 If the key of duplicated records is specified, all of them are removed. */
355 bool tcbdbout3(TCBDB *bdb, const void *kbuf, int ksiz);
358 /* Retrieve a record in a B+ tree database object.
359 `bdb' specifies the B+ tree database object.
360 `kbuf' specifies the pointer to the region of the key.
361 `ksiz' specifies the size of the region of the key.
362 `sp' specifies the pointer to the variable into which the size of the region of the return
364 If successful, the return value is the pointer to the region of the value of the corresponding
365 record. `NULL' is returned if no record corresponds.
366 If the key of duplicated records is specified, the first one is selected. Because an
367 additional zero code is appended at the end of the region of the return value, the return
368 value can be treated as a character string. Because the region of the return value is
369 allocated with the `malloc' call, it should be released with the `free' call when it is no
371 void *tcbdbget(TCBDB *bdb, const void *kbuf, int ksiz, int *sp);
374 /* Retrieve a string record in a B+ tree database object.
375 `bdb' specifies the B+ tree database object.
376 `kstr' specifies the string of the key.
377 If successful, the return value is the string of the value of the corresponding record.
378 `NULL' is returned if no record corresponds.
379 If the key of duplicated records is specified, the first one is selected. Because the region
380 of the return value is allocated with the `malloc' call, it should be released with the `free'
381 call when it is no longer in use. */
382 char *tcbdbget2(TCBDB *bdb, const char *kstr);
385 /* Retrieve a record in a B+ tree database object as a volatile buffer.
386 `bdb' specifies the B+ tree database object.
387 `kbuf' specifies the pointer to the region of the key.
388 `ksiz' specifies the size of the region of the key.
389 `sp' specifies the pointer to the variable into which the size of the region of the return
391 If successful, the return value is the pointer to the region of the value of the corresponding
392 record. `NULL' is returned if no record corresponds.
393 If the key of duplicated records is specified, the first one is selected. Because an
394 additional zero code is appended at the end of the region of the return value, the return
395 value can be treated as a character string. Because the region of the return value is
396 volatile and it may be spoiled by another operation of the database, the data should be copied
397 into another involatile buffer immediately. */
398 const void *tcbdbget3(TCBDB *bdb, const void *kbuf, int ksiz, int *sp);
401 /* Retrieve records in a B+ tree database object.
402 `bdb' specifies the B+ tree database object.
403 `kbuf' specifies the pointer to the region of the key.
404 `ksiz' specifies the size of the region of the key.
405 If successful, the return value is a list object of the values of the corresponding records.
406 `NULL' is returned if no record corresponds.
407 Because the object of the return value is created with the function `tclistnew', it should
408 be deleted with the function `tclistdel' when it is no longer in use. */
409 TCLIST *tcbdbget4(TCBDB *bdb, const void *kbuf, int ksiz);
412 /* Get the number of records corresponding a key in a B+ tree database object.
413 `bdb' specifies the B+ tree database object.
414 `kbuf' specifies the pointer to the region of the key.
415 `ksiz' specifies the size of the region of the key.
416 If successful, the return value is the number of the corresponding records, else, it is 0. */
417 int tcbdbvnum(TCBDB *bdb, const void *kbuf, int ksiz);
420 /* Get the number of records corresponding a string key in a B+ tree database object.
421 `bdb' specifies the B+ tree database object.
422 `kstr' specifies the string of the key.
423 If successful, the return value is the number of the corresponding records, else, it is 0. */
424 int tcbdbvnum2(TCBDB *bdb, const char *kstr);
427 /* Get the size of the value of a record in a B+ tree database object.
428 `bdb' specifies the B+ tree database object.
429 `kbuf' specifies the pointer to the region of the key.
430 `ksiz' specifies the size of the region of the key.
431 If successful, the return value is the size of the value of the corresponding record, else,
433 If the key of duplicated records is specified, the first one is selected. */
434 int tcbdbvsiz(TCBDB *bdb, const void *kbuf, int ksiz);
437 /* Get the size of the value of a string record in a B+ tree database object.
438 `bdb' specifies the B+ tree database object.
439 `kstr' specifies the string of the key.
440 If successful, the return value is the size of the value of the corresponding record, else,
442 If the key of duplicated records is specified, the first one is selected. */
443 int tcbdbvsiz2(TCBDB *bdb, const char *kstr);
446 /* Get keys of ranged records in a B+ tree database object.
447 `bdb' specifies the B+ tree database object.
448 `bkbuf' specifies the pointer to the region of the key of the beginning border. If it is
449 `NULL', the first record is specified.
450 `bksiz' specifies the size of the region of the beginning key.
451 `binc' specifies whether the beginning border is inclusive or not.
452 `ekbuf' specifies the pointer to the region of the key of the ending border. If it is `NULL',
453 the last record is specified.
454 `eksiz' specifies the size of the region of the ending key.
455 `einc' specifies whether the ending border is inclusive or not.
456 `max' specifies the maximum number of keys to be fetched. If it is negative, no limit is
458 The return value is a list object of the keys of the corresponding records. This function
459 does never fail and return an empty list even if no record corresponds.
460 Because the object of the return value is created with the function `tclistnew', it should
461 be deleted with the function `tclistdel' when it is no longer in use. */
462 TCLIST *tcbdbrange(TCBDB *bdb, const void *bkbuf, int bksiz, bool binc,
463 const void *ekbuf, int eksiz, bool einc, int max);
466 /* Get string keys of ranged records in a B+ tree database object.
467 `bdb' specifies the B+ tree database object.
468 `bkstr' specifies the string of the key of the beginning border. If it is `NULL', the first
470 `binc' specifies whether the beginning border is inclusive or not.
471 `ekstr' specifies the string of the key of the ending border. If it is `NULL', the last
473 `einc' specifies whether the ending border is inclusive or not.
474 `max' specifies the maximum number of keys to be fetched. If it is negative, no limit is
476 The return value is a list object of the keys of the corresponding records. This function
477 does never fail and return an empty list even if no record corresponds.
478 Because the object of the return value is created with the function `tclistnew', it should
479 be deleted with the function `tclistdel' when it is no longer in use. */
480 TCLIST *tcbdbrange2(TCBDB *bdb, const char *bkstr, bool binc,
481 const char *ekstr, bool einc, int max);
484 /* Get forward matching keys in a B+ tree database object.
485 `bdb' specifies the B+ tree database object.
486 `pbuf' specifies the pointer to the region of the prefix.
487 `psiz' specifies the size of the region of the prefix.
488 `max' specifies the maximum number of keys to be fetched. If it is negative, no limit is
490 The return value is a list object of the corresponding keys. This function does never fail
491 and return an empty list even if no key corresponds.
492 Because the object of the return value is created with the function `tclistnew', it should be
493 deleted with the function `tclistdel' when it is no longer in use. */
494 TCLIST *tcbdbfwmkeys(TCBDB *bdb, const void *pbuf, int psiz, int max);
497 /* Get forward matching string keys in a B+ tree database object.
498 `bdb' specifies the B+ tree database object.
499 `pstr' specifies the string of the prefix.
500 `max' specifies the maximum number of keys to be fetched. If it is negative, no limit is
502 The return value is a list object of the corresponding keys. This function does never fail
503 and return an empty list even if no key corresponds.
504 Because the object of the return value is created with the function `tclistnew', it should be
505 deleted with the function `tclistdel' when it is no longer in use. */
506 TCLIST *tcbdbfwmkeys2(TCBDB *bdb, const char *pstr, int max);
509 /* Synchronize updated contents of a B+ tree database object with the file and the device.
510 `bdb' specifies the B+ tree database object connected as a writer.
511 If successful, the return value is true, else, it is false.
512 This function is useful when another process connects the same database file. */
513 bool tcbdbsync(TCBDB *bdb);
516 /* Optimize the file of a B+ tree database object.
517 `bdb' specifies the B+ tree database object connected as a writer.
518 `lmemb' specifies the number of members in each leaf page. If it is not more than 0, the
519 current setting is not changed.
520 `nmemb' specifies the number of members in each non-leaf page. If it is not more than 0, the
521 current setting is not changed.
522 `bnum' specifies the number of elements of the bucket array. If it is not more than 0, the
523 default value is specified. The default value is two times of the number of pages.
524 `apow' specifies the size of record alignment by power of 2. If it is negative, the current
525 setting is not changed.
526 `fpow' specifies the maximum number of elements of the free block pool by power of 2. If it
527 is negative, the current setting is not changed.
528 `opts' specifies options by bitwise or: `BDBTLARGE' specifies that the size of the database
529 can be larger than 2GB by using 64-bit bucket array, `BDBTDEFLATE' specifies that each record
530 is compressed with Deflate encoding, `BDBTTCBS' specifies that each page is compressed with
531 TCBS encoding. If it is `UINT8_MAX', the current setting is not changed.
532 If successful, the return value is true, else, it is false.
533 This function is useful to reduce the size of the database file with data fragmentation by
534 successive updating. */
535 bool tcbdboptimize(TCBDB *bdb, int32_t lmemb, int32_t nmemb,
536 int64_t bnum, int8_t apow, int8_t fpow, uint8_t opts);
539 /* Remove all records of a B+ tree database object.
540 `bdb' specifies the B+ tree database object connected as a writer.
541 If successful, the return value is true, else, it is false. */
542 bool tcbdbvanish(TCBDB *bdb);
545 /* Copy the database file of a B+ tree database object.
546 `bdb' specifies the B+ tree database object.
547 `path' specifies the path of the destination file. If it begins with `@', the trailing
548 substring is executed as a command line.
549 If successful, the return value is true, else, it is false. False is returned if the executed
550 command returns non-zero code.
551 The database file is assured to be kept synchronized and not modified while the copying or
552 executing operation is in progress. So, this function is useful to create a backup file of
553 the database file. */
554 bool tcbdbcopy(TCBDB *bdb, const char *path);
557 /* Begin the transaction of a B+ tree database object.
558 `bdb' specifies the B+ tree database object connected as a writer.
559 If successful, the return value is true, else, it is false.
560 The database is locked by the thread while the transaction so that only one transaction can be
561 activated with a database object at the same time. Thus, the serializable isolation level is
562 assumed if every database operation is performed in the transaction. If the database is
563 closed during transaction, the transaction is aborted implicitly. */
564 bool tcbdbtranbegin(TCBDB *bdb);
567 /* Commit the transaction of a B+ tree database object.
568 `bdb' specifies the B+ tree database object connected as a writer.
569 If successful, the return value is true, else, it is false.
570 Update in the transaction is fixed when it is committed successfully. */
571 bool tcbdbtrancommit(TCBDB *bdb);
574 /* Abort the transaction of a B+ tree database object.
575 `bdb' specifies the B+ tree database object connected as a writer.
576 If successful, the return value is true, else, it is false.
577 Update in the transaction is discarded when it is aborted. The state of the database is
578 rollbacked to before transaction. */
579 bool tcbdbtranabort(TCBDB *bdb);
582 /* Get the file path of a B+ tree database object.
583 `bdb' specifies the B+ tree database object.
584 The return value is the path of the database file or `NULL' if the object does not connect to
585 any database file. */
586 const char *tcbdbpath(TCBDB *bdb);
589 /* Get the number of records of a B+ tree database object.
590 `bdb' specifies the B+ tree database object.
591 The return value is the number of records or 0 if the object does not connect to any database
593 uint64_t tcbdbrnum(TCBDB *bdb);
596 /* Get the size of the database file of a B+ tree database object.
597 `bdb' specifies the B+ tree database object.
598 The return value is the size of the database file or 0 if the object does not connect to any
600 uint64_t tcbdbfsiz(TCBDB *bdb);
603 /* Create a cursor object.
604 `bdb' specifies the B+ tree database object.
605 The return value is the new cursor object.
606 Note that the cursor is available only after initialization with the `tcbdbcurfirst' or the
607 `tcbdbcurjump' functions and so on. Moreover, the position of the cursor will be indefinite
608 when the database is updated after the initialization of the cursor. */
609 BDBCUR *tcbdbcurnew(TCBDB *bdb);
612 /* Delete a cursor object.
613 `cur' specifies the cursor object. */
614 void tcbdbcurdel(BDBCUR *cur);
617 /* Move a cursor object to the first record.
618 `cur' specifies the cursor object.
619 If successful, the return value is true, else, it is false. False is returned if there is
620 no record in the database. */
621 bool tcbdbcurfirst(BDBCUR *cur);
624 /* Move a cursor object to the last record.
625 `cur' specifies the cursor object.
626 If successful, the return value is true, else, it is false. False is returned if there is
627 no record in the database. */
628 bool tcbdbcurlast(BDBCUR *cur);
631 /* Move a cursor object to the front of records corresponding a key.
632 `cur' specifies the cursor object.
633 `kbuf' specifies the pointer to the region of the key.
634 `ksiz' specifies the size of the region of the key.
635 If successful, the return value is true, else, it is false. False is returned if there is
636 no record corresponding the condition.
637 The cursor is set to the first record corresponding the key or the next substitute if
638 completely matching record does not exist. */
639 bool tcbdbcurjump(BDBCUR *cur, const void *kbuf, int ksiz);
642 /* Move a cursor object to the front of records corresponding a key string.
643 `cur' specifies the cursor object.
644 `kstr' specifies the string of the key.
645 If successful, the return value is true, else, it is false. False is returned if there is
646 no record corresponding the condition.
647 The cursor is set to the first record corresponding the key or the next substitute if
648 completely matching record does not exist. */
649 bool tcbdbcurjump2(BDBCUR *cur, const char *kstr);
652 /* Move a cursor object to the previous record.
653 `cur' specifies the cursor object.
654 If successful, the return value is true, else, it is false. False is returned if there is
655 no previous record. */
656 bool tcbdbcurprev(BDBCUR *cur);
659 /* Move a cursor object to the next record.
660 `cur' specifies the cursor object.
661 If successful, the return value is true, else, it is false. False is returned if there is
663 bool tcbdbcurnext(BDBCUR *cur);
666 /* Insert a record around a cursor object.
667 `cur' specifies the cursor object of writer connection.
668 `vbuf' specifies the pointer to the region of the value.
669 `vsiz' specifies the size of the region of the value.
670 `cpmode' specifies detail adjustment: `BDBCPCURRENT', which means that the value of the
671 current record is overwritten, `BDBCPBEFORE', which means that the new record is inserted
672 before the current record, `BDBCPAFTER', which means that the new record is inserted after the
674 If successful, the return value is true, else, it is false. False is returned when the cursor
675 is at invalid position.
676 After insertion, the cursor is moved to the inserted record. */
677 bool tcbdbcurput(BDBCUR *cur, const void *vbuf, int vsiz, int cpmode);
680 /* Insert a string record around a cursor object.
681 `cur' specifies the cursor object of writer connection.
682 `vstr' specifies the string of the value.
683 `cpmode' specifies detail adjustment: `BDBCPCURRENT', which means that the value of the
684 current record is overwritten, `BDBCPBEFORE', which means that the new record is inserted
685 before the current record, `BDBCPAFTER', which means that the new record is inserted after the
687 If successful, the return value is true, else, it is false. False is returned when the cursor
688 is at invalid position.
689 After insertion, the cursor is moved to the inserted record. */
690 bool tcbdbcurput2(BDBCUR *cur, const char *vstr, int cpmode);
693 /* Delete the record where a cursor object is.
694 `cur' specifies the cursor object of writer connection.
695 If successful, the return value is true, else, it is false. False is returned when the cursor
696 is at invalid position.
697 After deletion, the cursor is moved to the next record if possible. */
698 bool tcbdbcurout(BDBCUR *cur);
701 /* Get the key of the record where the cursor object is.
702 `cur' specifies the cursor object.
703 `sp' specifies the pointer to the variable into which the size of the region of the return
705 If successful, the return value is the pointer to the region of the key, else, it is `NULL'.
706 `NULL' is returned when the cursor is at invalid position.
707 Because an additional zero code is appended at the end of the region of the return value,
708 the return value can be treated as a character string. Because the region of the return
709 value is allocated with the `malloc' call, it should be released with the `free' call when
710 it is no longer in use. */
711 char *tcbdbcurkey(BDBCUR *cur, int *sp);
714 /* Get the key string of the record where the cursor object is.
715 `cur' specifies the cursor object.
716 If successful, the return value is the string of the key, else, it is `NULL'. `NULL' is
717 returned when the cursor is at invalid position.
718 Because the region of the return value is allocated with the `malloc' call, it should be
719 released with the `free' call when it is no longer in use. */
720 char *tcbdbcurkey2(BDBCUR *cur);
723 /* Get the key of the record where the cursor object is, as a volatile buffer.
724 `cur' specifies the cursor object.
725 `sp' specifies the pointer to the variable into which the size of the region of the return
727 If successful, the return value is the pointer to the region of the key, else, it is `NULL'.
728 `NULL' is returned when the cursor is at invalid position.
729 Because an additional zero code is appended at the end of the region of the return value,
730 the return value can be treated as a character string. Because the region of the return value
731 is volatile and it may be spoiled by another operation of the database, the data should be
732 copied into another involatile buffer immediately. */
733 const char *tcbdbcurkey3(BDBCUR *cur, int *sp);
736 /* Get the value of the record where the cursor object is.
737 `cur' specifies the cursor object.
738 `sp' specifies the pointer to the variable into which the size of the region of the return
740 If successful, the return value is the pointer to the region of the value, else, it is `NULL'.
741 `NULL' is returned when the cursor is at invalid position.
742 Because an additional zero code is appended at the end of the region of the return value,
743 the return value can be treated as a character string. Because the region of the return
744 value is allocated with the `malloc' call, it should be released with the `free' call when
745 it is no longer in use. */
746 char *tcbdbcurval(BDBCUR *cur, int *sp);
749 /* Get the value string of the record where the cursor object is.
750 `cur' specifies the cursor object.
751 If successful, the return value is the string of the value, else, it is `NULL'. `NULL' is
752 returned when the cursor is at invalid position.
753 Because the region of the return value is allocated with the `malloc' call, it should be
754 released with the `free' call when it is no longer in use. */
755 char *tcbdbcurval2(BDBCUR *cur);
758 /* Get the value of the record where the cursor object is, as a volatile buffer.
759 `cur' specifies the cursor object.
760 `sp' specifies the pointer to the variable into which the size of the region of the return
762 If successful, the return value is the pointer to the region of the value, else, it is `NULL'.
763 `NULL' is returned when the cursor is at invalid position.
764 Because an additional zero code is appended at the end of the region of the return value,
765 the return value can be treated as a character string. Because the region of the return value
766 is volatile and it may be spoiled by another operation of the database, the data should be
767 copied into another involatile buffer immediately. */
768 const char *tcbdbcurval3(BDBCUR *cur, int *sp);
771 /* Get the key and the value of the record where the cursor object is.
772 `cur' specifies the cursor object.
773 `kxstr' specifies the object into which the key is wrote down.
774 `vxstr' specifies the object into which the value is wrote down.
775 If successful, the return value is true, else, it is false. False is returned when the cursor
776 is at invalid position. */
777 bool tcbdbcurrec(BDBCUR *cur, TCXSTR *kxstr, TCXSTR *vxstr);
781 /*************************************************************************************************
782 * features for experts
783 *************************************************************************************************/
786 /* Set the error code of a B+ tree database object.
787 `bdb' specifies the B+ tree database object.
788 `ecode' specifies the error code.
789 `file' specifies the file name of the code.
790 `line' specifies the line number of the code.
791 `func' specifies the function name of the code. */
792 void tcbdbsetecode(TCBDB *bdb, int ecode, const char *filename, int line, const char *func);
795 /* Set the file descriptor for debugging output.
796 `bdb' specifies the B+ tree database object.
797 `fd' specifies the file descriptor for debugging output. */
798 void tcbdbsetdbgfd(TCBDB *bdb, int fd);
801 /* Get the file descriptor for debugging output.
802 `bdb' specifies the B+ tree database object.
803 The return value is the file descriptor for debugging output. */
804 int tcbdbdbgfd(TCBDB *bdb);
807 /* Synchronize updating contents on memory.
808 `bdb' specifies the B+ tree database object connected as a writer.
809 `phys' specifies whether to synchronize physically.
810 If successful, the return value is true, else, it is false. */
811 bool tcbdbmemsync(TCBDB *bdb, bool phys);
814 /* Get the comparison function of a B+ tree database object.
815 `bdb' specifies the B+ tree database object.
816 The return value is the pointer to the comparison function. */
817 BDBCMP tcbdbcmpfunc(TCBDB *bdb);
820 /* Get the opaque object for the comparison function of a B+ tree database object.
821 `bdb' specifies the B+ tree database object.
822 The return value is the opaque object for the comparison function. */
823 void *tcbdbcmpop(TCBDB *bdb);
826 /* Get the maximum number of cached leaf nodes of a B+ tree database object.
827 `bdb' specifies the B+ tree database object.
828 The return value is the maximum number of cached leaf nodes. */
829 uint32_t tcbdblmemb(TCBDB *bdb);
832 /* Get the maximum number of cached non-leaf nodes of a B+ tree database object.
833 `bdb' specifies the B+ tree database object.
834 The return value is the maximum number of cached non-leaf nodes. */
835 uint32_t tcbdbnmemb(TCBDB *bdb);
838 /* Get the number of the leaf nodes of B+ tree database object.
839 `bdb' specifies the B+ tree database object.
840 If successful, the return value is the number of the leaf nodes or 0 if the object does not
841 connect to any database file. */
842 uint64_t tcbdblnum(TCBDB *bdb);
845 /* Get the number of the non-leaf nodes of B+ tree database object.
846 `bdb' specifies the B+ tree database object.
847 If successful, the return value is the number of the non-leaf nodes or 0 if the object does
848 not connect to any database file. */
849 uint64_t tcbdbnnum(TCBDB *bdb);
852 /* Get the number of elements of the bucket array of a B+ tree database object.
853 `bdb' specifies the B+ tree database object.
854 The return value is the number of elements of the bucket array or 0 if the object does not
855 connect to any database file. */
856 uint64_t tcbdbbnum(TCBDB *bdb);
859 /* Get the record alignment of a B+ tree database object.
860 `bdb' specifies the B+ tree database object.
861 The return value is the record alignment or 0 if the object does not connect to any database
863 uint32_t tcbdbalign(TCBDB *bdb);
866 /* Get the maximum number of the free block pool of a B+ tree database object.
867 `bdb' specifies the B+ tree database object.
868 The return value is the maximum number of the free block pool or 0 if the object does not
869 connect to any database file. */
870 uint32_t tcbdbfbpmax(TCBDB *bdb);
873 /* Get the inode number of the database file of a B+ tree database object.
874 `bdb' specifies the B+ tree database object.
875 The return value is the inode number of the database file or 0 the object does not connect to
876 any database file. */
877 uint64_t tcbdbinode(TCBDB *bdb);
880 /* Get the modification time of the database file of a B+ tree database object.
881 `bdb' specifies the B+ tree database object.
882 The return value is the inode number of the database file or 0 the object does not connect to
883 any database file. */
884 time_t tcbdbmtime(TCBDB *bdb);
887 /* Get the additional flags of a B+ tree database object.
888 `bdb' specifies the B+ tree database object.
889 The return value is the additional flags. */
890 uint8_t tcbdbflags(TCBDB *bdb);
893 /* Get the options of a B+ tree database object.
894 `bdb' specifies the B+ tree database object.
895 The return value is the options. */
896 uint8_t tcbdbopts(TCBDB *bdb);
899 /* Get the pointer to the opaque field of a B+ tree database object.
900 `bdb' specifies the B+ tree database object.
901 The return value is the pointer to the opaque field whose size is 128 bytes. */
902 char *tcbdbopaque(TCBDB *bdb);
905 /* Get the number of used elements of the bucket array of a B+ tree database object.
906 `bdb' specifies the B+ tree database object.
907 The return value is the number of used elements of the bucket array or 0 if the object does not
908 connect to any database file. */
909 uint64_t tcbdbbnumused(TCBDB *bdb);
912 /* Set the maximum size of each leaf node.
913 `bdb' specifies the B+ tree database object which is not opened.
914 `lsmax' specifies the maximum size of each leaf node. If it is not more than 0, the maximum
916 If successful, the return value is true, else, it is false.
917 Note that the tuning parameters of the database should be set before the database is opened. */
918 bool tcbdbsetlsmax(TCBDB *bdb, uint32_t lsmax);
921 /* Set the capacity number of records.
922 `bdb' specifies the B+ tree database object which is not opened.
923 `capnum' specifies the capacity number of records. If it is not more than 0, the capacity is
925 If successful, the return value is true, else, it is false.
926 When the number of records exceeds the capacity, forehand records are removed implicitly.
927 Note that the tuning parameters of the database should be set before the database is opened. */
928 bool tcbdbsetcapnum(TCBDB *bdb, uint64_t capnum);
931 /* Store a new record into a B+ tree database object with backward duplication.
932 `bdb' specifies the B+ tree database object connected as a writer.
933 `kbuf' specifies the pointer to the region of the key.
934 `ksiz' specifies the size of the region of the key.
935 `vbuf' specifies the pointer to the region of the value.
936 `vsiz' specifies the size of the region of the value.
937 If successful, the return value is true, else, it is false.
938 If a record with the same key exists in the database, the new record is placed after the
940 bool tcbdbputdupback(TCBDB *bdb, const void *kbuf, int ksiz, const void *vbuf, int vsiz);
943 /* Store a new string record into a B+ tree database object with backward duplication.
944 `bdb' specifies the B+ tree database object connected as a writer.
945 `kstr' specifies the string of the key.
946 `vstr' specifies the string of the value.
947 If successful, the return value is true, else, it is false.
948 If a record with the same key exists in the database, the new record is placed after the
950 bool tcbdbputdupback2(TCBDB *bdb, const char *kstr, const char *vstr);
953 /* Move a cursor object to the rear of records corresponding a key.
954 `cur' specifies the cursor object.
955 `kbuf' specifies the pointer to the region of the key.
956 `ksiz' specifies the size of the region of the key.
957 If successful, the return value is true, else, it is false. False is returned if there is
958 no record corresponding the condition.
959 The cursor is set to the last record corresponding the key or the previous substitute if
960 completely matching record does not exist. */
961 bool tcbdbcurjumpback(BDBCUR *cur, const void *kbuf, int ksiz);
964 /* Move a cursor object to the rear of records corresponding a key string.
965 `cur' specifies the cursor object.
966 `kstr' specifies the string of the key.
967 If successful, the return value is true, else, it is false. False is returned if there is
968 no record corresponding the condition.
969 The cursor is set to the last record corresponding the key or the previous substitute if
970 completely matching record does not exist. */
971 bool tcbdbcurjumpback2(BDBCUR *cur, const char *kstr);
974 /* Compare two keys by lexical order.
975 `aptr' specifies the pointer to the region of one key.
976 `asiz' specifies the size of the region of one key.
977 `bptr' specifies the pointer to the region of the other key.
978 `bsiz' specifies the size of the region of the other key.
979 `op' specifies the pointer to the optional opaque object.
980 The return value is positive if the former is big, negative if the latter is big, 0 if both
982 int tcbdbcmplexical(const char *aptr, int asiz, const char *bptr, int bsiz, void *op);
985 /* Compare two keys as decimal strings of real numbers.
986 `aptr' specifies the pointer to the region of one key.
987 `asiz' specifies the size of the region of one key.
988 `bptr' specifies the pointer to the region of the other key.
989 `bsiz' specifies the size of the region of the other key.
991 The return value is positive if the former is big, negative if the latter is big, 0 if both
993 int tcbdbcmpdecimal(const char *aptr, int asiz, const char *bptr, int bsiz, void *op);
996 /* Compare two keys as 32-bit integers in the native byte order.
997 `aptr' specifies the pointer to the region of one key.
998 `asiz' specifies the size of the region of one key.
999 `bptr' specifies the pointer to the region of the other key.
1000 `bsiz' specifies the size of the region of the other key.
1002 The return value is positive if the former is big, negative if the latter is big, 0 if both
1004 int tcbdbcmpint32(const char *aptr, int asiz, const char *bptr, int bsiz, void *op);
1007 /* Compare two keys as 64-bit integers in the native byte order.
1008 `aptr' specifies the pointer to the region of one key.
1009 `asiz' specifies the size of the region of one key.
1010 `bptr' specifies the pointer to the region of the other key.
1011 `bsiz' specifies the size of the region of the other key.
1013 The return value is positive if the former is big, negative if the latter is big, 0 if both
1015 int tcbdbcmpint64(const char *aptr, int asiz, const char *bptr, int bsiz, void *op);
1018 /* tricks for backward compatibility */
1019 #define tcbdbrange3 tcbdbfwmkeys2
1024 #endif /* duplication check */