tokyomessenger 0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. data/COPYING +504 -0
  2. data/README.rdoc +224 -0
  3. data/Rakefile +72 -0
  4. data/benchmarks/balancer.rb +101 -0
  5. data/benchmarks/bulk_db.rb +92 -0
  6. data/benchmarks/bulk_table.rb +104 -0
  7. data/benchmarks/db.rb +131 -0
  8. data/benchmarks/table.rb +186 -0
  9. data/ext/a.h +496 -0
  10. data/ext/extconf.rb +27 -0
  11. data/ext/md5.c +381 -0
  12. data/ext/md5.h +101 -0
  13. data/ext/tc_myconf.c +493 -0
  14. data/ext/tc_myconf.h +543 -0
  15. data/ext/tcadb.c +4339 -0
  16. data/ext/tcadb.h +533 -0
  17. data/ext/tcbdb.c +4180 -0
  18. data/ext/tcbdb.h +1086 -0
  19. data/ext/tcfdb.c +2746 -0
  20. data/ext/tcfdb.h +842 -0
  21. data/ext/tchdb.c +5153 -0
  22. data/ext/tchdb.h +856 -0
  23. data/ext/tcrdb.c +2637 -0
  24. data/ext/tcrdb.h +785 -0
  25. data/ext/tctdb.c +6199 -0
  26. data/ext/tctdb.h +1070 -0
  27. data/ext/tcutil.c +10528 -0
  28. data/ext/tcutil.h +4166 -0
  29. data/ext/tokyo_messenger.c +147 -0
  30. data/ext/tokyo_messenger.h +49 -0
  31. data/ext/tokyo_messenger_db.c +227 -0
  32. data/ext/tokyo_messenger_db.h +8 -0
  33. data/ext/tokyo_messenger_module.c +453 -0
  34. data/ext/tokyo_messenger_module.h +10 -0
  35. data/ext/tokyo_messenger_query.c +226 -0
  36. data/ext/tokyo_messenger_query.h +9 -0
  37. data/ext/tokyo_messenger_table.c +319 -0
  38. data/ext/tokyo_messenger_table.h +8 -0
  39. data/ext/tt_myconf.c +169 -0
  40. data/ext/tt_myconf.h +408 -0
  41. data/ext/ttutil.c +1509 -0
  42. data/ext/ttutil.h +480 -0
  43. data/lib/tokyo_messenger/balancer.rb +188 -0
  44. data/spec/ext.lua +4 -0
  45. data/spec/plu_db.rb +538 -0
  46. data/spec/spec.rb +1 -0
  47. data/spec/spec_base.rb +17 -0
  48. data/spec/start_tyrants.sh +36 -0
  49. data/spec/stop_tyrants.sh +9 -0
  50. data/spec/tokyo_tyrant_balancer_db_spec.rb +160 -0
  51. data/spec/tokyo_tyrant_balancer_table_spec.rb +177 -0
  52. data/spec/tokyo_tyrant_query_spec.rb +159 -0
  53. data/spec/tokyo_tyrant_spec.rb +254 -0
  54. data/spec/tokyo_tyrant_table_spec.rb +301 -0
  55. metadata +117 -0
@@ -0,0 +1,842 @@
1
+ /*************************************************************************************************
2
+ * The fixed-length database API of Tokyo Cabinet
3
+ * Copyright (C) 2006-2009 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
+ *************************************************************************************************/
15
+
16
+
17
+ #ifndef _TCFDB_H /* duplication check */
18
+ #define _TCFDB_H
19
+
20
+ #include "tcutil.h"
21
+
22
+ /*************************************************************************************************
23
+ * API
24
+ *************************************************************************************************/
25
+
26
+
27
+ typedef struct { /* type of structure for a fixed-length database */
28
+ void *mmtx; /* mutex for method */
29
+ void *amtx; /* mutex for attribute */
30
+ void *rmtxs; /* mutexes for records */
31
+ void *tmtx; /* mutex for transaction */
32
+ void *wmtx; /* mutex for write ahead logging */
33
+ void *eckey; /* key for thread specific error code */
34
+ char *rpath; /* real path for locking */
35
+ uint8_t type; /* database type */
36
+ uint8_t flags; /* additional flags */
37
+ uint32_t width; /* width of the value of each record */
38
+ uint64_t limsiz; /* limit size of the file */
39
+ int wsiz; /* size of the width region */
40
+ int rsiz; /* size of each record */
41
+ uint64_t limid; /* limit ID number */
42
+ char *path; /* path of the database file */
43
+ int fd; /* file descriptor of the database file */
44
+ uint32_t omode; /* open mode */
45
+ uint64_t rnum; /* number of the records */
46
+ uint64_t fsiz; /* size of the database file */
47
+ uint64_t min; /* minimum ID number */
48
+ uint64_t max; /* maximum ID number */
49
+ uint64_t iter; /* ID number of the iterator */
50
+ char *map; /* pointer to the mapped memory */
51
+ unsigned char *array; /* pointer to the array region */
52
+ int ecode; /* last happened error code */
53
+ bool fatal; /* whether a fatal error occured */
54
+ uint64_t inode; /* inode number */
55
+ time_t mtime; /* modification time */
56
+ bool tran; /* whether in the transaction */
57
+ int walfd; /* file descriptor of write ahead logging */
58
+ uint64_t walend; /* end offset of write ahead logging */
59
+ int dbgfd; /* file descriptor for debugging */
60
+ int64_t cnt_writerec; /* tesing counter for record write times */
61
+ int64_t cnt_readrec; /* tesing counter for record read times */
62
+ int64_t cnt_truncfile; /* tesing counter for file truncate times */
63
+ } TCFDB;
64
+
65
+ enum { /* enumeration for additional flags */
66
+ FDBFOPEN = 1 << 0, /* whether opened */
67
+ FDBFFATAL = 1 << 1 /* whetehr with fatal error */
68
+ };
69
+
70
+ enum { /* enumeration for open modes */
71
+ FDBOREADER = 1 << 0, /* open as a reader */
72
+ FDBOWRITER = 1 << 1, /* open as a writer */
73
+ FDBOCREAT = 1 << 2, /* writer creating */
74
+ FDBOTRUNC = 1 << 3, /* writer truncating */
75
+ FDBONOLCK = 1 << 4, /* open without locking */
76
+ FDBOLCKNB = 1 << 5, /* lock without blocking */
77
+ FDBOTSYNC = 1 << 6 /* synchronize every transaction */
78
+ };
79
+
80
+ enum { /* enumeration for ID constants */
81
+ FDBIDMIN = -1, /* minimum number */
82
+ FDBIDPREV = -2, /* less by one than the minimum */
83
+ FDBIDMAX = -3, /* maximum number */
84
+ FDBIDNEXT = -4 /* greater by one than the miximum */
85
+ };
86
+
87
+
88
+ /* Get the message string corresponding to an error code.
89
+ `ecode' specifies the error code.
90
+ The return value is the message string of the error code. */
91
+ const char *tcfdberrmsg(int ecode);
92
+
93
+
94
+ /* Create a fixed-length database object.
95
+ The return value is the new fixed-length database object. */
96
+ TCFDB *tcfdbnew(void);
97
+
98
+
99
+ /* Delete a fixed-length database object.
100
+ `fdb' specifies the fixed-length database object.
101
+ If the database is not closed, it is closed implicitly. Note that the deleted object and its
102
+ derivatives can not be used anymore. */
103
+ void tcfdbdel(TCFDB *fdb);
104
+
105
+
106
+ /* Get the last happened error code of a fixed-length database object.
107
+ `fdb' specifies the fixed-length database object.
108
+ The return value is the last happened error code.
109
+ The following error codes are defined: `TCESUCCESS' for success, `TCETHREAD' for threading
110
+ error, `TCEINVALID' for invalid operation, `TCENOFILE' for file not found, `TCENOPERM' for no
111
+ permission, `TCEMETA' for invalid meta data, `TCERHEAD' for invalid record header, `TCEOPEN'
112
+ for open error, `TCECLOSE' for close error, `TCETRUNC' for trunc error, `TCESYNC' for sync
113
+ error, `TCESTAT' for stat error, `TCESEEK' for seek error, `TCEREAD' for read error,
114
+ `TCEWRITE' for write error, `TCEMMAP' for mmap error, `TCELOCK' for lock error, `TCEUNLINK'
115
+ for unlink error, `TCERENAME' for rename error, `TCEMKDIR' for mkdir error, `TCERMDIR' for
116
+ rmdir error, `TCEKEEP' for existing record, `TCENOREC' for no record found, and `TCEMISC' for
117
+ miscellaneous error. */
118
+ int tcfdbecode(TCFDB *fdb);
119
+
120
+
121
+ /* Set mutual exclusion control of a fixed-length database object for threading.
122
+ `fdb' specifies the fixed-length database object which is not opened.
123
+ If successful, the return value is true, else, it is false.
124
+ Note that the mutual exclusion control is needed if the object is shared by plural threads and
125
+ this function should be called before the database is opened. */
126
+ bool tcfdbsetmutex(TCFDB *fdb);
127
+
128
+
129
+ /* Set the tuning parameters of a fixed-length database object.
130
+ `fdb' specifies the fixed-length database object which is not opened.
131
+ `width' specifies the width of the value of each record. If it is not more than 0, the
132
+ default value is specified. The default value is 255.
133
+ `limsiz' specifies the limit size of the database file. If it is not more than 0, the default
134
+ value is specified. The default value is 268435456.
135
+ If successful, the return value is true, else, it is false.
136
+ Note that the tuning parameters should be set before the database is opened. */
137
+ bool tcfdbtune(TCFDB *fdb, int32_t width, int64_t limsiz);
138
+
139
+
140
+ /* Open a database file and connect a fixed-length database object.
141
+ `fdb' specifies the fixed-length database object which is not opened.
142
+ `path' specifies the path of the database file.
143
+ `omode' specifies the connection mode: `FDBOWRITER' as a writer, `FDBOREADER' as a reader.
144
+ If the mode is `FDBOWRITER', the following may be added by bitwise-or: `FDBOCREAT', which
145
+ means it creates a new database if not exist, `FDBOTRUNC', which means it creates a new
146
+ database regardless if one exists, `FDBOTSYNC', which means every transaction synchronizes
147
+ updated contents with the device. Both of `FDBOREADER' and `FDBOWRITER' can be added to by
148
+ bitwise-or: `FDBONOLCK', which means it opens the database file without file locking, or
149
+ `FDBOLCKNB', which means locking is performed without blocking.
150
+ If successful, the return value is true, else, it is false. */
151
+ bool tcfdbopen(TCFDB *fdb, const char *path, int omode);
152
+
153
+
154
+ /* Close a fixed-length database object.
155
+ `fdb' specifies the fixed-length database object.
156
+ If successful, the return value is true, else, it is false.
157
+ Update of a database is assured to be written when the database is closed. If a writer opens
158
+ a database but does not close it appropriately, the database will be broken. */
159
+ bool tcfdbclose(TCFDB *fdb);
160
+
161
+
162
+ /* Store a record into a fixed-length database object.
163
+ `fdb' specifies the fixed-length database object connected as a writer.
164
+ `id' specifies the ID number. It should be more than 0. If it is `FDBIDMIN', the minimum ID
165
+ number of existing records is specified. If it is `FDBIDPREV', the number less by one than
166
+ the minimum ID number of existing records is specified. If it is `FDBIDMAX', the maximum ID
167
+ number of existing records is specified. If it is `FDBIDNEXT', the number greater by one than
168
+ the maximum ID number of existing records is specified.
169
+ `vbuf' specifies the pointer to the region of the value.
170
+ `vsiz' specifies the size of the region of the value. If the size of the value is greater
171
+ than the width tuning parameter of the database, the size is cut down to the width.
172
+ If successful, the return value is true, else, it is false.
173
+ If a record with the same key exists in the database, it is overwritten. */
174
+ bool tcfdbput(TCFDB *fdb, int64_t id, const void *vbuf, int vsiz);
175
+
176
+
177
+ /* Store a record with a decimal key into a fixed-length database object.
178
+ `fdb' specifies the fixed-length database object connected as a writer.
179
+ `kbuf' specifies the pointer to the region of the decimal key. It should be more than 0. If
180
+ it is "min", the minimum ID number of existing records is specified. If it is "prev", the
181
+ number less by one than the minimum ID number of existing records is specified. If it is
182
+ "max", the maximum ID number of existing records is specified. If it is "next", the number
183
+ greater by one than the maximum ID number of existing records is specified.
184
+ `ksiz' specifies the size of the region of the key.
185
+ `vbuf' specifies the pointer to the region of the value.
186
+ `vsiz' specifies the size of the region of the value. If the size of the value is greater
187
+ than the width tuning parameter of the database, the size is cut down to the width.
188
+ If successful, the return value is true, else, it is false.
189
+ If a record with the same key exists in the database, it is overwritten. */
190
+ bool tcfdbput2(TCFDB *fdb, const void *kbuf, int ksiz, const void *vbuf, int vsiz);
191
+
192
+
193
+ /* Store a string record with a decimal key into a fixed-length database object.
194
+ `fdb' specifies the fixed-length database object connected as a writer.
195
+ `kstr' specifies the string of the decimal key. It should be more than 0. If it is "min",
196
+ the minimum ID number of existing records is specified. If it is "prev", the number less by
197
+ one than the minimum ID number of existing records is specified. If it is "max", the maximum
198
+ ID number of existing records is specified. If it is "next", the number greater by one than
199
+ the maximum ID number of existing records is specified.
200
+ `vstr' specifies the string of the value.
201
+ If successful, the return value is true, else, it is false.
202
+ If a record with the same key exists in the database, it is overwritten. */
203
+ bool tcfdbput3(TCFDB *fdb, const char *kstr, const void *vstr);
204
+
205
+
206
+ /* Store a new record into a fixed-length database object.
207
+ `fdb' specifies the fixed-length database object connected as a writer.
208
+ `id' specifies the ID number. It should be more than 0. If it is `FDBIDMIN', the minimum ID
209
+ number of existing records is specified. If it is `FDBIDPREV', the number less by one than
210
+ the minimum ID number of existing records is specified. If it is `FDBIDMAX', the maximum ID
211
+ number of existing records is specified. If it is `FDBIDNEXT', the number greater by one than
212
+ the maximum ID number of existing records is specified.
213
+ `vbuf' specifies the pointer to the region of the value.
214
+ `vsiz' specifies the size of the region of the value. If the size of the value is greater
215
+ than the width tuning parameter of the database, the size is cut down to the width.
216
+ If successful, the return value is true, else, it is false.
217
+ If a record with the same key exists in the database, this function has no effect. */
218
+ bool tcfdbputkeep(TCFDB *fdb, int64_t id, const void *vbuf, int vsiz);
219
+
220
+
221
+ /* Store a new record with a decimal key into a fixed-length database object.
222
+ `fdb' specifies the fixed-length database object connected as a writer.
223
+ `kbuf' specifies the pointer to the region of the decimal key. It should be more than 0. If
224
+ it is "min", the minimum ID number of existing records is specified. If it is "prev", the
225
+ number less by one than the minimum ID number of existing records is specified. If it is
226
+ "max", the maximum ID number of existing records is specified. If it is "next", the number
227
+ greater by one than the maximum ID number of existing records is specified.
228
+ `ksiz' specifies the size of the region of the key.
229
+ `vbuf' specifies the pointer to the region of the value.
230
+ `vsiz' specifies the size of the region of the value. If the size of the value is greater
231
+ than the width tuning parameter of the database, the size is cut down to the width.
232
+ If successful, the return value is true, else, it is false.
233
+ If a record with the same key exists in the database, this function has no effect. */
234
+ bool tcfdbputkeep2(TCFDB *fdb, const void *kbuf, int ksiz, const void *vbuf, int vsiz);
235
+
236
+
237
+ /* Store a new string record with a decimal key into a fixed-length database object.
238
+ `fdb' specifies the fixed-length database object connected as a writer.
239
+ `kstr' specifies the string of the decimal key. It should be more than 0. If it is "min",
240
+ the minimum ID number of existing records is specified. If it is "prev", the number less by
241
+ one than the minimum ID number of existing records is specified. If it is "max", the maximum
242
+ ID number of existing records is specified. If it is "next", the number greater by one than
243
+ the maximum ID number of existing records is specified.
244
+ `vstr' specifies the string 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, this function has no effect. */
247
+ bool tcfdbputkeep3(TCFDB *fdb, const char *kstr, const void *vstr);
248
+
249
+
250
+ /* Concatenate a value at the end of the existing record in a fixed-length database object.
251
+ `fdb' specifies the fixed-length database object connected as a writer.
252
+ `id' specifies the ID number. It should be more than 0. If it is `FDBIDMIN', the minimum ID
253
+ number of existing records is specified. If it is `FDBIDPREV', the number less by one than
254
+ the minimum ID number of existing records is specified. If it is `FDBIDMAX', the maximum ID
255
+ number of existing records is specified. If it is `FDBIDNEXT', the number greater by one than
256
+ the maximum ID number of existing records is specified.
257
+ `vbuf' specifies the pointer to the region of the value.
258
+ `vsiz' specifies the size of the region of the value. If the size of the value is greater
259
+ than the width tuning parameter of the database, the size is cut down to the width.
260
+ If successful, the return value is true, else, it is false.
261
+ If there is no corresponding record, a new record is created. */
262
+ bool tcfdbputcat(TCFDB *fdb, int64_t id, const void *vbuf, int vsiz);
263
+
264
+
265
+ /* Concatenate a value with a decimal key in a fixed-length database object.
266
+ `fdb' specifies the fixed-length database object connected as a writer.
267
+ `kbuf' specifies the pointer to the region of the decimal key. It should be more than 0. If
268
+ it is "min", the minimum ID number of existing records is specified. If it is "prev", the
269
+ number less by one than the minimum ID number of existing records is specified. If it is
270
+ "max", the maximum ID number of existing records is specified. If it is "next", the number
271
+ greater by one than the maximum ID number of existing records is specified.
272
+ `ksiz' specifies the size of the region of the key.
273
+ `vbuf' specifies the pointer to the region of the value.
274
+ `vsiz' specifies the size of the region of the value. If the size of the value is greater
275
+ than the width tuning parameter of the database, the size is cut down to the width.
276
+ If successful, the return value is true, else, it is false.
277
+ If there is no corresponding record, a new record is created. */
278
+ bool tcfdbputcat2(TCFDB *fdb, const void *kbuf, int ksiz, const void *vbuf, int vsiz);
279
+
280
+
281
+ /* Concatenate a string value with a decimal key in a fixed-length database object.
282
+ `fdb' specifies the fixed-length database object connected as a writer.
283
+ `kstr' specifies the string of the decimal key. It should be more than 0. If it is "min",
284
+ the minimum ID number of existing records is specified. If it is "prev", the number less by
285
+ one than the minimum ID number of existing records is specified. If it is "max", the maximum
286
+ ID number of existing records is specified. If it is "next", the number greater by one than
287
+ the maximum ID number of existing records is specified.
288
+ `vstr' specifies the string of the value.
289
+ If successful, the return value is true, else, it is false.
290
+ If there is no corresponding record, a new record is created. */
291
+ bool tcfdbputcat3(TCFDB *fdb, const char *kstr, const void *vstr);
292
+
293
+
294
+ /* Remove a record of a fixed-length database object.
295
+ `fdb' specifies the fixed-length database object connected as a writer.
296
+ `id' specifies the ID number. It should be more than 0. If it is `FDBIDMIN', the minimum ID
297
+ number of existing records is specified. If it is `FDBIDMAX', the maximum ID number of
298
+ existing records is specified.
299
+ If successful, the return value is true, else, it is false. */
300
+ bool tcfdbout(TCFDB *fdb, int64_t id);
301
+
302
+
303
+ /* Remove a record with a decimal key of a fixed-length database object.
304
+ `fdb' specifies the fixed-length database object connected as a writer.
305
+ `kbuf' specifies the pointer to the region of the decimal key. It should be more than 0. If
306
+ it is "min", the minimum ID number of existing records is specified. If it is "max", the
307
+ maximum ID number of existing records is specified.
308
+ `ksiz' specifies the size of the region of the key.
309
+ If successful, the return value is true, else, it is false. */
310
+ bool tcfdbout2(TCFDB *fdb, const void *kbuf, int ksiz);
311
+
312
+
313
+ /* Remove a string record with a decimal key of a fixed-length database object.
314
+ `fdb' specifies the fixed-length database object connected as a writer.
315
+ `kstr' specifies the string of the decimal key. It should be more than 0. If it is "min",
316
+ the minimum ID number of existing records is specified. If it is "max", the maximum ID number
317
+ of existing records is specified.
318
+ If successful, the return value is true, else, it is false. */
319
+ bool tcfdbout3(TCFDB *fdb, const char *kstr);
320
+
321
+
322
+ /* Retrieve a record in a fixed-length database object.
323
+ `fdb' specifies the fixed-length database object.
324
+ `id' specifies the ID number. It should be more than 0. If it is `FDBIDMIN', the minimum ID
325
+ number of existing records is specified. If it is `FDBIDMAX', the maximum ID number of
326
+ existing records is specified.
327
+ `sp' specifies the pointer to the variable into which the size of the region of the return
328
+ value is assigned.
329
+ If successful, the return value is the pointer to the region of the value of the corresponding
330
+ record. `NULL' is returned if no record corresponds.
331
+ Because an additional zero code is appended at the end of the region of the return value,
332
+ the return value can be treated as a character string. Because the region of the return
333
+ value is allocated with the `malloc' call, it should be released with the `free' call when
334
+ it is no longer in use. */
335
+ void *tcfdbget(TCFDB *fdb, int64_t id, int *sp);
336
+
337
+
338
+ /* Retrieve a record with a decimal key in a fixed-length database object.
339
+ `fdb' specifies the fixed-length database object.
340
+ `kbuf' specifies the pointer to the region of the decimal key. It should be more than 0. If
341
+ it is "min", the minimum ID number of existing records is specified. If it is "max", the
342
+ maximum ID number of existing records is specified.
343
+ `ksiz' specifies the size of the region of the key.
344
+ `sp' specifies the pointer to the variable into which the size of the region of the return
345
+ value is assigned.
346
+ If successful, the return value is the pointer to the region of the value of the corresponding
347
+ record. `NULL' is returned if no record corresponds.
348
+ Because an additional zero code is appended at the end of the region of the return value,
349
+ the return value can be treated as a character string. Because the region of the return
350
+ value is allocated with the `malloc' call, it should be released with the `free' call when
351
+ it is no longer in use. */
352
+ void *tcfdbget2(TCFDB *fdb, const void *kbuf, int ksiz, int *sp);
353
+
354
+
355
+ /* Retrieve a string record with a decimal key in a fixed-length database object.
356
+ `fdb' specifies the fixed-length database object.
357
+ `kstr' specifies the string of the decimal key. It should be more than 0. If it is "min",
358
+ the minimum ID number of existing records is specified. If it is "max", the maximum ID number
359
+ of existing records is specified.
360
+ If successful, the return value is the string of the value of the corresponding record.
361
+ `NULL' is returned if no record corresponds.
362
+ Because an additional zero code is appended at the end of the region of the return value,
363
+ the return value can be treated as a character string. Because the region of the return
364
+ value is allocated with the `malloc' call, it should be released with the `free' call when
365
+ it is no longer in use. */
366
+ char *tcfdbget3(TCFDB *fdb, const char *kstr);
367
+
368
+
369
+ /* Retrieve a record in a fixed-length database object and write the value into a buffer.
370
+ `fdb' specifies the fixed-length database object.
371
+ `id' specifies the ID number. It should be more than 0. If it is `FDBIDMIN', the minimum ID
372
+ number of existing records is specified. If it is `FDBIDMAX', the maximum ID number of
373
+ existing records is specified.
374
+ `vbuf' specifies the pointer to the buffer into which the value of the corresponding record is
375
+ written.
376
+ `max' specifies the size of the buffer.
377
+ If successful, the return value is the size of the written data, else, it is -1. -1 is
378
+ returned if no record corresponds to the specified key.
379
+ Note that an additional zero code is not appended at the end of the region of the writing
380
+ buffer. */
381
+ int tcfdbget4(TCFDB *fdb, int64_t id, void *vbuf, int max);
382
+
383
+
384
+ /* Get the size of the value of a record in a fixed-length database object.
385
+ `fdb' specifies the fixed-length database object.
386
+ `id' specifies the ID number. It should be more than 0. If it is `FDBIDMIN', the minimum ID
387
+ number of existing records is specified. If it is `FDBIDMAX', the maximum ID number of
388
+ existing records is specified.
389
+ If successful, the return value is the size of the value of the corresponding record, else,
390
+ it is -1. */
391
+ int tcfdbvsiz(TCFDB *fdb, int64_t id);
392
+
393
+
394
+ /* Get the size of the value with a decimal key in a fixed-length database object.
395
+ `fdb' specifies the fixed-length database object.
396
+ `kbuf' specifies the pointer to the region of the decimal key. It should be more than 0. If
397
+ it is "min", the minimum ID number of existing records is specified. If it is "max", the
398
+ maximum ID number of existing records is specified.
399
+ `ksiz' specifies the size of the region of the key.
400
+ If successful, the return value is the size of the value of the corresponding record, else,
401
+ it is -1. */
402
+ int tcfdbvsiz2(TCFDB *fdb, const void *kbuf, int ksiz);
403
+
404
+
405
+ /* Get the size of the string value with a decimal key in a fixed-length database object.
406
+ `fdb' specifies the fixed-length database object.
407
+ `kstr' specifies the string of the decimal key. It should be more than 0. If it is "min",
408
+ the minimum ID number of existing records is specified. If it is "max", the maximum ID number
409
+ of existing records is specified.
410
+ If successful, the return value is the size of the value of the corresponding record, else,
411
+ it is -1. */
412
+ int tcfdbvsiz3(TCFDB *fdb, const char *kstr);
413
+
414
+
415
+ /* Initialize the iterator of a fixed-length database object.
416
+ `fdb' specifies the fixed-length database object.
417
+ If successful, the return value is true, else, it is false.
418
+ The iterator is used in order to access the key of every record stored in a database. */
419
+ bool tcfdbiterinit(TCFDB *fdb);
420
+
421
+
422
+ /* Get the next ID number of the iterator of a fixed-length database object.
423
+ `fdb' specifies the fixed-length database object.
424
+ If successful, the return value is the next ID number of the iterator, else, it is 0. 0 is
425
+ returned when no record is to be get out of the iterator.
426
+ It is possible to access every record by iteration of calling this function. It is allowed to
427
+ update or remove records whose keys are fetched while the iteration. The order of this
428
+ traversal access method is ascending of the ID number. */
429
+ uint64_t tcfdbiternext(TCFDB *fdb);
430
+
431
+
432
+ /* Get the next decimay key of the iterator of a fixed-length database object.
433
+ `fdb' specifies the fixed-length database object.
434
+ `sp' specifies the pointer to the variable into which the size of the region of the return
435
+ value is assigned.
436
+ If successful, the return value is the pointer to the region of the next decimal key, else, it
437
+ is `NULL'. `NULL' is returned when no record is to be get out of the iterator.
438
+ Because an additional zero code is appended at the end of the region of the return value, the
439
+ return value can be treated as a character string. Because the region of the return value is
440
+ allocated with the `malloc' call, it should be released with the `free' call when it is no
441
+ longer in use. It is possible to access every record by iteration of calling this function.
442
+ It is allowed to update or remove records whose keys are fetched while the iteration. The
443
+ order of this traversal access method is ascending of the ID number. */
444
+ void *tcfdbiternext2(TCFDB *fdb, int *sp);
445
+
446
+
447
+ /* Get the next decimay key string of the iterator of a fixed-length database object.
448
+ `fdb' specifies the fixed-length database object.
449
+ If successful, the return value is the string of the next decimal key, else, it is `NULL'.
450
+ `NULL' is returned when no record is to be get out of the iterator.
451
+ Because the region of the return value is allocated with the `malloc' call, it should be
452
+ released with the `free' call when it is no longer in use. It is possible to access every
453
+ record by iteration of calling this function. It is allowed to update or remove records whose
454
+ keys are fetched while the iteration. The order of this traversal access method is ascending
455
+ of the ID number. */
456
+ char *tcfdbiternext3(TCFDB *fdb);
457
+
458
+
459
+ /* Get range matching ID numbers in a fixed-length database object.
460
+ `fdb' specifies the fixed-length database object.
461
+ `lower' specifies the lower limit of the range. If it is `FDBIDMIN', the minimum ID is
462
+ specified.
463
+ `upper' specifies the upper limit of the range. If it is `FDBIDMAX', the maximum ID is
464
+ specified.
465
+ `max' specifies the maximum number of keys to be fetched. If it is negative, no limit is
466
+ specified.
467
+ `np' specifies the pointer to the variable into which the number of elements of the return
468
+ value is assigned.
469
+ If successful, the return value is the pointer to an array of ID numbers of the corresponding
470
+ records. `NULL' is returned on failure. This function does never fail. It returns an empty
471
+ array even if no key corresponds.
472
+ Because the region of the return value is allocated with the `malloc' call, it should be
473
+ released with the `free' call when it is no longer in use. */
474
+ uint64_t *tcfdbrange(TCFDB *fdb, int64_t lower, int64_t upper, int max, int *np);
475
+
476
+
477
+ /* Get range matching decimal keys in a fixed-length database object.
478
+ `fdb' specifies the fixed-length database object.
479
+ `lbuf' specifies the pointer to the region of the lower key. If it is "min", the minimum ID
480
+ number of existing records is specified.
481
+ `lsiz' specifies the size of the region of the lower key.
482
+ `ubuf' specifies the pointer to the region of the upper key. If it is "max", the maximum ID
483
+ number of existing records is specified.
484
+ `usiz' specifies the size of the region of the upper key.
485
+ `max' specifies the maximum number of keys to be fetched. If it is negative, no limit is
486
+ specified.
487
+ The return value is a list object of the corresponding decimal keys. This function does never
488
+ fail. It returns an empty list even if no key corresponds.
489
+ Because the object of the return value is created with the function `tclistnew', it should be
490
+ deleted with the function `tclistdel' when it is no longer in use. Note that this function
491
+ may be very slow because every key in the database is scanned. */
492
+ TCLIST *tcfdbrange2(TCFDB *fdb, const void *lbuf, int lsiz, const void *ubuf, int usiz, int max);
493
+
494
+
495
+ /* Get range matching decimal keys with strings in a fixed-length database object.
496
+ `fdb' specifies the fixed-length database object.
497
+ `lstr' specifies the string of the lower key. If it is "min", the minimum ID number of
498
+ existing records is specified.
499
+ `ustr' specifies the string of the upper key. If it is "max", the maximum ID number of
500
+ existing records is specified.
501
+ `max' specifies the maximum number of keys to be fetched. If it is negative, no limit is
502
+ specified.
503
+ The return value is a list object of the corresponding decimal keys. This function does never
504
+ fail. It returns an empty list even if no key corresponds.
505
+ Because the object of the return value is created with the function `tclistnew', it should be
506
+ deleted with the function `tclistdel' when it is no longer in use. Note that this function
507
+ may be very slow because every key in the database is scanned. */
508
+ TCLIST *tcfdbrange3(TCFDB *fdb, const char *lstr, const char *ustr, int max);
509
+
510
+
511
+ /* Get keys with an interval notation in a fixed-length database object.
512
+ `fdb' specifies the fixed-length database object.
513
+ `ibuf' specifies the pointer to the region of the interval notation.
514
+ `isiz' specifies the size of the region of the interval notation.
515
+ `max' specifies the maximum number of keys to be fetched. If it is negative, no limit is
516
+ specified.
517
+ The return value is a list object of the corresponding decimal keys. This function does never
518
+ fail. It returns an empty list even if no key corresponds.
519
+ Because the object of the return value is created with the function `tclistnew', it should be
520
+ deleted with the function `tclistdel' when it is no longer in use. Note that this function
521
+ may be very slow because every key in the database is scanned. */
522
+ TCLIST *tcfdbrange4(TCFDB *fdb, const void *ibuf, int isiz, int max);
523
+
524
+
525
+ /* Get keys with an interval notation string in a fixed-length database object.
526
+ `fdb' specifies the fixed-length database object.
527
+ `istr' specifies the pointer to the region of the interval notation string.
528
+ `max' specifies the maximum number of keys to be fetched. If it is negative, no limit is
529
+ specified.
530
+ The return value is a list object of the corresponding decimal keys. This function does never
531
+ fail. It returns an empty list even if no key corresponds.
532
+ Because the object of the return value is created with the function `tclistnew', it should be
533
+ deleted with the function `tclistdel' when it is no longer in use. Note that this function
534
+ may be very slow because every key in the database is scanned. */
535
+ TCLIST *tcfdbrange5(TCFDB *fdb, const void *istr, int max);
536
+
537
+
538
+ /* Add an integer to a record in a fixed-length database object.
539
+ `fdb' specifies the fixed-length database object connected as a writer.
540
+ `id' specifies the ID number. It should be more than 0. If it is `FDBIDMIN', the minimum ID
541
+ number of existing records is specified. If it is `FDBIDPREV', the number less by one than
542
+ the minimum ID number of existing records is specified. If it is `FDBIDMAX', the maximum ID
543
+ number of existing records is specified. If it is `FDBIDNEXT', the number greater by one than
544
+ the maximum ID number of existing records is specified.
545
+ `num' specifies the additional value.
546
+ If successful, the return value is the summation value, else, it is `INT_MIN'.
547
+ If the corresponding record exists, the value is treated as an integer and is added to. If no
548
+ record corresponds, a new record of the additional value is stored. */
549
+ int tcfdbaddint(TCFDB *fdb, int64_t id, int num);
550
+
551
+
552
+ /* Add a real number to a record in a fixed-length database object.
553
+ `fdb' specifies the fixed-length database object connected as a writer.
554
+ `id' specifies the ID number. It should be more than 0. If it is `FDBIDMIN', the minimum ID
555
+ number of existing records is specified. If it is `FDBIDPREV', the number less by one than
556
+ the minimum ID number of existing records is specified. If it is `FDBIDMAX', the maximum ID
557
+ number of existing records is specified. If it is `FDBIDNEXT', the number greater by one than
558
+ the maximum ID number of existing records is specified.
559
+ `num' specifies the additional value.
560
+ If successful, the return value is the summation value, else, it is Not-a-Number.
561
+ If the corresponding record exists, the value is treated as a real number and is added to. If
562
+ no record corresponds, a new record of the additional value is stored. */
563
+ double tcfdbadddouble(TCFDB *fdb, int64_t id, double num);
564
+
565
+
566
+ /* Synchronize updated contents of a fixed-length database object with the file and the device.
567
+ `fdb' specifies the fixed-length database object connected as a writer.
568
+ If successful, the return value is true, else, it is false.
569
+ This function is useful when another process connects to the same database file. */
570
+ bool tcfdbsync(TCFDB *fdb);
571
+
572
+
573
+ /* Optimize the file of a fixed-length database object.
574
+ `fdb' specifies the fixed-length database object connected as a writer.
575
+ `width' specifies the width of the value of each record. If it is not more than 0, the current
576
+ setting is not changed.
577
+ `limsiz' specifies the limit size of the database file. If it is not more than 0, the current
578
+ setting is not changed.
579
+ If successful, the return value is true, else, it is false. */
580
+ bool tcfdboptimize(TCFDB *fdb, int32_t width, int64_t limsiz);
581
+
582
+
583
+ /* Remove all records of a fixed-length database object.
584
+ `fdb' specifies the fixed-length database object connected as a writer.
585
+ If successful, the return value is true, else, it is false. */
586
+ bool tcfdbvanish(TCFDB *fdb);
587
+
588
+
589
+ /* Copy the database file of a fixed-length database object.
590
+ `fdb' specifies the fixed-length database object.
591
+ `path' specifies the path of the destination file. If it begins with `@', the trailing
592
+ substring is executed as a command line.
593
+ If successful, the return value is true, else, it is false. False is returned if the executed
594
+ command returns non-zero code.
595
+ The database file is assured to be kept synchronized and not modified while the copying or
596
+ executing operation is in progress. So, this function is useful to create a backup file of
597
+ the database file. */
598
+ bool tcfdbcopy(TCFDB *fdb, const char *path);
599
+
600
+
601
+ /* Begin the transaction of a fixed-length database object.
602
+ `fdb' specifies the fixed-length database object connected as a writer.
603
+ If successful, the return value is true, else, it is false.
604
+ The database is locked by the thread while the transaction so that only one transaction can be
605
+ activated with a database object at the same time. Thus, the serializable isolation level is
606
+ assumed if every database operation is performed in the transaction. All updated regions are
607
+ kept track of by write ahead logging while the transaction. If the database is closed during
608
+ transaction, the transaction is aborted implicitly. */
609
+ bool tcfdbtranbegin(TCFDB *fdb);
610
+
611
+
612
+ /* Commit the transaction of a fixed-length database object.
613
+ `fdb' specifies the fixed-length database object connected as a writer.
614
+ If successful, the return value is true, else, it is false.
615
+ Update in the transaction is fixed when it is committed successfully. */
616
+ bool tcfdbtrancommit(TCFDB *fdb);
617
+
618
+
619
+ /* Abort the transaction of a fixed-length database object.
620
+ `fdb' specifies the fixed-length database object connected as a writer.
621
+ If successful, the return value is true, else, it is false.
622
+ Update in the transaction is discarded when it is aborted. The state of the database is
623
+ rollbacked to before transaction. */
624
+ bool tcfdbtranabort(TCFDB *fdb);
625
+
626
+
627
+ /* Get the file path of a fixed-length database object.
628
+ `fdb' specifies the fixed-length database object.
629
+ The return value is the path of the database file or `NULL' if the object does not connect to
630
+ any database file. */
631
+ const char *tcfdbpath(TCFDB *fdb);
632
+
633
+
634
+ /* Get the number of records of a fixed-length database object.
635
+ `fdb' specifies the fixed-length database object.
636
+ The return value is the number of records or 0 if the object does not connect to any database
637
+ file. */
638
+ uint64_t tcfdbrnum(TCFDB *fdb);
639
+
640
+
641
+ /* Get the size of the database file of a fixed-length database object.
642
+ `fdb' specifies the fixed-length database object.
643
+ The return value is the size of the database file or 0 if the object does not connect to any
644
+ database file. */
645
+ uint64_t tcfdbfsiz(TCFDB *fdb);
646
+
647
+
648
+
649
+ /*************************************************************************************************
650
+ * features for experts
651
+ *************************************************************************************************/
652
+
653
+
654
+ /* Set the error code of a fixed-length database object.
655
+ `fdb' specifies the fixed-length database object.
656
+ `ecode' specifies the error code.
657
+ `file' specifies the file name of the code.
658
+ `line' specifies the line number of the code.
659
+ `func' specifies the function name of the code. */
660
+ void tcfdbsetecode(TCFDB *fdb, int ecode, const char *filename, int line, const char *func);
661
+
662
+
663
+ /* Set the file descriptor for debugging output.
664
+ `fdb' specifies the fixed-length database object.
665
+ `fd' specifies the file descriptor for debugging output. */
666
+ void tcfdbsetdbgfd(TCFDB *fdb, int fd);
667
+
668
+
669
+ /* Get the file descriptor for debugging output.
670
+ `fdb' specifies the fixed-length database object.
671
+ The return value is the file descriptor for debugging output. */
672
+ int tcfdbdbgfd(TCFDB *fdb);
673
+
674
+
675
+ /* Check whether mutual exclusion control is set to a fixed-length database object.
676
+ `fdb' specifies the fixed-length database object.
677
+ If mutual exclusion control is set, it is true, else it is false. */
678
+ bool tcfdbhasmutex(TCFDB *fdb);
679
+
680
+
681
+ /* Synchronize updating contents on memory of a fixed-length database object.
682
+ `fdb' specifies the fixed-length database object connected as a writer.
683
+ `phys' specifies whether to synchronize physically.
684
+ If successful, the return value is true, else, it is false. */
685
+ bool tcfdbmemsync(TCFDB *fdb, bool phys);
686
+
687
+
688
+ /* Get the minimum ID number of records of a fixed-length database object.
689
+ `fdb' specifies the fixed-length database object.
690
+ The return value is the minimum ID number of records or 0 if the object does not connect to
691
+ any database file. */
692
+ uint64_t tcfdbmin(TCFDB *fdb);
693
+
694
+
695
+ /* Get the maximum ID number of records of a fixed-length database object.
696
+ `fdb' specifies the fixed-length database object.
697
+ The return value is the maximum ID number of records or 0 if the object does not connect to
698
+ any database file. */
699
+ uint64_t tcfdbmax(TCFDB *fdb);
700
+
701
+
702
+ /* Get the width of the value of each record of a fixed-length database object.
703
+ `fdb' specifies the fixed-length database object.
704
+ The return value is the width of the value of each record or 0 if the object does not connect
705
+ to any database file. */
706
+ uint32_t tcfdbwidth(TCFDB *fdb);
707
+
708
+
709
+ /* Get the limit file size of a fixed-length database object.
710
+ `fdb' specifies the fixed-length database object.
711
+ The return value is the limit file size or 0 if the object does not connect to any database
712
+ file. */
713
+ uint64_t tcfdblimsiz(TCFDB *fdb);
714
+
715
+
716
+ /* Get the limit ID number of a fixed-length database object.
717
+ `fdb' specifies the fixed-length database object.
718
+ The return value is the limit ID number or 0 if the object does not connect to any database
719
+ file. */
720
+ uint64_t tcfdblimid(TCFDB *fdb);
721
+
722
+
723
+ /* Get the inode number of the database file of a fixed-length database object.
724
+ `fdb' specifies the fixed-length database object.
725
+ The return value is the inode number of the database file or 0 if the object does not connect
726
+ to any database file. */
727
+ uint64_t tcfdbinode(TCFDB *fdb);
728
+
729
+
730
+ /* Get the modification time of the database file of a fixed-length database object.
731
+ `fdb' specifies the fixed-length database object.
732
+ The return value is the inode number of the database file or 0 if the object does not connect
733
+ to any database file. */
734
+ time_t tcfdbmtime(TCFDB *fdb);
735
+
736
+
737
+ /* Get the connection mode of a fixed-length database object.
738
+ `fdb' specifies the fixed-length database object.
739
+ The return value is the connection mode. */
740
+ int tcfdbomode(TCFDB *fdb);
741
+
742
+
743
+ /* Get the database type of a fixed-length database object.
744
+ `fdb' specifies the fixed-length database object.
745
+ The return value is the database type. */
746
+ uint8_t tcfdbtype(TCFDB *fdb);
747
+
748
+
749
+ /* Get the additional flags of a fixed-length database object.
750
+ `fdb' specifies the fixed-length database object.
751
+ The return value is the additional flags. */
752
+ uint8_t tcfdbflags(TCFDB *fdb);
753
+
754
+
755
+ /* Get the pointer to the opaque field of a fixed-length database object.
756
+ `fdb' specifies the fixed-length database object.
757
+ The return value is the pointer to the opaque field whose size is 128 bytes. */
758
+ char *tcfdbopaque(TCFDB *fdb);
759
+
760
+
761
+ /* Store a record into a fixed-length database object with a duplication handler.
762
+ `fdb' specifies the fixed-length database object connected as a writer.
763
+ `id' specifies the ID number. It should be more than 0. If it is `FDBIDMIN', the minimum ID
764
+ number of existing records is specified. If it is `FDBIDPREV', the number less by one than
765
+ the minimum ID number of existing records is specified. If it is `FDBIDMAX', the maximum ID
766
+ number of existing records is specified. If it is `FDBIDNEXT', the number greater by one than
767
+ the maximum ID number of existing records is specified.
768
+ `vbuf' specifies the pointer to the region of the value. `NULL' means that record addition is
769
+ ommited if there is no corresponding record.
770
+ `vsiz' specifies the size of the region of the value. If the size of the value is greater
771
+ than the width tuning parameter of the database, the size is cut down to the width.
772
+ `proc' specifies the pointer to the callback function to process duplication. It receives
773
+ four parameters. The first parameter is the pointer to the region of the value. The second
774
+ parameter is the size of the region of the value. The third parameter is the pointer to the
775
+ variable into which the size of the region of the return value is assigned. The fourth
776
+ parameter is the pointer to the optional opaque object. It returns the pointer to the result
777
+ object allocated with `malloc'. It is released by the caller. If it is `NULL', the record is
778
+ not modified. If it is `(void *)-1', the record is removed.
779
+ `op' specifies an arbitrary pointer to be given as a parameter of the callback function. If
780
+ it is not needed, `NULL' can be specified.
781
+ If successful, the return value is true, else, it is false.
782
+ Note that the callback function can not perform any database operation because the function
783
+ is called in the critical section guarded by the same locks of database operations. */
784
+ bool tcfdbputproc(TCFDB *fdb, int64_t id, const void *vbuf, int vsiz, TCPDPROC proc, void *op);
785
+
786
+
787
+ /* Move the iterator to the record corresponding a key of a fixed-length database object.
788
+ `fdb' specifies the fixed-length database object.
789
+ `id' specifies the ID number. It should be more than 0. If it is `FDBIDMIN', the minimum ID
790
+ number of existing records is specified. If it is `FDBIDMAX', the maximum ID number of
791
+ existing records is specified.
792
+ If successful, the return value is true, else, it is false. False is returned if there is
793
+ no record corresponding the condition. */
794
+ bool tcfdbiterinit2(TCFDB *fdb, int64_t id);
795
+
796
+
797
+ /* Move the iterator to the decimal record of a fixed-length database object.
798
+ `fdb' specifies the fixed-length database object.
799
+ `kbuf' specifies the pointer to the region of the decimal key. It should be more than 0. If
800
+ it is "min", the minimum ID number of existing records is specified. If it is "max", the
801
+ maximum ID number of existing records is specified.
802
+ `ksiz' specifies the size of the region of the key.
803
+ If successful, the return value is true, else, it is false. False is returned if there is
804
+ no record corresponding the condition. */
805
+ bool tcfdbiterinit3(TCFDB *fdb, const void *kbuf, int ksiz);
806
+
807
+
808
+ /* Move the iterator to the decimal string record of a fixed-length database object.
809
+ `fdb' specifies the fixed-length database object.
810
+ `kstr' specifies the string of the decimal key. It should be more than 0. If it is "min",
811
+ the minimum ID number of existing records is specified. If it is "max", the maximum ID number
812
+ of existing records is specified.
813
+ If successful, the return value is true, else, it is false. False is returned if there is
814
+ no record corresponding the condition. */
815
+ bool tcfdbiterinit4(TCFDB *fdb, const char *kstr);
816
+
817
+
818
+ /* Process each record atomically of a fixed-length database object.
819
+ `fdb' specifies the fixed-length database object.
820
+ `iter' specifies the pointer to the iterator function called for each record. It receives
821
+ five parameters. The first parameter is the pointer to the region of the key. The second
822
+ parameter is the size of the region of the key. The third parameter is the pointer to the
823
+ region of the value. The fourth parameter is the size of the region of the value. The fifth
824
+ parameter is the pointer to the optional opaque object. It returns true to continue iteration
825
+ or false to stop iteration.
826
+ `op' specifies an arbitrary pointer to be given as a parameter of the iterator function. If
827
+ it is not needed, `NULL' can be specified.
828
+ If successful, the return value is true, else, it is false.
829
+ Note that the callback function can not perform any database operation because the function
830
+ is called in the critical section guarded by the same locks of database operations. */
831
+ bool tcfdbforeach(TCFDB *fdb, TCITER iter, void *op);
832
+
833
+
834
+ /* Generate the ID number from arbitrary binary data.
835
+ `kbuf' specifies the pointer to the region of the key.
836
+ `ksiz' specifies the size of the region of the key.
837
+ The return value is the ID number. */
838
+ int64_t tcfdbkeytoid(const char *kbuf, int ksiz);
839
+
840
+ #endif /* duplication check */
841
+
842
+ /* END OF FILE */