tokyocabinet 1.29

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/extconf.rb +28 -0
  2. data/tokyocabinet.c +3893 -0
  3. metadata +56 -0
@@ -0,0 +1,28 @@
1
+ require "mkmf"
2
+
3
+ dir_config('tokyocabinet')
4
+
5
+ home = ENV["HOME"]
6
+ ENV["PATH"] = ENV["PATH"] + ":/usr/local/bin:$home/bin:."
7
+ tccflags = `tcucodec conf -i 2>/dev/null`.chomp
8
+ tcldflags = `tcucodec conf -l 2>/dev/null`.chomp
9
+ tcldflags = tcldflags.gsub(/-l[\S]+/, "").strip
10
+ tclibs = `tcucodec conf -l 2>/dev/null`.chomp
11
+ tclibs = tclibs.gsub(/-L[\S]+/, "").strip
12
+
13
+ tccflags = "-I/usr/local/include" if(tccflags.length < 1)
14
+ tcldflags = "-L/usr/local/lib" if(tcldflags.length < 1)
15
+ tclibs = "-ltokyocabinet -lz -lbz2 -lpthread -lm -lc" if(tclibs.length < 1)
16
+
17
+ $CFLAGS = "-I. #{tccflags} -Wall #{$CFLAGS} -O2"
18
+ $LDFLAGS = "#{$LDFLAGS} -L. #{tcldflags}"
19
+ $libs = "#{$libs} #{tclibs}"
20
+
21
+ printf("setting variables ...\n")
22
+ printf(" \$CFLAGS = %s\n", $CFLAGS)
23
+ printf(" \$LDFLAGS = %s\n", $LDFLAGS)
24
+ printf(" \$libs = %s\n", $libs)
25
+
26
+ if have_header('tcutil.h')
27
+ create_makefile('tokyocabinet')
28
+ end
@@ -0,0 +1,3893 @@
1
+ /*************************************************************************************************
2
+ * Ruby binding 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
+ #include "ruby.h"
18
+ #include <tcutil.h>
19
+ #include <tchdb.h>
20
+ #include <tcbdb.h>
21
+ #include <tcfdb.h>
22
+ #include <tctdb.h>
23
+ #include <tcadb.h>
24
+ #include <stdlib.h>
25
+ #include <stdbool.h>
26
+ #include <stdint.h>
27
+ #include <limits.h>
28
+ #include <math.h>
29
+
30
+ #define HDBVNDATA "@hdb"
31
+ #define BDBVNDATA "@bdb"
32
+ #define BDBCURVNDATA "@bdbcur"
33
+ #define FDBVNDATA "@fdb"
34
+ #define TDBVNDATA "@tdb"
35
+ #define TDBQRYVNDATA "@tdbqry"
36
+ #define ADBVNDATA "@adb"
37
+ #define NUMBUFSIZ 32
38
+
39
+ #if !defined(RSTRING_PTR)
40
+ #define RSTRING_PTR(TC_s) (RSTRING(TC_s)->ptr)
41
+ #endif
42
+ #if !defined(RSTRING_LEN)
43
+ #define RSTRING_LEN(TC_s) (RSTRING(TC_s)->len)
44
+ #endif
45
+ #if !defined(RARRAY_LEN)
46
+ #define RARRAY_LEN(TC_a) (RARRAY(TC_a)->len)
47
+ #endif
48
+
49
+
50
+ /* private function prototypes */
51
+ static VALUE StringValueEx(VALUE vobj);
52
+ static TCLIST *varytolist(VALUE vary);
53
+ static VALUE listtovary(TCLIST *list);
54
+ static TCMAP *vhashtomap(VALUE vhash);
55
+ static VALUE maptovhash(TCMAP *map);
56
+ static void hdb_init(void);
57
+ static VALUE hdb_initialize(VALUE vself);
58
+ static VALUE hdb_errmsg(int argc, VALUE *argv, VALUE vself);
59
+ static VALUE hdb_ecode(VALUE vself);
60
+ static VALUE hdb_tune(int argc, VALUE *argv, VALUE vself);
61
+ static VALUE hdb_setcache(int argc, VALUE *argv, VALUE vself);
62
+ static VALUE hdb_setxmsiz(int argc, VALUE *argv, VALUE vself);
63
+ static VALUE hdb_setdfunit(int argc, VALUE *argv, VALUE vself);
64
+ static VALUE hdb_open(int argc, VALUE *argv, VALUE vself);
65
+ static VALUE hdb_close(VALUE vself);
66
+ static VALUE hdb_put(VALUE vself, VALUE vkey, VALUE vval);
67
+ static VALUE hdb_putkeep(VALUE vself, VALUE vkey, VALUE vval);
68
+ static VALUE hdb_putcat(VALUE vself, VALUE vkey, VALUE vval);
69
+ static VALUE hdb_putasync(VALUE vself, VALUE vkey, VALUE vval);
70
+ static VALUE hdb_out(VALUE vself, VALUE vkey);
71
+ static VALUE hdb_get(VALUE vself, VALUE vkey);
72
+ static VALUE hdb_vsiz(VALUE vself, VALUE vkey);
73
+ static VALUE hdb_iterinit(VALUE vself);
74
+ static VALUE hdb_iternext(VALUE vself);
75
+ static VALUE hdb_fwmkeys(int argc, VALUE *argv, VALUE vself);
76
+ static VALUE hdb_addint(VALUE vself, VALUE vkey, VALUE vnum);
77
+ static VALUE hdb_adddouble(VALUE vself, VALUE vkey, VALUE vnum);
78
+ static VALUE hdb_sync(VALUE vself);
79
+ static VALUE hdb_optimize(int argc, VALUE *argv, VALUE vself);
80
+ static VALUE hdb_vanish(VALUE vself);
81
+ static VALUE hdb_copy(VALUE vself, VALUE vpath);
82
+ static VALUE hdb_tranbegin(VALUE vself);
83
+ static VALUE hdb_trancommit(VALUE vself);
84
+ static VALUE hdb_tranabort(VALUE vself);
85
+ static VALUE hdb_path(VALUE vself);
86
+ static VALUE hdb_rnum(VALUE vself);
87
+ static VALUE hdb_fsiz(VALUE vself);
88
+ static VALUE hdb_fetch(int argc, VALUE *argv, VALUE vself);
89
+ static VALUE hdb_check(VALUE vself, VALUE vkey);
90
+ static VALUE hdb_check_value(VALUE vself, VALUE vval);
91
+ static VALUE hdb_get_reverse(VALUE vself, VALUE vval);
92
+ static VALUE hdb_empty(VALUE vself);
93
+ static VALUE hdb_each(VALUE vself);
94
+ static VALUE hdb_each_key(VALUE vself);
95
+ static VALUE hdb_each_value(VALUE vself);
96
+ static VALUE hdb_keys(VALUE vself);
97
+ static VALUE hdb_values(VALUE vself);
98
+ static void bdb_init(void);
99
+ static int bdb_cmpobj(const char *aptr, int asiz, const char *bptr, int bsiz, VALUE vcmp);
100
+ static VALUE bdb_initialize(VALUE vself);
101
+ static VALUE bdb_errmsg(int argc, VALUE *argv, VALUE vself);
102
+ static VALUE bdb_ecode(VALUE vself);
103
+ static VALUE bdb_setcmpfunc(VALUE vself, VALUE vcmp);
104
+ static VALUE bdb_tune(int argc, VALUE *argv, VALUE vself);
105
+ static VALUE bdb_setcache(int argc, VALUE *argv, VALUE vself);
106
+ static VALUE bdb_setxmsiz(int argc, VALUE *argv, VALUE vself);
107
+ static VALUE bdb_setdfunit(int argc, VALUE *argv, VALUE vself);
108
+ static VALUE bdb_open(int argc, VALUE *argv, VALUE vself);
109
+ static VALUE bdb_close(VALUE vself);
110
+ static VALUE bdb_put(VALUE vself, VALUE vkey, VALUE vval);
111
+ static VALUE bdb_putkeep(VALUE vself, VALUE vkey, VALUE vval);
112
+ static VALUE bdb_putcat(VALUE vself, VALUE vkey, VALUE vval);
113
+ static VALUE bdb_putdup(VALUE vself, VALUE vkey, VALUE vval);
114
+ static VALUE bdb_putlist(VALUE vself, VALUE vkey, VALUE vvals);
115
+ static VALUE bdb_out(VALUE vself, VALUE vkey);
116
+ static VALUE bdb_outlist(VALUE vself, VALUE vkey);
117
+ static VALUE bdb_get(VALUE vself, VALUE vkey);
118
+ static VALUE bdb_getlist(VALUE vself, VALUE vkey);
119
+ static VALUE bdb_vnum(VALUE vself, VALUE vkey);
120
+ static VALUE bdb_vsiz(VALUE vself, VALUE vkey);
121
+ static VALUE bdb_range(int argc, VALUE *argv, VALUE vself);
122
+ static VALUE bdb_fwmkeys(int argc, VALUE *argv, VALUE vself);
123
+ static VALUE bdb_addint(VALUE vself, VALUE vkey, VALUE vnum);
124
+ static VALUE bdb_adddouble(VALUE vself, VALUE vkey, VALUE vnum);
125
+ static VALUE bdb_sync(VALUE vself);
126
+ static VALUE bdb_optimize(int argc, VALUE *argv, VALUE vself);
127
+ static VALUE bdb_vanish(VALUE vself);
128
+ static VALUE bdb_copy(VALUE vself, VALUE vpath);
129
+ static VALUE bdb_tranbegin(VALUE vself);
130
+ static VALUE bdb_trancommit(VALUE vself);
131
+ static VALUE bdb_tranabort(VALUE vself);
132
+ static VALUE bdb_path(VALUE vself);
133
+ static VALUE bdb_rnum(VALUE vself);
134
+ static VALUE bdb_fsiz(VALUE vself);
135
+ static VALUE bdb_fetch(int argc, VALUE *argv, VALUE vself);
136
+ static VALUE bdb_check(VALUE vself, VALUE vkey);
137
+ static VALUE bdb_check_value(VALUE vself, VALUE vval);
138
+ static VALUE bdb_get_reverse(VALUE vself, VALUE vval);
139
+ static VALUE bdb_empty(VALUE vself);
140
+ static VALUE bdb_each(VALUE vself);
141
+ static VALUE bdb_each_key(VALUE vself);
142
+ static VALUE bdb_each_value(VALUE vself);
143
+ static VALUE bdb_keys(VALUE vself);
144
+ static VALUE bdb_values(VALUE vself);
145
+ static void bdbcur_init(void);
146
+ static VALUE bdbcur_initialize(VALUE vself, VALUE vbdb);
147
+ static VALUE bdbcur_first(VALUE vself);
148
+ static VALUE bdbcur_last(VALUE vself);
149
+ static VALUE bdbcur_jump(VALUE vself, VALUE vkey);
150
+ static VALUE bdbcur_prev(VALUE vself);
151
+ static VALUE bdbcur_next(VALUE vself);
152
+ static VALUE bdbcur_put(int argc, VALUE *argv, VALUE vself);
153
+ static VALUE bdbcur_out(VALUE vself);
154
+ static VALUE bdbcur_key(VALUE vself);
155
+ static VALUE bdbcur_val(VALUE vself);
156
+ static void fdb_init(void);
157
+ static VALUE fdb_initialize(VALUE vself);
158
+ static VALUE fdb_errmsg(int argc, VALUE *argv, VALUE vself);
159
+ static VALUE fdb_ecode(VALUE vself);
160
+ static VALUE fdb_tune(int argc, VALUE *argv, VALUE vself);
161
+ static VALUE fdb_open(int argc, VALUE *argv, VALUE vself);
162
+ static VALUE fdb_close(VALUE vself);
163
+ static VALUE fdb_put(VALUE vself, VALUE vkey, VALUE vval);
164
+ static VALUE fdb_putkeep(VALUE vself, VALUE vkey, VALUE vval);
165
+ static VALUE fdb_putcat(VALUE vself, VALUE vkey, VALUE vval);
166
+ static VALUE fdb_out(VALUE vself, VALUE vkey);
167
+ static VALUE fdb_get(VALUE vself, VALUE vkey);
168
+ static VALUE fdb_vsiz(VALUE vself, VALUE vkey);
169
+ static VALUE fdb_iterinit(VALUE vself);
170
+ static VALUE fdb_iternext(VALUE vself);
171
+ static VALUE fdb_range(int argc, VALUE *argv, VALUE vself);
172
+ static VALUE fdb_addint(VALUE vself, VALUE vkey, VALUE vnum);
173
+ static VALUE fdb_adddouble(VALUE vself, VALUE vkey, VALUE vnum);
174
+ static VALUE fdb_sync(VALUE vself);
175
+ static VALUE fdb_optimize(int argc, VALUE *argv, VALUE vself);
176
+ static VALUE fdb_vanish(VALUE vself);
177
+ static VALUE fdb_copy(VALUE vself, VALUE vpath);
178
+ static VALUE fdb_tranbegin(VALUE vself);
179
+ static VALUE fdb_trancommit(VALUE vself);
180
+ static VALUE fdb_tranabort(VALUE vself);
181
+ static VALUE fdb_path(VALUE vself);
182
+ static VALUE fdb_rnum(VALUE vself);
183
+ static VALUE fdb_fsiz(VALUE vself);
184
+ static VALUE fdb_fetch(int argc, VALUE *argv, VALUE vself);
185
+ static VALUE fdb_check(VALUE vself, VALUE vkey);
186
+ static VALUE fdb_check_value(VALUE vself, VALUE vval);
187
+ static VALUE fdb_get_reverse(VALUE vself, VALUE vval);
188
+ static VALUE fdb_empty(VALUE vself);
189
+ static VALUE fdb_each(VALUE vself);
190
+ static VALUE fdb_each_key(VALUE vself);
191
+ static VALUE fdb_each_value(VALUE vself);
192
+ static VALUE fdb_keys(VALUE vself);
193
+ static VALUE fdb_values(VALUE vself);
194
+ static void tdb_init(void);
195
+ static VALUE tdb_initialize(VALUE vself);
196
+ static VALUE tdb_errmsg(int argc, VALUE *argv, VALUE vself);
197
+ static VALUE tdb_ecode(VALUE vself);
198
+ static VALUE tdb_tune(int argc, VALUE *argv, VALUE vself);
199
+ static VALUE tdb_setcache(int argc, VALUE *argv, VALUE vself);
200
+ static VALUE tdb_setxmsiz(int argc, VALUE *argv, VALUE vself);
201
+ static VALUE tdb_setdfunit(int argc, VALUE *argv, VALUE vself);
202
+ static VALUE tdb_open(int argc, VALUE *argv, VALUE vself);
203
+ static VALUE tdb_close(VALUE vself);
204
+ static VALUE tdb_put(VALUE vself, VALUE vkey, VALUE vcols);
205
+ static VALUE tdb_putkeep(VALUE vself, VALUE vkey, VALUE vcols);
206
+ static VALUE tdb_putcat(VALUE vself, VALUE vkey, VALUE vcols);
207
+ static VALUE tdb_out(VALUE vself, VALUE vkey);
208
+ static VALUE tdb_get(VALUE vself, VALUE vkey);
209
+ static VALUE tdb_vsiz(VALUE vself, VALUE vkey);
210
+ static VALUE tdb_iterinit(VALUE vself);
211
+ static VALUE tdb_iternext(VALUE vself);
212
+ static VALUE tdb_fwmkeys(int argc, VALUE *argv, VALUE vself);
213
+ static VALUE tdb_addint(VALUE vself, VALUE vkey, VALUE vnum);
214
+ static VALUE tdb_adddouble(VALUE vself, VALUE vkey, VALUE vnum);
215
+ static VALUE tdb_sync(VALUE vself);
216
+ static VALUE tdb_optimize(int argc, VALUE *argv, VALUE vself);
217
+ static VALUE tdb_vanish(VALUE vself);
218
+ static VALUE tdb_copy(VALUE vself, VALUE vpath);
219
+ static VALUE tdb_tranbegin(VALUE vself);
220
+ static VALUE tdb_trancommit(VALUE vself);
221
+ static VALUE tdb_tranabort(VALUE vself);
222
+ static VALUE tdb_path(VALUE vself);
223
+ static VALUE tdb_rnum(VALUE vself);
224
+ static VALUE tdb_fsiz(VALUE vself);
225
+ static VALUE tdb_setindex(VALUE vself, VALUE vname, VALUE vtype);
226
+ static VALUE tdb_genuid(VALUE vself);
227
+ static VALUE tdb_fetch(int argc, VALUE *argv, VALUE vself);
228
+ static VALUE tdb_check(VALUE vself, VALUE vkey);
229
+ static VALUE tdb_empty(VALUE vself);
230
+ static VALUE tdb_each(VALUE vself);
231
+ static VALUE tdb_each_key(VALUE vself);
232
+ static VALUE tdb_each_value(VALUE vself);
233
+ static VALUE tdb_keys(VALUE vself);
234
+ static VALUE tdb_values(VALUE vself);
235
+ static void tdbqry_init(void);
236
+ static int tdbqry_procrec(const void *pkbuf, int pksiz, TCMAP *cols, void *opq);
237
+ static VALUE tdbqry_initialize(VALUE vself, VALUE vtdb);
238
+ static VALUE tdbqry_addcond(VALUE vself, VALUE vname, VALUE vop, VALUE vexpr);
239
+ static VALUE tdbqry_setorder(int argc, VALUE *argv, VALUE vself);
240
+ static VALUE tdbqry_setlimit(int argc, VALUE *argv, VALUE vself);
241
+ static VALUE tdbqry_search(VALUE vself);
242
+ static VALUE tdbqry_searchout(VALUE vself);
243
+ static VALUE tdbqry_proc(VALUE vself, VALUE vproc);
244
+ static VALUE tdbqry_hint(VALUE vself);
245
+ static VALUE tdbqry_metasearch(int argc, VALUE *argv, VALUE vself);
246
+ static VALUE tdbqry_kwic(int argc, VALUE *argv, VALUE vself);
247
+ static void adb_init(void);
248
+ static VALUE adb_initialize(VALUE vself);
249
+ static VALUE adb_open(VALUE vself, VALUE vname);
250
+ static VALUE adb_close(VALUE vself);
251
+ static VALUE adb_put(VALUE vself, VALUE vkey, VALUE vval);
252
+ static VALUE adb_putkeep(VALUE vself, VALUE vkey, VALUE vval);
253
+ static VALUE adb_putcat(VALUE vself, VALUE vkey, VALUE vval);
254
+ static VALUE adb_out(VALUE vself, VALUE vkey);
255
+ static VALUE adb_get(VALUE vself, VALUE vkey);
256
+ static VALUE adb_vsiz(VALUE vself, VALUE vkey);
257
+ static VALUE adb_iterinit(VALUE vself);
258
+ static VALUE adb_iternext(VALUE vself);
259
+ static VALUE adb_fwmkeys(int argc, VALUE *argv, VALUE vself);
260
+ static VALUE adb_addint(VALUE vself, VALUE vkey, VALUE vnum);
261
+ static VALUE adb_adddouble(VALUE vself, VALUE vkey, VALUE vnum);
262
+ static VALUE adb_sync(VALUE vself);
263
+ static VALUE adb_optimize(int argc, VALUE *argv, VALUE vself);
264
+ static VALUE adb_vanish(VALUE vself);
265
+ static VALUE adb_copy(VALUE vself, VALUE vpath);
266
+ static VALUE adb_tranbegin(VALUE vself);
267
+ static VALUE adb_trancommit(VALUE vself);
268
+ static VALUE adb_tranabort(VALUE vself);
269
+ static VALUE adb_path(VALUE vself);
270
+ static VALUE adb_rnum(VALUE vself);
271
+ static VALUE adb_size(VALUE vself);
272
+ static VALUE adb_misc(int argc, VALUE *argv, VALUE vself);
273
+ static VALUE adb_fetch(int argc, VALUE *argv, VALUE vself);
274
+ static VALUE adb_check(VALUE vself, VALUE vkey);
275
+ static VALUE adb_check_value(VALUE vself, VALUE vval);
276
+ static VALUE adb_get_reverse(VALUE vself, VALUE vval);
277
+ static VALUE adb_empty(VALUE vself);
278
+ static VALUE adb_each(VALUE vself);
279
+ static VALUE adb_each_key(VALUE vself);
280
+ static VALUE adb_each_value(VALUE vself);
281
+ static VALUE adb_keys(VALUE vself);
282
+ static VALUE adb_values(VALUE vself);
283
+
284
+
285
+
286
+ /*************************************************************************************************
287
+ * public objects
288
+ *************************************************************************************************/
289
+
290
+
291
+ VALUE mod_tokyocabinet;
292
+ VALUE cls_hdb;
293
+ VALUE cls_hdb_data;
294
+ VALUE cls_bdb;
295
+ VALUE cls_bdb_data;
296
+ VALUE cls_bdbcur;
297
+ VALUE cls_bdbcur_data;
298
+ ID bdb_cmp_call_mid;
299
+ VALUE cls_fdb;
300
+ VALUE cls_fdb_data;
301
+ VALUE cls_tdb;
302
+ VALUE cls_tdb_data;
303
+ VALUE cls_tdbqry;
304
+ VALUE cls_tdbqry_data;
305
+ VALUE cls_adb;
306
+ VALUE cls_adb_data;
307
+
308
+
309
+ int Init_tokyocabinet(void){
310
+ mod_tokyocabinet = rb_define_module("TokyoCabinet");
311
+ rb_define_const(mod_tokyocabinet, "VERSION", rb_str_new2(tcversion));
312
+ hdb_init();
313
+ bdb_init();
314
+ bdbcur_init();
315
+ fdb_init();
316
+ tdb_init();
317
+ tdbqry_init();
318
+ adb_init();
319
+ return 0;
320
+ }
321
+
322
+
323
+
324
+ /*************************************************************************************************
325
+ * private objects
326
+ *************************************************************************************************/
327
+
328
+
329
+ static VALUE StringValueEx(VALUE vobj){
330
+ char kbuf[NUMBUFSIZ];
331
+ int ksiz;
332
+ switch(TYPE(vobj)){
333
+ case T_FIXNUM:
334
+ ksiz = sprintf(kbuf, "%d", (int)FIX2INT(vobj));
335
+ return rb_str_new(kbuf, ksiz);
336
+ case T_BIGNUM:
337
+ ksiz = sprintf(kbuf, "%lld", (long long)NUM2LL(vobj));
338
+ return rb_str_new(kbuf, ksiz);
339
+ case T_TRUE:
340
+ ksiz = sprintf(kbuf, "true");
341
+ return rb_str_new(kbuf, ksiz);
342
+ case T_FALSE:
343
+ ksiz = sprintf(kbuf, "false");
344
+ return rb_str_new(kbuf, ksiz);
345
+ case T_NIL:
346
+ ksiz = sprintf(kbuf, "nil");
347
+ return rb_str_new(kbuf, ksiz);
348
+ }
349
+ return StringValue(vobj);
350
+ }
351
+
352
+
353
+ static TCLIST *varytolist(VALUE vary){
354
+ VALUE vval;
355
+ TCLIST *list;
356
+ int i, num;
357
+ num = RARRAY_LEN(vary);
358
+ list = tclistnew2(num);
359
+ for(i = 0; i < num; i++){
360
+ vval = rb_ary_entry(vary, i);
361
+ vval = StringValueEx(vval);
362
+ tclistpush(list, RSTRING_PTR(vval), RSTRING_LEN(vval));
363
+ }
364
+ return list;
365
+ }
366
+
367
+
368
+ static VALUE listtovary(TCLIST *list){
369
+ VALUE vary;
370
+ const char *vbuf;
371
+ int i, num, vsiz;
372
+ num = tclistnum(list);
373
+ vary = rb_ary_new2(num);
374
+ for(i = 0; i < num; i++){
375
+ vbuf = tclistval(list, i, &vsiz);
376
+ rb_ary_push(vary, rb_str_new(vbuf, vsiz));
377
+ }
378
+ return vary;
379
+ }
380
+
381
+
382
+ static TCMAP *vhashtomap(VALUE vhash){
383
+ VALUE vkeys, vkey, vval;
384
+ TCMAP *map;
385
+ int i, num;
386
+ map = tcmapnew2(31);
387
+ vkeys = rb_funcall(vhash, rb_intern("keys"), 0);
388
+ num = RARRAY_LEN(vkeys);
389
+ for(i = 0; i < num; i++){
390
+ vkey = rb_ary_entry(vkeys, i);
391
+ vval = rb_hash_aref(vhash, vkey);
392
+ vkey = StringValueEx(vkey);
393
+ vval = StringValueEx(vval);
394
+ tcmapput(map, RSTRING_PTR(vkey), RSTRING_LEN(vkey), RSTRING_PTR(vval), RSTRING_LEN(vval));
395
+ }
396
+ return map;
397
+ }
398
+
399
+
400
+ static VALUE maptovhash(TCMAP *map){
401
+ const char *kbuf, *vbuf;
402
+ int ksiz, vsiz;
403
+ VALUE vhash;
404
+ vhash = rb_hash_new();
405
+ tcmapiterinit(map);
406
+ while((kbuf = tcmapiternext(map, &ksiz)) != NULL){
407
+ vbuf = tcmapiterval(kbuf, &vsiz);
408
+ rb_hash_aset(vhash, rb_str_new(kbuf, ksiz), rb_str_new(vbuf, vsiz));
409
+ }
410
+ return vhash;
411
+ }
412
+
413
+
414
+ static void hdb_init(void){
415
+ cls_hdb = rb_define_class_under(mod_tokyocabinet, "HDB", rb_cObject);
416
+ cls_hdb_data = rb_define_class_under(mod_tokyocabinet, "HDB_data", rb_cObject);
417
+ rb_define_const(cls_hdb, "ESUCCESS", INT2NUM(TCESUCCESS));
418
+ rb_define_const(cls_hdb, "ETHREAD", INT2NUM(TCETHREAD));
419
+ rb_define_const(cls_hdb, "EINVALID", INT2NUM(TCEINVALID));
420
+ rb_define_const(cls_hdb, "ENOFILE", INT2NUM(TCENOFILE));
421
+ rb_define_const(cls_hdb, "ENOPERM", INT2NUM(TCENOPERM));
422
+ rb_define_const(cls_hdb, "EMETA", INT2NUM(TCEMETA));
423
+ rb_define_const(cls_hdb, "ERHEAD", INT2NUM(TCERHEAD));
424
+ rb_define_const(cls_hdb, "EOPEN", INT2NUM(TCEOPEN));
425
+ rb_define_const(cls_hdb, "ECLOSE", INT2NUM(TCECLOSE));
426
+ rb_define_const(cls_hdb, "ETRUNC", INT2NUM(TCETRUNC));
427
+ rb_define_const(cls_hdb, "ESYNC", INT2NUM(TCESYNC));
428
+ rb_define_const(cls_hdb, "ESTAT", INT2NUM(TCESTAT));
429
+ rb_define_const(cls_hdb, "ESEEK", INT2NUM(TCESEEK));
430
+ rb_define_const(cls_hdb, "EREAD", INT2NUM(TCEREAD));
431
+ rb_define_const(cls_hdb, "EWRITE", INT2NUM(TCEWRITE));
432
+ rb_define_const(cls_hdb, "EMMAP", INT2NUM(TCEMMAP));
433
+ rb_define_const(cls_hdb, "ELOCK", INT2NUM(TCELOCK));
434
+ rb_define_const(cls_hdb, "EUNLINK", INT2NUM(TCEUNLINK));
435
+ rb_define_const(cls_hdb, "ERENAME", INT2NUM(TCERENAME));
436
+ rb_define_const(cls_hdb, "EMKDIR", INT2NUM(TCEMKDIR));
437
+ rb_define_const(cls_hdb, "ERMDIR", INT2NUM(TCERMDIR));
438
+ rb_define_const(cls_hdb, "EKEEP", INT2NUM(TCEKEEP));
439
+ rb_define_const(cls_hdb, "ENOREC", INT2NUM(TCENOREC));
440
+ rb_define_const(cls_hdb, "EMISC", INT2NUM(TCEMISC));
441
+ rb_define_const(cls_hdb, "TLARGE", INT2NUM(HDBTLARGE));
442
+ rb_define_const(cls_hdb, "TDEFLATE", INT2NUM(HDBTDEFLATE));
443
+ rb_define_const(cls_hdb, "TBZIP", INT2NUM(HDBTBZIP));
444
+ rb_define_const(cls_hdb, "TTCBS", INT2NUM(HDBTTCBS));
445
+ rb_define_const(cls_hdb, "OREADER", INT2NUM(HDBOREADER));
446
+ rb_define_const(cls_hdb, "OWRITER", INT2NUM(HDBOWRITER));
447
+ rb_define_const(cls_hdb, "OCREAT", INT2NUM(HDBOCREAT));
448
+ rb_define_const(cls_hdb, "OTRUNC", INT2NUM(HDBOTRUNC));
449
+ rb_define_const(cls_hdb, "ONOLCK", INT2NUM(HDBONOLCK));
450
+ rb_define_const(cls_hdb, "OLCKNB", INT2NUM(HDBOLCKNB));
451
+ rb_define_const(cls_hdb, "OTSYNC", INT2NUM(HDBOTSYNC));
452
+ rb_define_private_method(cls_hdb, "initialize", hdb_initialize, 0);
453
+ rb_define_method(cls_hdb, "errmsg", hdb_errmsg, -1);
454
+ rb_define_method(cls_hdb, "ecode", hdb_ecode, 0);
455
+ rb_define_method(cls_hdb, "tune", hdb_tune, -1);
456
+ rb_define_method(cls_hdb, "setcache", hdb_setcache, -1);
457
+ rb_define_method(cls_hdb, "setxmsiz", hdb_setxmsiz, -1);
458
+ rb_define_method(cls_hdb, "setdfunit", hdb_setdfunit, -1);
459
+ rb_define_method(cls_hdb, "open", hdb_open, -1);
460
+ rb_define_method(cls_hdb, "close", hdb_close, 0);
461
+ rb_define_method(cls_hdb, "put", hdb_put, 2);
462
+ rb_define_method(cls_hdb, "putkeep", hdb_putkeep, 2);
463
+ rb_define_method(cls_hdb, "putcat", hdb_putcat, 2);
464
+ rb_define_method(cls_hdb, "putasync", hdb_putasync, 2);
465
+ rb_define_method(cls_hdb, "out", hdb_out, 1);
466
+ rb_define_method(cls_hdb, "get", hdb_get, 1);
467
+ rb_define_method(cls_hdb, "vsiz", hdb_vsiz, 1);
468
+ rb_define_method(cls_hdb, "iterinit", hdb_iterinit, 0);
469
+ rb_define_method(cls_hdb, "iternext", hdb_iternext, 0);
470
+ rb_define_method(cls_hdb, "fwmkeys", hdb_fwmkeys, -1);
471
+ rb_define_method(cls_hdb, "addint", hdb_addint, 2);
472
+ rb_define_method(cls_hdb, "adddouble", hdb_adddouble, 2);
473
+ rb_define_method(cls_hdb, "sync", hdb_sync, 0);
474
+ rb_define_method(cls_hdb, "optimize", hdb_optimize, -1);
475
+ rb_define_method(cls_hdb, "vanish", hdb_vanish, 0);
476
+ rb_define_method(cls_hdb, "copy", hdb_copy, 1);
477
+ rb_define_method(cls_hdb, "tranbegin", hdb_tranbegin, 0);
478
+ rb_define_method(cls_hdb, "trancommit", hdb_trancommit, 0);
479
+ rb_define_method(cls_hdb, "tranabort", hdb_tranabort, 0);
480
+ rb_define_method(cls_hdb, "path", hdb_path, 0);
481
+ rb_define_method(cls_hdb, "rnum", hdb_rnum, 0);
482
+ rb_define_method(cls_hdb, "fsiz", hdb_fsiz, 0);
483
+ rb_define_method(cls_hdb, "[]", hdb_get, 1);
484
+ rb_define_method(cls_hdb, "[]=", hdb_put, 2);
485
+ rb_define_method(cls_hdb, "store", hdb_put, 2);
486
+ rb_define_method(cls_hdb, "delete", hdb_out, 1);
487
+ rb_define_method(cls_hdb, "fetch", hdb_fetch, -1);
488
+ rb_define_method(cls_hdb, "has_key?", hdb_check, 1);
489
+ rb_define_method(cls_hdb, "key?", hdb_check, 1);
490
+ rb_define_method(cls_hdb, "include?", hdb_check, 1);
491
+ rb_define_method(cls_hdb, "member?", hdb_check, 1);
492
+ rb_define_method(cls_hdb, "has_value?", hdb_check_value, 1);
493
+ rb_define_method(cls_hdb, "value?", hdb_check_value, 1);
494
+ rb_define_method(cls_hdb, "key", hdb_get_reverse, 1);
495
+ rb_define_method(cls_hdb, "clear", hdb_vanish, 0);
496
+ rb_define_method(cls_hdb, "size", hdb_rnum, 0);
497
+ rb_define_method(cls_hdb, "length", hdb_rnum, 0);
498
+ rb_define_method(cls_hdb, "empty?", hdb_empty, 0);
499
+ rb_define_method(cls_hdb, "each", hdb_each, 0);
500
+ rb_define_method(cls_hdb, "each_pair", hdb_each, 0);
501
+ rb_define_method(cls_hdb, "each_key", hdb_each_key, 0);
502
+ rb_define_method(cls_hdb, "each_value", hdb_each_value, 0);
503
+ rb_define_method(cls_hdb, "keys", hdb_keys, 0);
504
+ rb_define_method(cls_hdb, "values", hdb_values, 0);
505
+ }
506
+
507
+
508
+ static VALUE hdb_initialize(VALUE vself){
509
+ VALUE vhdb;
510
+ TCHDB *hdb;
511
+ hdb = tchdbnew();
512
+ tchdbsetmutex(hdb);
513
+ vhdb = Data_Wrap_Struct(cls_hdb_data, 0, tchdbdel, hdb);
514
+ rb_iv_set(vself, HDBVNDATA, vhdb);
515
+ return Qnil;
516
+ }
517
+
518
+
519
+ static VALUE hdb_errmsg(int argc, VALUE *argv, VALUE vself){
520
+ VALUE vhdb, vecode;
521
+ TCHDB *hdb;
522
+ const char *msg;
523
+ int ecode;
524
+ rb_scan_args(argc, argv, "01", &vecode);
525
+ vhdb = rb_iv_get(vself, HDBVNDATA);
526
+ Data_Get_Struct(vhdb, TCHDB, hdb);
527
+ ecode = (vecode == Qnil) ? tchdbecode(hdb) : NUM2INT(vecode);
528
+ msg = tchdberrmsg(ecode);
529
+ return rb_str_new2(msg);
530
+ }
531
+
532
+
533
+ static VALUE hdb_ecode(VALUE vself){
534
+ VALUE vhdb;
535
+ TCHDB *hdb;
536
+ vhdb = rb_iv_get(vself, HDBVNDATA);
537
+ Data_Get_Struct(vhdb, TCHDB, hdb);
538
+ return INT2NUM(tchdbecode(hdb));
539
+ }
540
+
541
+
542
+ static VALUE hdb_tune(int argc, VALUE *argv, VALUE vself){
543
+ VALUE vhdb, vbnum, vapow, vfpow, vopts;
544
+ TCHDB *hdb;
545
+ int apow, fpow, opts;
546
+ int64_t bnum;
547
+ rb_scan_args(argc, argv, "04", &vbnum, &vapow, &vfpow, &vopts);
548
+ bnum = (vbnum == Qnil) ? -1 : NUM2LL(vbnum);
549
+ apow = (vapow == Qnil) ? -1 : NUM2INT(vapow);
550
+ fpow = (vfpow == Qnil) ? -1 : NUM2INT(vfpow);
551
+ opts = (vopts == Qnil) ? 0 : NUM2INT(vopts);
552
+ vhdb = rb_iv_get(vself, HDBVNDATA);
553
+ Data_Get_Struct(vhdb, TCHDB, hdb);
554
+ return tchdbtune(hdb, bnum, apow, fpow, opts) ? Qtrue : Qfalse;
555
+ }
556
+
557
+
558
+ static VALUE hdb_setcache(int argc, VALUE *argv, VALUE vself){
559
+ VALUE vhdb, vrcnum;
560
+ TCHDB *hdb;
561
+ int rcnum;
562
+ rb_scan_args(argc, argv, "01", &vrcnum);
563
+ rcnum = (vrcnum == Qnil) ? -1 : NUM2INT(vrcnum);
564
+ vhdb = rb_iv_get(vself, HDBVNDATA);
565
+ Data_Get_Struct(vhdb, TCHDB, hdb);
566
+ return tchdbsetcache(hdb, rcnum) ? Qtrue : Qfalse;
567
+ }
568
+
569
+
570
+ static VALUE hdb_setxmsiz(int argc, VALUE *argv, VALUE vself){
571
+ VALUE vhdb, vxmsiz;
572
+ TCHDB *hdb;
573
+ int64_t xmsiz;
574
+ rb_scan_args(argc, argv, "01", &vxmsiz);
575
+ xmsiz = (vxmsiz == Qnil) ? -1 : NUM2LL(vxmsiz);
576
+ vhdb = rb_iv_get(vself, HDBVNDATA);
577
+ Data_Get_Struct(vhdb, TCHDB, hdb);
578
+ return tchdbsetxmsiz(hdb, xmsiz) ? Qtrue : Qfalse;
579
+ }
580
+
581
+
582
+ static VALUE hdb_setdfunit(int argc, VALUE *argv, VALUE vself){
583
+ VALUE vhdb, vdfunit;
584
+ TCHDB *hdb;
585
+ int32_t dfunit;
586
+ rb_scan_args(argc, argv, "01", &vdfunit);
587
+ dfunit = (vdfunit == Qnil) ? -1 : NUM2INT(vdfunit);
588
+ vhdb = rb_iv_get(vself, HDBVNDATA);
589
+ Data_Get_Struct(vhdb, TCHDB, hdb);
590
+ return tchdbsetdfunit(hdb, dfunit) ? Qtrue : Qfalse;
591
+ }
592
+
593
+
594
+ static VALUE hdb_open(int argc, VALUE *argv, VALUE vself){
595
+ VALUE vhdb, vpath, vomode;
596
+ TCHDB *hdb;
597
+ int omode;
598
+ rb_scan_args(argc, argv, "11", &vpath, &vomode);
599
+ Check_Type(vpath, T_STRING);
600
+ omode = (vomode == Qnil) ? HDBOREADER : NUM2INT(vomode);
601
+ vhdb = rb_iv_get(vself, HDBVNDATA);
602
+ Data_Get_Struct(vhdb, TCHDB, hdb);
603
+ return tchdbopen(hdb, RSTRING_PTR(vpath), omode) ? Qtrue : Qfalse;
604
+ }
605
+
606
+
607
+ static VALUE hdb_close(VALUE vself){
608
+ VALUE vhdb;
609
+ TCHDB *hdb;
610
+ vhdb = rb_iv_get(vself, HDBVNDATA);
611
+ Data_Get_Struct(vhdb, TCHDB, hdb);
612
+ return tchdbclose(hdb) ? Qtrue : Qfalse;
613
+ }
614
+
615
+
616
+ static VALUE hdb_put(VALUE vself, VALUE vkey, VALUE vval){
617
+ VALUE vhdb;
618
+ TCHDB *hdb;
619
+ vkey = StringValueEx(vkey);
620
+ vval = StringValueEx(vval);
621
+ vhdb = rb_iv_get(vself, HDBVNDATA);
622
+ Data_Get_Struct(vhdb, TCHDB, hdb);
623
+ return tchdbput(hdb, RSTRING_PTR(vkey), RSTRING_LEN(vkey),
624
+ RSTRING_PTR(vval), RSTRING_LEN(vval)) ? Qtrue : Qfalse;
625
+ }
626
+
627
+
628
+ static VALUE hdb_putkeep(VALUE vself, VALUE vkey, VALUE vval){
629
+ VALUE vhdb;
630
+ TCHDB *hdb;
631
+ vkey = StringValueEx(vkey);
632
+ vval = StringValueEx(vval);
633
+ vhdb = rb_iv_get(vself, HDBVNDATA);
634
+ Data_Get_Struct(vhdb, TCHDB, hdb);
635
+ return tchdbputkeep(hdb, RSTRING_PTR(vkey), RSTRING_LEN(vkey),
636
+ RSTRING_PTR(vval), RSTRING_LEN(vval)) ? Qtrue : Qfalse;
637
+ }
638
+
639
+
640
+ static VALUE hdb_putcat(VALUE vself, VALUE vkey, VALUE vval){
641
+ VALUE vhdb;
642
+ TCHDB *hdb;
643
+ vkey = StringValueEx(vkey);
644
+ vval = StringValueEx(vval);
645
+ vhdb = rb_iv_get(vself, HDBVNDATA);
646
+ Data_Get_Struct(vhdb, TCHDB, hdb);
647
+ return tchdbputcat(hdb, RSTRING_PTR(vkey), RSTRING_LEN(vkey),
648
+ RSTRING_PTR(vval), RSTRING_LEN(vval)) ? Qtrue : Qfalse;
649
+ }
650
+
651
+
652
+ static VALUE hdb_putasync(VALUE vself, VALUE vkey, VALUE vval){
653
+ VALUE vhdb;
654
+ TCHDB *hdb;
655
+ vkey = StringValueEx(vkey);
656
+ vval = StringValueEx(vval);
657
+ vhdb = rb_iv_get(vself, HDBVNDATA);
658
+ Data_Get_Struct(vhdb, TCHDB, hdb);
659
+ return tchdbputasync(hdb, RSTRING_PTR(vkey), RSTRING_LEN(vkey),
660
+ RSTRING_PTR(vval), RSTRING_LEN(vval)) ? Qtrue : Qfalse;
661
+ }
662
+
663
+
664
+ static VALUE hdb_out(VALUE vself, VALUE vkey){
665
+ VALUE vhdb;
666
+ TCHDB *hdb;
667
+ vkey = StringValueEx(vkey);
668
+ vhdb = rb_iv_get(vself, HDBVNDATA);
669
+ Data_Get_Struct(vhdb, TCHDB, hdb);
670
+ return tchdbout(hdb, RSTRING_PTR(vkey), RSTRING_LEN(vkey)) ? Qtrue : Qfalse;
671
+ }
672
+
673
+
674
+ static VALUE hdb_get(VALUE vself, VALUE vkey){
675
+ VALUE vhdb, vval;
676
+ TCHDB *hdb;
677
+ char *vbuf;
678
+ int vsiz;
679
+ vkey = StringValueEx(vkey);
680
+ vhdb = rb_iv_get(vself, HDBVNDATA);
681
+ Data_Get_Struct(vhdb, TCHDB, hdb);
682
+ if(!(vbuf = tchdbget(hdb, RSTRING_PTR(vkey), RSTRING_LEN(vkey), &vsiz))) return Qnil;
683
+ vval = rb_str_new(vbuf, vsiz);
684
+ tcfree(vbuf);
685
+ return vval;
686
+ }
687
+
688
+
689
+ static VALUE hdb_vsiz(VALUE vself, VALUE vkey){
690
+ VALUE vhdb;
691
+ TCHDB *hdb;
692
+ vkey = StringValueEx(vkey);
693
+ vhdb = rb_iv_get(vself, HDBVNDATA);
694
+ Data_Get_Struct(vhdb, TCHDB, hdb);
695
+ return INT2NUM(tchdbvsiz(hdb, RSTRING_PTR(vkey), RSTRING_LEN(vkey)));
696
+ }
697
+
698
+
699
+ static VALUE hdb_iterinit(VALUE vself){
700
+ VALUE vhdb;
701
+ TCHDB *hdb;
702
+ vhdb = rb_iv_get(vself, HDBVNDATA);
703
+ Data_Get_Struct(vhdb, TCHDB, hdb);
704
+ return tchdbiterinit(hdb) ? Qtrue : Qfalse;
705
+ }
706
+
707
+
708
+ static VALUE hdb_iternext(VALUE vself){
709
+ VALUE vhdb, vval;
710
+ TCHDB *hdb;
711
+ char *vbuf;
712
+ int vsiz;
713
+ vhdb = rb_iv_get(vself, HDBVNDATA);
714
+ Data_Get_Struct(vhdb, TCHDB, hdb);
715
+ if(!(vbuf = tchdbiternext(hdb, &vsiz))) return Qnil;
716
+ vval = rb_str_new(vbuf, vsiz);
717
+ tcfree(vbuf);
718
+ return vval;
719
+ }
720
+
721
+
722
+ static VALUE hdb_fwmkeys(int argc, VALUE *argv, VALUE vself){
723
+ VALUE vhdb, vprefix, vmax, vary;
724
+ TCHDB *hdb;
725
+ TCLIST *keys;
726
+ int max;
727
+ rb_scan_args(argc, argv, "11", &vprefix, &vmax);
728
+ vprefix = StringValueEx(vprefix);
729
+ max = (vmax == Qnil) ? -1 : NUM2INT(vmax);
730
+ vhdb = rb_iv_get(vself, HDBVNDATA);
731
+ Data_Get_Struct(vhdb, TCHDB, hdb);
732
+ keys = tchdbfwmkeys(hdb, RSTRING_PTR(vprefix), RSTRING_LEN(vprefix), max);
733
+ vary = listtovary(keys);
734
+ tclistdel(keys);
735
+ return vary;
736
+ }
737
+
738
+
739
+ static VALUE hdb_addint(VALUE vself, VALUE vkey, VALUE vnum){
740
+ VALUE vhdb;
741
+ TCHDB *hdb;
742
+ int num;
743
+ vkey = StringValueEx(vkey);
744
+ vhdb = rb_iv_get(vself, HDBVNDATA);
745
+ Data_Get_Struct(vhdb, TCHDB, hdb);
746
+ num = tchdbaddint(hdb, RSTRING_PTR(vkey), RSTRING_LEN(vkey), NUM2INT(vnum));
747
+ return num == INT_MIN ? Qnil : INT2NUM(num);
748
+ }
749
+
750
+
751
+ static VALUE hdb_adddouble(VALUE vself, VALUE vkey, VALUE vnum){
752
+ VALUE vhdb;
753
+ TCHDB *hdb;
754
+ double num;
755
+ vkey = StringValueEx(vkey);
756
+ vhdb = rb_iv_get(vself, HDBVNDATA);
757
+ Data_Get_Struct(vhdb, TCHDB, hdb);
758
+ num = tchdbadddouble(hdb, RSTRING_PTR(vkey), RSTRING_LEN(vkey), NUM2DBL(vnum));
759
+ return isnan(num) ? Qnil : rb_float_new(num);
760
+ }
761
+
762
+
763
+ static VALUE hdb_sync(VALUE vself){
764
+ VALUE vhdb;
765
+ TCHDB *hdb;
766
+ vhdb = rb_iv_get(vself, HDBVNDATA);
767
+ Data_Get_Struct(vhdb, TCHDB, hdb);
768
+ return tchdbsync(hdb) ? Qtrue : Qfalse;
769
+ }
770
+
771
+
772
+ static VALUE hdb_optimize(int argc, VALUE *argv, VALUE vself){
773
+ VALUE vhdb, vbnum, vapow, vfpow, vopts;
774
+ TCHDB *hdb;
775
+ int apow, fpow, opts;
776
+ int64_t bnum;
777
+ rb_scan_args(argc, argv, "04", &vbnum, &vapow, &vfpow, &vopts);
778
+ bnum = (vbnum == Qnil) ? -1 : NUM2LL(vbnum);
779
+ apow = (vapow == Qnil) ? -1 : NUM2INT(vapow);
780
+ fpow = (vfpow == Qnil) ? -1 : NUM2INT(vfpow);
781
+ opts = (vopts == Qnil) ? UINT8_MAX : NUM2INT(vopts);
782
+ vhdb = rb_iv_get(vself, HDBVNDATA);
783
+ Data_Get_Struct(vhdb, TCHDB, hdb);
784
+ return tchdboptimize(hdb, bnum, apow, fpow, opts) ? Qtrue : Qfalse;
785
+ }
786
+
787
+
788
+ static VALUE hdb_vanish(VALUE vself){
789
+ VALUE vhdb;
790
+ TCHDB *hdb;
791
+ vhdb = rb_iv_get(vself, HDBVNDATA);
792
+ Data_Get_Struct(vhdb, TCHDB, hdb);
793
+ return tchdbvanish(hdb) ? Qtrue : Qfalse;
794
+ }
795
+
796
+
797
+ static VALUE hdb_copy(VALUE vself, VALUE vpath){
798
+ VALUE vhdb;
799
+ TCHDB *hdb;
800
+ Check_Type(vpath, T_STRING);
801
+ vhdb = rb_iv_get(vself, HDBVNDATA);
802
+ Data_Get_Struct(vhdb, TCHDB, hdb);
803
+ return tchdbcopy(hdb, RSTRING_PTR(vpath)) ? Qtrue : Qfalse;
804
+ }
805
+
806
+
807
+ static VALUE hdb_tranbegin(VALUE vself){
808
+ VALUE vhdb;
809
+ TCHDB *hdb;
810
+ vhdb = rb_iv_get(vself, HDBVNDATA);
811
+ Data_Get_Struct(vhdb, TCHDB, hdb);
812
+ return tchdbtranbegin(hdb) ? Qtrue : Qfalse;
813
+ }
814
+
815
+
816
+ static VALUE hdb_trancommit(VALUE vself){
817
+ VALUE vhdb;
818
+ TCHDB *hdb;
819
+ vhdb = rb_iv_get(vself, HDBVNDATA);
820
+ Data_Get_Struct(vhdb, TCHDB, hdb);
821
+ return tchdbtrancommit(hdb) ? Qtrue : Qfalse;
822
+ }
823
+
824
+
825
+ static VALUE hdb_tranabort(VALUE vself){
826
+ VALUE vhdb;
827
+ TCHDB *hdb;
828
+ vhdb = rb_iv_get(vself, HDBVNDATA);
829
+ Data_Get_Struct(vhdb, TCHDB, hdb);
830
+ return tchdbtranabort(hdb) ? Qtrue : Qfalse;
831
+ }
832
+
833
+
834
+ static VALUE hdb_path(VALUE vself){
835
+ VALUE vhdb;
836
+ TCHDB *hdb;
837
+ const char *path;
838
+ vhdb = rb_iv_get(vself, HDBVNDATA);
839
+ Data_Get_Struct(vhdb, TCHDB, hdb);
840
+ if(!(path = tchdbpath(hdb))) return Qnil;
841
+ return rb_str_new2(path);
842
+ }
843
+
844
+
845
+ static VALUE hdb_rnum(VALUE vself){
846
+ VALUE vhdb;
847
+ TCHDB *hdb;
848
+ vhdb = rb_iv_get(vself, HDBVNDATA);
849
+ Data_Get_Struct(vhdb, TCHDB, hdb);
850
+ return LL2NUM(tchdbrnum(hdb));
851
+ }
852
+
853
+
854
+ static VALUE hdb_fsiz(VALUE vself){
855
+ VALUE vhdb;
856
+ TCHDB *hdb;
857
+ vhdb = rb_iv_get(vself, HDBVNDATA);
858
+ Data_Get_Struct(vhdb, TCHDB, hdb);
859
+ return LL2NUM(tchdbfsiz(hdb));
860
+ }
861
+
862
+
863
+ static VALUE hdb_fetch(int argc, VALUE *argv, VALUE vself){
864
+ VALUE vhdb, vkey, vdef, vval;
865
+ TCHDB *hdb;
866
+ char *vbuf;
867
+ int vsiz;
868
+ rb_scan_args(argc, argv, "11", &vkey, &vdef);
869
+ vkey = StringValueEx(vkey);
870
+ vhdb = rb_iv_get(vself, HDBVNDATA);
871
+ Data_Get_Struct(vhdb, TCHDB, hdb);
872
+ if((vbuf = tchdbget(hdb, RSTRING_PTR(vkey), RSTRING_LEN(vkey), &vsiz)) != NULL){
873
+ vval = rb_str_new(vbuf, vsiz);
874
+ tcfree(vbuf);
875
+ } else {
876
+ vval = vdef;
877
+ }
878
+ return vval;
879
+ }
880
+
881
+
882
+ static VALUE hdb_check(VALUE vself, VALUE vkey){
883
+ VALUE vhdb;
884
+ TCHDB *hdb;
885
+ vkey = StringValueEx(vkey);
886
+ vhdb = rb_iv_get(vself, HDBVNDATA);
887
+ Data_Get_Struct(vhdb, TCHDB, hdb);
888
+ return tchdbvsiz(hdb, RSTRING_PTR(vkey), RSTRING_LEN(vkey)) >= 0 ? Qtrue : Qfalse;
889
+ }
890
+
891
+
892
+ static VALUE hdb_check_value(VALUE vself, VALUE vval){
893
+ VALUE vhdb;
894
+ TCHDB *hdb;
895
+ TCXSTR *kxstr, *vxstr;
896
+ bool hit;
897
+ vval = StringValueEx(vval);
898
+ vhdb = rb_iv_get(vself, HDBVNDATA);
899
+ Data_Get_Struct(vhdb, TCHDB, hdb);
900
+ hit = false;
901
+ kxstr = tcxstrnew();
902
+ vxstr = tcxstrnew();
903
+ tchdbiterinit(hdb);
904
+ while(tchdbiternext3(hdb, kxstr, vxstr)){
905
+ if(tcxstrsize(vxstr) == RSTRING_LEN(vval) &&
906
+ memcmp(tcxstrptr(vxstr), RSTRING_PTR(vval), RSTRING_LEN(vval)) == 0){
907
+ hit = true;
908
+ break;
909
+ }
910
+ }
911
+ tcxstrdel(vxstr);
912
+ tcxstrdel(kxstr);
913
+ return hit ? Qtrue : Qfalse;
914
+ }
915
+
916
+
917
+ static VALUE hdb_get_reverse(VALUE vself, VALUE vval){
918
+ VALUE vhdb, vrv;
919
+ TCHDB *hdb;
920
+ TCXSTR *kxstr, *vxstr;
921
+ vval = StringValueEx(vval);
922
+ vhdb = rb_iv_get(vself, HDBVNDATA);
923
+ Data_Get_Struct(vhdb, TCHDB, hdb);
924
+ vrv = Qnil;
925
+ kxstr = tcxstrnew();
926
+ vxstr = tcxstrnew();
927
+ tchdbiterinit(hdb);
928
+ while(tchdbiternext3(hdb, kxstr, vxstr)){
929
+ if(tcxstrsize(vxstr) == RSTRING_LEN(vval) &&
930
+ memcmp(tcxstrptr(vxstr), RSTRING_PTR(vval), RSTRING_LEN(vval)) == 0){
931
+ vrv = rb_str_new(tcxstrptr(kxstr), tcxstrsize(kxstr));
932
+ break;
933
+ }
934
+ }
935
+ tcxstrdel(vxstr);
936
+ tcxstrdel(kxstr);
937
+ return vrv;
938
+ }
939
+
940
+
941
+ static VALUE hdb_empty(VALUE vself){
942
+ VALUE vhdb;
943
+ TCHDB *hdb;
944
+ vhdb = rb_iv_get(vself, HDBVNDATA);
945
+ Data_Get_Struct(vhdb, TCHDB, hdb);
946
+ return tchdbrnum(hdb) < 1 ? Qtrue : Qfalse;
947
+ }
948
+
949
+
950
+ static VALUE hdb_each(VALUE vself){
951
+ VALUE vhdb, vrv;
952
+ TCHDB *hdb;
953
+ TCXSTR *kxstr, *vxstr;
954
+ if(rb_block_given_p() != Qtrue) rb_raise(rb_eArgError, "no block given");
955
+ vhdb = rb_iv_get(vself, HDBVNDATA);
956
+ Data_Get_Struct(vhdb, TCHDB, hdb);
957
+ vrv = Qnil;
958
+ kxstr = tcxstrnew();
959
+ vxstr = tcxstrnew();
960
+ tchdbiterinit(hdb);
961
+ while(tchdbiternext3(hdb, kxstr, vxstr)){
962
+ vrv = rb_yield_values(2, rb_str_new(tcxstrptr(kxstr), tcxstrsize(kxstr)),
963
+ rb_str_new(tcxstrptr(vxstr), tcxstrsize(vxstr)));
964
+ }
965
+ tcxstrdel(vxstr);
966
+ tcxstrdel(kxstr);
967
+ return vrv;
968
+ }
969
+
970
+
971
+ static VALUE hdb_each_key(VALUE vself){
972
+ VALUE vhdb, vrv;
973
+ TCHDB *hdb;
974
+ TCXSTR *kxstr, *vxstr;
975
+ if(rb_block_given_p() != Qtrue) rb_raise(rb_eArgError, "no block given");
976
+ vhdb = rb_iv_get(vself, HDBVNDATA);
977
+ Data_Get_Struct(vhdb, TCHDB, hdb);
978
+ vrv = Qnil;
979
+ kxstr = tcxstrnew();
980
+ vxstr = tcxstrnew();
981
+ tchdbiterinit(hdb);
982
+ while(tchdbiternext3(hdb, kxstr, vxstr)){
983
+ vrv = rb_yield(rb_str_new(tcxstrptr(kxstr), tcxstrsize(kxstr)));
984
+ }
985
+ tcxstrdel(vxstr);
986
+ tcxstrdel(kxstr);
987
+ return vrv;
988
+ }
989
+
990
+
991
+ static VALUE hdb_each_value(VALUE vself){
992
+ VALUE vhdb, vrv;
993
+ TCHDB *hdb;
994
+ TCXSTR *kxstr, *vxstr;
995
+ if(rb_block_given_p() != Qtrue) rb_raise(rb_eArgError, "no block given");
996
+ vhdb = rb_iv_get(vself, HDBVNDATA);
997
+ Data_Get_Struct(vhdb, TCHDB, hdb);
998
+ vrv = Qnil;
999
+ kxstr = tcxstrnew();
1000
+ vxstr = tcxstrnew();
1001
+ tchdbiterinit(hdb);
1002
+ while(tchdbiternext3(hdb, kxstr, vxstr)){
1003
+ vrv = rb_yield(rb_str_new(tcxstrptr(vxstr), tcxstrsize(vxstr)));
1004
+ }
1005
+ tcxstrdel(vxstr);
1006
+ tcxstrdel(kxstr);
1007
+ return vrv;
1008
+ }
1009
+
1010
+
1011
+ static VALUE hdb_keys(VALUE vself){
1012
+ VALUE vhdb, vary;
1013
+ TCHDB *hdb;
1014
+ TCXSTR *kxstr, *vxstr;
1015
+ vhdb = rb_iv_get(vself, HDBVNDATA);
1016
+ Data_Get_Struct(vhdb, TCHDB, hdb);
1017
+ vary = rb_ary_new2(tchdbrnum(hdb));
1018
+ kxstr = tcxstrnew();
1019
+ vxstr = tcxstrnew();
1020
+ tchdbiterinit(hdb);
1021
+ while(tchdbiternext3(hdb, kxstr, vxstr)){
1022
+ rb_ary_push(vary, rb_str_new(tcxstrptr(kxstr), tcxstrsize(kxstr)));
1023
+ }
1024
+ tcxstrdel(vxstr);
1025
+ tcxstrdel(kxstr);
1026
+ return vary;
1027
+ }
1028
+
1029
+
1030
+ static VALUE hdb_values(VALUE vself){
1031
+ VALUE vhdb, vary;
1032
+ TCHDB *hdb;
1033
+ TCXSTR *kxstr, *vxstr;
1034
+ vhdb = rb_iv_get(vself, HDBVNDATA);
1035
+ Data_Get_Struct(vhdb, TCHDB, hdb);
1036
+ vary = rb_ary_new2(tchdbrnum(hdb));
1037
+ kxstr = tcxstrnew();
1038
+ vxstr = tcxstrnew();
1039
+ tchdbiterinit(hdb);
1040
+ while(tchdbiternext3(hdb, kxstr, vxstr)){
1041
+ rb_ary_push(vary, rb_str_new(tcxstrptr(vxstr), tcxstrsize(vxstr)));
1042
+ }
1043
+ tcxstrdel(vxstr);
1044
+ tcxstrdel(kxstr);
1045
+ return vary;
1046
+ }
1047
+
1048
+
1049
+ static void bdb_init(void){
1050
+ cls_bdb = rb_define_class_under(mod_tokyocabinet, "BDB", rb_cObject);
1051
+ cls_bdb_data = rb_define_class_under(mod_tokyocabinet, "BDB_data", rb_cObject);
1052
+ bdb_cmp_call_mid = rb_intern("call");
1053
+ rb_define_const(cls_bdb, "ESUCCESS", INT2NUM(TCESUCCESS));
1054
+ rb_define_const(cls_bdb, "ETHREAD", INT2NUM(TCETHREAD));
1055
+ rb_define_const(cls_bdb, "EINVALID", INT2NUM(TCEINVALID));
1056
+ rb_define_const(cls_bdb, "ENOFILE", INT2NUM(TCENOFILE));
1057
+ rb_define_const(cls_bdb, "ENOPERM", INT2NUM(TCENOPERM));
1058
+ rb_define_const(cls_bdb, "EMETA", INT2NUM(TCEMETA));
1059
+ rb_define_const(cls_bdb, "ERHEAD", INT2NUM(TCERHEAD));
1060
+ rb_define_const(cls_bdb, "EOPEN", INT2NUM(TCEOPEN));
1061
+ rb_define_const(cls_bdb, "ECLOSE", INT2NUM(TCECLOSE));
1062
+ rb_define_const(cls_bdb, "ETRUNC", INT2NUM(TCETRUNC));
1063
+ rb_define_const(cls_bdb, "ESYNC", INT2NUM(TCESYNC));
1064
+ rb_define_const(cls_bdb, "ESTAT", INT2NUM(TCESTAT));
1065
+ rb_define_const(cls_bdb, "ESEEK", INT2NUM(TCESEEK));
1066
+ rb_define_const(cls_bdb, "EREAD", INT2NUM(TCEREAD));
1067
+ rb_define_const(cls_bdb, "EWRITE", INT2NUM(TCEWRITE));
1068
+ rb_define_const(cls_bdb, "EMMAP", INT2NUM(TCEMMAP));
1069
+ rb_define_const(cls_bdb, "ELOCK", INT2NUM(TCELOCK));
1070
+ rb_define_const(cls_bdb, "EUNLINK", INT2NUM(TCEUNLINK));
1071
+ rb_define_const(cls_bdb, "ERENAME", INT2NUM(TCERENAME));
1072
+ rb_define_const(cls_bdb, "EMKDIR", INT2NUM(TCEMKDIR));
1073
+ rb_define_const(cls_bdb, "ERMDIR", INT2NUM(TCERMDIR));
1074
+ rb_define_const(cls_bdb, "EKEEP", INT2NUM(TCEKEEP));
1075
+ rb_define_const(cls_bdb, "ENOREC", INT2NUM(TCENOREC));
1076
+ rb_define_const(cls_bdb, "EMISC", INT2NUM(TCEMISC));
1077
+ rb_define_const(cls_bdb, "CMPLEXICAL", rb_str_new2("CMPLEXICAL"));
1078
+ rb_define_const(cls_bdb, "CMPDECIMAL", rb_str_new2("CMPDECIMAL"));
1079
+ rb_define_const(cls_bdb, "CMPINT32", rb_str_new2("CMPINT32"));
1080
+ rb_define_const(cls_bdb, "CMPINT64", rb_str_new2("CMPINT64"));
1081
+ rb_define_const(cls_bdb, "TLARGE", INT2NUM(BDBTLARGE));
1082
+ rb_define_const(cls_bdb, "TDEFLATE", INT2NUM(BDBTDEFLATE));
1083
+ rb_define_const(cls_bdb, "TBZIP", INT2NUM(BDBTBZIP));
1084
+ rb_define_const(cls_bdb, "TTCBS", INT2NUM(BDBTTCBS));
1085
+ rb_define_const(cls_bdb, "OREADER", INT2NUM(BDBOREADER));
1086
+ rb_define_const(cls_bdb, "OWRITER", INT2NUM(BDBOWRITER));
1087
+ rb_define_const(cls_bdb, "OCREAT", INT2NUM(BDBOCREAT));
1088
+ rb_define_const(cls_bdb, "OTRUNC", INT2NUM(BDBOTRUNC));
1089
+ rb_define_const(cls_bdb, "ONOLCK", INT2NUM(BDBONOLCK));
1090
+ rb_define_const(cls_bdb, "OLCKNB", INT2NUM(BDBOLCKNB));
1091
+ rb_define_const(cls_bdb, "OTSYNC", INT2NUM(BDBOTSYNC));
1092
+ rb_define_private_method(cls_bdb, "initialize", bdb_initialize, 0);
1093
+ rb_define_method(cls_bdb, "errmsg", bdb_errmsg, -1);
1094
+ rb_define_method(cls_bdb, "ecode", bdb_ecode, 0);
1095
+ rb_define_method(cls_bdb, "setcmpfunc", bdb_setcmpfunc, 1);
1096
+ rb_define_method(cls_bdb, "tune", bdb_tune, -1);
1097
+ rb_define_method(cls_bdb, "setcache", bdb_setcache, -1);
1098
+ rb_define_method(cls_bdb, "setxmsiz", bdb_setxmsiz, -1);
1099
+ rb_define_method(cls_bdb, "setdfunit", bdb_setdfunit, -1);
1100
+ rb_define_method(cls_bdb, "open", bdb_open, -1);
1101
+ rb_define_method(cls_bdb, "close", bdb_close, 0);
1102
+ rb_define_method(cls_bdb, "put", bdb_put, 2);
1103
+ rb_define_method(cls_bdb, "putkeep", bdb_putkeep, 2);
1104
+ rb_define_method(cls_bdb, "putcat", bdb_putcat, 2);
1105
+ rb_define_method(cls_bdb, "putdup", bdb_putdup, 2);
1106
+ rb_define_method(cls_bdb, "putlist", bdb_putlist, 2);
1107
+ rb_define_method(cls_bdb, "out", bdb_out, 1);
1108
+ rb_define_method(cls_bdb, "outlist", bdb_outlist, 1);
1109
+ rb_define_method(cls_bdb, "get", bdb_get, 1);
1110
+ rb_define_method(cls_bdb, "getlist", bdb_getlist, 1);
1111
+ rb_define_method(cls_bdb, "vnum", bdb_vnum, 1);
1112
+ rb_define_method(cls_bdb, "vsiz", bdb_vsiz, 1);
1113
+ rb_define_method(cls_bdb, "range", bdb_range, -1);
1114
+ rb_define_method(cls_bdb, "fwmkeys", bdb_fwmkeys, -1);
1115
+ rb_define_method(cls_bdb, "addint", bdb_addint, 2);
1116
+ rb_define_method(cls_bdb, "adddouble", bdb_adddouble, 2);
1117
+ rb_define_method(cls_bdb, "sync", bdb_sync, 0);
1118
+ rb_define_method(cls_bdb, "optimize", bdb_optimize, -1);
1119
+ rb_define_method(cls_bdb, "vanish", bdb_vanish, 0);
1120
+ rb_define_method(cls_bdb, "copy", bdb_copy, 1);
1121
+ rb_define_method(cls_bdb, "tranbegin", bdb_tranbegin, 0);
1122
+ rb_define_method(cls_bdb, "trancommit", bdb_trancommit, 0);
1123
+ rb_define_method(cls_bdb, "tranabort", bdb_tranabort, 0);
1124
+ rb_define_method(cls_bdb, "path", bdb_path, 0);
1125
+ rb_define_method(cls_bdb, "rnum", bdb_rnum, 0);
1126
+ rb_define_method(cls_bdb, "fsiz", bdb_fsiz, 0);
1127
+ rb_define_method(cls_bdb, "[]", bdb_get, 1);
1128
+ rb_define_method(cls_bdb, "[]=", bdb_put, 2);
1129
+ rb_define_method(cls_bdb, "store", bdb_put, 2);
1130
+ rb_define_method(cls_bdb, "delete", bdb_out, 1);
1131
+ rb_define_method(cls_bdb, "fetch", bdb_fetch, -1);
1132
+ rb_define_method(cls_bdb, "has_key?", bdb_check, 1);
1133
+ rb_define_method(cls_bdb, "key?", bdb_check, 1);
1134
+ rb_define_method(cls_bdb, "include?", bdb_check, 1);
1135
+ rb_define_method(cls_bdb, "member?", bdb_check, 1);
1136
+ rb_define_method(cls_bdb, "has_value?", bdb_check_value, 1);
1137
+ rb_define_method(cls_bdb, "value?", bdb_check_value, 1);
1138
+ rb_define_method(cls_bdb, "key", bdb_get_reverse, 1);
1139
+ rb_define_method(cls_bdb, "clear", bdb_vanish, 0);
1140
+ rb_define_method(cls_bdb, "size", bdb_rnum, 0);
1141
+ rb_define_method(cls_bdb, "length", bdb_rnum, 0);
1142
+ rb_define_method(cls_bdb, "empty?", bdb_empty, 0);
1143
+ rb_define_method(cls_bdb, "each", bdb_each, 0);
1144
+ rb_define_method(cls_bdb, "each_pair", bdb_each, 0);
1145
+ rb_define_method(cls_bdb, "each_key", bdb_each_key, 0);
1146
+ rb_define_method(cls_bdb, "each_value", bdb_each_value, 0);
1147
+ rb_define_method(cls_bdb, "keys", bdb_keys, 0);
1148
+ rb_define_method(cls_bdb, "values", bdb_values, 0);
1149
+ }
1150
+
1151
+
1152
+ static int bdb_cmpobj(const char *aptr, int asiz, const char *bptr, int bsiz, VALUE vcmp){
1153
+ VALUE vrv;
1154
+ vrv = rb_funcall(vcmp, bdb_cmp_call_mid, 2, rb_str_new(aptr, asiz), rb_str_new(bptr, bsiz));
1155
+ return (vrv == Qnil) ? 0 : NUM2INT(vrv);
1156
+ }
1157
+
1158
+
1159
+ static VALUE bdb_initialize(VALUE vself){
1160
+ VALUE vbdb;
1161
+ TCBDB *bdb;
1162
+ bdb = tcbdbnew();
1163
+ tcbdbsetmutex(bdb);
1164
+ vbdb = Data_Wrap_Struct(cls_bdb_data, 0, tcbdbdel, bdb);
1165
+ rb_iv_set(vself, BDBVNDATA, vbdb);
1166
+ return Qnil;
1167
+ }
1168
+
1169
+
1170
+ static VALUE bdb_errmsg(int argc, VALUE *argv, VALUE vself){
1171
+ VALUE vbdb, vecode;
1172
+ TCBDB *bdb;
1173
+ const char *msg;
1174
+ int ecode;
1175
+ rb_scan_args(argc, argv, "01", &vecode);
1176
+ vbdb = rb_iv_get(vself, BDBVNDATA);
1177
+ Data_Get_Struct(vbdb, TCBDB, bdb);
1178
+ ecode = (vecode == Qnil) ? tcbdbecode(bdb) : NUM2INT(vecode);
1179
+ msg = tcbdberrmsg(ecode);
1180
+ return rb_str_new2(msg);
1181
+ }
1182
+
1183
+
1184
+ static VALUE bdb_ecode(VALUE vself){
1185
+ VALUE vbdb;
1186
+ TCBDB *bdb;
1187
+ vbdb = rb_iv_get(vself, BDBVNDATA);
1188
+ Data_Get_Struct(vbdb, TCBDB, bdb);
1189
+ return INT2NUM(tcbdbecode(bdb));
1190
+ }
1191
+
1192
+
1193
+ static VALUE bdb_setcmpfunc(VALUE vself, VALUE vcmp){
1194
+ VALUE vbdb;
1195
+ TCBDB *bdb;
1196
+ TCCMP cmp;
1197
+ cmp = (TCCMP)bdb_cmpobj;
1198
+ if(TYPE(vcmp) == T_STRING){
1199
+ if(!strcmp(RSTRING_PTR(vcmp), "CMPLEXICAL")){
1200
+ cmp = tccmplexical;
1201
+ } else if(!strcmp(RSTRING_PTR(vcmp), "CMPDECIMAL")){
1202
+ cmp = tccmpdecimal;
1203
+ } else if(!strcmp(RSTRING_PTR(vcmp), "CMPINT32")){
1204
+ cmp = tccmpint32;
1205
+ } else if(!strcmp(RSTRING_PTR(vcmp), "CMPINT64")){
1206
+ cmp = tccmpint64;
1207
+ } else {
1208
+ rb_raise(rb_eArgError, "unknown comparison function: %s", RSTRING_PTR(vcmp));
1209
+ }
1210
+ } else if(!rb_respond_to(vcmp, bdb_cmp_call_mid)){
1211
+ rb_raise(rb_eArgError, "call method is not implemented");
1212
+ }
1213
+ vbdb = rb_iv_get(vself, BDBVNDATA);
1214
+ Data_Get_Struct(vbdb, TCBDB, bdb);
1215
+ return tcbdbsetcmpfunc(bdb, cmp, (void *)(intptr_t)vcmp);
1216
+ }
1217
+
1218
+
1219
+ static VALUE bdb_tune(int argc, VALUE *argv, VALUE vself){
1220
+ VALUE vbdb, vlmemb, vnmemb, vbnum, vapow, vfpow, vopts;
1221
+ TCBDB *bdb;
1222
+ int lmemb, nmemb, apow, fpow, opts;
1223
+ int64_t bnum;
1224
+ rb_scan_args(argc, argv, "06", &vlmemb, &vnmemb, &vbnum, &vapow, &vfpow, &vopts);
1225
+ lmemb = (vlmemb == Qnil) ? -1 : NUM2INT(vlmemb);
1226
+ nmemb = (vnmemb == Qnil) ? -1 : NUM2INT(vnmemb);
1227
+ bnum = (vbnum == Qnil) ? -1 : NUM2LL(vbnum);
1228
+ apow = (vapow == Qnil) ? -1 : NUM2INT(vapow);
1229
+ fpow = (vfpow == Qnil) ? -1 : NUM2INT(vfpow);
1230
+ opts = (vopts == Qnil) ? 0 : NUM2INT(vopts);
1231
+ vbdb = rb_iv_get(vself, BDBVNDATA);
1232
+ Data_Get_Struct(vbdb, TCBDB, bdb);
1233
+ return tcbdbtune(bdb, lmemb, nmemb, bnum, apow, fpow, opts) ? Qtrue : Qfalse;
1234
+ }
1235
+
1236
+
1237
+ static VALUE bdb_setcache(int argc, VALUE *argv, VALUE vself){
1238
+ VALUE vbdb, vlcnum, vncnum;
1239
+ TCBDB *bdb;
1240
+ int lcnum, ncnum;
1241
+ rb_scan_args(argc, argv, "02", &vlcnum, &vncnum);
1242
+ lcnum = (vlcnum == Qnil) ? -1 : NUM2INT(vlcnum);
1243
+ ncnum = (vncnum == Qnil) ? -1 : NUM2INT(vncnum);
1244
+ vbdb = rb_iv_get(vself, BDBVNDATA);
1245
+ Data_Get_Struct(vbdb, TCBDB, bdb);
1246
+ return tcbdbsetcache(bdb, lcnum, ncnum);
1247
+ }
1248
+
1249
+
1250
+ static VALUE bdb_setxmsiz(int argc, VALUE *argv, VALUE vself){
1251
+ VALUE vbdb, vxmsiz;
1252
+ TCBDB *bdb;
1253
+ int64_t xmsiz;
1254
+ rb_scan_args(argc, argv, "01", &vxmsiz);
1255
+ xmsiz = (vxmsiz == Qnil) ? -1 : NUM2LL(vxmsiz);
1256
+ vbdb = rb_iv_get(vself, BDBVNDATA);
1257
+ Data_Get_Struct(vbdb, TCBDB, bdb);
1258
+ return tcbdbsetxmsiz(bdb, xmsiz) ? Qtrue : Qfalse;
1259
+ }
1260
+
1261
+
1262
+ static VALUE bdb_setdfunit(int argc, VALUE *argv, VALUE vself){
1263
+ VALUE vbdb, vdfunit;
1264
+ TCBDB *bdb;
1265
+ int32_t dfunit;
1266
+ rb_scan_args(argc, argv, "01", &vdfunit);
1267
+ dfunit = (vdfunit == Qnil) ? -1 : NUM2INT(vdfunit);
1268
+ vbdb = rb_iv_get(vself, BDBVNDATA);
1269
+ Data_Get_Struct(vbdb, TCBDB, bdb);
1270
+ return tcbdbsetdfunit(bdb, dfunit) ? Qtrue : Qfalse;
1271
+ }
1272
+
1273
+
1274
+ static VALUE bdb_open(int argc, VALUE *argv, VALUE vself){
1275
+ VALUE vbdb, vpath, vomode;
1276
+ TCBDB *bdb;
1277
+ int omode;
1278
+ rb_scan_args(argc, argv, "11", &vpath, &vomode);
1279
+ Check_Type(vpath, T_STRING);
1280
+ omode = (vomode == Qnil) ? BDBOREADER : NUM2INT(vomode);
1281
+ vbdb = rb_iv_get(vself, BDBVNDATA);
1282
+ Data_Get_Struct(vbdb, TCBDB, bdb);
1283
+ return tcbdbopen(bdb, RSTRING_PTR(vpath), omode) ? Qtrue : Qfalse;
1284
+ }
1285
+
1286
+
1287
+ static VALUE bdb_close(VALUE vself){
1288
+ VALUE vbdb;
1289
+ TCBDB *bdb;
1290
+ vbdb = rb_iv_get(vself, BDBVNDATA);
1291
+ Data_Get_Struct(vbdb, TCBDB, bdb);
1292
+ return tcbdbclose(bdb) ? Qtrue : Qfalse;
1293
+ }
1294
+
1295
+
1296
+ static VALUE bdb_put(VALUE vself, VALUE vkey, VALUE vval){
1297
+ VALUE vbdb;
1298
+ TCBDB *bdb;
1299
+ vkey = StringValueEx(vkey);
1300
+ vval = StringValueEx(vval);
1301
+ vbdb = rb_iv_get(vself, BDBVNDATA);
1302
+ Data_Get_Struct(vbdb, TCBDB, bdb);
1303
+ return tcbdbput(bdb, RSTRING_PTR(vkey), RSTRING_LEN(vkey), RSTRING_PTR(vval),
1304
+ RSTRING_LEN(vval)) ? Qtrue : Qfalse;
1305
+ }
1306
+
1307
+
1308
+ static VALUE bdb_putkeep(VALUE vself, VALUE vkey, VALUE vval){
1309
+ VALUE vbdb;
1310
+ TCBDB *bdb;
1311
+ vkey = StringValueEx(vkey);
1312
+ vval = StringValueEx(vval);
1313
+ vbdb = rb_iv_get(vself, BDBVNDATA);
1314
+ Data_Get_Struct(vbdb, TCBDB, bdb);
1315
+ return tcbdbputkeep(bdb, RSTRING_PTR(vkey), RSTRING_LEN(vkey), RSTRING_PTR(vval),
1316
+ RSTRING_LEN(vval)) ? Qtrue : Qfalse;
1317
+ }
1318
+
1319
+
1320
+ static VALUE bdb_putcat(VALUE vself, VALUE vkey, VALUE vval){
1321
+ VALUE vbdb;
1322
+ TCBDB *bdb;
1323
+ vkey = StringValueEx(vkey);
1324
+ vval = StringValueEx(vval);
1325
+ vbdb = rb_iv_get(vself, BDBVNDATA);
1326
+ Data_Get_Struct(vbdb, TCBDB, bdb);
1327
+ return tcbdbputcat(bdb, RSTRING_PTR(vkey), RSTRING_LEN(vkey), RSTRING_PTR(vval),
1328
+ RSTRING_LEN(vval)) ? Qtrue : Qfalse;
1329
+ }
1330
+
1331
+
1332
+ static VALUE bdb_putdup(VALUE vself, VALUE vkey, VALUE vval){
1333
+ VALUE vbdb;
1334
+ TCBDB *bdb;
1335
+ vkey = StringValueEx(vkey);
1336
+ vval = StringValueEx(vval);
1337
+ vbdb = rb_iv_get(vself, BDBVNDATA);
1338
+ Data_Get_Struct(vbdb, TCBDB, bdb);
1339
+ return tcbdbputdup(bdb, RSTRING_PTR(vkey), RSTRING_LEN(vkey), RSTRING_PTR(vval),
1340
+ RSTRING_LEN(vval)) ? Qtrue : Qfalse;
1341
+ }
1342
+
1343
+
1344
+ static VALUE bdb_putlist(VALUE vself, VALUE vkey, VALUE vvals){
1345
+ VALUE vbdb;
1346
+ TCBDB *bdb;
1347
+ TCLIST *tvals;
1348
+ bool err;
1349
+ vkey = StringValueEx(vkey);
1350
+ Check_Type(vvals, T_ARRAY);
1351
+ tvals = varytolist(vvals);
1352
+ vbdb = rb_iv_get(vself, BDBVNDATA);
1353
+ Data_Get_Struct(vbdb, TCBDB, bdb);
1354
+ err = false;
1355
+ if(!tcbdbputdup3(bdb, RSTRING_PTR(vkey), RSTRING_LEN(vkey), tvals)) err = true;
1356
+ tclistdel(tvals);
1357
+ return err ? Qfalse : Qtrue;
1358
+ }
1359
+
1360
+
1361
+ static VALUE bdb_out(VALUE vself, VALUE vkey){
1362
+ VALUE vbdb;
1363
+ TCBDB *bdb;
1364
+ vkey = StringValueEx(vkey);
1365
+ vbdb = rb_iv_get(vself, BDBVNDATA);
1366
+ Data_Get_Struct(vbdb, TCBDB, bdb);
1367
+ return tcbdbout(bdb, RSTRING_PTR(vkey), RSTRING_LEN(vkey)) ? Qtrue : Qfalse;
1368
+ }
1369
+
1370
+
1371
+ static VALUE bdb_outlist(VALUE vself, VALUE vkey){
1372
+ VALUE vbdb;
1373
+ TCBDB *bdb;
1374
+ vkey = StringValueEx(vkey);
1375
+ vbdb = rb_iv_get(vself, BDBVNDATA);
1376
+ Data_Get_Struct(vbdb, TCBDB, bdb);
1377
+ return tcbdbout3(bdb, RSTRING_PTR(vkey), RSTRING_LEN(vkey)) ? Qtrue : Qfalse;
1378
+ }
1379
+
1380
+
1381
+ static VALUE bdb_get(VALUE vself, VALUE vkey){
1382
+ VALUE vbdb;
1383
+ TCBDB *bdb;
1384
+ const char *vbuf;
1385
+ int vsiz;
1386
+ vkey = StringValueEx(vkey);
1387
+ vbdb = rb_iv_get(vself, BDBVNDATA);
1388
+ Data_Get_Struct(vbdb, TCBDB, bdb);
1389
+ if(!(vbuf = tcbdbget3(bdb, RSTRING_PTR(vkey), RSTRING_LEN(vkey), &vsiz))) return Qnil;
1390
+ return rb_str_new(vbuf, vsiz);
1391
+ }
1392
+
1393
+
1394
+ static VALUE bdb_getlist(VALUE vself, VALUE vkey){
1395
+ VALUE vbdb, vary;
1396
+ TCBDB *bdb;
1397
+ TCLIST *vals;
1398
+ vkey = StringValueEx(vkey);
1399
+ vbdb = rb_iv_get(vself, BDBVNDATA);
1400
+ Data_Get_Struct(vbdb, TCBDB, bdb);
1401
+ if(!(vals = tcbdbget4(bdb, RSTRING_PTR(vkey), RSTRING_LEN(vkey)))) return Qnil;
1402
+ vary = listtovary(vals);
1403
+ tclistdel(vals);
1404
+ return vary;
1405
+ }
1406
+
1407
+
1408
+ static VALUE bdb_vnum(VALUE vself, VALUE vkey){
1409
+ VALUE vbdb;
1410
+ TCBDB *bdb;
1411
+ vkey = StringValueEx(vkey);
1412
+ vbdb = rb_iv_get(vself, BDBVNDATA);
1413
+ Data_Get_Struct(vbdb, TCBDB, bdb);
1414
+ return INT2NUM(tcbdbvnum(bdb, RSTRING_PTR(vkey), RSTRING_LEN(vkey)));
1415
+ }
1416
+
1417
+
1418
+ static VALUE bdb_vsiz(VALUE vself, VALUE vkey){
1419
+ VALUE vbdb;
1420
+ TCBDB *bdb;
1421
+ vkey = StringValueEx(vkey);
1422
+ vbdb = rb_iv_get(vself, BDBVNDATA);
1423
+ Data_Get_Struct(vbdb, TCBDB, bdb);
1424
+ return INT2NUM(tcbdbvsiz(bdb, RSTRING_PTR(vkey), RSTRING_LEN(vkey)));
1425
+ }
1426
+
1427
+
1428
+ static VALUE bdb_range(int argc, VALUE *argv, VALUE vself){
1429
+ VALUE vbdb, vbkey, vbinc, vekey, veinc, vmax, vary;
1430
+ TCBDB *bdb;
1431
+ TCLIST *keys;
1432
+ const char *bkbuf, *ekbuf;
1433
+ int bksiz, eksiz, max;
1434
+ bool binc, einc;
1435
+ rb_scan_args(argc, argv, "05", &vbkey, &vbinc, &vekey, &veinc, &vmax);
1436
+ if(vbkey != Qnil) vbkey = StringValueEx(vbkey);
1437
+ if(vekey != Qnil) vekey = StringValueEx(vekey);
1438
+ binc = (vbinc != Qnil && vbinc != Qfalse);
1439
+ einc = (veinc != Qnil && veinc != Qfalse);
1440
+ max = (vmax == Qnil) ? -1 : NUM2INT(vmax);
1441
+ vbdb = rb_iv_get(vself, BDBVNDATA);
1442
+ Data_Get_Struct(vbdb, TCBDB, bdb);
1443
+ if(vbkey != Qnil){
1444
+ bkbuf = RSTRING_PTR(vbkey);
1445
+ bksiz = RSTRING_LEN(vbkey);
1446
+ } else {
1447
+ bkbuf = NULL;
1448
+ bksiz = -1;
1449
+ }
1450
+ if(vekey != Qnil){
1451
+ ekbuf = RSTRING_PTR(vekey);
1452
+ eksiz = RSTRING_LEN(vekey);
1453
+ } else {
1454
+ ekbuf = NULL;
1455
+ eksiz = -1;
1456
+ }
1457
+ keys = tcbdbrange(bdb, bkbuf, bksiz, binc, ekbuf, eksiz, einc, max);
1458
+ vary = listtovary(keys);
1459
+ tclistdel(keys);
1460
+ return vary;
1461
+ }
1462
+
1463
+
1464
+ static VALUE bdb_fwmkeys(int argc, VALUE *argv, VALUE vself){
1465
+ VALUE vbdb, vprefix, vmax, vary;
1466
+ TCBDB *bdb;
1467
+ TCLIST *keys;
1468
+ int max;
1469
+ rb_scan_args(argc, argv, "11", &vprefix, &vmax);
1470
+ vprefix = StringValueEx(vprefix);
1471
+ vbdb = rb_iv_get(vself, BDBVNDATA);
1472
+ Data_Get_Struct(vbdb, TCBDB, bdb);
1473
+ max = (vmax == Qnil) ? -1 : NUM2INT(vmax);
1474
+ keys = tcbdbfwmkeys(bdb, RSTRING_PTR(vprefix), RSTRING_LEN(vprefix), max);
1475
+ vary = listtovary(keys);
1476
+ tclistdel(keys);
1477
+ return vary;
1478
+ }
1479
+
1480
+
1481
+ static VALUE bdb_addint(VALUE vself, VALUE vkey, VALUE vnum){
1482
+ VALUE vbdb;
1483
+ TCBDB *bdb;
1484
+ int num;
1485
+ vkey = StringValueEx(vkey);
1486
+ vbdb = rb_iv_get(vself, BDBVNDATA);
1487
+ Data_Get_Struct(vbdb, TCBDB, bdb);
1488
+ num = tcbdbaddint(bdb, RSTRING_PTR(vkey), RSTRING_LEN(vkey), NUM2INT(vnum));
1489
+ return num == INT_MIN ? Qnil : INT2NUM(num);
1490
+ }
1491
+
1492
+
1493
+ static VALUE bdb_adddouble(VALUE vself, VALUE vkey, VALUE vnum){
1494
+ VALUE vbdb;
1495
+ TCBDB *bdb;
1496
+ double num;
1497
+ vkey = StringValueEx(vkey);
1498
+ vbdb = rb_iv_get(vself, BDBVNDATA);
1499
+ Data_Get_Struct(vbdb, TCBDB, bdb);
1500
+ num = tcbdbadddouble(bdb, RSTRING_PTR(vkey), RSTRING_LEN(vkey), NUM2DBL(vnum));
1501
+ return isnan(num) ? Qnil : rb_float_new(num);
1502
+ }
1503
+
1504
+
1505
+ static VALUE bdb_sync(VALUE vself){
1506
+ VALUE vbdb;
1507
+ TCBDB *bdb;
1508
+ vbdb = rb_iv_get(vself, BDBVNDATA);
1509
+ Data_Get_Struct(vbdb, TCBDB, bdb);
1510
+ return tcbdbsync(bdb) ? Qtrue : Qfalse;
1511
+ }
1512
+
1513
+
1514
+ static VALUE bdb_optimize(int argc, VALUE *argv, VALUE vself){
1515
+ VALUE vbdb, vlmemb, vnmemb, vbnum, vapow, vfpow, vopts;
1516
+ TCBDB *bdb;
1517
+ int lmemb, nmemb, apow, fpow, opts;
1518
+ int64_t bnum;
1519
+ rb_scan_args(argc, argv, "06", &vlmemb, &vnmemb, &vbnum, &vapow, &vfpow, &vopts);
1520
+ lmemb = (vlmemb == Qnil) ? -1 : NUM2INT(vlmemb);
1521
+ nmemb = (vnmemb == Qnil) ? -1 : NUM2INT(vnmemb);
1522
+ bnum = (vbnum == Qnil) ? -1 : NUM2LL(vbnum);
1523
+ apow = (vapow == Qnil) ? -1 : NUM2INT(vapow);
1524
+ fpow = (vfpow == Qnil) ? -1 : NUM2INT(vfpow);
1525
+ opts = (vopts == Qnil) ? UINT8_MAX : NUM2INT(vopts);
1526
+ vbdb = rb_iv_get(vself, BDBVNDATA);
1527
+ Data_Get_Struct(vbdb, TCBDB, bdb);
1528
+ return tcbdboptimize(bdb, lmemb, nmemb, bnum, apow, fpow, opts) ? Qtrue : Qfalse;
1529
+ }
1530
+
1531
+
1532
+ static VALUE bdb_vanish(VALUE vself){
1533
+ VALUE vbdb;
1534
+ TCBDB *bdb;
1535
+ vbdb = rb_iv_get(vself, BDBVNDATA);
1536
+ Data_Get_Struct(vbdb, TCBDB, bdb);
1537
+ return tcbdbvanish(bdb) ? Qtrue : Qfalse;
1538
+ }
1539
+
1540
+
1541
+ static VALUE bdb_copy(VALUE vself, VALUE vpath){
1542
+ VALUE vbdb;
1543
+ TCBDB *bdb;
1544
+ Check_Type(vpath, T_STRING);
1545
+ vbdb = rb_iv_get(vself, BDBVNDATA);
1546
+ Data_Get_Struct(vbdb, TCBDB, bdb);
1547
+ return tcbdbcopy(bdb, RSTRING_PTR(vpath)) ? Qtrue : Qfalse;
1548
+ }
1549
+
1550
+
1551
+ static VALUE bdb_tranbegin(VALUE vself){
1552
+ VALUE vbdb;
1553
+ TCBDB *bdb;
1554
+ vbdb = rb_iv_get(vself, BDBVNDATA);
1555
+ Data_Get_Struct(vbdb, TCBDB, bdb);
1556
+ return tcbdbtranbegin(bdb) ? Qtrue : Qfalse;
1557
+ }
1558
+
1559
+
1560
+ static VALUE bdb_trancommit(VALUE vself){
1561
+ VALUE vbdb;
1562
+ TCBDB *bdb;
1563
+ vbdb = rb_iv_get(vself, BDBVNDATA);
1564
+ Data_Get_Struct(vbdb, TCBDB, bdb);
1565
+ return tcbdbtrancommit(bdb) ? Qtrue : Qfalse;
1566
+ }
1567
+
1568
+
1569
+ static VALUE bdb_tranabort(VALUE vself){
1570
+ VALUE vbdb;
1571
+ TCBDB *bdb;
1572
+ vbdb = rb_iv_get(vself, BDBVNDATA);
1573
+ Data_Get_Struct(vbdb, TCBDB, bdb);
1574
+ return tcbdbtranabort(bdb) ? Qtrue : Qfalse;
1575
+ }
1576
+
1577
+
1578
+ static VALUE bdb_path(VALUE vself){
1579
+ VALUE vbdb;
1580
+ TCBDB *bdb;
1581
+ const char *path;
1582
+ vbdb = rb_iv_get(vself, BDBVNDATA);
1583
+ Data_Get_Struct(vbdb, TCBDB, bdb);
1584
+ if(!(path = tcbdbpath(bdb))) return Qnil;
1585
+ return rb_str_new2(path);
1586
+ }
1587
+
1588
+
1589
+ static VALUE bdb_rnum(VALUE vself){
1590
+ VALUE vbdb;
1591
+ TCBDB *bdb;
1592
+ vbdb = rb_iv_get(vself, BDBVNDATA);
1593
+ Data_Get_Struct(vbdb, TCBDB, bdb);
1594
+ return LL2NUM(tcbdbrnum(bdb));
1595
+ }
1596
+
1597
+
1598
+ static VALUE bdb_fsiz(VALUE vself){
1599
+ VALUE vbdb;
1600
+ TCBDB *bdb;
1601
+ vbdb = rb_iv_get(vself, BDBVNDATA);
1602
+ Data_Get_Struct(vbdb, TCBDB, bdb);
1603
+ return LL2NUM(tcbdbfsiz(bdb));
1604
+ }
1605
+
1606
+
1607
+ static VALUE bdb_fetch(int argc, VALUE *argv, VALUE vself){
1608
+ VALUE vbdb, vkey, vdef, vval;
1609
+ TCBDB *bdb;
1610
+ char *vbuf;
1611
+ int vsiz;
1612
+ rb_scan_args(argc, argv, "11", &vkey, &vdef);
1613
+ vkey = StringValueEx(vkey);
1614
+ vbdb = rb_iv_get(vself, BDBVNDATA);
1615
+ Data_Get_Struct(vbdb, TCBDB, bdb);
1616
+ if((vbuf = tcbdbget(bdb, RSTRING_PTR(vkey), RSTRING_LEN(vkey), &vsiz)) != NULL){
1617
+ vval = rb_str_new(vbuf, vsiz);
1618
+ tcfree(vbuf);
1619
+ } else {
1620
+ vval = vdef;
1621
+ }
1622
+ return vval;
1623
+ }
1624
+
1625
+
1626
+ static VALUE bdb_check(VALUE vself, VALUE vkey){
1627
+ VALUE vbdb;
1628
+ TCBDB *bdb;
1629
+ vkey = StringValueEx(vkey);
1630
+ vbdb = rb_iv_get(vself, BDBVNDATA);
1631
+ Data_Get_Struct(vbdb, TCBDB, bdb);
1632
+ return tcbdbvsiz(bdb, RSTRING_PTR(vkey), RSTRING_LEN(vkey)) >= 0 ? Qtrue : Qfalse;
1633
+ }
1634
+
1635
+
1636
+ static VALUE bdb_check_value(VALUE vself, VALUE vval){
1637
+ VALUE vbdb;
1638
+ TCBDB *bdb;
1639
+ BDBCUR *cur;
1640
+ const char *tvbuf;
1641
+ bool hit;
1642
+ int tvsiz;
1643
+ vval = StringValueEx(vval);
1644
+ vbdb = rb_iv_get(vself, BDBVNDATA);
1645
+ Data_Get_Struct(vbdb, TCBDB, bdb);
1646
+ hit = false;
1647
+ cur = tcbdbcurnew(bdb);
1648
+ tcbdbcurfirst(cur);
1649
+ while((tvbuf = tcbdbcurval3(cur, &tvsiz)) != NULL){
1650
+ if(tvsiz == RSTRING_LEN(vval) && memcmp(tvbuf, RSTRING_PTR(vval), RSTRING_LEN(vval)) == 0){
1651
+ hit = true;
1652
+ break;
1653
+ }
1654
+ tcbdbcurnext(cur);
1655
+ }
1656
+ tcbdbcurdel(cur);
1657
+ return hit ? Qtrue : Qfalse;
1658
+ }
1659
+
1660
+
1661
+ static VALUE bdb_get_reverse(VALUE vself, VALUE vval){
1662
+ VALUE vbdb, vrv;
1663
+ TCBDB *bdb;
1664
+ BDBCUR *cur;
1665
+ const char *tvbuf, *tkbuf;
1666
+ int tvsiz, tksiz;
1667
+ vval = StringValueEx(vval);
1668
+ vbdb = rb_iv_get(vself, BDBVNDATA);
1669
+ Data_Get_Struct(vbdb, TCBDB, bdb);
1670
+ vrv = Qnil;
1671
+ cur = tcbdbcurnew(bdb);
1672
+ tcbdbcurfirst(cur);
1673
+ while((tvbuf = tcbdbcurval3(cur, &tvsiz)) != NULL){
1674
+ if(tvsiz == RSTRING_LEN(vval) && memcmp(tvbuf, RSTRING_PTR(vval), RSTRING_LEN(vval)) == 0){
1675
+ if((tkbuf = tcbdbcurkey3(cur, &tksiz)) != NULL)
1676
+ vrv = rb_str_new(tkbuf, tksiz);
1677
+ break;
1678
+ }
1679
+ tcbdbcurnext(cur);
1680
+ }
1681
+ tcbdbcurdel(cur);
1682
+ return vrv;
1683
+ }
1684
+
1685
+
1686
+ static VALUE bdb_empty(VALUE vself){
1687
+ VALUE vbdb;
1688
+ TCBDB *bdb;
1689
+ vbdb = rb_iv_get(vself, BDBVNDATA);
1690
+ Data_Get_Struct(vbdb, TCBDB, bdb);
1691
+ return tcbdbrnum(bdb) < 1 ? Qtrue : Qfalse;
1692
+ }
1693
+
1694
+
1695
+ static VALUE bdb_each(VALUE vself){
1696
+ VALUE vbdb, vrv;
1697
+ TCBDB *bdb;
1698
+ BDBCUR *cur;
1699
+ TCXSTR *kxstr, *vxstr;
1700
+ if(rb_block_given_p() != Qtrue) rb_raise(rb_eArgError, "no block given");
1701
+ vbdb = rb_iv_get(vself, BDBVNDATA);
1702
+ Data_Get_Struct(vbdb, TCBDB, bdb);
1703
+ vrv = Qnil;
1704
+ kxstr = tcxstrnew();
1705
+ vxstr = tcxstrnew();
1706
+ cur = tcbdbcurnew(bdb);
1707
+ tcbdbcurfirst(cur);
1708
+ while(tcbdbcurrec(cur, kxstr, vxstr)){
1709
+ vrv = rb_yield_values(2, rb_str_new(tcxstrptr(kxstr), tcxstrsize(kxstr)),
1710
+ rb_str_new(tcxstrptr(vxstr), tcxstrsize(vxstr)));
1711
+ tcbdbcurnext(cur);
1712
+ }
1713
+ tcbdbcurdel(cur);
1714
+ tcxstrdel(vxstr);
1715
+ tcxstrdel(kxstr);
1716
+ return vrv;
1717
+ }
1718
+
1719
+
1720
+ static VALUE bdb_each_key(VALUE vself){
1721
+ VALUE vbdb, vrv;
1722
+ TCBDB *bdb;
1723
+ BDBCUR *cur;
1724
+ const char *kbuf;
1725
+ int ksiz;
1726
+ if(rb_block_given_p() != Qtrue) rb_raise(rb_eArgError, "no block given");
1727
+ vbdb = rb_iv_get(vself, BDBVNDATA);
1728
+ Data_Get_Struct(vbdb, TCBDB, bdb);
1729
+ vrv = Qnil;
1730
+ cur = tcbdbcurnew(bdb);
1731
+ tcbdbcurfirst(cur);
1732
+ while((kbuf = tcbdbcurkey3(cur, &ksiz)) != NULL){
1733
+ vrv = rb_yield(rb_str_new(kbuf, ksiz));
1734
+ tcbdbcurnext(cur);
1735
+ }
1736
+ tcbdbcurdel(cur);
1737
+ return vrv;
1738
+ }
1739
+
1740
+
1741
+ static VALUE bdb_each_value(VALUE vself){
1742
+ VALUE vbdb, vrv;
1743
+ TCBDB *bdb;
1744
+ BDBCUR *cur;
1745
+ const char *vbuf;
1746
+ int vsiz;
1747
+ if(rb_block_given_p() != Qtrue) rb_raise(rb_eArgError, "no block given");
1748
+ vbdb = rb_iv_get(vself, BDBVNDATA);
1749
+ Data_Get_Struct(vbdb, TCBDB, bdb);
1750
+ vrv = Qnil;
1751
+ cur = tcbdbcurnew(bdb);
1752
+ tcbdbcurfirst(cur);
1753
+ while((vbuf = tcbdbcurval3(cur, &vsiz)) != NULL){
1754
+ vrv = rb_yield(rb_str_new(vbuf, vsiz));
1755
+ tcbdbcurnext(cur);
1756
+ }
1757
+ tcbdbcurdel(cur);
1758
+ return vrv;
1759
+ }
1760
+
1761
+
1762
+ static VALUE bdb_keys(VALUE vself){
1763
+ VALUE vbdb, vary;
1764
+ TCBDB *bdb;
1765
+ BDBCUR *cur;
1766
+ const char *kbuf;
1767
+ int ksiz;
1768
+ vbdb = rb_iv_get(vself, BDBVNDATA);
1769
+ Data_Get_Struct(vbdb, TCBDB, bdb);
1770
+ vary = rb_ary_new2(tcbdbrnum(bdb));
1771
+ cur = tcbdbcurnew(bdb);
1772
+ tcbdbcurfirst(cur);
1773
+ while((kbuf = tcbdbcurkey3(cur, &ksiz)) != NULL){
1774
+ rb_ary_push(vary, rb_str_new(kbuf, ksiz));
1775
+ tcbdbcurnext(cur);
1776
+ }
1777
+ tcbdbcurdel(cur);
1778
+ return vary;
1779
+ }
1780
+
1781
+
1782
+ static VALUE bdb_values(VALUE vself){
1783
+ VALUE vbdb, vary;
1784
+ TCBDB *bdb;
1785
+ BDBCUR *cur;
1786
+ const char *vbuf;
1787
+ int vsiz;
1788
+ vbdb = rb_iv_get(vself, BDBVNDATA);
1789
+ Data_Get_Struct(vbdb, TCBDB, bdb);
1790
+ vary = rb_ary_new2(tcbdbrnum(bdb));
1791
+ cur = tcbdbcurnew(bdb);
1792
+ tcbdbcurfirst(cur);
1793
+ while((vbuf = tcbdbcurval3(cur, &vsiz)) != NULL){
1794
+ rb_ary_push(vary, rb_str_new(vbuf, vsiz));
1795
+ tcbdbcurnext(cur);
1796
+ }
1797
+ tcbdbcurdel(cur);
1798
+ return vary;
1799
+ }
1800
+
1801
+
1802
+ static void bdbcur_init(void){
1803
+ cls_bdbcur = rb_define_class_under(mod_tokyocabinet, "BDBCUR", rb_cObject);
1804
+ cls_bdbcur_data = rb_define_class_under(mod_tokyocabinet, "BDBCUR_data", rb_cObject);
1805
+ rb_define_const(cls_bdbcur, "CPCURRENT", INT2NUM(BDBCPCURRENT));
1806
+ rb_define_const(cls_bdbcur, "CPBEFORE", INT2NUM(BDBCPBEFORE));
1807
+ rb_define_const(cls_bdbcur, "CPAFTER", INT2NUM(BDBCPAFTER));
1808
+ rb_define_private_method(cls_bdbcur, "initialize", bdbcur_initialize, 1);
1809
+ rb_define_method(cls_bdbcur, "first", bdbcur_first, 0);
1810
+ rb_define_method(cls_bdbcur, "last", bdbcur_last, 0);
1811
+ rb_define_method(cls_bdbcur, "jump", bdbcur_jump, 1);
1812
+ rb_define_method(cls_bdbcur, "prev", bdbcur_prev, 0);
1813
+ rb_define_method(cls_bdbcur, "next", bdbcur_next, 0);
1814
+ rb_define_method(cls_bdbcur, "put", bdbcur_put, -1);
1815
+ rb_define_method(cls_bdbcur, "out", bdbcur_out, 0);
1816
+ rb_define_method(cls_bdbcur, "key", bdbcur_key, 0);
1817
+ rb_define_method(cls_bdbcur, "val", bdbcur_val, 0);
1818
+ }
1819
+
1820
+
1821
+ static VALUE bdbcur_initialize(VALUE vself, VALUE vbdb){
1822
+ VALUE vcur;
1823
+ TCBDB *bdb;
1824
+ BDBCUR *cur;
1825
+ Check_Type(vbdb, T_OBJECT);
1826
+ vbdb = rb_iv_get(vbdb, BDBVNDATA);
1827
+ Data_Get_Struct(vbdb, TCBDB, bdb);
1828
+ cur = tcbdbcurnew(bdb);
1829
+ vcur = Data_Wrap_Struct(cls_bdbcur_data, 0, tcbdbcurdel, cur);
1830
+ rb_iv_set(vself, BDBCURVNDATA, vcur);
1831
+ rb_iv_set(vself, BDBVNDATA, vbdb);
1832
+ return Qnil;
1833
+ }
1834
+
1835
+
1836
+ static VALUE bdbcur_first(VALUE vself){
1837
+ VALUE vcur;
1838
+ BDBCUR *cur;
1839
+ vcur = rb_iv_get(vself, BDBCURVNDATA);
1840
+ Data_Get_Struct(vcur, BDBCUR, cur);
1841
+ return tcbdbcurfirst(cur) ? Qtrue : Qfalse;
1842
+ }
1843
+
1844
+
1845
+ static VALUE bdbcur_last(VALUE vself){
1846
+ VALUE vcur;
1847
+ BDBCUR *cur;
1848
+ vcur = rb_iv_get(vself, BDBCURVNDATA);
1849
+ Data_Get_Struct(vcur, BDBCUR, cur);
1850
+ return tcbdbcurlast(cur) ? Qtrue : Qfalse;
1851
+ }
1852
+
1853
+
1854
+ static VALUE bdbcur_jump(VALUE vself, VALUE vkey){
1855
+ VALUE vcur;
1856
+ BDBCUR *cur;
1857
+ vkey = StringValueEx(vkey);
1858
+ vcur = rb_iv_get(vself, BDBCURVNDATA);
1859
+ Data_Get_Struct(vcur, BDBCUR, cur);
1860
+ return tcbdbcurjump(cur, RSTRING_PTR(vkey), RSTRING_LEN(vkey)) ? Qtrue : Qfalse;
1861
+ }
1862
+
1863
+
1864
+ static VALUE bdbcur_prev(VALUE vself){
1865
+ VALUE vcur;
1866
+ BDBCUR *cur;
1867
+ vcur = rb_iv_get(vself, BDBCURVNDATA);
1868
+ Data_Get_Struct(vcur, BDBCUR, cur);
1869
+ return tcbdbcurprev(cur) ? Qtrue : Qfalse;
1870
+ }
1871
+
1872
+
1873
+ static VALUE bdbcur_next(VALUE vself){
1874
+ VALUE vcur;
1875
+ BDBCUR *cur;
1876
+ vcur = rb_iv_get(vself, BDBCURVNDATA);
1877
+ Data_Get_Struct(vcur, BDBCUR, cur);
1878
+ return tcbdbcurnext(cur) ? Qtrue : Qfalse;
1879
+ }
1880
+
1881
+
1882
+ static VALUE bdbcur_put(int argc, VALUE *argv, VALUE vself){
1883
+ VALUE vcur, vval, vcpmode;
1884
+ BDBCUR *cur;
1885
+ int cpmode;
1886
+ rb_scan_args(argc, argv, "11", &vval, &vcpmode);
1887
+ vval = StringValueEx(vval);
1888
+ cpmode = (vcpmode == Qnil) ? BDBCPCURRENT : NUM2INT(vcpmode);
1889
+ vcur = rb_iv_get(vself, BDBCURVNDATA);
1890
+ Data_Get_Struct(vcur, BDBCUR, cur);
1891
+ return tcbdbcurput(cur, RSTRING_PTR(vval), RSTRING_LEN(vval), cpmode) ? Qtrue : Qfalse;
1892
+ }
1893
+
1894
+
1895
+ static VALUE bdbcur_out(VALUE vself){
1896
+ VALUE vcur;
1897
+ BDBCUR *cur;
1898
+ vcur = rb_iv_get(vself, BDBCURVNDATA);
1899
+ Data_Get_Struct(vcur, BDBCUR, cur);
1900
+ return tcbdbcurout(cur) ? Qtrue : Qfalse;
1901
+ }
1902
+
1903
+
1904
+ static VALUE bdbcur_key(VALUE vself){
1905
+ VALUE vcur, vkey;
1906
+ BDBCUR *cur;
1907
+ char *kbuf;
1908
+ int ksiz;
1909
+ vcur = rb_iv_get(vself, BDBCURVNDATA);
1910
+ Data_Get_Struct(vcur, BDBCUR, cur);
1911
+ if(!(kbuf = tcbdbcurkey(cur, &ksiz))) return Qnil;
1912
+ vkey = rb_str_new(kbuf, ksiz);
1913
+ tcfree(kbuf);
1914
+ return vkey;
1915
+ }
1916
+
1917
+
1918
+ static VALUE bdbcur_val(VALUE vself){
1919
+ VALUE vcur, vval;
1920
+ BDBCUR *cur;
1921
+ char *vbuf;
1922
+ int vsiz;
1923
+ vcur = rb_iv_get(vself, BDBCURVNDATA);
1924
+ Data_Get_Struct(vcur, BDBCUR, cur);
1925
+ if(!(vbuf = tcbdbcurval(cur, &vsiz))) return Qnil;
1926
+ vval = rb_str_new(vbuf, vsiz);
1927
+ tcfree(vbuf);
1928
+ return vval;
1929
+ }
1930
+
1931
+
1932
+ static void fdb_init(void){
1933
+ cls_fdb = rb_define_class_under(mod_tokyocabinet, "FDB", rb_cObject);
1934
+ cls_fdb_data = rb_define_class_under(mod_tokyocabinet, "FDB_data", rb_cObject);
1935
+ rb_define_const(cls_fdb, "ESUCCESS", INT2NUM(TCESUCCESS));
1936
+ rb_define_const(cls_fdb, "ETHREAD", INT2NUM(TCETHREAD));
1937
+ rb_define_const(cls_fdb, "EINVALID", INT2NUM(TCEINVALID));
1938
+ rb_define_const(cls_fdb, "ENOFILE", INT2NUM(TCENOFILE));
1939
+ rb_define_const(cls_fdb, "ENOPERM", INT2NUM(TCENOPERM));
1940
+ rb_define_const(cls_fdb, "EMETA", INT2NUM(TCEMETA));
1941
+ rb_define_const(cls_fdb, "ERHEAD", INT2NUM(TCERHEAD));
1942
+ rb_define_const(cls_fdb, "EOPEN", INT2NUM(TCEOPEN));
1943
+ rb_define_const(cls_fdb, "ECLOSE", INT2NUM(TCECLOSE));
1944
+ rb_define_const(cls_fdb, "ETRUNC", INT2NUM(TCETRUNC));
1945
+ rb_define_const(cls_fdb, "ESYNC", INT2NUM(TCESYNC));
1946
+ rb_define_const(cls_fdb, "ESTAT", INT2NUM(TCESTAT));
1947
+ rb_define_const(cls_fdb, "ESEEK", INT2NUM(TCESEEK));
1948
+ rb_define_const(cls_fdb, "EREAD", INT2NUM(TCEREAD));
1949
+ rb_define_const(cls_fdb, "EWRITE", INT2NUM(TCEWRITE));
1950
+ rb_define_const(cls_fdb, "EMMAP", INT2NUM(TCEMMAP));
1951
+ rb_define_const(cls_fdb, "ELOCK", INT2NUM(TCELOCK));
1952
+ rb_define_const(cls_fdb, "EUNLINK", INT2NUM(TCEUNLINK));
1953
+ rb_define_const(cls_fdb, "ERENAME", INT2NUM(TCERENAME));
1954
+ rb_define_const(cls_fdb, "EMKDIR", INT2NUM(TCEMKDIR));
1955
+ rb_define_const(cls_fdb, "ERMDIR", INT2NUM(TCERMDIR));
1956
+ rb_define_const(cls_fdb, "EKEEP", INT2NUM(TCEKEEP));
1957
+ rb_define_const(cls_fdb, "ENOREC", INT2NUM(TCENOREC));
1958
+ rb_define_const(cls_fdb, "EMISC", INT2NUM(TCEMISC));
1959
+ rb_define_const(cls_fdb, "OREADER", INT2NUM(FDBOREADER));
1960
+ rb_define_const(cls_fdb, "OWRITER", INT2NUM(FDBOWRITER));
1961
+ rb_define_const(cls_fdb, "OCREAT", INT2NUM(FDBOCREAT));
1962
+ rb_define_const(cls_fdb, "OTRUNC", INT2NUM(FDBOTRUNC));
1963
+ rb_define_const(cls_fdb, "ONOLCK", INT2NUM(FDBONOLCK));
1964
+ rb_define_const(cls_fdb, "OLCKNB", INT2NUM(FDBOLCKNB));
1965
+ rb_define_private_method(cls_fdb, "initialize", fdb_initialize, 0);
1966
+ rb_define_method(cls_fdb, "errmsg", fdb_errmsg, -1);
1967
+ rb_define_method(cls_fdb, "ecode", fdb_ecode, 0);
1968
+ rb_define_method(cls_fdb, "tune", fdb_tune, -1);
1969
+ rb_define_method(cls_fdb, "open", fdb_open, -1);
1970
+ rb_define_method(cls_fdb, "close", fdb_close, 0);
1971
+ rb_define_method(cls_fdb, "put", fdb_put, 2);
1972
+ rb_define_method(cls_fdb, "putkeep", fdb_putkeep, 2);
1973
+ rb_define_method(cls_fdb, "putcat", fdb_putcat, 2);
1974
+ rb_define_method(cls_fdb, "out", fdb_out, 1);
1975
+ rb_define_method(cls_fdb, "get", fdb_get, 1);
1976
+ rb_define_method(cls_fdb, "vsiz", fdb_vsiz, 1);
1977
+ rb_define_method(cls_fdb, "iterinit", fdb_iterinit, 0);
1978
+ rb_define_method(cls_fdb, "iternext", fdb_iternext, 0);
1979
+ rb_define_method(cls_fdb, "range", fdb_range, -1);
1980
+ rb_define_method(cls_fdb, "addint", fdb_addint, 2);
1981
+ rb_define_method(cls_fdb, "adddouble", fdb_adddouble, 2);
1982
+ rb_define_method(cls_fdb, "sync", fdb_sync, 0);
1983
+ rb_define_method(cls_fdb, "optimize", fdb_optimize, -1);
1984
+ rb_define_method(cls_fdb, "vanish", fdb_vanish, 0);
1985
+ rb_define_method(cls_fdb, "copy", fdb_copy, 1);
1986
+ rb_define_method(cls_fdb, "tranbegin", fdb_tranbegin, 0);
1987
+ rb_define_method(cls_fdb, "trancommit", fdb_trancommit, 0);
1988
+ rb_define_method(cls_fdb, "tranabort", fdb_tranabort, 0);
1989
+ rb_define_method(cls_fdb, "path", fdb_path, 0);
1990
+ rb_define_method(cls_fdb, "rnum", fdb_rnum, 0);
1991
+ rb_define_method(cls_fdb, "fsiz", fdb_fsiz, 0);
1992
+ rb_define_method(cls_fdb, "[]", fdb_get, 1);
1993
+ rb_define_method(cls_fdb, "[]=", fdb_put, 2);
1994
+ rb_define_method(cls_fdb, "store", fdb_put, 2);
1995
+ rb_define_method(cls_fdb, "delete", fdb_out, 1);
1996
+ rb_define_method(cls_fdb, "fetch", fdb_fetch, -1);
1997
+ rb_define_method(cls_fdb, "has_key?", fdb_check, 1);
1998
+ rb_define_method(cls_fdb, "key?", fdb_check, 1);
1999
+ rb_define_method(cls_fdb, "include?", fdb_check, 1);
2000
+ rb_define_method(cls_fdb, "member?", fdb_check, 1);
2001
+ rb_define_method(cls_fdb, "has_value?", fdb_check_value, 1);
2002
+ rb_define_method(cls_fdb, "value?", fdb_check_value, 1);
2003
+ rb_define_method(cls_fdb, "key", fdb_get_reverse, 1);
2004
+ rb_define_method(cls_fdb, "clear", fdb_vanish, 0);
2005
+ rb_define_method(cls_fdb, "size", fdb_rnum, 0);
2006
+ rb_define_method(cls_fdb, "length", fdb_rnum, 0);
2007
+ rb_define_method(cls_fdb, "empty?", fdb_empty, 0);
2008
+ rb_define_method(cls_fdb, "each", fdb_each, 0);
2009
+ rb_define_method(cls_fdb, "each_pair", fdb_each, 0);
2010
+ rb_define_method(cls_fdb, "each_key", fdb_each_key, 0);
2011
+ rb_define_method(cls_fdb, "each_value", fdb_each_value, 0);
2012
+ rb_define_method(cls_fdb, "keys", fdb_keys, 0);
2013
+ rb_define_method(cls_fdb, "values", fdb_values, 0);
2014
+ }
2015
+
2016
+
2017
+ static VALUE fdb_initialize(VALUE vself){
2018
+ VALUE vfdb;
2019
+ TCFDB *fdb;
2020
+ fdb = tcfdbnew();
2021
+ tcfdbsetmutex(fdb);
2022
+ vfdb = Data_Wrap_Struct(cls_fdb_data, 0, tcfdbdel, fdb);
2023
+ rb_iv_set(vself, FDBVNDATA, vfdb);
2024
+ return Qnil;
2025
+ }
2026
+
2027
+
2028
+ static VALUE fdb_errmsg(int argc, VALUE *argv, VALUE vself){
2029
+ VALUE vfdb, vecode;
2030
+ TCFDB *fdb;
2031
+ const char *msg;
2032
+ int ecode;
2033
+ rb_scan_args(argc, argv, "01", &vecode);
2034
+ vfdb = rb_iv_get(vself, FDBVNDATA);
2035
+ Data_Get_Struct(vfdb, TCFDB, fdb);
2036
+ ecode = (vecode == Qnil) ? tcfdbecode(fdb) : NUM2INT(vecode);
2037
+ msg = tcfdberrmsg(ecode);
2038
+ return rb_str_new2(msg);
2039
+ }
2040
+
2041
+
2042
+ static VALUE fdb_ecode(VALUE vself){
2043
+ VALUE vfdb;
2044
+ TCFDB *fdb;
2045
+ vfdb = rb_iv_get(vself, FDBVNDATA);
2046
+ Data_Get_Struct(vfdb, TCFDB, fdb);
2047
+ return INT2NUM(tcfdbecode(fdb));
2048
+ }
2049
+
2050
+
2051
+ static VALUE fdb_tune(int argc, VALUE *argv, VALUE vself){
2052
+ VALUE vfdb, vwidth, vlimsiz;
2053
+ TCFDB *fdb;
2054
+ int width;
2055
+ int64_t limsiz;
2056
+ rb_scan_args(argc, argv, "02", &vwidth, &vlimsiz);
2057
+ width = (vwidth == Qnil) ? -1 : NUM2INT(vwidth);
2058
+ limsiz = (vlimsiz == Qnil) ? -1 : NUM2LL(vlimsiz);
2059
+ vfdb = rb_iv_get(vself, FDBVNDATA);
2060
+ Data_Get_Struct(vfdb, TCFDB, fdb);
2061
+ return tcfdbtune(fdb, width, limsiz) ? Qtrue : Qfalse;
2062
+ }
2063
+
2064
+
2065
+ static VALUE fdb_open(int argc, VALUE *argv, VALUE vself){
2066
+ VALUE vfdb, vpath, vomode;
2067
+ TCFDB *fdb;
2068
+ int omode;
2069
+ rb_scan_args(argc, argv, "11", &vpath, &vomode);
2070
+ Check_Type(vpath, T_STRING);
2071
+ omode = (vomode == Qnil) ? FDBOREADER : NUM2INT(vomode);
2072
+ vfdb = rb_iv_get(vself, FDBVNDATA);
2073
+ Data_Get_Struct(vfdb, TCFDB, fdb);
2074
+ return tcfdbopen(fdb, RSTRING_PTR(vpath), omode) ? Qtrue : Qfalse;
2075
+ }
2076
+
2077
+
2078
+ static VALUE fdb_close(VALUE vself){
2079
+ VALUE vfdb;
2080
+ TCFDB *fdb;
2081
+ vfdb = rb_iv_get(vself, FDBVNDATA);
2082
+ Data_Get_Struct(vfdb, TCFDB, fdb);
2083
+ return tcfdbclose(fdb) ? Qtrue : Qfalse;
2084
+ }
2085
+
2086
+
2087
+ static VALUE fdb_put(VALUE vself, VALUE vkey, VALUE vval){
2088
+ VALUE vfdb;
2089
+ TCFDB *fdb;
2090
+ vkey = StringValueEx(vkey);
2091
+ vval = StringValueEx(vval);
2092
+ vfdb = rb_iv_get(vself, FDBVNDATA);
2093
+ Data_Get_Struct(vfdb, TCFDB, fdb);
2094
+ return tcfdbput2(fdb, RSTRING_PTR(vkey), RSTRING_LEN(vkey),
2095
+ RSTRING_PTR(vval), RSTRING_LEN(vval)) ? Qtrue : Qfalse;
2096
+ }
2097
+
2098
+
2099
+ static VALUE fdb_putkeep(VALUE vself, VALUE vkey, VALUE vval){
2100
+ VALUE vfdb;
2101
+ TCFDB *fdb;
2102
+ vkey = StringValueEx(vkey);
2103
+ vval = StringValueEx(vval);
2104
+ vfdb = rb_iv_get(vself, FDBVNDATA);
2105
+ Data_Get_Struct(vfdb, TCFDB, fdb);
2106
+ return tcfdbputkeep2(fdb, RSTRING_PTR(vkey), RSTRING_LEN(vkey),
2107
+ RSTRING_PTR(vval), RSTRING_LEN(vval)) ? Qtrue : Qfalse;
2108
+ }
2109
+
2110
+
2111
+ static VALUE fdb_putcat(VALUE vself, VALUE vkey, VALUE vval){
2112
+ VALUE vfdb;
2113
+ TCFDB *fdb;
2114
+ vkey = StringValueEx(vkey);
2115
+ vval = StringValueEx(vval);
2116
+ vfdb = rb_iv_get(vself, FDBVNDATA);
2117
+ Data_Get_Struct(vfdb, TCFDB, fdb);
2118
+ return tcfdbputcat2(fdb, RSTRING_PTR(vkey), RSTRING_LEN(vkey),
2119
+ RSTRING_PTR(vval), RSTRING_LEN(vval)) ? Qtrue : Qfalse;
2120
+ }
2121
+
2122
+
2123
+ static VALUE fdb_out(VALUE vself, VALUE vkey){
2124
+ VALUE vfdb;
2125
+ TCFDB *fdb;
2126
+ vkey = StringValueEx(vkey);
2127
+ vfdb = rb_iv_get(vself, FDBVNDATA);
2128
+ Data_Get_Struct(vfdb, TCFDB, fdb);
2129
+ return tcfdbout2(fdb, RSTRING_PTR(vkey), RSTRING_LEN(vkey)) ? Qtrue : Qfalse;
2130
+ }
2131
+
2132
+
2133
+ static VALUE fdb_get(VALUE vself, VALUE vkey){
2134
+ VALUE vfdb, vval;
2135
+ TCFDB *fdb;
2136
+ char *vbuf;
2137
+ int vsiz;
2138
+ vkey = StringValueEx(vkey);
2139
+ vfdb = rb_iv_get(vself, FDBVNDATA);
2140
+ Data_Get_Struct(vfdb, TCFDB, fdb);
2141
+ if(!(vbuf = tcfdbget2(fdb, RSTRING_PTR(vkey), RSTRING_LEN(vkey), &vsiz))) return Qnil;
2142
+ vval = rb_str_new(vbuf, vsiz);
2143
+ tcfree(vbuf);
2144
+ return vval;
2145
+ }
2146
+
2147
+
2148
+ static VALUE fdb_vsiz(VALUE vself, VALUE vkey){
2149
+ VALUE vfdb;
2150
+ TCFDB *fdb;
2151
+ vkey = StringValueEx(vkey);
2152
+ vfdb = rb_iv_get(vself, FDBVNDATA);
2153
+ Data_Get_Struct(vfdb, TCFDB, fdb);
2154
+ return INT2NUM(tcfdbvsiz2(fdb, RSTRING_PTR(vkey), RSTRING_LEN(vkey)));
2155
+ }
2156
+
2157
+
2158
+ static VALUE fdb_iterinit(VALUE vself){
2159
+ VALUE vfdb;
2160
+ TCFDB *fdb;
2161
+ vfdb = rb_iv_get(vself, FDBVNDATA);
2162
+ Data_Get_Struct(vfdb, TCFDB, fdb);
2163
+ return tcfdbiterinit(fdb) ? Qtrue : Qfalse;
2164
+ }
2165
+
2166
+
2167
+ static VALUE fdb_iternext(VALUE vself){
2168
+ VALUE vfdb, vval;
2169
+ TCFDB *fdb;
2170
+ char *vbuf;
2171
+ int vsiz;
2172
+ vfdb = rb_iv_get(vself, FDBVNDATA);
2173
+ Data_Get_Struct(vfdb, TCFDB, fdb);
2174
+ if(!(vbuf = tcfdbiternext2(fdb, &vsiz))) return Qnil;
2175
+ vval = rb_str_new(vbuf, vsiz);
2176
+ tcfree(vbuf);
2177
+ return vval;
2178
+ }
2179
+
2180
+
2181
+ static VALUE fdb_range(int argc, VALUE *argv, VALUE vself){
2182
+ VALUE vfdb, vinterval, vmax, vary;
2183
+ TCFDB *fdb;
2184
+ TCLIST *keys;
2185
+ int max;
2186
+ rb_scan_args(argc, argv, "11", &vinterval, &vmax);
2187
+ vinterval = StringValueEx(vinterval);
2188
+ max = (vmax == Qnil) ? -1 : NUM2INT(vmax);
2189
+ vfdb = rb_iv_get(vself, FDBVNDATA);
2190
+ Data_Get_Struct(vfdb, TCFDB, fdb);
2191
+ keys = tcfdbrange4(fdb, RSTRING_PTR(vinterval), RSTRING_LEN(vinterval), max);
2192
+ vary = listtovary(keys);
2193
+ tclistdel(keys);
2194
+ return vary;
2195
+ }
2196
+
2197
+
2198
+ static VALUE fdb_addint(VALUE vself, VALUE vkey, VALUE vnum){
2199
+ VALUE vfdb;
2200
+ TCFDB *fdb;
2201
+ int num;
2202
+ vkey = StringValueEx(vkey);
2203
+ vfdb = rb_iv_get(vself, FDBVNDATA);
2204
+ Data_Get_Struct(vfdb, TCFDB, fdb);
2205
+ num = tcfdbaddint(fdb, tcfdbkeytoid(RSTRING_PTR(vkey), RSTRING_LEN(vkey)), NUM2INT(vnum));
2206
+ return num == INT_MIN ? Qnil : INT2NUM(num);
2207
+ }
2208
+
2209
+
2210
+ static VALUE fdb_adddouble(VALUE vself, VALUE vkey, VALUE vnum){
2211
+ VALUE vfdb;
2212
+ TCFDB *fdb;
2213
+ double num;
2214
+ vkey = StringValueEx(vkey);
2215
+ vfdb = rb_iv_get(vself, FDBVNDATA);
2216
+ Data_Get_Struct(vfdb, TCFDB, fdb);
2217
+ num = tcfdbadddouble(fdb, tcfdbkeytoid(RSTRING_PTR(vkey), RSTRING_LEN(vkey)), NUM2DBL(vnum));
2218
+ return isnan(num) ? Qnil : rb_float_new(num);
2219
+ }
2220
+
2221
+
2222
+ static VALUE fdb_sync(VALUE vself){
2223
+ VALUE vfdb;
2224
+ TCFDB *fdb;
2225
+ vfdb = rb_iv_get(vself, FDBVNDATA);
2226
+ Data_Get_Struct(vfdb, TCFDB, fdb);
2227
+ return tcfdbsync(fdb) ? Qtrue : Qfalse;
2228
+ }
2229
+
2230
+
2231
+ static VALUE fdb_optimize(int argc, VALUE *argv, VALUE vself){
2232
+ VALUE vfdb, vwidth, vlimsiz;
2233
+ TCFDB *fdb;
2234
+ int width;
2235
+ int64_t limsiz;
2236
+ rb_scan_args(argc, argv, "02", &vwidth, &vlimsiz);
2237
+ width = (vwidth == Qnil) ? -1 : NUM2INT(vwidth);
2238
+ limsiz = (vlimsiz == Qnil) ? -1 : NUM2LL(vlimsiz);
2239
+ vfdb = rb_iv_get(vself, FDBVNDATA);
2240
+ Data_Get_Struct(vfdb, TCFDB, fdb);
2241
+ return tcfdboptimize(fdb, width, limsiz) ? Qtrue : Qfalse;
2242
+ }
2243
+
2244
+
2245
+ static VALUE fdb_vanish(VALUE vself){
2246
+ VALUE vfdb;
2247
+ TCFDB *fdb;
2248
+ vfdb = rb_iv_get(vself, FDBVNDATA);
2249
+ Data_Get_Struct(vfdb, TCFDB, fdb);
2250
+ return tcfdbvanish(fdb) ? Qtrue : Qfalse;
2251
+ }
2252
+
2253
+
2254
+ static VALUE fdb_copy(VALUE vself, VALUE vpath){
2255
+ VALUE vfdb;
2256
+ TCFDB *fdb;
2257
+ Check_Type(vpath, T_STRING);
2258
+ vfdb = rb_iv_get(vself, FDBVNDATA);
2259
+ Data_Get_Struct(vfdb, TCFDB, fdb);
2260
+ return tcfdbcopy(fdb, RSTRING_PTR(vpath)) ? Qtrue : Qfalse;
2261
+ }
2262
+
2263
+
2264
+ static VALUE fdb_tranbegin(VALUE vself){
2265
+ VALUE vfdb;
2266
+ TCFDB *fdb;
2267
+ vfdb = rb_iv_get(vself, FDBVNDATA);
2268
+ Data_Get_Struct(vfdb, TCFDB, fdb);
2269
+ return tcfdbtranbegin(fdb) ? Qtrue : Qfalse;
2270
+ }
2271
+
2272
+
2273
+ static VALUE fdb_trancommit(VALUE vself){
2274
+ VALUE vfdb;
2275
+ TCFDB *fdb;
2276
+ vfdb = rb_iv_get(vself, FDBVNDATA);
2277
+ Data_Get_Struct(vfdb, TCFDB, fdb);
2278
+ return tcfdbtrancommit(fdb) ? Qtrue : Qfalse;
2279
+ }
2280
+
2281
+
2282
+ static VALUE fdb_tranabort(VALUE vself){
2283
+ VALUE vfdb;
2284
+ TCFDB *fdb;
2285
+ vfdb = rb_iv_get(vself, FDBVNDATA);
2286
+ Data_Get_Struct(vfdb, TCFDB, fdb);
2287
+ return tcfdbtranabort(fdb) ? Qtrue : Qfalse;
2288
+ }
2289
+
2290
+
2291
+ static VALUE fdb_path(VALUE vself){
2292
+ VALUE vfdb;
2293
+ TCFDB *fdb;
2294
+ const char *path;
2295
+ vfdb = rb_iv_get(vself, FDBVNDATA);
2296
+ Data_Get_Struct(vfdb, TCFDB, fdb);
2297
+ if(!(path = tcfdbpath(fdb))) return Qnil;
2298
+ return rb_str_new2(path);
2299
+ }
2300
+
2301
+
2302
+ static VALUE fdb_rnum(VALUE vself){
2303
+ VALUE vfdb;
2304
+ TCFDB *fdb;
2305
+ vfdb = rb_iv_get(vself, FDBVNDATA);
2306
+ Data_Get_Struct(vfdb, TCFDB, fdb);
2307
+ return LL2NUM(tcfdbrnum(fdb));
2308
+ }
2309
+
2310
+
2311
+ static VALUE fdb_fsiz(VALUE vself){
2312
+ VALUE vfdb;
2313
+ TCFDB *fdb;
2314
+ vfdb = rb_iv_get(vself, FDBVNDATA);
2315
+ Data_Get_Struct(vfdb, TCFDB, fdb);
2316
+ return LL2NUM(tcfdbfsiz(fdb));
2317
+ }
2318
+
2319
+
2320
+ static VALUE fdb_fetch(int argc, VALUE *argv, VALUE vself){
2321
+ VALUE vfdb, vkey, vdef, vval;
2322
+ TCFDB *fdb;
2323
+ char *vbuf;
2324
+ int vsiz;
2325
+ rb_scan_args(argc, argv, "11", &vkey, &vdef);
2326
+ vkey = StringValueEx(vkey);
2327
+ vfdb = rb_iv_get(vself, FDBVNDATA);
2328
+ Data_Get_Struct(vfdb, TCFDB, fdb);
2329
+ if((vbuf = tcfdbget2(fdb, RSTRING_PTR(vkey), RSTRING_LEN(vkey), &vsiz)) != NULL){
2330
+ vval = rb_str_new(vbuf, vsiz);
2331
+ tcfree(vbuf);
2332
+ } else {
2333
+ vval = vdef;
2334
+ }
2335
+ return vval;
2336
+ }
2337
+
2338
+
2339
+ static VALUE fdb_check(VALUE vself, VALUE vkey){
2340
+ VALUE vfdb;
2341
+ TCFDB *fdb;
2342
+ vkey = StringValueEx(vkey);
2343
+ vfdb = rb_iv_get(vself, FDBVNDATA);
2344
+ Data_Get_Struct(vfdb, TCFDB, fdb);
2345
+ return tcfdbvsiz2(fdb, RSTRING_PTR(vkey), RSTRING_LEN(vkey)) >= 0 ? Qtrue : Qfalse;
2346
+ }
2347
+
2348
+
2349
+ static VALUE fdb_check_value(VALUE vself, VALUE vval){
2350
+ VALUE vfdb;
2351
+ TCFDB *fdb;
2352
+ char *tvbuf;
2353
+ bool hit;
2354
+ int tvsiz;
2355
+ uint64_t id;
2356
+ vval = StringValueEx(vval);
2357
+ vfdb = rb_iv_get(vself, FDBVNDATA);
2358
+ Data_Get_Struct(vfdb, TCFDB, fdb);
2359
+ hit = false;
2360
+ tcfdbiterinit(fdb);
2361
+ while((id = tcfdbiternext(fdb)) > 0){
2362
+ tvbuf = tcfdbget(fdb, id, &tvsiz);
2363
+ if(tvbuf && tvsiz == RSTRING_LEN(vval) &&
2364
+ memcmp(tvbuf, RSTRING_PTR(vval), RSTRING_LEN(vval)) == 0){
2365
+ tcfree(tvbuf);
2366
+ hit = true;
2367
+ break;
2368
+ }
2369
+ tcfree(tvbuf);
2370
+ }
2371
+ return hit ? Qtrue : Qfalse;
2372
+ }
2373
+
2374
+
2375
+ static VALUE fdb_get_reverse(VALUE vself, VALUE vval){
2376
+ VALUE vfdb, vrv;
2377
+ TCFDB *fdb;
2378
+ char *tvbuf, kbuf[NUMBUFSIZ];
2379
+ int tvsiz, ksiz;
2380
+ uint64_t id;
2381
+ vval = StringValueEx(vval);
2382
+ vfdb = rb_iv_get(vself, FDBVNDATA);
2383
+ Data_Get_Struct(vfdb, TCFDB, fdb);
2384
+ vrv = Qnil;
2385
+ tcfdbiterinit(fdb);
2386
+ while((id = tcfdbiternext(fdb)) > 0){
2387
+ tvbuf = tcfdbget(fdb, id, &tvsiz);
2388
+ if(tvbuf && tvsiz == RSTRING_LEN(vval) &&
2389
+ memcmp(tvbuf, RSTRING_PTR(vval), RSTRING_LEN(vval)) == 0){
2390
+ tcfree(tvbuf);
2391
+ ksiz = sprintf(kbuf, "%llu", (unsigned long long)id);
2392
+ vrv = rb_str_new(kbuf, ksiz);
2393
+ break;
2394
+ }
2395
+ tcfree(tvbuf);
2396
+ }
2397
+ return vrv;
2398
+ }
2399
+
2400
+
2401
+ static VALUE fdb_empty(VALUE vself){
2402
+ VALUE vfdb;
2403
+ TCFDB *fdb;
2404
+ vfdb = rb_iv_get(vself, FDBVNDATA);
2405
+ Data_Get_Struct(vfdb, TCFDB, fdb);
2406
+ return tcfdbrnum(fdb) < 1 ? Qtrue : Qfalse;
2407
+ }
2408
+
2409
+
2410
+ static VALUE fdb_each(VALUE vself){
2411
+ VALUE vfdb, vrv;
2412
+ TCFDB *fdb;
2413
+ char *vbuf, kbuf[NUMBUFSIZ];
2414
+ int vsiz, ksiz;
2415
+ uint64_t id;
2416
+ if(rb_block_given_p() != Qtrue) rb_raise(rb_eArgError, "no block given");
2417
+ vfdb = rb_iv_get(vself, FDBVNDATA);
2418
+ Data_Get_Struct(vfdb, TCFDB, fdb);
2419
+ vrv = Qnil;
2420
+ tcfdbiterinit(fdb);
2421
+ while((id = tcfdbiternext(fdb)) > 0){
2422
+ vbuf = tcfdbget(fdb, id, &vsiz);
2423
+ if(vbuf){
2424
+ ksiz = sprintf(kbuf, "%llu", (unsigned long long)id);
2425
+ vrv = rb_yield_values(2, rb_str_new(kbuf, ksiz), rb_str_new(vbuf, vsiz));
2426
+ }
2427
+ tcfree(vbuf);
2428
+ }
2429
+ return vrv;
2430
+ }
2431
+
2432
+
2433
+ static VALUE fdb_each_key(VALUE vself){
2434
+ VALUE vfdb, vrv;
2435
+ TCFDB *fdb;
2436
+ char kbuf[NUMBUFSIZ];
2437
+ int ksiz;
2438
+ uint64_t id;
2439
+ if(rb_block_given_p() != Qtrue) rb_raise(rb_eArgError, "no block given");
2440
+ vfdb = rb_iv_get(vself, FDBVNDATA);
2441
+ Data_Get_Struct(vfdb, TCFDB, fdb);
2442
+ vrv = Qnil;
2443
+ tcfdbiterinit(fdb);
2444
+ while((id = tcfdbiternext(fdb)) > 0){
2445
+ ksiz = sprintf(kbuf, "%llu", (unsigned long long)id);
2446
+ vrv = rb_yield(rb_str_new(kbuf, ksiz));
2447
+ }
2448
+ return vrv;
2449
+ }
2450
+
2451
+
2452
+ static VALUE fdb_each_value(VALUE vself){
2453
+ VALUE vfdb, vrv;
2454
+ TCFDB *fdb;
2455
+ char *vbuf;
2456
+ int vsiz;
2457
+ uint64_t id;
2458
+ if(rb_block_given_p() != Qtrue) rb_raise(rb_eArgError, "no block given");
2459
+ vfdb = rb_iv_get(vself, FDBVNDATA);
2460
+ Data_Get_Struct(vfdb, TCFDB, fdb);
2461
+ vrv = Qnil;
2462
+ tcfdbiterinit(fdb);
2463
+ while((id = tcfdbiternext(fdb)) > 0){
2464
+ vbuf = tcfdbget(fdb, id, &vsiz);
2465
+ if(vbuf){
2466
+ vrv = rb_yield(rb_str_new(vbuf, vsiz));
2467
+ }
2468
+ free(vbuf);
2469
+ }
2470
+ return vrv;
2471
+ }
2472
+
2473
+
2474
+ static VALUE fdb_keys(VALUE vself){
2475
+ VALUE vfdb, vary;
2476
+ TCFDB *fdb;
2477
+ char kbuf[NUMBUFSIZ];
2478
+ int ksiz;
2479
+ uint64_t id;
2480
+ vfdb = rb_iv_get(vself, FDBVNDATA);
2481
+ Data_Get_Struct(vfdb, TCFDB, fdb);
2482
+ vary = rb_ary_new2(tcfdbrnum(fdb));
2483
+ tcfdbiterinit(fdb);
2484
+ while((id = tcfdbiternext(fdb)) > 0){
2485
+ ksiz = sprintf(kbuf, "%llu", (unsigned long long)id);
2486
+ rb_ary_push(vary, rb_str_new(kbuf, ksiz));
2487
+ }
2488
+ return vary;
2489
+ }
2490
+
2491
+
2492
+ static VALUE fdb_values(VALUE vself){
2493
+ VALUE vfdb, vary;
2494
+ TCFDB *fdb;
2495
+ char *vbuf;
2496
+ int vsiz;
2497
+ uint64_t id;
2498
+ vfdb = rb_iv_get(vself, FDBVNDATA);
2499
+ Data_Get_Struct(vfdb, TCFDB, fdb);
2500
+ vary = rb_ary_new2(tcfdbrnum(fdb));
2501
+ tcfdbiterinit(fdb);
2502
+ while((id = tcfdbiternext(fdb)) > 0){
2503
+ vbuf = tcfdbget(fdb, id, &vsiz);
2504
+ if(vbuf){
2505
+ rb_ary_push(vary, rb_str_new(vbuf, vsiz));
2506
+ }
2507
+ free(vbuf);
2508
+ }
2509
+ return vary;
2510
+ }
2511
+
2512
+
2513
+ static void tdb_init(void){
2514
+ cls_tdb = rb_define_class_under(mod_tokyocabinet, "TDB", rb_cObject);
2515
+ cls_tdb_data = rb_define_class_under(mod_tokyocabinet, "TDB_data", rb_cObject);
2516
+ rb_define_const(cls_tdb, "ESUCCESS", INT2NUM(TCESUCCESS));
2517
+ rb_define_const(cls_tdb, "ETHREAD", INT2NUM(TCETHREAD));
2518
+ rb_define_const(cls_tdb, "EINVALID", INT2NUM(TCEINVALID));
2519
+ rb_define_const(cls_tdb, "ENOFILE", INT2NUM(TCENOFILE));
2520
+ rb_define_const(cls_tdb, "ENOPERM", INT2NUM(TCENOPERM));
2521
+ rb_define_const(cls_tdb, "EMETA", INT2NUM(TCEMETA));
2522
+ rb_define_const(cls_tdb, "ERHEAD", INT2NUM(TCERHEAD));
2523
+ rb_define_const(cls_tdb, "EOPEN", INT2NUM(TCEOPEN));
2524
+ rb_define_const(cls_tdb, "ECLOSE", INT2NUM(TCECLOSE));
2525
+ rb_define_const(cls_tdb, "ETRUNC", INT2NUM(TCETRUNC));
2526
+ rb_define_const(cls_tdb, "ESYNC", INT2NUM(TCESYNC));
2527
+ rb_define_const(cls_tdb, "ESTAT", INT2NUM(TCESTAT));
2528
+ rb_define_const(cls_tdb, "ESEEK", INT2NUM(TCESEEK));
2529
+ rb_define_const(cls_tdb, "EREAD", INT2NUM(TCEREAD));
2530
+ rb_define_const(cls_tdb, "EWRITE", INT2NUM(TCEWRITE));
2531
+ rb_define_const(cls_tdb, "EMMAP", INT2NUM(TCEMMAP));
2532
+ rb_define_const(cls_tdb, "ELOCK", INT2NUM(TCELOCK));
2533
+ rb_define_const(cls_tdb, "EUNLINK", INT2NUM(TCEUNLINK));
2534
+ rb_define_const(cls_tdb, "ERENAME", INT2NUM(TCERENAME));
2535
+ rb_define_const(cls_tdb, "EMKDIR", INT2NUM(TCEMKDIR));
2536
+ rb_define_const(cls_tdb, "ERMDIR", INT2NUM(TCERMDIR));
2537
+ rb_define_const(cls_tdb, "EKEEP", INT2NUM(TCEKEEP));
2538
+ rb_define_const(cls_tdb, "ENOREC", INT2NUM(TCENOREC));
2539
+ rb_define_const(cls_tdb, "EMISC", INT2NUM(TCEMISC));
2540
+ rb_define_const(cls_tdb, "TLARGE", INT2NUM(TDBTLARGE));
2541
+ rb_define_const(cls_tdb, "TDEFLATE", INT2NUM(TDBTDEFLATE));
2542
+ rb_define_const(cls_tdb, "TBZIP", INT2NUM(TDBTBZIP));
2543
+ rb_define_const(cls_tdb, "TTCBS", INT2NUM(TDBTTCBS));
2544
+ rb_define_const(cls_tdb, "OREADER", INT2NUM(TDBOREADER));
2545
+ rb_define_const(cls_tdb, "OWRITER", INT2NUM(TDBOWRITER));
2546
+ rb_define_const(cls_tdb, "OCREAT", INT2NUM(TDBOCREAT));
2547
+ rb_define_const(cls_tdb, "OTRUNC", INT2NUM(TDBOTRUNC));
2548
+ rb_define_const(cls_tdb, "ONOLCK", INT2NUM(TDBONOLCK));
2549
+ rb_define_const(cls_tdb, "OLCKNB", INT2NUM(TDBOLCKNB));
2550
+ rb_define_const(cls_tdb, "OTSYNC", INT2NUM(TDBOTSYNC));
2551
+ rb_define_const(cls_tdb, "ITLEXICAL", INT2NUM(TDBITLEXICAL));
2552
+ rb_define_const(cls_tdb, "ITDECIMAL", INT2NUM(TDBITDECIMAL));
2553
+ rb_define_const(cls_tdb, "ITTOKEN", INT2NUM(TDBITTOKEN));
2554
+ rb_define_const(cls_tdb, "ITQGRAM", INT2NUM(TDBITQGRAM));
2555
+ rb_define_const(cls_tdb, "ITVOID", INT2NUM(TDBITVOID));
2556
+ rb_define_const(cls_tdb, "ITKEEP", INT2NUM(TDBITKEEP));
2557
+ rb_define_private_method(cls_tdb, "initialize", tdb_initialize, 0);
2558
+ rb_define_method(cls_tdb, "errmsg", tdb_errmsg, -1);
2559
+ rb_define_method(cls_tdb, "ecode", tdb_ecode, 0);
2560
+ rb_define_method(cls_tdb, "tune", tdb_tune, -1);
2561
+ rb_define_method(cls_tdb, "setcache", tdb_setcache, -1);
2562
+ rb_define_method(cls_tdb, "setxmsiz", tdb_setxmsiz, -1);
2563
+ rb_define_method(cls_tdb, "setdfunit", tdb_setdfunit, -1);
2564
+ rb_define_method(cls_tdb, "open", tdb_open, -1);
2565
+ rb_define_method(cls_tdb, "close", tdb_close, 0);
2566
+ rb_define_method(cls_tdb, "put", tdb_put, 2);
2567
+ rb_define_method(cls_tdb, "putkeep", tdb_putkeep, 2);
2568
+ rb_define_method(cls_tdb, "putcat", tdb_putcat, 2);
2569
+ rb_define_method(cls_tdb, "out", tdb_out, 1);
2570
+ rb_define_method(cls_tdb, "get", tdb_get, 1);
2571
+ rb_define_method(cls_tdb, "vsiz", tdb_vsiz, 1);
2572
+ rb_define_method(cls_tdb, "iterinit", tdb_iterinit, 0);
2573
+ rb_define_method(cls_tdb, "iternext", tdb_iternext, 0);
2574
+ rb_define_method(cls_tdb, "fwmkeys", tdb_fwmkeys, -1);
2575
+ rb_define_method(cls_tdb, "addint", tdb_addint, 2);
2576
+ rb_define_method(cls_tdb, "adddouble", tdb_adddouble, 2);
2577
+ rb_define_method(cls_tdb, "sync", tdb_sync, 0);
2578
+ rb_define_method(cls_tdb, "optimize", tdb_optimize, -1);
2579
+ rb_define_method(cls_tdb, "vanish", tdb_vanish, 0);
2580
+ rb_define_method(cls_tdb, "copy", tdb_copy, 1);
2581
+ rb_define_method(cls_tdb, "tranbegin", tdb_tranbegin, 0);
2582
+ rb_define_method(cls_tdb, "trancommit", tdb_trancommit, 0);
2583
+ rb_define_method(cls_tdb, "tranabort", tdb_tranabort, 0);
2584
+ rb_define_method(cls_tdb, "path", tdb_path, 0);
2585
+ rb_define_method(cls_tdb, "rnum", tdb_rnum, 0);
2586
+ rb_define_method(cls_tdb, "fsiz", tdb_fsiz, 0);
2587
+ rb_define_method(cls_tdb, "setindex", tdb_setindex, 2);
2588
+ rb_define_method(cls_tdb, "genuid", tdb_genuid, 0);
2589
+ rb_define_method(cls_tdb, "[]", tdb_get, 1);
2590
+ rb_define_method(cls_tdb, "[]=", tdb_put, 2);
2591
+ rb_define_method(cls_tdb, "store", tdb_put, 2);
2592
+ rb_define_method(cls_tdb, "delete", tdb_out, 1);
2593
+ rb_define_method(cls_tdb, "fetch", tdb_fetch, -1);
2594
+ rb_define_method(cls_tdb, "has_key?", tdb_check, 1);
2595
+ rb_define_method(cls_tdb, "key?", tdb_check, 1);
2596
+ rb_define_method(cls_tdb, "include?", tdb_check, 1);
2597
+ rb_define_method(cls_tdb, "member?", tdb_check, 1);
2598
+ rb_define_method(cls_tdb, "clear", tdb_vanish, 0);
2599
+ rb_define_method(cls_tdb, "size", tdb_rnum, 0);
2600
+ rb_define_method(cls_tdb, "length", tdb_rnum, 0);
2601
+ rb_define_method(cls_tdb, "empty?", tdb_empty, 0);
2602
+ rb_define_method(cls_tdb, "each", tdb_each, 0);
2603
+ rb_define_method(cls_tdb, "each_pair", tdb_each, 0);
2604
+ rb_define_method(cls_tdb, "each_key", tdb_each_key, 0);
2605
+ rb_define_method(cls_tdb, "each_value", tdb_each_value, 0);
2606
+ rb_define_method(cls_tdb, "keys", tdb_keys, 0);
2607
+ rb_define_method(cls_tdb, "values", tdb_values, 0);
2608
+ }
2609
+
2610
+
2611
+ static VALUE tdb_initialize(VALUE vself){
2612
+ VALUE vtdb;
2613
+ TCTDB *tdb;
2614
+ tdb = tctdbnew();
2615
+ tctdbsetmutex(tdb);
2616
+ vtdb = Data_Wrap_Struct(cls_tdb_data, 0, tctdbdel, tdb);
2617
+ rb_iv_set(vself, TDBVNDATA, vtdb);
2618
+ return Qnil;
2619
+ }
2620
+
2621
+
2622
+ static VALUE tdb_errmsg(int argc, VALUE *argv, VALUE vself){
2623
+ VALUE vtdb, vecode;
2624
+ TCTDB *tdb;
2625
+ const char *msg;
2626
+ int ecode;
2627
+ rb_scan_args(argc, argv, "01", &vecode);
2628
+ vtdb = rb_iv_get(vself, TDBVNDATA);
2629
+ Data_Get_Struct(vtdb, TCTDB, tdb);
2630
+ ecode = (vecode == Qnil) ? tctdbecode(tdb) : NUM2INT(vecode);
2631
+ msg = tctdberrmsg(ecode);
2632
+ return rb_str_new2(msg);
2633
+ }
2634
+
2635
+
2636
+ static VALUE tdb_ecode(VALUE vself){
2637
+ VALUE vtdb;
2638
+ TCTDB *tdb;
2639
+ vtdb = rb_iv_get(vself, TDBVNDATA);
2640
+ Data_Get_Struct(vtdb, TCTDB, tdb);
2641
+ return INT2NUM(tctdbecode(tdb));
2642
+ }
2643
+
2644
+
2645
+ static VALUE tdb_tune(int argc, VALUE *argv, VALUE vself){
2646
+ VALUE vtdb, vbnum, vapow, vfpow, vopts;
2647
+ TCTDB *tdb;
2648
+ int apow, fpow, opts;
2649
+ int64_t bnum;
2650
+ rb_scan_args(argc, argv, "04", &vbnum, &vapow, &vfpow, &vopts);
2651
+ bnum = (vbnum == Qnil) ? -1 : NUM2LL(vbnum);
2652
+ apow = (vapow == Qnil) ? -1 : NUM2INT(vapow);
2653
+ fpow = (vfpow == Qnil) ? -1 : NUM2INT(vfpow);
2654
+ opts = (vopts == Qnil) ? 0 : NUM2INT(vopts);
2655
+ vtdb = rb_iv_get(vself, TDBVNDATA);
2656
+ Data_Get_Struct(vtdb, TCTDB, tdb);
2657
+ return tctdbtune(tdb, bnum, apow, fpow, opts) ? Qtrue : Qfalse;
2658
+ }
2659
+
2660
+
2661
+ static VALUE tdb_setcache(int argc, VALUE *argv, VALUE vself){
2662
+ VALUE vtdb, vrcnum, vlcnum, vncnum;
2663
+ TCTDB *tdb;
2664
+ int rcnum, lcnum, ncnum;
2665
+ rb_scan_args(argc, argv, "03", &vrcnum, &vlcnum, &vncnum);
2666
+ rcnum = (vrcnum == Qnil) ? -1 : NUM2INT(vrcnum);
2667
+ lcnum = (vlcnum == Qnil) ? -1 : NUM2INT(vlcnum);
2668
+ ncnum = (vncnum == Qnil) ? -1 : NUM2INT(vncnum);
2669
+ vtdb = rb_iv_get(vself, TDBVNDATA);
2670
+ Data_Get_Struct(vtdb, TCTDB, tdb);
2671
+ return tctdbsetcache(tdb, rcnum, lcnum, ncnum) ? Qtrue : Qfalse;
2672
+ }
2673
+
2674
+
2675
+ static VALUE tdb_setxmsiz(int argc, VALUE *argv, VALUE vself){
2676
+ VALUE vtdb, vxmsiz;
2677
+ TCTDB *tdb;
2678
+ int64_t xmsiz;
2679
+ rb_scan_args(argc, argv, "01", &vxmsiz);
2680
+ xmsiz = (vxmsiz == Qnil) ? -1 : NUM2LL(vxmsiz);
2681
+ vtdb = rb_iv_get(vself, TDBVNDATA);
2682
+ Data_Get_Struct(vtdb, TCTDB, tdb);
2683
+ return tctdbsetxmsiz(tdb, xmsiz) ? Qtrue : Qfalse;
2684
+ }
2685
+
2686
+
2687
+ static VALUE tdb_setdfunit(int argc, VALUE *argv, VALUE vself){
2688
+ VALUE vtdb, vdfunit;
2689
+ TCTDB *tdb;
2690
+ int32_t dfunit;
2691
+ rb_scan_args(argc, argv, "01", &vdfunit);
2692
+ dfunit = (vdfunit == Qnil) ? -1 : NUM2INT(vdfunit);
2693
+ vtdb = rb_iv_get(vself, TDBVNDATA);
2694
+ Data_Get_Struct(vtdb, TCTDB, tdb);
2695
+ return tctdbsetdfunit(tdb, dfunit) ? Qtrue : Qfalse;
2696
+ }
2697
+
2698
+
2699
+ static VALUE tdb_open(int argc, VALUE *argv, VALUE vself){
2700
+ VALUE vtdb, vpath, vomode;
2701
+ TCTDB *tdb;
2702
+ int omode;
2703
+ rb_scan_args(argc, argv, "11", &vpath, &vomode);
2704
+ Check_Type(vpath, T_STRING);
2705
+ omode = (vomode == Qnil) ? TDBOREADER : NUM2INT(vomode);
2706
+ vtdb = rb_iv_get(vself, TDBVNDATA);
2707
+ Data_Get_Struct(vtdb, TCTDB, tdb);
2708
+ return tctdbopen(tdb, RSTRING_PTR(vpath), omode) ? Qtrue : Qfalse;
2709
+ }
2710
+
2711
+
2712
+ static VALUE tdb_close(VALUE vself){
2713
+ VALUE vtdb;
2714
+ TCTDB *tdb;
2715
+ vtdb = rb_iv_get(vself, TDBVNDATA);
2716
+ Data_Get_Struct(vtdb, TCTDB, tdb);
2717
+ return tctdbclose(tdb) ? Qtrue : Qfalse;
2718
+ }
2719
+
2720
+
2721
+ static VALUE tdb_put(VALUE vself, VALUE vpkey, VALUE vcols){
2722
+ VALUE vtdb, vrv;
2723
+ TCTDB *tdb;
2724
+ TCMAP *cols;
2725
+ vpkey = StringValueEx(vpkey);
2726
+ Check_Type(vcols, T_HASH);
2727
+ cols = vhashtomap(vcols);
2728
+ vtdb = rb_iv_get(vself, TDBVNDATA);
2729
+ Data_Get_Struct(vtdb, TCTDB, tdb);
2730
+ vrv = tctdbput(tdb, RSTRING_PTR(vpkey), RSTRING_LEN(vpkey), cols) ? Qtrue : Qfalse;
2731
+ tcmapdel(cols);
2732
+ return vrv;
2733
+ }
2734
+
2735
+
2736
+ static VALUE tdb_putkeep(VALUE vself, VALUE vpkey, VALUE vcols){
2737
+ VALUE vtdb, vrv;
2738
+ TCTDB *tdb;
2739
+ TCMAP *cols;
2740
+ vpkey = StringValueEx(vpkey);
2741
+ Check_Type(vcols, T_HASH);
2742
+ cols = vhashtomap(vcols);
2743
+ vtdb = rb_iv_get(vself, TDBVNDATA);
2744
+ Data_Get_Struct(vtdb, TCTDB, tdb);
2745
+ vrv = tctdbputkeep(tdb, RSTRING_PTR(vpkey), RSTRING_LEN(vpkey), cols) ? Qtrue : Qfalse;
2746
+ tcmapdel(cols);
2747
+ return vrv;
2748
+ }
2749
+
2750
+
2751
+ static VALUE tdb_putcat(VALUE vself, VALUE vpkey, VALUE vcols){
2752
+ VALUE vtdb, vrv;
2753
+ TCTDB *tdb;
2754
+ TCMAP *cols;
2755
+ vpkey = StringValueEx(vpkey);
2756
+ Check_Type(vcols, T_HASH);
2757
+ cols = vhashtomap(vcols);
2758
+ vtdb = rb_iv_get(vself, TDBVNDATA);
2759
+ Data_Get_Struct(vtdb, TCTDB, tdb);
2760
+ vrv = tctdbputcat(tdb, RSTRING_PTR(vpkey), RSTRING_LEN(vpkey), cols) ? Qtrue : Qfalse;
2761
+ tcmapdel(cols);
2762
+ return vrv;
2763
+ }
2764
+
2765
+
2766
+ static VALUE tdb_out(VALUE vself, VALUE vpkey){
2767
+ VALUE vtdb;
2768
+ TCTDB *tdb;
2769
+ vpkey = StringValueEx(vpkey);
2770
+ vtdb = rb_iv_get(vself, TDBVNDATA);
2771
+ Data_Get_Struct(vtdb, TCTDB, tdb);
2772
+ return tctdbout(tdb, RSTRING_PTR(vpkey), RSTRING_LEN(vpkey)) ? Qtrue : Qfalse;
2773
+ }
2774
+
2775
+
2776
+ static VALUE tdb_get(VALUE vself, VALUE vpkey){
2777
+ VALUE vtdb, vcols;
2778
+ TCTDB *tdb;
2779
+ TCMAP *cols;
2780
+ vpkey = StringValueEx(vpkey);
2781
+ vtdb = rb_iv_get(vself, TDBVNDATA);
2782
+ Data_Get_Struct(vtdb, TCTDB, tdb);
2783
+ if(!(cols = tctdbget(tdb, RSTRING_PTR(vpkey), RSTRING_LEN(vpkey)))) return Qnil;
2784
+ vcols = maptovhash(cols);
2785
+ tcmapdel(cols);
2786
+ return vcols;
2787
+ }
2788
+
2789
+
2790
+ static VALUE tdb_vsiz(VALUE vself, VALUE vpkey){
2791
+ VALUE vtdb;
2792
+ TCTDB *tdb;
2793
+ vpkey = StringValueEx(vpkey);
2794
+ vtdb = rb_iv_get(vself, TDBVNDATA);
2795
+ Data_Get_Struct(vtdb, TCTDB, tdb);
2796
+ return INT2NUM(tctdbvsiz(tdb, RSTRING_PTR(vpkey), RSTRING_LEN(vpkey)));
2797
+ }
2798
+
2799
+
2800
+ static VALUE tdb_iterinit(VALUE vself){
2801
+ VALUE vtdb;
2802
+ TCTDB *tdb;
2803
+ vtdb = rb_iv_get(vself, TDBVNDATA);
2804
+ Data_Get_Struct(vtdb, TCTDB, tdb);
2805
+ return tctdbiterinit(tdb) ? Qtrue : Qfalse;
2806
+ }
2807
+
2808
+
2809
+ static VALUE tdb_iternext(VALUE vself){
2810
+ VALUE vtdb, vval;
2811
+ TCTDB *tdb;
2812
+ char *vbuf;
2813
+ int vsiz;
2814
+ vtdb = rb_iv_get(vself, TDBVNDATA);
2815
+ Data_Get_Struct(vtdb, TCTDB, tdb);
2816
+ if(!(vbuf = tctdbiternext(tdb, &vsiz))) return Qnil;
2817
+ vval = rb_str_new(vbuf, vsiz);
2818
+ tcfree(vbuf);
2819
+ return vval;
2820
+ }
2821
+
2822
+
2823
+ static VALUE tdb_fwmkeys(int argc, VALUE *argv, VALUE vself){
2824
+ VALUE vtdb, vprefix, vmax, vary;
2825
+ TCTDB *tdb;
2826
+ TCLIST *pkeys;
2827
+ int max;
2828
+ rb_scan_args(argc, argv, "11", &vprefix, &vmax);
2829
+ vprefix = StringValueEx(vprefix);
2830
+ max = (vmax == Qnil) ? -1 : NUM2INT(vmax);
2831
+ vtdb = rb_iv_get(vself, TDBVNDATA);
2832
+ Data_Get_Struct(vtdb, TCTDB, tdb);
2833
+ pkeys = tctdbfwmkeys(tdb, RSTRING_PTR(vprefix), RSTRING_LEN(vprefix), max);
2834
+ vary = listtovary(pkeys);
2835
+ tclistdel(pkeys);
2836
+ return vary;
2837
+ }
2838
+
2839
+
2840
+ static VALUE tdb_addint(VALUE vself, VALUE vpkey, VALUE vnum){
2841
+ VALUE vtdb;
2842
+ TCTDB *tdb;
2843
+ int num;
2844
+ vpkey = StringValueEx(vpkey);
2845
+ vtdb = rb_iv_get(vself, TDBVNDATA);
2846
+ Data_Get_Struct(vtdb, TCTDB, tdb);
2847
+ num = tctdbaddint(tdb, RSTRING_PTR(vpkey), RSTRING_LEN(vpkey), NUM2INT(vnum));
2848
+ return num == INT_MIN ? Qnil : INT2NUM(num);
2849
+ }
2850
+
2851
+
2852
+ static VALUE tdb_adddouble(VALUE vself, VALUE vpkey, VALUE vnum){
2853
+ VALUE vtdb;
2854
+ TCTDB *tdb;
2855
+ double num;
2856
+ vpkey = StringValueEx(vpkey);
2857
+ vtdb = rb_iv_get(vself, TDBVNDATA);
2858
+ Data_Get_Struct(vtdb, TCTDB, tdb);
2859
+ num = tctdbadddouble(tdb, RSTRING_PTR(vpkey), RSTRING_LEN(vpkey), NUM2DBL(vnum));
2860
+ return isnan(num) ? Qnil : rb_float_new(num);
2861
+ }
2862
+
2863
+
2864
+ static VALUE tdb_sync(VALUE vself){
2865
+ VALUE vtdb;
2866
+ TCTDB *tdb;
2867
+ vtdb = rb_iv_get(vself, TDBVNDATA);
2868
+ Data_Get_Struct(vtdb, TCTDB, tdb);
2869
+ return tctdbsync(tdb) ? Qtrue : Qfalse;
2870
+ }
2871
+
2872
+
2873
+ static VALUE tdb_optimize(int argc, VALUE *argv, VALUE vself){
2874
+ VALUE vtdb, vbnum, vapow, vfpow, vopts;
2875
+ TCTDB *tdb;
2876
+ int apow, fpow, opts;
2877
+ int64_t bnum;
2878
+ rb_scan_args(argc, argv, "04", &vbnum, &vapow, &vfpow, &vopts);
2879
+ bnum = (vbnum == Qnil) ? -1 : NUM2LL(vbnum);
2880
+ apow = (vapow == Qnil) ? -1 : NUM2INT(vapow);
2881
+ fpow = (vfpow == Qnil) ? -1 : NUM2INT(vfpow);
2882
+ opts = (vopts == Qnil) ? UINT8_MAX : NUM2INT(vopts);
2883
+ vtdb = rb_iv_get(vself, TDBVNDATA);
2884
+ Data_Get_Struct(vtdb, TCTDB, tdb);
2885
+ return tctdboptimize(tdb, bnum, apow, fpow, opts) ? Qtrue : Qfalse;
2886
+ }
2887
+
2888
+
2889
+ static VALUE tdb_vanish(VALUE vself){
2890
+ VALUE vtdb;
2891
+ TCTDB *tdb;
2892
+ vtdb = rb_iv_get(vself, TDBVNDATA);
2893
+ Data_Get_Struct(vtdb, TCTDB, tdb);
2894
+ return tctdbvanish(tdb) ? Qtrue : Qfalse;
2895
+ }
2896
+
2897
+
2898
+ static VALUE tdb_copy(VALUE vself, VALUE vpath){
2899
+ VALUE vtdb;
2900
+ TCTDB *tdb;
2901
+ Check_Type(vpath, T_STRING);
2902
+ vtdb = rb_iv_get(vself, TDBVNDATA);
2903
+ Data_Get_Struct(vtdb, TCTDB, tdb);
2904
+ return tctdbcopy(tdb, RSTRING_PTR(vpath)) ? Qtrue : Qfalse;
2905
+ }
2906
+
2907
+
2908
+ static VALUE tdb_tranbegin(VALUE vself){
2909
+ VALUE vtdb;
2910
+ TCTDB *tdb;
2911
+ vtdb = rb_iv_get(vself, TDBVNDATA);
2912
+ Data_Get_Struct(vtdb, TCTDB, tdb);
2913
+ return tctdbtranbegin(tdb) ? Qtrue : Qfalse;
2914
+ }
2915
+
2916
+
2917
+ static VALUE tdb_trancommit(VALUE vself){
2918
+ VALUE vtdb;
2919
+ TCTDB *tdb;
2920
+ vtdb = rb_iv_get(vself, TDBVNDATA);
2921
+ Data_Get_Struct(vtdb, TCTDB, tdb);
2922
+ return tctdbtrancommit(tdb) ? Qtrue : Qfalse;
2923
+ }
2924
+
2925
+
2926
+ static VALUE tdb_tranabort(VALUE vself){
2927
+ VALUE vtdb;
2928
+ TCTDB *tdb;
2929
+ vtdb = rb_iv_get(vself, TDBVNDATA);
2930
+ Data_Get_Struct(vtdb, TCTDB, tdb);
2931
+ return tctdbtranabort(tdb) ? Qtrue : Qfalse;
2932
+ }
2933
+
2934
+
2935
+ static VALUE tdb_path(VALUE vself){
2936
+ VALUE vtdb, vpath;
2937
+ TCTDB *tdb;
2938
+ const char *path;
2939
+ vtdb = rb_iv_get(vself, TDBVNDATA);
2940
+ Data_Get_Struct(vtdb, TCTDB, tdb);
2941
+ if(!(path = tctdbpath(tdb))) return Qnil;
2942
+ vpath = rb_str_new2(path);
2943
+ return vpath;
2944
+ }
2945
+
2946
+
2947
+ static VALUE tdb_rnum(VALUE vself){
2948
+ VALUE vtdb;
2949
+ TCTDB *tdb;
2950
+ vtdb = rb_iv_get(vself, TDBVNDATA);
2951
+ Data_Get_Struct(vtdb, TCTDB, tdb);
2952
+ return LL2NUM(tctdbrnum(tdb));
2953
+ }
2954
+
2955
+
2956
+ static VALUE tdb_fsiz(VALUE vself){
2957
+ VALUE vtdb;
2958
+ TCTDB *tdb;
2959
+ vtdb = rb_iv_get(vself, TDBVNDATA);
2960
+ Data_Get_Struct(vtdb, TCTDB, tdb);
2961
+ return LL2NUM(tctdbfsiz(tdb));
2962
+ }
2963
+
2964
+
2965
+ static VALUE tdb_setindex(VALUE vself, VALUE vname, VALUE vtype){
2966
+ VALUE vtdb;
2967
+ TCTDB *tdb;
2968
+ Check_Type(vname, T_STRING);
2969
+ vtdb = rb_iv_get(vself, TDBVNDATA);
2970
+ Data_Get_Struct(vtdb, TCTDB, tdb);
2971
+ return tctdbsetindex(tdb, RSTRING_PTR(vname), NUM2INT(vtype)) ? Qtrue : Qfalse;
2972
+ }
2973
+
2974
+
2975
+ static VALUE tdb_genuid(VALUE vself){
2976
+ VALUE vtdb;
2977
+ TCTDB *tdb;
2978
+ vtdb = rb_iv_get(vself, TDBVNDATA);
2979
+ Data_Get_Struct(vtdb, TCTDB, tdb);
2980
+ return LL2NUM(tctdbgenuid(tdb));
2981
+ }
2982
+
2983
+
2984
+ static VALUE tdb_fetch(int argc, VALUE *argv, VALUE vself){
2985
+ VALUE vtdb, vpkey, vdef, vcols;
2986
+ TCTDB *tdb;
2987
+ TCMAP *cols;
2988
+ rb_scan_args(argc, argv, "11", &vpkey, &vdef);
2989
+ vpkey = StringValueEx(vpkey);
2990
+ vtdb = rb_iv_get(vself, TDBVNDATA);
2991
+ Data_Get_Struct(vtdb, TCTDB, tdb);
2992
+ if((cols = tctdbget(tdb, RSTRING_PTR(vpkey), RSTRING_LEN(vpkey))) != NULL){
2993
+ vcols = maptovhash(cols);
2994
+ tcmapdel(cols);
2995
+ } else {
2996
+ vcols = vdef;
2997
+ }
2998
+ return vcols;
2999
+ }
3000
+
3001
+
3002
+ static VALUE tdb_check(VALUE vself, VALUE vpkey){
3003
+ VALUE vtdb;
3004
+ TCTDB *tdb;
3005
+ vpkey = StringValueEx(vpkey);
3006
+ vtdb = rb_iv_get(vself, TDBVNDATA);
3007
+ Data_Get_Struct(vtdb, TCTDB, tdb);
3008
+ return tctdbvsiz(tdb, RSTRING_PTR(vpkey), RSTRING_LEN(vpkey)) >= 0 ? Qtrue : Qfalse;
3009
+ }
3010
+
3011
+
3012
+ static VALUE tdb_empty(VALUE vself){
3013
+ VALUE vtdb;
3014
+ TCTDB *tdb;
3015
+ vtdb = rb_iv_get(vself, TDBVNDATA);
3016
+ Data_Get_Struct(vtdb, TCTDB, tdb);
3017
+ return tctdbrnum(tdb) < 1 ? Qtrue : Qfalse;
3018
+ }
3019
+
3020
+
3021
+ static VALUE tdb_each(VALUE vself){
3022
+ VALUE vtdb, vrv;
3023
+ TCTDB *tdb;
3024
+ TCMAP *cols;
3025
+ char *kbuf;
3026
+ int ksiz;
3027
+ if(rb_block_given_p() != Qtrue) rb_raise(rb_eArgError, "no block given");
3028
+ vtdb = rb_iv_get(vself, TDBVNDATA);
3029
+ Data_Get_Struct(vtdb, TCTDB, tdb);
3030
+ vrv = Qnil;
3031
+ tctdbiterinit(tdb);
3032
+ while((kbuf = tctdbiternext(tdb, &ksiz)) != NULL){
3033
+ if((cols = tctdbget(tdb, kbuf, ksiz)) != NULL){
3034
+ vrv = rb_yield_values(2, rb_str_new(kbuf, ksiz), maptovhash(cols));
3035
+ tcmapdel(cols);
3036
+ }
3037
+ tcfree(kbuf);
3038
+ }
3039
+ return vrv;
3040
+ }
3041
+
3042
+
3043
+ static VALUE tdb_each_key(VALUE vself){
3044
+ VALUE vtdb, vrv;
3045
+ TCTDB *tdb;
3046
+ char *kbuf;
3047
+ int ksiz;
3048
+ if(rb_block_given_p() != Qtrue) rb_raise(rb_eArgError, "no block given");
3049
+ vtdb = rb_iv_get(vself, TDBVNDATA);
3050
+ Data_Get_Struct(vtdb, TCTDB, tdb);
3051
+ vrv = Qnil;
3052
+ tctdbiterinit(tdb);
3053
+ while((kbuf = tctdbiternext(tdb, &ksiz)) != NULL){
3054
+ vrv = rb_yield(rb_str_new(kbuf, ksiz));
3055
+ tcfree(kbuf);
3056
+ }
3057
+ return vrv;
3058
+ }
3059
+
3060
+
3061
+ static VALUE tdb_each_value(VALUE vself){
3062
+ VALUE vtdb, vrv;
3063
+ TCTDB *tdb;
3064
+ TCMAP *cols;
3065
+ char *kbuf;
3066
+ int ksiz;
3067
+ if(rb_block_given_p() != Qtrue) rb_raise(rb_eArgError, "no block given");
3068
+ vtdb = rb_iv_get(vself, TDBVNDATA);
3069
+ Data_Get_Struct(vtdb, TCTDB, tdb);
3070
+ vrv = Qnil;
3071
+ tctdbiterinit(tdb);
3072
+ while((kbuf = tctdbiternext(tdb, &ksiz)) != NULL){
3073
+ if((cols = tctdbget(tdb, kbuf, ksiz)) != NULL){
3074
+ vrv = rb_yield(maptovhash(cols));
3075
+ tcmapdel(cols);
3076
+ }
3077
+ tcfree(kbuf);
3078
+ }
3079
+ return vrv;
3080
+ }
3081
+
3082
+
3083
+ static VALUE tdb_keys(VALUE vself){
3084
+ VALUE vtdb, vary;
3085
+ TCTDB *tdb;
3086
+ char *kbuf;
3087
+ int ksiz;
3088
+ vtdb = rb_iv_get(vself, TDBVNDATA);
3089
+ Data_Get_Struct(vtdb, TCTDB, tdb);
3090
+ vary = rb_ary_new2(tctdbrnum(tdb));
3091
+ tctdbiterinit(tdb);
3092
+ while((kbuf = tctdbiternext(tdb, &ksiz)) != NULL){
3093
+ rb_ary_push(vary, rb_str_new(kbuf, ksiz));
3094
+ tcfree(kbuf);
3095
+ }
3096
+ return vary;
3097
+ }
3098
+
3099
+
3100
+ static VALUE tdb_values(VALUE vself){
3101
+ VALUE vtdb, vary;
3102
+ TCTDB *tdb;
3103
+ TCMAP *cols;
3104
+ char *kbuf;
3105
+ int ksiz;
3106
+ vtdb = rb_iv_get(vself, TDBVNDATA);
3107
+ Data_Get_Struct(vtdb, TCTDB, tdb);
3108
+ vary = rb_ary_new2(tctdbrnum(tdb));
3109
+ tctdbiterinit(tdb);
3110
+ while((kbuf = tctdbiternext(tdb, &ksiz)) != NULL){
3111
+ if((cols = tctdbget(tdb, kbuf, ksiz)) != NULL){
3112
+ rb_ary_push(vary, maptovhash(cols));
3113
+ tcmapdel(cols);
3114
+ }
3115
+ tcfree(kbuf);
3116
+ }
3117
+ return vary;
3118
+ }
3119
+
3120
+
3121
+ static void tdbqry_init(void){
3122
+ cls_tdbqry = rb_define_class_under(mod_tokyocabinet, "TDBQRY", rb_cObject);
3123
+ cls_tdbqry_data = rb_define_class_under(mod_tokyocabinet, "TDBQRY_data", rb_cObject);
3124
+ rb_define_const(cls_tdbqry, "QCSTREQ", INT2NUM(TDBQCSTREQ));
3125
+ rb_define_const(cls_tdbqry, "QCSTRINC", INT2NUM(TDBQCSTRINC));
3126
+ rb_define_const(cls_tdbqry, "QCSTRBW", INT2NUM(TDBQCSTRBW));
3127
+ rb_define_const(cls_tdbqry, "QCSTREW", INT2NUM(TDBQCSTREW));
3128
+ rb_define_const(cls_tdbqry, "QCSTRAND", INT2NUM(TDBQCSTRAND));
3129
+ rb_define_const(cls_tdbqry, "QCSTROR", INT2NUM(TDBQCSTROR));
3130
+ rb_define_const(cls_tdbqry, "QCSTROREQ", INT2NUM(TDBQCSTROREQ));
3131
+ rb_define_const(cls_tdbqry, "QCSTRRX", INT2NUM(TDBQCSTRRX));
3132
+ rb_define_const(cls_tdbqry, "QCNUMEQ", INT2NUM(TDBQCNUMEQ));
3133
+ rb_define_const(cls_tdbqry, "QCNUMGT", INT2NUM(TDBQCNUMGT));
3134
+ rb_define_const(cls_tdbqry, "QCNUMGE", INT2NUM(TDBQCNUMGE));
3135
+ rb_define_const(cls_tdbqry, "QCNUMLT", INT2NUM(TDBQCNUMLT));
3136
+ rb_define_const(cls_tdbqry, "QCNUMLE", INT2NUM(TDBQCNUMLE));
3137
+ rb_define_const(cls_tdbqry, "QCNUMBT", INT2NUM(TDBQCNUMBT));
3138
+ rb_define_const(cls_tdbqry, "QCNUMOREQ", INT2NUM(TDBQCNUMOREQ));
3139
+ rb_define_const(cls_tdbqry, "QCFTSPH", INT2NUM(TDBQCFTSPH));
3140
+ rb_define_const(cls_tdbqry, "QCFTSAND", INT2NUM(TDBQCFTSAND));
3141
+ rb_define_const(cls_tdbqry, "QCFTSOR", INT2NUM(TDBQCFTSOR));
3142
+ rb_define_const(cls_tdbqry, "QCFTSEX", INT2NUM(TDBQCFTSEX));
3143
+ rb_define_const(cls_tdbqry, "QCNEGATE", INT2NUM(TDBQCNEGATE));
3144
+ rb_define_const(cls_tdbqry, "QCNOIDX", INT2NUM(TDBQCNOIDX));
3145
+ rb_define_const(cls_tdbqry, "QOSTRASC", INT2NUM(TDBQOSTRASC));
3146
+ rb_define_const(cls_tdbqry, "QOSTRDESC", INT2NUM(TDBQOSTRDESC));
3147
+ rb_define_const(cls_tdbqry, "QONUMASC", INT2NUM(TDBQONUMASC));
3148
+ rb_define_const(cls_tdbqry, "QONUMDESC", INT2NUM(TDBQONUMDESC));
3149
+ rb_define_const(cls_tdbqry, "QPPUT", INT2NUM(TDBQPPUT));
3150
+ rb_define_const(cls_tdbqry, "QPOUT", INT2NUM(TDBQPOUT));
3151
+ rb_define_const(cls_tdbqry, "QPSTOP", INT2NUM(TDBQPSTOP));
3152
+ rb_define_const(cls_tdbqry, "MSUNION", INT2NUM(TDBMSUNION));
3153
+ rb_define_const(cls_tdbqry, "MSISECT", INT2NUM(TDBMSISECT));
3154
+ rb_define_const(cls_tdbqry, "MSDIFF", INT2NUM(TDBMSDIFF));
3155
+ rb_define_const(cls_tdbqry, "KWMUTAB", INT2NUM(TCKWMUTAB));
3156
+ rb_define_const(cls_tdbqry, "KWMUCTRL", INT2NUM(TCKWMUCTRL));
3157
+ rb_define_const(cls_tdbqry, "KWMUBRCT", INT2NUM(TCKWMUBRCT));
3158
+ rb_define_const(cls_tdbqry, "KWNOOVER", INT2NUM(TCKWNOOVER));
3159
+ rb_define_const(cls_tdbqry, "KWPULEAD", INT2NUM(TCKWPULEAD));
3160
+ rb_define_private_method(cls_tdbqry, "initialize", tdbqry_initialize, 1);
3161
+ rb_define_method(cls_tdbqry, "addcond", tdbqry_addcond, 3);
3162
+ rb_define_method(cls_tdbqry, "setorder", tdbqry_setorder, -1);
3163
+ rb_define_method(cls_tdbqry, "setlimit", tdbqry_setlimit, -1);
3164
+ rb_define_method(cls_tdbqry, "setmax", tdbqry_setlimit, -1);
3165
+ rb_define_method(cls_tdbqry, "search", tdbqry_search, 0);
3166
+ rb_define_method(cls_tdbqry, "searchout", tdbqry_searchout, 0);
3167
+ rb_define_method(cls_tdbqry, "proc", tdbqry_proc, 0);
3168
+ rb_define_method(cls_tdbqry, "hint", tdbqry_hint, 0);
3169
+ rb_define_method(cls_tdbqry, "metasearch", tdbqry_metasearch, -1);
3170
+ rb_define_method(cls_tdbqry, "kwic", tdbqry_kwic, -1);
3171
+ }
3172
+
3173
+
3174
+ static int tdbqry_procrec(const void *pkbuf, int pksiz, TCMAP *cols, void *opq){
3175
+ VALUE vpkey, vcols, vrv, vkeys, vkey, vval;
3176
+ int i, rv, num;
3177
+ vpkey = rb_str_new(pkbuf, pksiz);
3178
+ vcols = maptovhash(cols);
3179
+ vrv = rb_yield_values(2, vpkey, vcols);
3180
+ rv = (vrv == Qnil) ? 0 : NUM2INT(vrv);
3181
+ if(rv & TDBQPPUT){
3182
+ tcmapclear(cols);
3183
+ vkeys = rb_funcall(vcols, rb_intern("keys"), 0);
3184
+ num = RARRAY_LEN(vkeys);
3185
+ for(i = 0; i < num; i++){
3186
+ vkey = rb_ary_entry(vkeys, i);
3187
+ vval = rb_hash_aref(vcols, vkey);
3188
+ vkey = StringValueEx(vkey);
3189
+ vval = StringValueEx(vval);
3190
+ tcmapput(cols, RSTRING_PTR(vkey), RSTRING_LEN(vkey), RSTRING_PTR(vval), RSTRING_LEN(vval));
3191
+ }
3192
+ }
3193
+ return rv;
3194
+ }
3195
+
3196
+
3197
+ static VALUE tdbqry_initialize(VALUE vself, VALUE vtdb){
3198
+ VALUE vqry;
3199
+ TCTDB *tdb;
3200
+ TDBQRY *qry;
3201
+ Check_Type(vtdb, T_OBJECT);
3202
+ vtdb = rb_iv_get(vtdb, TDBVNDATA);
3203
+ Data_Get_Struct(vtdb, TCTDB, tdb);
3204
+ qry = tctdbqrynew(tdb);
3205
+ vqry = Data_Wrap_Struct(cls_tdbqry_data, 0, tctdbqrydel, qry);
3206
+ rb_iv_set(vself, TDBQRYVNDATA, vqry);
3207
+ rb_iv_set(vself, TDBVNDATA, vtdb);
3208
+ return Qnil;
3209
+ }
3210
+
3211
+
3212
+ static VALUE tdbqry_addcond(VALUE vself, VALUE vname, VALUE vop, VALUE vexpr){
3213
+ VALUE vqry;
3214
+ TDBQRY *qry;
3215
+ vname = StringValueEx(vname);
3216
+ vexpr = StringValueEx(vexpr);
3217
+ vqry = rb_iv_get(vself, TDBQRYVNDATA);
3218
+ Data_Get_Struct(vqry, TDBQRY, qry);
3219
+ tctdbqryaddcond(qry, RSTRING_PTR(vname), NUM2INT(vop), RSTRING_PTR(vexpr));
3220
+ return Qnil;
3221
+ }
3222
+
3223
+
3224
+ static VALUE tdbqry_setorder(int argc, VALUE *argv, VALUE vself){
3225
+ VALUE vqry, vname, vtype;
3226
+ TDBQRY *qry;
3227
+ int type;
3228
+ rb_scan_args(argc, argv, "11", &vname, &vtype);
3229
+ vname = StringValueEx(vname);
3230
+ type = (vtype == Qnil) ? TDBQOSTRASC : NUM2INT(vtype);
3231
+ vqry = rb_iv_get(vself, TDBQRYVNDATA);
3232
+ Data_Get_Struct(vqry, TDBQRY, qry);
3233
+ tctdbqrysetorder(qry, RSTRING_PTR(vname), type);
3234
+ return Qnil;
3235
+ }
3236
+
3237
+
3238
+ static VALUE tdbqry_setlimit(int argc, VALUE *argv, VALUE vself){
3239
+ VALUE vqry, vmax, vskip;
3240
+ TDBQRY *qry;
3241
+ int max, skip;
3242
+ rb_scan_args(argc, argv, "02", &vmax, &vskip);
3243
+ max = (vmax == Qnil) ? -1 : NUM2INT(vmax);
3244
+ skip = (vskip == Qnil) ? -1 : NUM2INT(vskip);
3245
+ vqry = rb_iv_get(vself, TDBQRYVNDATA);
3246
+ Data_Get_Struct(vqry, TDBQRY, qry);
3247
+ tctdbqrysetlimit(qry, max, skip);
3248
+ return Qnil;
3249
+ }
3250
+
3251
+
3252
+ static VALUE tdbqry_search(VALUE vself){
3253
+ VALUE vqry, vary;
3254
+ TDBQRY *qry;
3255
+ TCLIST *res;
3256
+ vqry = rb_iv_get(vself, TDBQRYVNDATA);
3257
+ Data_Get_Struct(vqry, TDBQRY, qry);
3258
+ res = tctdbqrysearch(qry);
3259
+ vary = listtovary(res);
3260
+ tclistdel(res);
3261
+ return vary;
3262
+ }
3263
+
3264
+
3265
+ static VALUE tdbqry_searchout(VALUE vself){
3266
+ VALUE vqry;
3267
+ TDBQRY *qry;
3268
+ vqry = rb_iv_get(vself, TDBQRYVNDATA);
3269
+ Data_Get_Struct(vqry, TDBQRY, qry);
3270
+ return tctdbqrysearchout(qry) ? Qtrue : Qfalse;
3271
+ }
3272
+
3273
+
3274
+ static VALUE tdbqry_proc(VALUE vself, VALUE vproc){
3275
+ VALUE vqry;
3276
+ TDBQRY *qry;
3277
+ if(rb_block_given_p() != Qtrue) rb_raise(rb_eArgError, "no block given");
3278
+ vqry = rb_iv_get(vself, TDBQRYVNDATA);
3279
+ Data_Get_Struct(vqry, TDBQRY, qry);
3280
+ return tctdbqryproc(qry, (TDBQRYPROC)tdbqry_procrec, NULL) ? Qtrue : Qfalse;
3281
+ }
3282
+
3283
+
3284
+ static VALUE tdbqry_hint(VALUE vself){
3285
+ VALUE vqry;
3286
+ TDBQRY *qry;
3287
+ vqry = rb_iv_get(vself, TDBQRYVNDATA);
3288
+ Data_Get_Struct(vqry, TDBQRY, qry);
3289
+ return rb_str_new2(tctdbqryhint(qry));
3290
+ }
3291
+
3292
+
3293
+ static VALUE tdbqry_metasearch(int argc, VALUE *argv, VALUE vself){
3294
+ VALUE vqry, vothers, vtype, voqry, vary;
3295
+ TDBQRY *qry, **qrys;
3296
+ TCLIST *res;
3297
+ int i, type, num, qnum;
3298
+ rb_scan_args(argc, argv, "11", &vothers, &vtype);
3299
+ Check_Type(vothers, T_ARRAY);
3300
+ type = (vtype == Qnil) ? TDBMSUNION : NUM2INT(vtype);
3301
+ vqry = rb_iv_get(vself, TDBQRYVNDATA);
3302
+ Data_Get_Struct(vqry, TDBQRY, qry);
3303
+ num = RARRAY_LEN(vothers);
3304
+ qrys = tcmalloc(sizeof(*qrys) * (num + 1));
3305
+ qnum = 0;
3306
+ qrys[qnum++] = qry;
3307
+ for(i = 0; i < num; i++){
3308
+ voqry = rb_ary_entry(vothers, i);
3309
+ if(rb_obj_is_instance_of(voqry, cls_tdbqry) == Qtrue){
3310
+ voqry = rb_iv_get(voqry, TDBQRYVNDATA);
3311
+ Data_Get_Struct(voqry, TDBQRY, qrys[qnum++]);
3312
+ }
3313
+ }
3314
+ res = tctdbmetasearch(qrys, qnum, type);
3315
+ vary = listtovary(res);
3316
+ tcfree(qrys);
3317
+ tclistdel(res);
3318
+ return vary;
3319
+ }
3320
+
3321
+
3322
+ static VALUE tdbqry_kwic(int argc, VALUE *argv, VALUE vself){
3323
+ VALUE vqry, vcols, vname, vwidth, vopts, vval, vary;
3324
+ TDBQRY *qry;
3325
+ TCMAP *cols;
3326
+ const char *name;
3327
+ int width, opts;
3328
+ rb_scan_args(argc, argv, "13", &vcols, &vname, &vwidth, &vopts);
3329
+ Check_Type(vcols, T_HASH);
3330
+ width = (vwidth == Qnil) ? -1 : NUM2INT(vwidth);
3331
+ opts = (vopts == Qnil) ? 0 : NUM2INT(vopts);
3332
+ if(vname == Qnil){
3333
+ cols = vhashtomap(vcols);
3334
+ name = NULL;
3335
+ } else {
3336
+ vname = StringValueEx(vname);
3337
+ cols = tcmapnew2(1);
3338
+ vval = rb_hash_aref(vcols, vname);
3339
+ if(vval != Qnil) tcmapput(cols, RSTRING_PTR(vname), RSTRING_LEN(vname),
3340
+ RSTRING_PTR(vval), RSTRING_LEN(vval));
3341
+ name = RSTRING_PTR(vname);
3342
+ }
3343
+ if(width < 0){
3344
+ width = 1 << 30;
3345
+ opts |= TCKWNOOVER | TCKWPULEAD;
3346
+ }
3347
+ vqry = rb_iv_get(vself, TDBQRYVNDATA);
3348
+ Data_Get_Struct(vqry, TDBQRY, qry);
3349
+ TCLIST *texts = tctdbqrykwic(qry, cols, name, width, opts);
3350
+ vary = listtovary(texts);
3351
+ tclistdel(texts);
3352
+ tcmapdel(cols);
3353
+ return vary;
3354
+ }
3355
+
3356
+
3357
+ static void adb_init(void){
3358
+ cls_adb = rb_define_class_under(mod_tokyocabinet, "ADB", rb_cObject);
3359
+ cls_adb_data = rb_define_class_under(mod_tokyocabinet, "ADB_data", rb_cObject);
3360
+ rb_define_private_method(cls_adb, "initialize", adb_initialize, 0);
3361
+ rb_define_method(cls_adb, "open", adb_open, 1);
3362
+ rb_define_method(cls_adb, "close", adb_close, 0);
3363
+ rb_define_method(cls_adb, "put", adb_put, 2);
3364
+ rb_define_method(cls_adb, "putkeep", adb_putkeep, 2);
3365
+ rb_define_method(cls_adb, "putcat", adb_putcat, 2);
3366
+ rb_define_method(cls_adb, "out", adb_out, 1);
3367
+ rb_define_method(cls_adb, "get", adb_get, 1);
3368
+ rb_define_method(cls_adb, "vsiz", adb_vsiz, 1);
3369
+ rb_define_method(cls_adb, "iterinit", adb_iterinit, 0);
3370
+ rb_define_method(cls_adb, "iternext", adb_iternext, 0);
3371
+ rb_define_method(cls_adb, "fwmkeys", adb_fwmkeys, -1);
3372
+ rb_define_method(cls_adb, "addint", adb_addint, 2);
3373
+ rb_define_method(cls_adb, "adddouble", adb_adddouble, 2);
3374
+ rb_define_method(cls_adb, "sync", adb_sync, 0);
3375
+ rb_define_method(cls_adb, "optimize", adb_optimize, -1);
3376
+ rb_define_method(cls_adb, "vanish", adb_vanish, 0);
3377
+ rb_define_method(cls_adb, "copy", adb_copy, 1);
3378
+ rb_define_method(cls_adb, "tranbegin", adb_tranbegin, 0);
3379
+ rb_define_method(cls_adb, "trancommit", adb_trancommit, 0);
3380
+ rb_define_method(cls_adb, "tranabort", adb_tranabort, 0);
3381
+ rb_define_method(cls_adb, "path", adb_path, 0);
3382
+ rb_define_method(cls_adb, "rnum", adb_rnum, 0);
3383
+ rb_define_method(cls_adb, "size", adb_size, 0);
3384
+ rb_define_method(cls_adb, "misc", adb_misc, -1);
3385
+ rb_define_method(cls_adb, "[]", adb_get, 1);
3386
+ rb_define_method(cls_adb, "[]=", adb_put, 2);
3387
+ rb_define_method(cls_adb, "store", adb_put, 2);
3388
+ rb_define_method(cls_adb, "delete", adb_out, 1);
3389
+ rb_define_method(cls_adb, "fetch", adb_fetch, -1);
3390
+ rb_define_method(cls_adb, "has_key?", adb_check, 1);
3391
+ rb_define_method(cls_adb, "key?", adb_check, 1);
3392
+ rb_define_method(cls_adb, "include?", adb_check, 1);
3393
+ rb_define_method(cls_adb, "member?", adb_check, 1);
3394
+ rb_define_method(cls_adb, "has_value?", adb_check_value, 1);
3395
+ rb_define_method(cls_adb, "value?", adb_check_value, 1);
3396
+ rb_define_method(cls_adb, "key", adb_get_reverse, 1);
3397
+ rb_define_method(cls_adb, "clear", adb_vanish, 0);
3398
+ rb_define_method(cls_adb, "size", adb_rnum, 0);
3399
+ rb_define_method(cls_adb, "length", adb_rnum, 0);
3400
+ rb_define_method(cls_adb, "empty?", adb_empty, 0);
3401
+ rb_define_method(cls_adb, "each", adb_each, 0);
3402
+ rb_define_method(cls_adb, "each_pair", adb_each, 0);
3403
+ rb_define_method(cls_adb, "each_key", adb_each_key, 0);
3404
+ rb_define_method(cls_adb, "each_value", adb_each_value, 0);
3405
+ rb_define_method(cls_adb, "keys", adb_keys, 0);
3406
+ rb_define_method(cls_adb, "values", adb_values, 0);
3407
+ }
3408
+
3409
+
3410
+ static VALUE adb_initialize(VALUE vself){
3411
+ VALUE vadb;
3412
+ TCADB *adb;
3413
+ adb = tcadbnew();
3414
+ vadb = Data_Wrap_Struct(cls_adb_data, 0, tcadbdel, adb);
3415
+ rb_iv_set(vself, ADBVNDATA, vadb);
3416
+ return Qnil;
3417
+ }
3418
+
3419
+
3420
+ static VALUE adb_open(VALUE vself, VALUE vname){
3421
+ VALUE vadb;
3422
+ TCADB *adb;
3423
+ Check_Type(vname, T_STRING);
3424
+ vadb = rb_iv_get(vself, ADBVNDATA);
3425
+ Data_Get_Struct(vadb, TCADB, adb);
3426
+ return tcadbopen(adb, RSTRING_PTR(vname)) ? Qtrue : Qfalse;
3427
+ }
3428
+
3429
+
3430
+ static VALUE adb_close(VALUE vself){
3431
+ VALUE vadb;
3432
+ TCADB *adb;
3433
+ vadb = rb_iv_get(vself, ADBVNDATA);
3434
+ Data_Get_Struct(vadb, TCADB, adb);
3435
+ return tcadbclose(adb) ? Qtrue : Qfalse;
3436
+ }
3437
+
3438
+
3439
+ static VALUE adb_put(VALUE vself, VALUE vkey, VALUE vval){
3440
+ VALUE vadb;
3441
+ TCADB *adb;
3442
+ vkey = StringValueEx(vkey);
3443
+ vval = StringValueEx(vval);
3444
+ vadb = rb_iv_get(vself, ADBVNDATA);
3445
+ Data_Get_Struct(vadb, TCADB, adb);
3446
+ return tcadbput(adb, RSTRING_PTR(vkey), RSTRING_LEN(vkey),
3447
+ RSTRING_PTR(vval), RSTRING_LEN(vval)) ? Qtrue : Qfalse;
3448
+ }
3449
+
3450
+
3451
+ static VALUE adb_putkeep(VALUE vself, VALUE vkey, VALUE vval){
3452
+ VALUE vadb;
3453
+ TCADB *adb;
3454
+ vkey = StringValueEx(vkey);
3455
+ vval = StringValueEx(vval);
3456
+ vadb = rb_iv_get(vself, ADBVNDATA);
3457
+ Data_Get_Struct(vadb, TCADB, adb);
3458
+ return tcadbputkeep(adb, RSTRING_PTR(vkey), RSTRING_LEN(vkey),
3459
+ RSTRING_PTR(vval), RSTRING_LEN(vval)) ? Qtrue : Qfalse;
3460
+ }
3461
+
3462
+
3463
+ static VALUE adb_putcat(VALUE vself, VALUE vkey, VALUE vval){
3464
+ VALUE vadb;
3465
+ TCADB *adb;
3466
+ vkey = StringValueEx(vkey);
3467
+ vval = StringValueEx(vval);
3468
+ vadb = rb_iv_get(vself, ADBVNDATA);
3469
+ Data_Get_Struct(vadb, TCADB, adb);
3470
+ return tcadbputcat(adb, RSTRING_PTR(vkey), RSTRING_LEN(vkey),
3471
+ RSTRING_PTR(vval), RSTRING_LEN(vval)) ? Qtrue : Qfalse;
3472
+ }
3473
+
3474
+
3475
+ static VALUE adb_out(VALUE vself, VALUE vkey){
3476
+ VALUE vadb;
3477
+ TCADB *adb;
3478
+ vkey = StringValueEx(vkey);
3479
+ vadb = rb_iv_get(vself, ADBVNDATA);
3480
+ Data_Get_Struct(vadb, TCADB, adb);
3481
+ return tcadbout(adb, RSTRING_PTR(vkey), RSTRING_LEN(vkey)) ? Qtrue : Qfalse;
3482
+ }
3483
+
3484
+
3485
+ static VALUE adb_get(VALUE vself, VALUE vkey){
3486
+ VALUE vadb, vval;
3487
+ TCADB *adb;
3488
+ char *vbuf;
3489
+ int vsiz;
3490
+ vkey = StringValueEx(vkey);
3491
+ vadb = rb_iv_get(vself, ADBVNDATA);
3492
+ Data_Get_Struct(vadb, TCADB, adb);
3493
+ if(!(vbuf = tcadbget(adb, RSTRING_PTR(vkey), RSTRING_LEN(vkey), &vsiz))) return Qnil;
3494
+ vval = rb_str_new(vbuf, vsiz);
3495
+ tcfree(vbuf);
3496
+ return vval;
3497
+ }
3498
+
3499
+
3500
+ static VALUE adb_vsiz(VALUE vself, VALUE vkey){
3501
+ VALUE vadb;
3502
+ TCADB *adb;
3503
+ vkey = StringValueEx(vkey);
3504
+ vadb = rb_iv_get(vself, ADBVNDATA);
3505
+ Data_Get_Struct(vadb, TCADB, adb);
3506
+ return INT2NUM(tcadbvsiz(adb, RSTRING_PTR(vkey), RSTRING_LEN(vkey)));
3507
+ }
3508
+
3509
+
3510
+ static VALUE adb_iterinit(VALUE vself){
3511
+ VALUE vadb;
3512
+ TCADB *adb;
3513
+ vadb = rb_iv_get(vself, ADBVNDATA);
3514
+ Data_Get_Struct(vadb, TCADB, adb);
3515
+ return tcadbiterinit(adb) ? Qtrue : Qfalse;
3516
+ }
3517
+
3518
+
3519
+ static VALUE adb_iternext(VALUE vself){
3520
+ VALUE vadb, vval;
3521
+ TCADB *adb;
3522
+ char *vbuf;
3523
+ int vsiz;
3524
+ vadb = rb_iv_get(vself, ADBVNDATA);
3525
+ Data_Get_Struct(vadb, TCADB, adb);
3526
+ if(!(vbuf = tcadbiternext(adb, &vsiz))) return Qnil;
3527
+ vval = rb_str_new(vbuf, vsiz);
3528
+ tcfree(vbuf);
3529
+ return vval;
3530
+ }
3531
+
3532
+
3533
+ static VALUE adb_fwmkeys(int argc, VALUE *argv, VALUE vself){
3534
+ VALUE vadb, vprefix, vmax, vary;
3535
+ TCADB *adb;
3536
+ TCLIST *keys;
3537
+ int max;
3538
+ rb_scan_args(argc, argv, "11", &vprefix, &vmax);
3539
+ vprefix = StringValueEx(vprefix);
3540
+ max = (vmax == Qnil) ? -1 : NUM2INT(vmax);
3541
+ vadb = rb_iv_get(vself, ADBVNDATA);
3542
+ Data_Get_Struct(vadb, TCADB, adb);
3543
+ keys = tcadbfwmkeys(adb, RSTRING_PTR(vprefix), RSTRING_LEN(vprefix), max);
3544
+ vary = listtovary(keys);
3545
+ tclistdel(keys);
3546
+ return vary;
3547
+ }
3548
+
3549
+
3550
+ static VALUE adb_addint(VALUE vself, VALUE vkey, VALUE vnum){
3551
+ VALUE vadb;
3552
+ TCADB *adb;
3553
+ int num;
3554
+ vkey = StringValueEx(vkey);
3555
+ vadb = rb_iv_get(vself, ADBVNDATA);
3556
+ Data_Get_Struct(vadb, TCADB, adb);
3557
+ num = tcadbaddint(adb, RSTRING_PTR(vkey), RSTRING_LEN(vkey), NUM2INT(vnum));
3558
+ return num == INT_MIN ? Qnil : INT2NUM(num);
3559
+ }
3560
+
3561
+
3562
+ static VALUE adb_adddouble(VALUE vself, VALUE vkey, VALUE vnum){
3563
+ VALUE vadb;
3564
+ TCADB *adb;
3565
+ double num;
3566
+ vkey = StringValueEx(vkey);
3567
+ vadb = rb_iv_get(vself, ADBVNDATA);
3568
+ Data_Get_Struct(vadb, TCADB, adb);
3569
+ num = tcadbadddouble(adb, RSTRING_PTR(vkey), RSTRING_LEN(vkey), NUM2DBL(vnum));
3570
+ return isnan(num) ? Qnil : rb_float_new(num);
3571
+ }
3572
+
3573
+
3574
+ static VALUE adb_sync(VALUE vself){
3575
+ VALUE vadb;
3576
+ TCADB *adb;
3577
+ vadb = rb_iv_get(vself, ADBVNDATA);
3578
+ Data_Get_Struct(vadb, TCADB, adb);
3579
+ return tcadbsync(adb) ? Qtrue : Qfalse;
3580
+ }
3581
+
3582
+
3583
+ static VALUE adb_optimize(int argc, VALUE *argv, VALUE vself){
3584
+ VALUE vadb, vparams;
3585
+ TCADB *adb;
3586
+ const char *params;
3587
+ rb_scan_args(argc, argv, "01", &vparams);
3588
+ if(vparams == Qnil){
3589
+ params = NULL;
3590
+ } else {
3591
+ Check_Type(vparams, T_STRING);
3592
+ params = RSTRING_PTR(vparams);
3593
+ }
3594
+ vadb = rb_iv_get(vself, ADBVNDATA);
3595
+ Data_Get_Struct(vadb, TCADB, adb);
3596
+ return tcadboptimize(adb, params) ? Qtrue : Qfalse;
3597
+ }
3598
+
3599
+
3600
+ static VALUE adb_vanish(VALUE vself){
3601
+ VALUE vadb;
3602
+ TCADB *adb;
3603
+ vadb = rb_iv_get(vself, ADBVNDATA);
3604
+ Data_Get_Struct(vadb, TCADB, adb);
3605
+ return tcadbvanish(adb) ? Qtrue : Qfalse;
3606
+ }
3607
+
3608
+
3609
+ static VALUE adb_copy(VALUE vself, VALUE vpath){
3610
+ VALUE vadb;
3611
+ TCADB *adb;
3612
+ Check_Type(vpath, T_STRING);
3613
+ vadb = rb_iv_get(vself, ADBVNDATA);
3614
+ Data_Get_Struct(vadb, TCADB, adb);
3615
+ return tcadbcopy(adb, RSTRING_PTR(vpath)) ? Qtrue : Qfalse;
3616
+ }
3617
+
3618
+
3619
+ static VALUE adb_tranbegin(VALUE vself){
3620
+ VALUE vadb;
3621
+ TCADB *adb;
3622
+ vadb = rb_iv_get(vself, ADBVNDATA);
3623
+ Data_Get_Struct(vadb, TCADB, adb);
3624
+ return tcadbtranbegin(adb) ? Qtrue : Qfalse;
3625
+ }
3626
+
3627
+
3628
+ static VALUE adb_trancommit(VALUE vself){
3629
+ VALUE vadb;
3630
+ TCADB *adb;
3631
+ vadb = rb_iv_get(vself, ADBVNDATA);
3632
+ Data_Get_Struct(vadb, TCADB, adb);
3633
+ return tcadbtrancommit(adb) ? Qtrue : Qfalse;
3634
+ }
3635
+
3636
+
3637
+ static VALUE adb_tranabort(VALUE vself){
3638
+ VALUE vadb;
3639
+ TCADB *adb;
3640
+ vadb = rb_iv_get(vself, ADBVNDATA);
3641
+ Data_Get_Struct(vadb, TCADB, adb);
3642
+ return tcadbtranabort(adb) ? Qtrue : Qfalse;
3643
+ }
3644
+
3645
+
3646
+ static VALUE adb_path(VALUE vself){
3647
+ VALUE vadb;
3648
+ TCADB *adb;
3649
+ const char *path;
3650
+ vadb = rb_iv_get(vself, ADBVNDATA);
3651
+ Data_Get_Struct(vadb, TCADB, adb);
3652
+ if(!(path = tcadbpath(adb))) return Qnil;
3653
+ return rb_str_new2(path);
3654
+ }
3655
+
3656
+
3657
+ static VALUE adb_rnum(VALUE vself){
3658
+ VALUE vadb;
3659
+ TCADB *adb;
3660
+ vadb = rb_iv_get(vself, ADBVNDATA);
3661
+ Data_Get_Struct(vadb, TCADB, adb);
3662
+ return LL2NUM(tcadbrnum(adb));
3663
+ }
3664
+
3665
+
3666
+ static VALUE adb_size(VALUE vself){
3667
+ VALUE vadb;
3668
+ TCADB *adb;
3669
+ vadb = rb_iv_get(vself, ADBVNDATA);
3670
+ Data_Get_Struct(vadb, TCADB, adb);
3671
+ return LL2NUM(tcadbsize(adb));
3672
+ }
3673
+
3674
+
3675
+ static VALUE adb_misc(int argc, VALUE *argv, VALUE vself){
3676
+ VALUE vadb, vname, vargs, vrv;
3677
+ TCADB *adb;
3678
+ TCLIST *targs, *res;
3679
+ rb_scan_args(argc, argv, "11", &vname, &vargs);
3680
+ vname = StringValueEx(vname);
3681
+ if(vargs == Qnil){
3682
+ targs = tclistnew2(1);
3683
+ } else {
3684
+ Check_Type(vargs, T_ARRAY);
3685
+ targs = varytolist(vargs);
3686
+ }
3687
+ vadb = rb_iv_get(vself, ADBVNDATA);
3688
+ Data_Get_Struct(vadb, TCADB, adb);
3689
+ res = tcadbmisc(adb, RSTRING_PTR(vname), targs);
3690
+ if(res){
3691
+ vrv = listtovary(res);
3692
+ tclistdel(res);
3693
+ } else {
3694
+ vrv = Qnil;
3695
+ }
3696
+ tclistdel(targs);
3697
+ return vrv;
3698
+ }
3699
+
3700
+
3701
+ static VALUE adb_fetch(int argc, VALUE *argv, VALUE vself){
3702
+ VALUE vadb, vkey, vdef, vval;
3703
+ TCADB *adb;
3704
+ char *vbuf;
3705
+ int vsiz;
3706
+ rb_scan_args(argc, argv, "11", &vkey, &vdef);
3707
+ vkey = StringValueEx(vkey);
3708
+ vadb = rb_iv_get(vself, ADBVNDATA);
3709
+ Data_Get_Struct(vadb, TCADB, adb);
3710
+ if((vbuf = tcadbget(adb, RSTRING_PTR(vkey), RSTRING_LEN(vkey), &vsiz)) != NULL){
3711
+ vval = rb_str_new(vbuf, vsiz);
3712
+ tcfree(vbuf);
3713
+ } else {
3714
+ vval = vdef;
3715
+ }
3716
+ return vval;
3717
+ }
3718
+
3719
+
3720
+ static VALUE adb_check(VALUE vself, VALUE vkey){
3721
+ VALUE vadb;
3722
+ TCADB *adb;
3723
+ vkey = StringValueEx(vkey);
3724
+ vadb = rb_iv_get(vself, ADBVNDATA);
3725
+ Data_Get_Struct(vadb, TCADB, adb);
3726
+ return tcadbvsiz(adb, RSTRING_PTR(vkey), RSTRING_LEN(vkey)) >= 0 ? Qtrue : Qfalse;
3727
+ }
3728
+
3729
+
3730
+ static VALUE adb_check_value(VALUE vself, VALUE vval){
3731
+ VALUE vadb;
3732
+ TCADB *adb;
3733
+ char *tkbuf, *tvbuf;
3734
+ bool hit;
3735
+ int tksiz, tvsiz;
3736
+ vval = StringValueEx(vval);
3737
+ vadb = rb_iv_get(vself, ADBVNDATA);
3738
+ Data_Get_Struct(vadb, TCADB, adb);
3739
+ hit = false;
3740
+ tcadbiterinit(adb);
3741
+ while((tkbuf = tcadbiternext(adb, &tksiz)) != NULL){
3742
+ tvbuf = tcadbget(adb, tkbuf, tksiz, &tvsiz);
3743
+ if(tvbuf && tvsiz == RSTRING_LEN(vval) &&
3744
+ memcmp(tvbuf, RSTRING_PTR(vval), RSTRING_LEN(vval)) == 0){
3745
+ tcfree(tvbuf);
3746
+ tcfree(tkbuf);
3747
+ hit = true;
3748
+ break;
3749
+ }
3750
+ tcfree(tvbuf);
3751
+ tcfree(tkbuf);
3752
+ }
3753
+ return hit ? Qtrue : Qfalse;
3754
+ }
3755
+
3756
+
3757
+ static VALUE adb_get_reverse(VALUE vself, VALUE vval){
3758
+ VALUE vadb, vrv;
3759
+ TCADB *adb;
3760
+ char *tkbuf, *tvbuf;
3761
+ int tksiz, tvsiz;
3762
+ vval = StringValueEx(vval);
3763
+ vadb = rb_iv_get(vself, ADBVNDATA);
3764
+ Data_Get_Struct(vadb, TCADB, adb);
3765
+ vrv = Qnil;
3766
+ tcadbiterinit(adb);
3767
+ while((tkbuf = tcadbiternext(adb, &tksiz)) != NULL){
3768
+ tvbuf = tcadbget(adb, tkbuf, tksiz, &tvsiz);
3769
+ if(tvbuf && tvsiz == RSTRING_LEN(vval) &&
3770
+ memcmp(tvbuf, RSTRING_PTR(vval), RSTRING_LEN(vval)) == 0){
3771
+ vrv = rb_str_new(tkbuf, tksiz);
3772
+ tcfree(tvbuf);
3773
+ tcfree(tkbuf);
3774
+ break;
3775
+ }
3776
+ tcfree(tvbuf);
3777
+ tcfree(tkbuf);
3778
+ }
3779
+ return vrv;
3780
+ }
3781
+
3782
+
3783
+ static VALUE adb_empty(VALUE vself){
3784
+ VALUE vadb;
3785
+ TCADB *adb;
3786
+ vadb = rb_iv_get(vself, ADBVNDATA);
3787
+ Data_Get_Struct(vadb, TCADB, adb);
3788
+ return tcadbrnum(adb) < 1 ? Qtrue : Qfalse;
3789
+ }
3790
+
3791
+
3792
+ static VALUE adb_each(VALUE vself){
3793
+ VALUE vadb, vrv;
3794
+ TCADB *adb;
3795
+ char *tkbuf, *tvbuf;
3796
+ int tksiz, tvsiz;
3797
+ if(rb_block_given_p() != Qtrue) rb_raise(rb_eArgError, "no block given");
3798
+ vadb = rb_iv_get(vself, ADBVNDATA);
3799
+ Data_Get_Struct(vadb, TCADB, adb);
3800
+ vrv = Qnil;
3801
+ tcadbiterinit(adb);
3802
+ while((tkbuf = tcadbiternext(adb, &tksiz)) != NULL){
3803
+ tvbuf = tcadbget(adb, tkbuf, tksiz, &tvsiz);
3804
+ if(tvbuf){
3805
+ vrv = rb_yield_values(2, rb_str_new(tkbuf, tksiz), rb_str_new(tvbuf, tvsiz));
3806
+ tcfree(tvbuf);
3807
+ }
3808
+ tcfree(tkbuf);
3809
+ }
3810
+ return vrv;
3811
+ }
3812
+
3813
+
3814
+ static VALUE adb_each_key(VALUE vself){
3815
+ VALUE vadb, vrv;
3816
+ TCADB *adb;
3817
+ char *tkbuf;
3818
+ int tksiz;
3819
+ if(rb_block_given_p() != Qtrue) rb_raise(rb_eArgError, "no block given");
3820
+ vadb = rb_iv_get(vself, ADBVNDATA);
3821
+ Data_Get_Struct(vadb, TCADB, adb);
3822
+ vrv = Qnil;
3823
+ tcadbiterinit(adb);
3824
+ while((tkbuf = tcadbiternext(adb, &tksiz)) != NULL){
3825
+ vrv = rb_yield(rb_str_new(tkbuf, tksiz));
3826
+ tcfree(tkbuf);
3827
+ }
3828
+ return vrv;
3829
+ }
3830
+
3831
+
3832
+ static VALUE adb_each_value(VALUE vself){
3833
+ VALUE vadb, vrv;
3834
+ TCADB *adb;
3835
+ char *tkbuf, *tvbuf;
3836
+ int tksiz, tvsiz;
3837
+ if(rb_block_given_p() != Qtrue) rb_raise(rb_eArgError, "no block given");
3838
+ vadb = rb_iv_get(vself, ADBVNDATA);
3839
+ Data_Get_Struct(vadb, TCADB, adb);
3840
+ vrv = Qnil;
3841
+ tcadbiterinit(adb);
3842
+ while((tkbuf = tcadbiternext(adb, &tksiz)) != NULL){
3843
+ tvbuf = tcadbget(adb, tkbuf, tksiz, &tvsiz);
3844
+ if(tvbuf){
3845
+ vrv = rb_yield(rb_str_new(tvbuf, tvsiz));
3846
+ tcfree(tvbuf);
3847
+ }
3848
+ tcfree(tkbuf);
3849
+ }
3850
+ return vrv;
3851
+ }
3852
+
3853
+
3854
+ static VALUE adb_keys(VALUE vself){
3855
+ VALUE vadb, vary;
3856
+ TCADB *adb;
3857
+ char *tkbuf;
3858
+ int tksiz;
3859
+ vadb = rb_iv_get(vself, ADBVNDATA);
3860
+ Data_Get_Struct(vadb, TCADB, adb);
3861
+ vary = rb_ary_new2(tcadbrnum(adb));
3862
+ tcadbiterinit(adb);
3863
+ while((tkbuf = tcadbiternext(adb, &tksiz)) != NULL){
3864
+ rb_ary_push(vary, rb_str_new(tkbuf, tksiz));
3865
+ tcfree(tkbuf);
3866
+ }
3867
+ return vary;
3868
+ }
3869
+
3870
+
3871
+ static VALUE adb_values(VALUE vself){
3872
+ VALUE vadb, vary;
3873
+ TCADB *adb;
3874
+ char *tkbuf, *tvbuf;
3875
+ int tksiz, tvsiz;
3876
+ vadb = rb_iv_get(vself, ADBVNDATA);
3877
+ Data_Get_Struct(vadb, TCADB, adb);
3878
+ vary = rb_ary_new2(tcadbrnum(adb));
3879
+ tcadbiterinit(adb);
3880
+ while((tkbuf = tcadbiternext(adb, &tksiz)) != NULL){
3881
+ tvbuf = tcadbget(adb, tkbuf, tksiz, &tvsiz);
3882
+ if(tvbuf){
3883
+ rb_ary_push(vary, rb_str_new(tvbuf, tvsiz));
3884
+ tcfree(tvbuf);
3885
+ }
3886
+ tcfree(tkbuf);
3887
+ }
3888
+ return vary;
3889
+ }
3890
+
3891
+
3892
+
3893
+ /* END OF FILE */