-
Notifications
You must be signed in to change notification settings - Fork 2
/
tokyocabinet.c
3892 lines (3409 loc) · 117 KB
/
tokyocabinet.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*************************************************************************************************
* Ruby binding of Tokyo Cabinet
* Copyright (C) 2006-2010 FAL Labs
* This file is part of Tokyo Cabinet.
* Tokyo Cabinet is free software; you can redistribute it and/or modify it under the terms of
* the GNU Lesser General Public License as published by the Free Software Foundation; either
* version 2.1 of the License or any later version. Tokyo Cabinet is distributed in the hope
* that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
* You should have received a copy of the GNU Lesser General Public License along with Tokyo
* Cabinet; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA.
*************************************************************************************************/
#include "ruby.h"
#include <tcutil.h>
#include <tchdb.h>
#include <tcbdb.h>
#include <tcfdb.h>
#include <tctdb.h>
#include <tcadb.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <limits.h>
#include <math.h>
#define HDBVNDATA "@hdb"
#define BDBVNDATA "@bdb"
#define BDBCURVNDATA "@bdbcur"
#define FDBVNDATA "@fdb"
#define TDBVNDATA "@tdb"
#define TDBQRYVNDATA "@tdbqry"
#define ADBVNDATA "@adb"
#define NUMBUFSIZ 32
#if !defined(RSTRING_PTR)
#define RSTRING_PTR(TC_s) (RSTRING(TC_s)->ptr)
#endif
#if !defined(RSTRING_LEN)
#define RSTRING_LEN(TC_s) (RSTRING(TC_s)->len)
#endif
#if !defined(RARRAY_LEN)
#define RARRAY_LEN(TC_a) (RARRAY(TC_a)->len)
#endif
/* private function prototypes */
static VALUE StringValueEx(VALUE vobj);
static TCLIST *varytolist(VALUE vary);
static VALUE listtovary(TCLIST *list);
static TCMAP *vhashtomap(VALUE vhash);
static VALUE maptovhash(TCMAP *map);
static void hdb_init(void);
static VALUE hdb_initialize(VALUE vself);
static VALUE hdb_errmsg(int argc, VALUE *argv, VALUE vself);
static VALUE hdb_ecode(VALUE vself);
static VALUE hdb_tune(int argc, VALUE *argv, VALUE vself);
static VALUE hdb_setcache(int argc, VALUE *argv, VALUE vself);
static VALUE hdb_setxmsiz(int argc, VALUE *argv, VALUE vself);
static VALUE hdb_setdfunit(int argc, VALUE *argv, VALUE vself);
static VALUE hdb_open(int argc, VALUE *argv, VALUE vself);
static VALUE hdb_close(VALUE vself);
static VALUE hdb_put(VALUE vself, VALUE vkey, VALUE vval);
static VALUE hdb_putkeep(VALUE vself, VALUE vkey, VALUE vval);
static VALUE hdb_putcat(VALUE vself, VALUE vkey, VALUE vval);
static VALUE hdb_putasync(VALUE vself, VALUE vkey, VALUE vval);
static VALUE hdb_out(VALUE vself, VALUE vkey);
static VALUE hdb_get(VALUE vself, VALUE vkey);
static VALUE hdb_vsiz(VALUE vself, VALUE vkey);
static VALUE hdb_iterinit(VALUE vself);
static VALUE hdb_iternext(VALUE vself);
static VALUE hdb_fwmkeys(int argc, VALUE *argv, VALUE vself);
static VALUE hdb_addint(VALUE vself, VALUE vkey, VALUE vnum);
static VALUE hdb_adddouble(VALUE vself, VALUE vkey, VALUE vnum);
static VALUE hdb_sync(VALUE vself);
static VALUE hdb_optimize(int argc, VALUE *argv, VALUE vself);
static VALUE hdb_vanish(VALUE vself);
static VALUE hdb_copy(VALUE vself, VALUE vpath);
static VALUE hdb_tranbegin(VALUE vself);
static VALUE hdb_trancommit(VALUE vself);
static VALUE hdb_tranabort(VALUE vself);
static VALUE hdb_path(VALUE vself);
static VALUE hdb_rnum(VALUE vself);
static VALUE hdb_fsiz(VALUE vself);
static VALUE hdb_fetch(int argc, VALUE *argv, VALUE vself);
static VALUE hdb_check(VALUE vself, VALUE vkey);
static VALUE hdb_check_value(VALUE vself, VALUE vval);
static VALUE hdb_get_reverse(VALUE vself, VALUE vval);
static VALUE hdb_empty(VALUE vself);
static VALUE hdb_each(VALUE vself);
static VALUE hdb_each_key(VALUE vself);
static VALUE hdb_each_value(VALUE vself);
static VALUE hdb_keys(VALUE vself);
static VALUE hdb_values(VALUE vself);
static void bdb_init(void);
static int bdb_cmpobj(const char *aptr, int asiz, const char *bptr, int bsiz, VALUE vcmp);
static VALUE bdb_initialize(VALUE vself);
static VALUE bdb_errmsg(int argc, VALUE *argv, VALUE vself);
static VALUE bdb_ecode(VALUE vself);
static VALUE bdb_setcmpfunc(VALUE vself, VALUE vcmp);
static VALUE bdb_tune(int argc, VALUE *argv, VALUE vself);
static VALUE bdb_setcache(int argc, VALUE *argv, VALUE vself);
static VALUE bdb_setxmsiz(int argc, VALUE *argv, VALUE vself);
static VALUE bdb_setdfunit(int argc, VALUE *argv, VALUE vself);
static VALUE bdb_open(int argc, VALUE *argv, VALUE vself);
static VALUE bdb_close(VALUE vself);
static VALUE bdb_put(VALUE vself, VALUE vkey, VALUE vval);
static VALUE bdb_putkeep(VALUE vself, VALUE vkey, VALUE vval);
static VALUE bdb_putcat(VALUE vself, VALUE vkey, VALUE vval);
static VALUE bdb_putdup(VALUE vself, VALUE vkey, VALUE vval);
static VALUE bdb_putlist(VALUE vself, VALUE vkey, VALUE vvals);
static VALUE bdb_out(VALUE vself, VALUE vkey);
static VALUE bdb_outlist(VALUE vself, VALUE vkey);
static VALUE bdb_get(VALUE vself, VALUE vkey);
static VALUE bdb_getlist(VALUE vself, VALUE vkey);
static VALUE bdb_vnum(VALUE vself, VALUE vkey);
static VALUE bdb_vsiz(VALUE vself, VALUE vkey);
static VALUE bdb_range(int argc, VALUE *argv, VALUE vself);
static VALUE bdb_fwmkeys(int argc, VALUE *argv, VALUE vself);
static VALUE bdb_addint(VALUE vself, VALUE vkey, VALUE vnum);
static VALUE bdb_adddouble(VALUE vself, VALUE vkey, VALUE vnum);
static VALUE bdb_sync(VALUE vself);
static VALUE bdb_optimize(int argc, VALUE *argv, VALUE vself);
static VALUE bdb_vanish(VALUE vself);
static VALUE bdb_copy(VALUE vself, VALUE vpath);
static VALUE bdb_tranbegin(VALUE vself);
static VALUE bdb_trancommit(VALUE vself);
static VALUE bdb_tranabort(VALUE vself);
static VALUE bdb_path(VALUE vself);
static VALUE bdb_rnum(VALUE vself);
static VALUE bdb_fsiz(VALUE vself);
static VALUE bdb_fetch(int argc, VALUE *argv, VALUE vself);
static VALUE bdb_check(VALUE vself, VALUE vkey);
static VALUE bdb_check_value(VALUE vself, VALUE vval);
static VALUE bdb_get_reverse(VALUE vself, VALUE vval);
static VALUE bdb_empty(VALUE vself);
static VALUE bdb_each(VALUE vself);
static VALUE bdb_each_key(VALUE vself);
static VALUE bdb_each_value(VALUE vself);
static VALUE bdb_keys(VALUE vself);
static VALUE bdb_values(VALUE vself);
static void bdbcur_init(void);
static VALUE bdbcur_initialize(VALUE vself, VALUE vbdb);
static VALUE bdbcur_first(VALUE vself);
static VALUE bdbcur_last(VALUE vself);
static VALUE bdbcur_jump(VALUE vself, VALUE vkey);
static VALUE bdbcur_prev(VALUE vself);
static VALUE bdbcur_next(VALUE vself);
static VALUE bdbcur_put(int argc, VALUE *argv, VALUE vself);
static VALUE bdbcur_out(VALUE vself);
static VALUE bdbcur_key(VALUE vself);
static VALUE bdbcur_val(VALUE vself);
static void fdb_init(void);
static VALUE fdb_initialize(VALUE vself);
static VALUE fdb_errmsg(int argc, VALUE *argv, VALUE vself);
static VALUE fdb_ecode(VALUE vself);
static VALUE fdb_tune(int argc, VALUE *argv, VALUE vself);
static VALUE fdb_open(int argc, VALUE *argv, VALUE vself);
static VALUE fdb_close(VALUE vself);
static VALUE fdb_put(VALUE vself, VALUE vkey, VALUE vval);
static VALUE fdb_putkeep(VALUE vself, VALUE vkey, VALUE vval);
static VALUE fdb_putcat(VALUE vself, VALUE vkey, VALUE vval);
static VALUE fdb_out(VALUE vself, VALUE vkey);
static VALUE fdb_get(VALUE vself, VALUE vkey);
static VALUE fdb_vsiz(VALUE vself, VALUE vkey);
static VALUE fdb_iterinit(VALUE vself);
static VALUE fdb_iternext(VALUE vself);
static VALUE fdb_range(int argc, VALUE *argv, VALUE vself);
static VALUE fdb_addint(VALUE vself, VALUE vkey, VALUE vnum);
static VALUE fdb_adddouble(VALUE vself, VALUE vkey, VALUE vnum);
static VALUE fdb_sync(VALUE vself);
static VALUE fdb_optimize(int argc, VALUE *argv, VALUE vself);
static VALUE fdb_vanish(VALUE vself);
static VALUE fdb_copy(VALUE vself, VALUE vpath);
static VALUE fdb_tranbegin(VALUE vself);
static VALUE fdb_trancommit(VALUE vself);
static VALUE fdb_tranabort(VALUE vself);
static VALUE fdb_path(VALUE vself);
static VALUE fdb_rnum(VALUE vself);
static VALUE fdb_fsiz(VALUE vself);
static VALUE fdb_fetch(int argc, VALUE *argv, VALUE vself);
static VALUE fdb_check(VALUE vself, VALUE vkey);
static VALUE fdb_check_value(VALUE vself, VALUE vval);
static VALUE fdb_get_reverse(VALUE vself, VALUE vval);
static VALUE fdb_empty(VALUE vself);
static VALUE fdb_each(VALUE vself);
static VALUE fdb_each_key(VALUE vself);
static VALUE fdb_each_value(VALUE vself);
static VALUE fdb_keys(VALUE vself);
static VALUE fdb_values(VALUE vself);
static void tdb_init(void);
static VALUE tdb_initialize(VALUE vself);
static VALUE tdb_errmsg(int argc, VALUE *argv, VALUE vself);
static VALUE tdb_ecode(VALUE vself);
static VALUE tdb_tune(int argc, VALUE *argv, VALUE vself);
static VALUE tdb_setcache(int argc, VALUE *argv, VALUE vself);
static VALUE tdb_setxmsiz(int argc, VALUE *argv, VALUE vself);
static VALUE tdb_setdfunit(int argc, VALUE *argv, VALUE vself);
static VALUE tdb_open(int argc, VALUE *argv, VALUE vself);
static VALUE tdb_close(VALUE vself);
static VALUE tdb_put(VALUE vself, VALUE vkey, VALUE vcols);
static VALUE tdb_putkeep(VALUE vself, VALUE vkey, VALUE vcols);
static VALUE tdb_putcat(VALUE vself, VALUE vkey, VALUE vcols);
static VALUE tdb_out(VALUE vself, VALUE vkey);
static VALUE tdb_get(VALUE vself, VALUE vkey);
static VALUE tdb_vsiz(VALUE vself, VALUE vkey);
static VALUE tdb_iterinit(VALUE vself);
static VALUE tdb_iternext(VALUE vself);
static VALUE tdb_fwmkeys(int argc, VALUE *argv, VALUE vself);
static VALUE tdb_addint(VALUE vself, VALUE vkey, VALUE vnum);
static VALUE tdb_adddouble(VALUE vself, VALUE vkey, VALUE vnum);
static VALUE tdb_sync(VALUE vself);
static VALUE tdb_optimize(int argc, VALUE *argv, VALUE vself);
static VALUE tdb_vanish(VALUE vself);
static VALUE tdb_copy(VALUE vself, VALUE vpath);
static VALUE tdb_tranbegin(VALUE vself);
static VALUE tdb_trancommit(VALUE vself);
static VALUE tdb_tranabort(VALUE vself);
static VALUE tdb_path(VALUE vself);
static VALUE tdb_rnum(VALUE vself);
static VALUE tdb_fsiz(VALUE vself);
static VALUE tdb_setindex(VALUE vself, VALUE vname, VALUE vtype);
static VALUE tdb_genuid(VALUE vself);
static VALUE tdb_fetch(int argc, VALUE *argv, VALUE vself);
static VALUE tdb_check(VALUE vself, VALUE vkey);
static VALUE tdb_empty(VALUE vself);
static VALUE tdb_each(VALUE vself);
static VALUE tdb_each_key(VALUE vself);
static VALUE tdb_each_value(VALUE vself);
static VALUE tdb_keys(VALUE vself);
static VALUE tdb_values(VALUE vself);
static void tdbqry_init(void);
static int tdbqry_procrec(const void *pkbuf, int pksiz, TCMAP *cols, void *opq);
static VALUE tdbqry_initialize(VALUE vself, VALUE vtdb);
static VALUE tdbqry_addcond(VALUE vself, VALUE vname, VALUE vop, VALUE vexpr);
static VALUE tdbqry_setorder(int argc, VALUE *argv, VALUE vself);
static VALUE tdbqry_setlimit(int argc, VALUE *argv, VALUE vself);
static VALUE tdbqry_search(VALUE vself);
static VALUE tdbqry_searchout(VALUE vself);
static VALUE tdbqry_proc(VALUE vself, VALUE vproc);
static VALUE tdbqry_hint(VALUE vself);
static VALUE tdbqry_metasearch(int argc, VALUE *argv, VALUE vself);
static VALUE tdbqry_kwic(int argc, VALUE *argv, VALUE vself);
static void adb_init(void);
static VALUE adb_initialize(VALUE vself);
static VALUE adb_open(VALUE vself, VALUE vname);
static VALUE adb_close(VALUE vself);
static VALUE adb_put(VALUE vself, VALUE vkey, VALUE vval);
static VALUE adb_putkeep(VALUE vself, VALUE vkey, VALUE vval);
static VALUE adb_putcat(VALUE vself, VALUE vkey, VALUE vval);
static VALUE adb_out(VALUE vself, VALUE vkey);
static VALUE adb_get(VALUE vself, VALUE vkey);
static VALUE adb_vsiz(VALUE vself, VALUE vkey);
static VALUE adb_iterinit(VALUE vself);
static VALUE adb_iternext(VALUE vself);
static VALUE adb_fwmkeys(int argc, VALUE *argv, VALUE vself);
static VALUE adb_addint(VALUE vself, VALUE vkey, VALUE vnum);
static VALUE adb_adddouble(VALUE vself, VALUE vkey, VALUE vnum);
static VALUE adb_sync(VALUE vself);
static VALUE adb_optimize(int argc, VALUE *argv, VALUE vself);
static VALUE adb_vanish(VALUE vself);
static VALUE adb_copy(VALUE vself, VALUE vpath);
static VALUE adb_tranbegin(VALUE vself);
static VALUE adb_trancommit(VALUE vself);
static VALUE adb_tranabort(VALUE vself);
static VALUE adb_path(VALUE vself);
static VALUE adb_rnum(VALUE vself);
static VALUE adb_size(VALUE vself);
static VALUE adb_misc(int argc, VALUE *argv, VALUE vself);
static VALUE adb_fetch(int argc, VALUE *argv, VALUE vself);
static VALUE adb_check(VALUE vself, VALUE vkey);
static VALUE adb_check_value(VALUE vself, VALUE vval);
static VALUE adb_get_reverse(VALUE vself, VALUE vval);
static VALUE adb_empty(VALUE vself);
static VALUE adb_each(VALUE vself);
static VALUE adb_each_key(VALUE vself);
static VALUE adb_each_value(VALUE vself);
static VALUE adb_keys(VALUE vself);
static VALUE adb_values(VALUE vself);
/*************************************************************************************************
* public objects
*************************************************************************************************/
VALUE mod_tokyocabinet;
VALUE cls_hdb;
VALUE cls_hdb_data;
VALUE cls_bdb;
VALUE cls_bdb_data;
VALUE cls_bdbcur;
VALUE cls_bdbcur_data;
ID bdb_cmp_call_mid;
VALUE cls_fdb;
VALUE cls_fdb_data;
VALUE cls_tdb;
VALUE cls_tdb_data;
VALUE cls_tdbqry;
VALUE cls_tdbqry_data;
VALUE cls_adb;
VALUE cls_adb_data;
int Init_tokyocabinet(void){
mod_tokyocabinet = rb_define_module("TokyoCabinet");
rb_define_const(mod_tokyocabinet, "VERSION", rb_str_new2(tcversion));
hdb_init();
bdb_init();
bdbcur_init();
fdb_init();
tdb_init();
tdbqry_init();
adb_init();
return 0;
}
/*************************************************************************************************
* private objects
*************************************************************************************************/
static VALUE StringValueEx(VALUE vobj){
char kbuf[NUMBUFSIZ];
int ksiz;
switch(TYPE(vobj)){
case T_FIXNUM:
ksiz = sprintf(kbuf, "%d", (int)FIX2INT(vobj));
return rb_str_new(kbuf, ksiz);
case T_BIGNUM:
ksiz = sprintf(kbuf, "%lld", (long long)NUM2LL(vobj));
return rb_str_new(kbuf, ksiz);
case T_TRUE:
ksiz = sprintf(kbuf, "true");
return rb_str_new(kbuf, ksiz);
case T_FALSE:
ksiz = sprintf(kbuf, "false");
return rb_str_new(kbuf, ksiz);
case T_NIL:
ksiz = sprintf(kbuf, "nil");
return rb_str_new(kbuf, ksiz);
}
return StringValue(vobj);
}
static TCLIST *varytolist(VALUE vary){
VALUE vval;
TCLIST *list;
int i, num;
num = RARRAY_LEN(vary);
list = tclistnew2(num);
for(i = 0; i < num; i++){
vval = rb_ary_entry(vary, i);
vval = StringValueEx(vval);
tclistpush(list, RSTRING_PTR(vval), RSTRING_LEN(vval));
}
return list;
}
static VALUE listtovary(TCLIST *list){
VALUE vary;
const char *vbuf;
int i, num, vsiz;
num = tclistnum(list);
vary = rb_ary_new2(num);
for(i = 0; i < num; i++){
vbuf = tclistval(list, i, &vsiz);
rb_ary_push(vary, rb_str_new(vbuf, vsiz));
}
return vary;
}
static TCMAP *vhashtomap(VALUE vhash){
VALUE vkeys, vkey, vval;
TCMAP *map;
int i, num;
map = tcmapnew2(31);
vkeys = rb_funcall(vhash, rb_intern("keys"), 0);
num = RARRAY_LEN(vkeys);
for(i = 0; i < num; i++){
vkey = rb_ary_entry(vkeys, i);
vval = rb_hash_aref(vhash, vkey);
vkey = StringValueEx(vkey);
vval = StringValueEx(vval);
tcmapput(map, RSTRING_PTR(vkey), RSTRING_LEN(vkey), RSTRING_PTR(vval), RSTRING_LEN(vval));
}
return map;
}
static VALUE maptovhash(TCMAP *map){
const char *kbuf, *vbuf;
int ksiz, vsiz;
VALUE vhash;
vhash = rb_hash_new();
tcmapiterinit(map);
while((kbuf = tcmapiternext(map, &ksiz)) != NULL){
vbuf = tcmapiterval(kbuf, &vsiz);
rb_hash_aset(vhash, rb_str_new(kbuf, ksiz), rb_str_new(vbuf, vsiz));
}
return vhash;
}
static void hdb_init(void){
cls_hdb = rb_define_class_under(mod_tokyocabinet, "HDB", rb_cObject);
cls_hdb_data = rb_define_class_under(mod_tokyocabinet, "HDB_data", rb_cObject);
rb_define_const(cls_hdb, "ESUCCESS", INT2NUM(TCESUCCESS));
rb_define_const(cls_hdb, "ETHREAD", INT2NUM(TCETHREAD));
rb_define_const(cls_hdb, "EINVALID", INT2NUM(TCEINVALID));
rb_define_const(cls_hdb, "ENOFILE", INT2NUM(TCENOFILE));
rb_define_const(cls_hdb, "ENOPERM", INT2NUM(TCENOPERM));
rb_define_const(cls_hdb, "EMETA", INT2NUM(TCEMETA));
rb_define_const(cls_hdb, "ERHEAD", INT2NUM(TCERHEAD));
rb_define_const(cls_hdb, "EOPEN", INT2NUM(TCEOPEN));
rb_define_const(cls_hdb, "ECLOSE", INT2NUM(TCECLOSE));
rb_define_const(cls_hdb, "ETRUNC", INT2NUM(TCETRUNC));
rb_define_const(cls_hdb, "ESYNC", INT2NUM(TCESYNC));
rb_define_const(cls_hdb, "ESTAT", INT2NUM(TCESTAT));
rb_define_const(cls_hdb, "ESEEK", INT2NUM(TCESEEK));
rb_define_const(cls_hdb, "EREAD", INT2NUM(TCEREAD));
rb_define_const(cls_hdb, "EWRITE", INT2NUM(TCEWRITE));
rb_define_const(cls_hdb, "EMMAP", INT2NUM(TCEMMAP));
rb_define_const(cls_hdb, "ELOCK", INT2NUM(TCELOCK));
rb_define_const(cls_hdb, "EUNLINK", INT2NUM(TCEUNLINK));
rb_define_const(cls_hdb, "ERENAME", INT2NUM(TCERENAME));
rb_define_const(cls_hdb, "EMKDIR", INT2NUM(TCEMKDIR));
rb_define_const(cls_hdb, "ERMDIR", INT2NUM(TCERMDIR));
rb_define_const(cls_hdb, "EKEEP", INT2NUM(TCEKEEP));
rb_define_const(cls_hdb, "ENOREC", INT2NUM(TCENOREC));
rb_define_const(cls_hdb, "EMISC", INT2NUM(TCEMISC));
rb_define_const(cls_hdb, "TLARGE", INT2NUM(HDBTLARGE));
rb_define_const(cls_hdb, "TDEFLATE", INT2NUM(HDBTDEFLATE));
rb_define_const(cls_hdb, "TBZIP", INT2NUM(HDBTBZIP));
rb_define_const(cls_hdb, "TTCBS", INT2NUM(HDBTTCBS));
rb_define_const(cls_hdb, "OREADER", INT2NUM(HDBOREADER));
rb_define_const(cls_hdb, "OWRITER", INT2NUM(HDBOWRITER));
rb_define_const(cls_hdb, "OCREAT", INT2NUM(HDBOCREAT));
rb_define_const(cls_hdb, "OTRUNC", INT2NUM(HDBOTRUNC));
rb_define_const(cls_hdb, "ONOLCK", INT2NUM(HDBONOLCK));
rb_define_const(cls_hdb, "OLCKNB", INT2NUM(HDBOLCKNB));
rb_define_const(cls_hdb, "OTSYNC", INT2NUM(HDBOTSYNC));
rb_define_private_method(cls_hdb, "initialize", hdb_initialize, 0);
rb_define_method(cls_hdb, "errmsg", hdb_errmsg, -1);
rb_define_method(cls_hdb, "ecode", hdb_ecode, 0);
rb_define_method(cls_hdb, "tune", hdb_tune, -1);
rb_define_method(cls_hdb, "setcache", hdb_setcache, -1);
rb_define_method(cls_hdb, "setxmsiz", hdb_setxmsiz, -1);
rb_define_method(cls_hdb, "setdfunit", hdb_setdfunit, -1);
rb_define_method(cls_hdb, "open", hdb_open, -1);
rb_define_method(cls_hdb, "close", hdb_close, 0);
rb_define_method(cls_hdb, "put", hdb_put, 2);
rb_define_method(cls_hdb, "putkeep", hdb_putkeep, 2);
rb_define_method(cls_hdb, "putcat", hdb_putcat, 2);
rb_define_method(cls_hdb, "putasync", hdb_putasync, 2);
rb_define_method(cls_hdb, "out", hdb_out, 1);
rb_define_method(cls_hdb, "get", hdb_get, 1);
rb_define_method(cls_hdb, "vsiz", hdb_vsiz, 1);
rb_define_method(cls_hdb, "iterinit", hdb_iterinit, 0);
rb_define_method(cls_hdb, "iternext", hdb_iternext, 0);
rb_define_method(cls_hdb, "fwmkeys", hdb_fwmkeys, -1);
rb_define_method(cls_hdb, "addint", hdb_addint, 2);
rb_define_method(cls_hdb, "adddouble", hdb_adddouble, 2);
rb_define_method(cls_hdb, "sync", hdb_sync, 0);
rb_define_method(cls_hdb, "optimize", hdb_optimize, -1);
rb_define_method(cls_hdb, "vanish", hdb_vanish, 0);
rb_define_method(cls_hdb, "copy", hdb_copy, 1);
rb_define_method(cls_hdb, "tranbegin", hdb_tranbegin, 0);
rb_define_method(cls_hdb, "trancommit", hdb_trancommit, 0);
rb_define_method(cls_hdb, "tranabort", hdb_tranabort, 0);
rb_define_method(cls_hdb, "path", hdb_path, 0);
rb_define_method(cls_hdb, "rnum", hdb_rnum, 0);
rb_define_method(cls_hdb, "fsiz", hdb_fsiz, 0);
rb_define_method(cls_hdb, "[]", hdb_get, 1);
rb_define_method(cls_hdb, "[]=", hdb_put, 2);
rb_define_method(cls_hdb, "store", hdb_put, 2);
rb_define_method(cls_hdb, "delete", hdb_out, 1);
rb_define_method(cls_hdb, "fetch", hdb_fetch, -1);
rb_define_method(cls_hdb, "has_key?", hdb_check, 1);
rb_define_method(cls_hdb, "key?", hdb_check, 1);
rb_define_method(cls_hdb, "include?", hdb_check, 1);
rb_define_method(cls_hdb, "member?", hdb_check, 1);
rb_define_method(cls_hdb, "has_value?", hdb_check_value, 1);
rb_define_method(cls_hdb, "value?", hdb_check_value, 1);
rb_define_method(cls_hdb, "key", hdb_get_reverse, 1);
rb_define_method(cls_hdb, "clear", hdb_vanish, 0);
rb_define_method(cls_hdb, "size", hdb_rnum, 0);
rb_define_method(cls_hdb, "length", hdb_rnum, 0);
rb_define_method(cls_hdb, "empty?", hdb_empty, 0);
rb_define_method(cls_hdb, "each", hdb_each, 0);
rb_define_method(cls_hdb, "each_pair", hdb_each, 0);
rb_define_method(cls_hdb, "each_key", hdb_each_key, 0);
rb_define_method(cls_hdb, "each_value", hdb_each_value, 0);
rb_define_method(cls_hdb, "keys", hdb_keys, 0);
rb_define_method(cls_hdb, "values", hdb_values, 0);
}
static VALUE hdb_initialize(VALUE vself){
VALUE vhdb;
TCHDB *hdb;
hdb = tchdbnew();
tchdbsetmutex(hdb);
vhdb = Data_Wrap_Struct(cls_hdb_data, 0, tchdbdel, hdb);
rb_iv_set(vself, HDBVNDATA, vhdb);
return Qnil;
}
static VALUE hdb_errmsg(int argc, VALUE *argv, VALUE vself){
VALUE vhdb, vecode;
TCHDB *hdb;
const char *msg;
int ecode;
rb_scan_args(argc, argv, "01", &vecode);
vhdb = rb_iv_get(vself, HDBVNDATA);
Data_Get_Struct(vhdb, TCHDB, hdb);
ecode = (vecode == Qnil) ? tchdbecode(hdb) : NUM2INT(vecode);
msg = tchdberrmsg(ecode);
return rb_str_new2(msg);
}
static VALUE hdb_ecode(VALUE vself){
VALUE vhdb;
TCHDB *hdb;
vhdb = rb_iv_get(vself, HDBVNDATA);
Data_Get_Struct(vhdb, TCHDB, hdb);
return INT2NUM(tchdbecode(hdb));
}
static VALUE hdb_tune(int argc, VALUE *argv, VALUE vself){
VALUE vhdb, vbnum, vapow, vfpow, vopts;
TCHDB *hdb;
int apow, fpow, opts;
int64_t bnum;
rb_scan_args(argc, argv, "04", &vbnum, &vapow, &vfpow, &vopts);
bnum = (vbnum == Qnil) ? -1 : NUM2LL(vbnum);
apow = (vapow == Qnil) ? -1 : NUM2INT(vapow);
fpow = (vfpow == Qnil) ? -1 : NUM2INT(vfpow);
opts = (vopts == Qnil) ? 0 : NUM2INT(vopts);
vhdb = rb_iv_get(vself, HDBVNDATA);
Data_Get_Struct(vhdb, TCHDB, hdb);
return tchdbtune(hdb, bnum, apow, fpow, opts) ? Qtrue : Qfalse;
}
static VALUE hdb_setcache(int argc, VALUE *argv, VALUE vself){
VALUE vhdb, vrcnum;
TCHDB *hdb;
int rcnum;
rb_scan_args(argc, argv, "01", &vrcnum);
rcnum = (vrcnum == Qnil) ? -1 : NUM2INT(vrcnum);
vhdb = rb_iv_get(vself, HDBVNDATA);
Data_Get_Struct(vhdb, TCHDB, hdb);
return tchdbsetcache(hdb, rcnum) ? Qtrue : Qfalse;
}
static VALUE hdb_setxmsiz(int argc, VALUE *argv, VALUE vself){
VALUE vhdb, vxmsiz;
TCHDB *hdb;
int64_t xmsiz;
rb_scan_args(argc, argv, "01", &vxmsiz);
xmsiz = (vxmsiz == Qnil) ? -1 : NUM2LL(vxmsiz);
vhdb = rb_iv_get(vself, HDBVNDATA);
Data_Get_Struct(vhdb, TCHDB, hdb);
return tchdbsetxmsiz(hdb, xmsiz) ? Qtrue : Qfalse;
}
static VALUE hdb_setdfunit(int argc, VALUE *argv, VALUE vself){
VALUE vhdb, vdfunit;
TCHDB *hdb;
int32_t dfunit;
rb_scan_args(argc, argv, "01", &vdfunit);
dfunit = (vdfunit == Qnil) ? -1 : NUM2INT(vdfunit);
vhdb = rb_iv_get(vself, HDBVNDATA);
Data_Get_Struct(vhdb, TCHDB, hdb);
return tchdbsetdfunit(hdb, dfunit) ? Qtrue : Qfalse;
}
static VALUE hdb_open(int argc, VALUE *argv, VALUE vself){
VALUE vhdb, vpath, vomode;
TCHDB *hdb;
int omode;
rb_scan_args(argc, argv, "11", &vpath, &vomode);
Check_Type(vpath, T_STRING);
omode = (vomode == Qnil) ? HDBOREADER : NUM2INT(vomode);
vhdb = rb_iv_get(vself, HDBVNDATA);
Data_Get_Struct(vhdb, TCHDB, hdb);
return tchdbopen(hdb, RSTRING_PTR(vpath), omode) ? Qtrue : Qfalse;
}
static VALUE hdb_close(VALUE vself){
VALUE vhdb;
TCHDB *hdb;
vhdb = rb_iv_get(vself, HDBVNDATA);
Data_Get_Struct(vhdb, TCHDB, hdb);
return tchdbclose(hdb) ? Qtrue : Qfalse;
}
static VALUE hdb_put(VALUE vself, VALUE vkey, VALUE vval){
VALUE vhdb;
TCHDB *hdb;
vkey = StringValueEx(vkey);
vval = StringValueEx(vval);
vhdb = rb_iv_get(vself, HDBVNDATA);
Data_Get_Struct(vhdb, TCHDB, hdb);
return tchdbput(hdb, RSTRING_PTR(vkey), RSTRING_LEN(vkey),
RSTRING_PTR(vval), RSTRING_LEN(vval)) ? Qtrue : Qfalse;
}
static VALUE hdb_putkeep(VALUE vself, VALUE vkey, VALUE vval){
VALUE vhdb;
TCHDB *hdb;
vkey = StringValueEx(vkey);
vval = StringValueEx(vval);
vhdb = rb_iv_get(vself, HDBVNDATA);
Data_Get_Struct(vhdb, TCHDB, hdb);
return tchdbputkeep(hdb, RSTRING_PTR(vkey), RSTRING_LEN(vkey),
RSTRING_PTR(vval), RSTRING_LEN(vval)) ? Qtrue : Qfalse;
}
static VALUE hdb_putcat(VALUE vself, VALUE vkey, VALUE vval){
VALUE vhdb;
TCHDB *hdb;
vkey = StringValueEx(vkey);
vval = StringValueEx(vval);
vhdb = rb_iv_get(vself, HDBVNDATA);
Data_Get_Struct(vhdb, TCHDB, hdb);
return tchdbputcat(hdb, RSTRING_PTR(vkey), RSTRING_LEN(vkey),
RSTRING_PTR(vval), RSTRING_LEN(vval)) ? Qtrue : Qfalse;
}
static VALUE hdb_putasync(VALUE vself, VALUE vkey, VALUE vval){
VALUE vhdb;
TCHDB *hdb;
vkey = StringValueEx(vkey);
vval = StringValueEx(vval);
vhdb = rb_iv_get(vself, HDBVNDATA);
Data_Get_Struct(vhdb, TCHDB, hdb);
return tchdbputasync(hdb, RSTRING_PTR(vkey), RSTRING_LEN(vkey),
RSTRING_PTR(vval), RSTRING_LEN(vval)) ? Qtrue : Qfalse;
}
static VALUE hdb_out(VALUE vself, VALUE vkey){
VALUE vhdb;
TCHDB *hdb;
vkey = StringValueEx(vkey);
vhdb = rb_iv_get(vself, HDBVNDATA);
Data_Get_Struct(vhdb, TCHDB, hdb);
return tchdbout(hdb, RSTRING_PTR(vkey), RSTRING_LEN(vkey)) ? Qtrue : Qfalse;
}
static VALUE hdb_get(VALUE vself, VALUE vkey){
VALUE vhdb, vval;
TCHDB *hdb;
char *vbuf;
int vsiz;
vkey = StringValueEx(vkey);
vhdb = rb_iv_get(vself, HDBVNDATA);
Data_Get_Struct(vhdb, TCHDB, hdb);
if(!(vbuf = tchdbget(hdb, RSTRING_PTR(vkey), RSTRING_LEN(vkey), &vsiz))) return Qnil;
vval = rb_str_new(vbuf, vsiz);
tcfree(vbuf);
return vval;
}
static VALUE hdb_vsiz(VALUE vself, VALUE vkey){
VALUE vhdb;
TCHDB *hdb;
vkey = StringValueEx(vkey);
vhdb = rb_iv_get(vself, HDBVNDATA);
Data_Get_Struct(vhdb, TCHDB, hdb);
return INT2NUM(tchdbvsiz(hdb, RSTRING_PTR(vkey), RSTRING_LEN(vkey)));
}
static VALUE hdb_iterinit(VALUE vself){
VALUE vhdb;
TCHDB *hdb;
vhdb = rb_iv_get(vself, HDBVNDATA);
Data_Get_Struct(vhdb, TCHDB, hdb);
return tchdbiterinit(hdb) ? Qtrue : Qfalse;
}
static VALUE hdb_iternext(VALUE vself){
VALUE vhdb, vval;
TCHDB *hdb;
char *vbuf;
int vsiz;
vhdb = rb_iv_get(vself, HDBVNDATA);
Data_Get_Struct(vhdb, TCHDB, hdb);
if(!(vbuf = tchdbiternext(hdb, &vsiz))) return Qnil;
vval = rb_str_new(vbuf, vsiz);
tcfree(vbuf);
return vval;
}
static VALUE hdb_fwmkeys(int argc, VALUE *argv, VALUE vself){
VALUE vhdb, vprefix, vmax, vary;
TCHDB *hdb;
TCLIST *keys;
int max;
rb_scan_args(argc, argv, "11", &vprefix, &vmax);
vprefix = StringValueEx(vprefix);
max = (vmax == Qnil) ? -1 : NUM2INT(vmax);
vhdb = rb_iv_get(vself, HDBVNDATA);
Data_Get_Struct(vhdb, TCHDB, hdb);
keys = tchdbfwmkeys(hdb, RSTRING_PTR(vprefix), RSTRING_LEN(vprefix), max);
vary = listtovary(keys);
tclistdel(keys);
return vary;
}
static VALUE hdb_addint(VALUE vself, VALUE vkey, VALUE vnum){
VALUE vhdb;
TCHDB *hdb;
int num;
vkey = StringValueEx(vkey);
vhdb = rb_iv_get(vself, HDBVNDATA);
Data_Get_Struct(vhdb, TCHDB, hdb);
num = tchdbaddint(hdb, RSTRING_PTR(vkey), RSTRING_LEN(vkey), NUM2INT(vnum));
return num == INT_MIN ? Qnil : INT2NUM(num);
}
static VALUE hdb_adddouble(VALUE vself, VALUE vkey, VALUE vnum){
VALUE vhdb;
TCHDB *hdb;
double num;
vkey = StringValueEx(vkey);
vhdb = rb_iv_get(vself, HDBVNDATA);
Data_Get_Struct(vhdb, TCHDB, hdb);
num = tchdbadddouble(hdb, RSTRING_PTR(vkey), RSTRING_LEN(vkey), NUM2DBL(vnum));
return isnan(num) ? Qnil : rb_float_new(num);
}
static VALUE hdb_sync(VALUE vself){
VALUE vhdb;
TCHDB *hdb;
vhdb = rb_iv_get(vself, HDBVNDATA);
Data_Get_Struct(vhdb, TCHDB, hdb);
return tchdbsync(hdb) ? Qtrue : Qfalse;
}
static VALUE hdb_optimize(int argc, VALUE *argv, VALUE vself){
VALUE vhdb, vbnum, vapow, vfpow, vopts;
TCHDB *hdb;
int apow, fpow, opts;
int64_t bnum;
rb_scan_args(argc, argv, "04", &vbnum, &vapow, &vfpow, &vopts);
bnum = (vbnum == Qnil) ? -1 : NUM2LL(vbnum);
apow = (vapow == Qnil) ? -1 : NUM2INT(vapow);
fpow = (vfpow == Qnil) ? -1 : NUM2INT(vfpow);
opts = (vopts == Qnil) ? UINT8_MAX : NUM2INT(vopts);
vhdb = rb_iv_get(vself, HDBVNDATA);
Data_Get_Struct(vhdb, TCHDB, hdb);
return tchdboptimize(hdb, bnum, apow, fpow, opts) ? Qtrue : Qfalse;
}
static VALUE hdb_vanish(VALUE vself){
VALUE vhdb;
TCHDB *hdb;
vhdb = rb_iv_get(vself, HDBVNDATA);
Data_Get_Struct(vhdb, TCHDB, hdb);
return tchdbvanish(hdb) ? Qtrue : Qfalse;
}
static VALUE hdb_copy(VALUE vself, VALUE vpath){
VALUE vhdb;
TCHDB *hdb;
Check_Type(vpath, T_STRING);
vhdb = rb_iv_get(vself, HDBVNDATA);
Data_Get_Struct(vhdb, TCHDB, hdb);
return tchdbcopy(hdb, RSTRING_PTR(vpath)) ? Qtrue : Qfalse;
}
static VALUE hdb_tranbegin(VALUE vself){
VALUE vhdb;
TCHDB *hdb;
vhdb = rb_iv_get(vself, HDBVNDATA);
Data_Get_Struct(vhdb, TCHDB, hdb);
return tchdbtranbegin(hdb) ? Qtrue : Qfalse;
}
static VALUE hdb_trancommit(VALUE vself){
VALUE vhdb;
TCHDB *hdb;
vhdb = rb_iv_get(vself, HDBVNDATA);
Data_Get_Struct(vhdb, TCHDB, hdb);
return tchdbtrancommit(hdb) ? Qtrue : Qfalse;
}
static VALUE hdb_tranabort(VALUE vself){
VALUE vhdb;
TCHDB *hdb;
vhdb = rb_iv_get(vself, HDBVNDATA);
Data_Get_Struct(vhdb, TCHDB, hdb);
return tchdbtranabort(hdb) ? Qtrue : Qfalse;
}
static VALUE hdb_path(VALUE vself){
VALUE vhdb;
TCHDB *hdb;
const char *path;
vhdb = rb_iv_get(vself, HDBVNDATA);
Data_Get_Struct(vhdb, TCHDB, hdb);
if(!(path = tchdbpath(hdb))) return Qnil;
return rb_str_new2(path);
}
static VALUE hdb_rnum(VALUE vself){
VALUE vhdb;
TCHDB *hdb;
vhdb = rb_iv_get(vself, HDBVNDATA);
Data_Get_Struct(vhdb, TCHDB, hdb);
return LL2NUM(tchdbrnum(hdb));
}
static VALUE hdb_fsiz(VALUE vself){
VALUE vhdb;
TCHDB *hdb;
vhdb = rb_iv_get(vself, HDBVNDATA);
Data_Get_Struct(vhdb, TCHDB, hdb);
return LL2NUM(tchdbfsiz(hdb));
}
static VALUE hdb_fetch(int argc, VALUE *argv, VALUE vself){
VALUE vhdb, vkey, vdef, vval;
TCHDB *hdb;
char *vbuf;
int vsiz;
rb_scan_args(argc, argv, "11", &vkey, &vdef);
vkey = StringValueEx(vkey);
vhdb = rb_iv_get(vself, HDBVNDATA);
Data_Get_Struct(vhdb, TCHDB, hdb);
if((vbuf = tchdbget(hdb, RSTRING_PTR(vkey), RSTRING_LEN(vkey), &vsiz)) != NULL){
vval = rb_str_new(vbuf, vsiz);
tcfree(vbuf);
} else {
vval = vdef;
}
return vval;
}
static VALUE hdb_check(VALUE vself, VALUE vkey){
VALUE vhdb;
TCHDB *hdb;
vkey = StringValueEx(vkey);
vhdb = rb_iv_get(vself, HDBVNDATA);
Data_Get_Struct(vhdb, TCHDB, hdb);
return tchdbvsiz(hdb, RSTRING_PTR(vkey), RSTRING_LEN(vkey)) >= 0 ? Qtrue : Qfalse;
}
static VALUE hdb_check_value(VALUE vself, VALUE vval){
VALUE vhdb;
TCHDB *hdb;
TCXSTR *kxstr, *vxstr;
bool hit;
vval = StringValueEx(vval);
vhdb = rb_iv_get(vself, HDBVNDATA);
Data_Get_Struct(vhdb, TCHDB, hdb);
hit = false;
kxstr = tcxstrnew();
vxstr = tcxstrnew();
tchdbiterinit(hdb);
while(tchdbiternext3(hdb, kxstr, vxstr)){
if(tcxstrsize(vxstr) == RSTRING_LEN(vval) &&
memcmp(tcxstrptr(vxstr), RSTRING_PTR(vval), RSTRING_LEN(vval)) == 0){
hit = true;
break;
}
}
tcxstrdel(vxstr);
tcxstrdel(kxstr);
return hit ? Qtrue : Qfalse;
}
static VALUE hdb_get_reverse(VALUE vself, VALUE vval){
VALUE vhdb, vrv;
TCHDB *hdb;
TCXSTR *kxstr, *vxstr;
vval = StringValueEx(vval);
vhdb = rb_iv_get(vself, HDBVNDATA);
Data_Get_Struct(vhdb, TCHDB, hdb);
vrv = Qnil;
kxstr = tcxstrnew();
vxstr = tcxstrnew();
tchdbiterinit(hdb);
while(tchdbiternext3(hdb, kxstr, vxstr)){
if(tcxstrsize(vxstr) == RSTRING_LEN(vval) &&
memcmp(tcxstrptr(vxstr), RSTRING_PTR(vval), RSTRING_LEN(vval)) == 0){
vrv = rb_str_new(tcxstrptr(kxstr), tcxstrsize(kxstr));
break;
}
}
tcxstrdel(vxstr);
tcxstrdel(kxstr);
return vrv;
}
static VALUE hdb_empty(VALUE vself){
VALUE vhdb;
TCHDB *hdb;
vhdb = rb_iv_get(vself, HDBVNDATA);
Data_Get_Struct(vhdb, TCHDB, hdb);
return tchdbrnum(hdb) < 1 ? Qtrue : Qfalse;
}
static VALUE hdb_each(VALUE vself){
VALUE vhdb, vrv;
TCHDB *hdb;
TCXSTR *kxstr, *vxstr;
if(!rb_block_given_p()) rb_raise(rb_eArgError, "no block given");
vhdb = rb_iv_get(vself, HDBVNDATA);
Data_Get_Struct(vhdb, TCHDB, hdb);
vrv = Qnil;
kxstr = tcxstrnew();
vxstr = tcxstrnew();
tchdbiterinit(hdb);
while(tchdbiternext3(hdb, kxstr, vxstr)){
vrv = rb_yield_values(2, rb_str_new(tcxstrptr(kxstr), tcxstrsize(kxstr)),
rb_str_new(tcxstrptr(vxstr), tcxstrsize(vxstr)));
}
tcxstrdel(vxstr);
tcxstrdel(kxstr);
return vrv;
}
static VALUE hdb_each_key(VALUE vself){
VALUE vhdb, vrv;
TCHDB *hdb;
TCXSTR *kxstr, *vxstr;
if(!rb_block_given_p()) rb_raise(rb_eArgError, "no block given");
vhdb = rb_iv_get(vself, HDBVNDATA);
Data_Get_Struct(vhdb, TCHDB, hdb);
vrv = Qnil;
kxstr = tcxstrnew();
vxstr = tcxstrnew();
tchdbiterinit(hdb);
while(tchdbiternext3(hdb, kxstr, vxstr)){
vrv = rb_yield(rb_str_new(tcxstrptr(kxstr), tcxstrsize(kxstr)));
}
tcxstrdel(vxstr);
tcxstrdel(kxstr);
return vrv;
}
static VALUE hdb_each_value(VALUE vself){
VALUE vhdb, vrv;
TCHDB *hdb;
TCXSTR *kxstr, *vxstr;
if(!rb_block_given_p()) rb_raise(rb_eArgError, "no block given");
vhdb = rb_iv_get(vself, HDBVNDATA);
Data_Get_Struct(vhdb, TCHDB, hdb);
vrv = Qnil;
kxstr = tcxstrnew();
vxstr = tcxstrnew();