actsasflinn-ruby-tokyotyrant 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/benchmarks/bulk_db.rb +74 -0
- data/benchmarks/bulk_table.rb +85 -0
- data/benchmarks/db.rb +114 -0
- data/benchmarks/table.rb +161 -0
- data/ext/tokyo_tyrant.c +162 -0
- data/ext/tokyo_tyrant.h +48 -0
- data/ext/tokyo_tyrant_db.c +242 -0
- data/ext/tokyo_tyrant_db.h +8 -0
- data/ext/tokyo_tyrant_module.c +298 -0
- data/ext/tokyo_tyrant_module.h +8 -0
- data/ext/tokyo_tyrant_query.c +149 -0
- data/ext/tokyo_tyrant_query.h +9 -0
- data/ext/tokyo_tyrant_table.c +259 -0
- data/ext/tokyo_tyrant_table.h +8 -0
- data/spec/plu_db.rb +538 -0
- data/spec/spec_base.rb +16 -0
- data/spec/tokyo_tyrant_query_spec.rb +71 -0
- data/spec/tokyo_tyrant_spec.rb +180 -0
- data/spec/tokyo_tyrant_table_spec.rb +190 -0
- metadata +24 -1
data/ext/tokyo_tyrant.h
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
#ifndef RUBY_TOKYOTYRANT
|
2
|
+
#define RUBY_TOKYOTYRANT
|
3
|
+
|
4
|
+
#include <ruby.h>
|
5
|
+
#include <string.h>
|
6
|
+
#include <math.h>
|
7
|
+
#include <time.h>
|
8
|
+
#include <locale.h>
|
9
|
+
#include <tcrdb.h>
|
10
|
+
#include <tokyo_tyrant_module.h>
|
11
|
+
#include <tokyo_tyrant_db.h>
|
12
|
+
#include <tokyo_tyrant_table.h>
|
13
|
+
#include <tokyo_tyrant_query.h>
|
14
|
+
|
15
|
+
#define RDBVNDATA "@rdb"
|
16
|
+
#define RDBQRYVNDATA "@rdbquery"
|
17
|
+
#define NUMBUFSIZ 32
|
18
|
+
#define TTPUT 0
|
19
|
+
#define TTPUTKEEP 1
|
20
|
+
#define TTPUTCAT 2
|
21
|
+
#define TTPUTNR 3
|
22
|
+
|
23
|
+
#if !defined(RSTRING_PTR)
|
24
|
+
#define RSTRING_PTR(TC_s) (RSTRING(TC_s)->ptr)
|
25
|
+
#endif
|
26
|
+
#if !defined(RSTRING_LEN)
|
27
|
+
#define RSTRING_LEN(TC_s) (RSTRING(TC_s)->len)
|
28
|
+
#endif
|
29
|
+
#if !defined(RARRAY_LEN)
|
30
|
+
#define RARRAY_LEN(TC_a) (RARRAY(TC_a)->len)
|
31
|
+
#endif
|
32
|
+
|
33
|
+
extern VALUE mTokyoTyrant;
|
34
|
+
extern VALUE eTokyoTyrantError;
|
35
|
+
extern VALUE cDB;
|
36
|
+
extern VALUE cTable;
|
37
|
+
extern VALUE cQuery;
|
38
|
+
|
39
|
+
extern VALUE StringValueEx(VALUE vobj);
|
40
|
+
extern TCLIST *varytolist(VALUE vary);
|
41
|
+
extern VALUE listtovary(TCLIST *list);
|
42
|
+
extern TCMAP *vhashtomap(VALUE vhash);
|
43
|
+
extern VALUE maptovhash(TCMAP *map);
|
44
|
+
extern VALUE maptovhashsym(TCMAP *map);
|
45
|
+
extern TCMAP *varytomap(VALUE vhash);
|
46
|
+
extern TCLIST *vhashtolist(VALUE vhash);
|
47
|
+
|
48
|
+
#endif
|
@@ -0,0 +1,242 @@
|
|
1
|
+
#include <tokyo_tyrant_db.h>
|
2
|
+
|
3
|
+
static VALUE cDB_put_method(VALUE vself, VALUE vkey, VALUE vstr, int method){
|
4
|
+
int ecode;
|
5
|
+
bool res;
|
6
|
+
TCRDB *db;
|
7
|
+
Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
|
8
|
+
|
9
|
+
vkey = StringValueEx(vkey);
|
10
|
+
vstr = StringValueEx(vstr);
|
11
|
+
|
12
|
+
switch(method){
|
13
|
+
case TTPUT:
|
14
|
+
res = tcrdbput2(db, RSTRING_PTR(vkey), RSTRING_PTR(vstr));
|
15
|
+
break;
|
16
|
+
case TTPUTKEEP:
|
17
|
+
res = tcrdbputkeep2(db, RSTRING_PTR(vkey), RSTRING_PTR(vstr));
|
18
|
+
break;
|
19
|
+
case TTPUTCAT:
|
20
|
+
res = tcrdbputcat2(db, RSTRING_PTR(vkey), RSTRING_PTR(vstr));
|
21
|
+
break;
|
22
|
+
case TTPUTNR:
|
23
|
+
res = tcrdbputnr2(db, RSTRING_PTR(vkey), RSTRING_PTR(vstr));
|
24
|
+
break;
|
25
|
+
default:
|
26
|
+
res = false;
|
27
|
+
break;
|
28
|
+
}
|
29
|
+
|
30
|
+
if(!res){
|
31
|
+
ecode = tcrdbecode(db);
|
32
|
+
rb_raise(eTokyoTyrantError, "put error: %s", tcrdberrmsg(ecode));
|
33
|
+
}
|
34
|
+
|
35
|
+
return Qtrue;
|
36
|
+
}
|
37
|
+
|
38
|
+
static VALUE cDB_put(VALUE vself, VALUE vkey, VALUE vstr){
|
39
|
+
return cDB_put_method(vself, vkey, vstr, TTPUT);
|
40
|
+
}
|
41
|
+
|
42
|
+
static VALUE cDB_mput(VALUE vself, VALUE vhash){
|
43
|
+
VALUE vary;
|
44
|
+
TCRDB *db;
|
45
|
+
TCLIST *list, *args;
|
46
|
+
Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
|
47
|
+
|
48
|
+
args = vhashtolist(vhash);
|
49
|
+
list = tcrdbmisc(db, "putlist", 0, args);
|
50
|
+
vary = listtovary(list);
|
51
|
+
tclistdel(list);
|
52
|
+
return vary;
|
53
|
+
}
|
54
|
+
|
55
|
+
static VALUE cDB_putkeep(VALUE vself, VALUE vkey, VALUE vstr){
|
56
|
+
return cDB_put_method(vself, vkey, vstr, TTPUTKEEP);
|
57
|
+
}
|
58
|
+
|
59
|
+
static VALUE cDB_putcat(VALUE vself, VALUE vkey, VALUE vstr){
|
60
|
+
return cDB_put_method(vself, vkey, vstr, TTPUTCAT);
|
61
|
+
}
|
62
|
+
|
63
|
+
static VALUE cDB_putnr(VALUE vself, VALUE vkey, VALUE vstr){
|
64
|
+
return cDB_put_method(vself, vkey, vstr, TTPUTNR);
|
65
|
+
}
|
66
|
+
|
67
|
+
static VALUE cDB_putshl(VALUE vself, VALUE vkey, VALUE vstr, VALUE vwidth){
|
68
|
+
int ecode;
|
69
|
+
TCRDB *db;
|
70
|
+
Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
|
71
|
+
|
72
|
+
vkey = StringValueEx(vkey);
|
73
|
+
vstr = StringValueEx(vstr);
|
74
|
+
|
75
|
+
if(!tcrdbputshl2(db, RSTRING_PTR(vkey), RSTRING_PTR(vstr), FIXNUM_P(vwidth))){
|
76
|
+
ecode = tcrdbecode(db);
|
77
|
+
rb_raise(eTokyoTyrantError, "put error: %s", tcrdberrmsg(ecode));
|
78
|
+
}
|
79
|
+
|
80
|
+
return Qtrue;
|
81
|
+
}
|
82
|
+
|
83
|
+
static VALUE cDB_get(VALUE vself, VALUE vkey){
|
84
|
+
VALUE vval;
|
85
|
+
char *vbuf;
|
86
|
+
TCRDB *db;
|
87
|
+
Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
|
88
|
+
|
89
|
+
vkey = StringValueEx(vkey);
|
90
|
+
if(!(vbuf = tcrdbget2(db, RSTRING_PTR(vkey)))) return Qnil;
|
91
|
+
vval = rb_str_new2(vbuf);
|
92
|
+
tcfree(vbuf);
|
93
|
+
return vval;
|
94
|
+
}
|
95
|
+
|
96
|
+
static VALUE cDB_mget(int argc, VALUE *argv, VALUE vself){
|
97
|
+
VALUE vkeys, vhash, vvalue;
|
98
|
+
TCRDB *db;
|
99
|
+
TCMAP *recs;
|
100
|
+
Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
|
101
|
+
rb_scan_args(argc, argv, "*", &vkeys);
|
102
|
+
|
103
|
+
// I really hope there is a better way to do this
|
104
|
+
if (RARRAY_LEN(vkeys) == 1) {
|
105
|
+
vvalue = rb_ary_entry(vkeys, 0);
|
106
|
+
switch (TYPE(vvalue)){
|
107
|
+
case T_STRING:
|
108
|
+
case T_FIXNUM:
|
109
|
+
break;
|
110
|
+
case T_ARRAY:
|
111
|
+
vkeys = vvalue;
|
112
|
+
break;
|
113
|
+
case T_OBJECT:
|
114
|
+
vkeys = rb_convert_type(vvalue, T_ARRAY, "Array", "to_a");
|
115
|
+
break;
|
116
|
+
}
|
117
|
+
}
|
118
|
+
|
119
|
+
Check_Type(vkeys, T_ARRAY);
|
120
|
+
|
121
|
+
recs = varytomap(vkeys);
|
122
|
+
if(!tcrdbget3(db, recs)) return Qnil;
|
123
|
+
vhash = maptovhash(recs);
|
124
|
+
tcmapdel(recs);
|
125
|
+
return vhash;
|
126
|
+
}
|
127
|
+
|
128
|
+
static VALUE cDB_vsiz(VALUE vself, VALUE vkey){
|
129
|
+
TCRDB *db;
|
130
|
+
Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
|
131
|
+
|
132
|
+
vkey = StringValueEx(vkey);
|
133
|
+
return INT2NUM(tcrdbvsiz2(db, RSTRING_PTR(vkey)));
|
134
|
+
}
|
135
|
+
|
136
|
+
static VALUE cDB_addint(VALUE vself, VALUE vkey, VALUE vnum){
|
137
|
+
int num;
|
138
|
+
TCRDB *db;
|
139
|
+
Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
|
140
|
+
vkey = StringValueEx(vkey);
|
141
|
+
|
142
|
+
num = tcrdbaddint(db, RSTRING_PTR(vkey), RSTRING_LEN(vkey), NUM2INT(vnum));
|
143
|
+
return num == INT_MIN ? Qnil : INT2NUM(num);
|
144
|
+
}
|
145
|
+
|
146
|
+
static VALUE cDB_adddouble(VALUE vself, VALUE vkey, VALUE vnum){
|
147
|
+
double num;
|
148
|
+
TCRDB *db;
|
149
|
+
Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
|
150
|
+
|
151
|
+
vkey = StringValueEx(vkey);
|
152
|
+
num = tcrdbadddouble(db, RSTRING_PTR(vkey), RSTRING_LEN(vkey), NUM2DBL(vnum));
|
153
|
+
return isnan(num) ? Qnil : rb_float_new(num);
|
154
|
+
}
|
155
|
+
|
156
|
+
static VALUE cDB_fetch(int argc, VALUE *argv, VALUE vself){
|
157
|
+
VALUE vkey, vdef, vval;
|
158
|
+
TCRDB *db;
|
159
|
+
char *vbuf;
|
160
|
+
int vsiz;
|
161
|
+
rb_scan_args(argc, argv, "11", &vkey, &vdef);
|
162
|
+
vkey = StringValueEx(vkey);
|
163
|
+
Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
|
164
|
+
if((vbuf = tcrdbget(db, RSTRING_PTR(vkey), RSTRING_LEN(vkey), &vsiz)) != NULL){
|
165
|
+
vval = rb_str_new(vbuf, vsiz);
|
166
|
+
tcfree(vbuf);
|
167
|
+
} else {
|
168
|
+
vval = vdef;
|
169
|
+
}
|
170
|
+
return vval;
|
171
|
+
}
|
172
|
+
|
173
|
+
static VALUE cDB_each(VALUE vself){
|
174
|
+
VALUE vrv;
|
175
|
+
TCRDB *db;
|
176
|
+
char *kxstr, *vxstr;
|
177
|
+
if(rb_block_given_p() != Qtrue) rb_raise(rb_eArgError, "no block given");
|
178
|
+
Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
|
179
|
+
vrv = Qnil;
|
180
|
+
tcrdbiterinit(db);
|
181
|
+
while((kxstr = tcrdbiternext2(db)) != NULL){
|
182
|
+
vxstr = tcrdbget2(db, kxstr);
|
183
|
+
vrv = rb_yield_values(2, rb_str_new2(kxstr), rb_str_new2(vxstr));
|
184
|
+
}
|
185
|
+
return vrv;
|
186
|
+
}
|
187
|
+
|
188
|
+
static VALUE cDB_each_value(VALUE vself){
|
189
|
+
VALUE vrv;
|
190
|
+
TCRDB *db;
|
191
|
+
char *kxstr, *vxstr;
|
192
|
+
if(rb_block_given_p() != Qtrue) rb_raise(rb_eArgError, "no block given");
|
193
|
+
Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
|
194
|
+
vrv = Qnil;
|
195
|
+
tcrdbiterinit(db);
|
196
|
+
while((kxstr = tcrdbiternext2(db)) != NULL){
|
197
|
+
vxstr = tcrdbget2(db, kxstr);
|
198
|
+
vrv = rb_yield_values(1, rb_str_new2(vxstr));
|
199
|
+
}
|
200
|
+
return vrv;
|
201
|
+
}
|
202
|
+
|
203
|
+
static VALUE cDB_values(VALUE vself){
|
204
|
+
VALUE vary;
|
205
|
+
TCRDB *db;
|
206
|
+
char *kxstr, *vxstr;
|
207
|
+
Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
|
208
|
+
vary = rb_ary_new2(tcrdbrnum(db));
|
209
|
+
tcrdbiterinit(db);
|
210
|
+
while((kxstr = tcrdbiternext2(db)) != NULL){
|
211
|
+
vxstr = tcrdbget2(db, kxstr);
|
212
|
+
rb_ary_push(vary, rb_str_new2(vxstr));
|
213
|
+
}
|
214
|
+
return vary;
|
215
|
+
}
|
216
|
+
|
217
|
+
void init_db(){
|
218
|
+
rb_define_method(cDB, "mput", cDB_mput, 1);
|
219
|
+
rb_define_method(cDB, "put", cDB_put, 2);
|
220
|
+
rb_define_alias(cDB, "[]=", "put");
|
221
|
+
rb_define_method(cDB, "putkeep", cDB_putkeep, 2);
|
222
|
+
rb_define_method(cDB, "putcat", cDB_putcat, 2);
|
223
|
+
rb_define_method(cDB, "putshl", cDB_putshl, 2);
|
224
|
+
rb_define_method(cDB, "putnr", cDB_putnr, 2);
|
225
|
+
rb_define_method(cDB, "get", cDB_get, 1);
|
226
|
+
rb_define_alias(cDB, "[]", "get");
|
227
|
+
rb_define_method(cDB, "mget", cDB_mget, -1);
|
228
|
+
rb_define_method(cDB, "vsiz", cDB_vsiz, 1);
|
229
|
+
/*
|
230
|
+
rb_define_method(cDB, "check_value", cDB_check_value, 1);
|
231
|
+
rb_define_alias(cDB, "has_value?", "check_value");
|
232
|
+
rb_define_alias(cDB, "value?", "check_value");
|
233
|
+
*/
|
234
|
+
rb_define_method(cDB, "addint", cDB_addint, 2);
|
235
|
+
rb_define_method(cDB, "adddouble", cDB_adddouble, 2);
|
236
|
+
|
237
|
+
rb_define_method(cDB, "fetch", cDB_fetch, -1);
|
238
|
+
rb_define_method(cDB, "each", cDB_each, 0);
|
239
|
+
rb_define_alias(cDB, "each_pair", "each");
|
240
|
+
rb_define_method(cDB, "each_value", cDB_each_value, 0);
|
241
|
+
rb_define_method(cDB, "values", cDB_values, 0);
|
242
|
+
}
|
@@ -0,0 +1,298 @@
|
|
1
|
+
#include <tokyo_tyrant_db.h>
|
2
|
+
|
3
|
+
static void mTokyoTyrant_free(TCRDB *db){
|
4
|
+
tcrdbdel(db);
|
5
|
+
}
|
6
|
+
|
7
|
+
static VALUE mTokyoTyrant_close(VALUE vself){
|
8
|
+
int ecode;
|
9
|
+
TCRDB *db;
|
10
|
+
Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
|
11
|
+
|
12
|
+
if(!tcrdbclose(db)){
|
13
|
+
ecode = tcrdbecode(db);
|
14
|
+
rb_raise(eTokyoTyrantError, "close error: %s", tcrdberrmsg(ecode));
|
15
|
+
}
|
16
|
+
return Qtrue;
|
17
|
+
}
|
18
|
+
|
19
|
+
static VALUE mTokyoTyrant_initialize(int argc, VALUE *argv, VALUE vself){
|
20
|
+
VALUE host, port;
|
21
|
+
int ecode;
|
22
|
+
TCRDB *db;
|
23
|
+
|
24
|
+
rb_scan_args(argc, argv, "02", &host, &port);
|
25
|
+
if(NIL_P(host)) host = rb_str_new2("127.0.0.1");
|
26
|
+
if(NIL_P(port)) port = INT2FIX(1978);
|
27
|
+
|
28
|
+
db = tcrdbnew();
|
29
|
+
|
30
|
+
if(!tcrdbopen(db, StringValuePtr(host), FIX2INT(port))){
|
31
|
+
ecode = tcrdbecode(db);
|
32
|
+
rb_raise(eTokyoTyrantError, "open error: %s", tcrdberrmsg(ecode));
|
33
|
+
}
|
34
|
+
|
35
|
+
rb_iv_set(vself, "@host", host);
|
36
|
+
rb_iv_set(vself, "@port", port);
|
37
|
+
rb_iv_set(vself, RDBVNDATA, Data_Wrap_Struct(rb_cObject, 0, mTokyoTyrant_free, db));
|
38
|
+
|
39
|
+
return Qtrue;
|
40
|
+
}
|
41
|
+
|
42
|
+
static VALUE mTokyoTyrant_errmsg(int argc, VALUE *argv, VALUE vself){
|
43
|
+
VALUE vecode;
|
44
|
+
const char *msg;
|
45
|
+
int ecode;
|
46
|
+
TCRDB *db;
|
47
|
+
Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
|
48
|
+
rb_scan_args(argc, argv, "01", &vecode);
|
49
|
+
|
50
|
+
ecode = (vecode == Qnil) ? tcrdbecode(db) : NUM2INT(vecode);
|
51
|
+
msg = tcrdberrmsg(ecode);
|
52
|
+
return rb_str_new2(msg);
|
53
|
+
}
|
54
|
+
|
55
|
+
static VALUE mTokyoTyrant_ecode(VALUE vself){
|
56
|
+
TCRDB *db;
|
57
|
+
Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
|
58
|
+
|
59
|
+
return INT2NUM(tcrdbecode(db));
|
60
|
+
}
|
61
|
+
|
62
|
+
static VALUE mTokyoTyrant_out(VALUE vself, VALUE vkey){
|
63
|
+
TCRDB *db;
|
64
|
+
Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
|
65
|
+
|
66
|
+
vkey = StringValueEx(vkey);
|
67
|
+
return tcrdbout2(db, RSTRING_PTR(vkey)) ? Qtrue : Qfalse;
|
68
|
+
}
|
69
|
+
|
70
|
+
static VALUE mTokyoTyrant_check(VALUE vself, VALUE vkey){
|
71
|
+
TCRDB *db;
|
72
|
+
Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
|
73
|
+
|
74
|
+
vkey = StringValueEx(vkey);
|
75
|
+
return tcrdbvsiz2(db, RSTRING_PTR(vkey)) >= 0 ? Qtrue : Qfalse;
|
76
|
+
}
|
77
|
+
|
78
|
+
static VALUE mTokyoTyrant_iterinit(VALUE vself){
|
79
|
+
TCRDB *db;
|
80
|
+
Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
|
81
|
+
|
82
|
+
return tcrdbiterinit(db) ? Qtrue : Qfalse;
|
83
|
+
}
|
84
|
+
|
85
|
+
static VALUE mTokyoTyrant_iternext(VALUE vself){
|
86
|
+
VALUE vval;
|
87
|
+
char *vbuf;
|
88
|
+
TCRDB *db;
|
89
|
+
Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
|
90
|
+
|
91
|
+
if(!(vbuf = tcrdbiternext2(db))) return Qnil;
|
92
|
+
vval = rb_str_new2(vbuf);
|
93
|
+
tcfree(vbuf);
|
94
|
+
|
95
|
+
return vval;
|
96
|
+
}
|
97
|
+
|
98
|
+
static VALUE mTokyoTyrant_fwmkeys(int argc, VALUE *argv, VALUE vself){
|
99
|
+
VALUE vprefix, vmax, vary;
|
100
|
+
TCLIST *keys;
|
101
|
+
int max;
|
102
|
+
TCRDB *db;
|
103
|
+
Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
|
104
|
+
rb_scan_args(argc, argv, "11", &vprefix, &vmax);
|
105
|
+
|
106
|
+
vprefix = StringValueEx(vprefix);
|
107
|
+
max = (vmax == Qnil) ? -1 : NUM2INT(vmax);
|
108
|
+
keys = tcrdbfwmkeys(db, RSTRING_PTR(vprefix), RSTRING_LEN(vprefix), max);
|
109
|
+
vary = listtovary(keys);
|
110
|
+
tclistdel(keys);
|
111
|
+
return vary;
|
112
|
+
}
|
113
|
+
|
114
|
+
static VALUE mTokyoTyrant_keys(VALUE vself){
|
115
|
+
/*
|
116
|
+
VALUE vary;
|
117
|
+
TCRDB *db;
|
118
|
+
char *kxstr;
|
119
|
+
Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
|
120
|
+
vary = rb_ary_new2(tcrdbrnum(db));
|
121
|
+
tcrdbiterinit(db);
|
122
|
+
while((kxstr = tcrdbiternext2(db)) != NULL){
|
123
|
+
rb_ary_push(vary, rb_str_new2(kxstr));
|
124
|
+
}
|
125
|
+
return vary;
|
126
|
+
*/
|
127
|
+
|
128
|
+
// Using forward matching keys with an empty string is 100x faster than iternext+get
|
129
|
+
VALUE vary;
|
130
|
+
TCLIST *keys;
|
131
|
+
TCRDB *db;
|
132
|
+
char *prefix;
|
133
|
+
Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
|
134
|
+
prefix = "";
|
135
|
+
keys = tcrdbfwmkeys2(db, prefix, -1);
|
136
|
+
vary = listtovary(keys);
|
137
|
+
tclistdel(keys);
|
138
|
+
return vary;
|
139
|
+
}
|
140
|
+
|
141
|
+
// TODO: Give this more attention, it's untested and needs defaults for scan_args
|
142
|
+
static VALUE mTokyoTyrant_ext(int argc, VALUE *argv, VALUE vself){
|
143
|
+
const char *res;
|
144
|
+
VALUE vname, vkey, vvalue, vopts;
|
145
|
+
TCRDB *db;
|
146
|
+
Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
|
147
|
+
|
148
|
+
rb_scan_args(argc, argv, "14", &vname, &vkey, &vvalue, &vopts);
|
149
|
+
|
150
|
+
vname = StringValueEx(vname);
|
151
|
+
vkey = StringValueEx(vkey);
|
152
|
+
vvalue = StringValueEx(vvalue);
|
153
|
+
res = tcrdbext2(db, RSTRING_PTR(vname), FIXNUM_P(vopts), RSTRING_PTR(vkey), RSTRING_PTR(vvalue));
|
154
|
+
return rb_str_new2(res);
|
155
|
+
}
|
156
|
+
|
157
|
+
static VALUE mTokyoTyrant_sync(VALUE vself){
|
158
|
+
TCRDB *db;
|
159
|
+
Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
|
160
|
+
|
161
|
+
return tcrdbsync(db) ? Qtrue : Qfalse;
|
162
|
+
}
|
163
|
+
|
164
|
+
static VALUE mTokyoTyrant_vanish(VALUE vself){
|
165
|
+
TCRDB *db;
|
166
|
+
Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
|
167
|
+
|
168
|
+
return tcrdbvanish(db) ? Qtrue : Qfalse;
|
169
|
+
}
|
170
|
+
|
171
|
+
static VALUE mTokyoTyrant_copy(VALUE vself, VALUE path){
|
172
|
+
TCRDB *db;
|
173
|
+
Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
|
174
|
+
|
175
|
+
Check_Type(path, T_STRING);
|
176
|
+
return tcrdbcopy(db, RSTRING_PTR(path)) ? Qtrue : Qfalse;
|
177
|
+
}
|
178
|
+
|
179
|
+
static VALUE mTokyoTyrant_restore(VALUE vself, VALUE path, uint64_t ts){
|
180
|
+
TCRDB *db;
|
181
|
+
Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
|
182
|
+
|
183
|
+
Check_Type(path, T_STRING);
|
184
|
+
return tcrdbrestore(db, RSTRING_PTR(path), ts) ? Qtrue : Qfalse;
|
185
|
+
}
|
186
|
+
|
187
|
+
static VALUE mTokyoTyrant_setmst(VALUE vself, VALUE host, VALUE port){
|
188
|
+
TCRDB *db;
|
189
|
+
Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
|
190
|
+
|
191
|
+
return tcrdbsetmst(db, StringValuePtr(host), FIX2INT(port)) ? Qtrue : Qfalse;
|
192
|
+
}
|
193
|
+
|
194
|
+
static VALUE mTokyoTyrant_rnum(VALUE vself){
|
195
|
+
TCRDB *db;
|
196
|
+
Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
|
197
|
+
|
198
|
+
return LL2NUM(tcrdbrnum(db));
|
199
|
+
}
|
200
|
+
|
201
|
+
static VALUE mTokyoTyrant_empty(VALUE vself){
|
202
|
+
TCRDB *db;
|
203
|
+
Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
|
204
|
+
|
205
|
+
return tcrdbrnum(db) < 1 ? Qtrue : Qfalse;
|
206
|
+
}
|
207
|
+
|
208
|
+
static VALUE mTokyoTyrant_size(VALUE vself){
|
209
|
+
TCRDB *db;
|
210
|
+
Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
|
211
|
+
|
212
|
+
return LL2NUM(tcrdbsize(db));
|
213
|
+
}
|
214
|
+
|
215
|
+
static VALUE mTokyoTyrant_stat(VALUE vself){
|
216
|
+
TCRDB *db;
|
217
|
+
Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
|
218
|
+
|
219
|
+
return rb_str_new2(tcrdbstat(db));
|
220
|
+
}
|
221
|
+
|
222
|
+
static VALUE mTokyoTyrant_misc(int argc, VALUE *argv, VALUE vself){
|
223
|
+
VALUE vname, vopts, vargs;
|
224
|
+
TCRDB *db;
|
225
|
+
TCLIST *list, *args;
|
226
|
+
VALUE vary;
|
227
|
+
Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
|
228
|
+
rb_scan_args(argc, argv, "13", &vname, &vopts, &vargs);
|
229
|
+
|
230
|
+
args = varytolist(vargs);
|
231
|
+
vname = StringValueEx(vname);
|
232
|
+
|
233
|
+
list = tcrdbmisc(db, RSTRING_PTR(vname), NUM2INT(vopts), args);
|
234
|
+
vary = listtovary(list);
|
235
|
+
tclistdel(list);
|
236
|
+
return vary;
|
237
|
+
}
|
238
|
+
|
239
|
+
static VALUE mTokyoTyrant_each_key(VALUE vself){
|
240
|
+
VALUE vrv;
|
241
|
+
TCRDB *db;
|
242
|
+
char *kxstr;
|
243
|
+
if(rb_block_given_p() != Qtrue) rb_raise(rb_eArgError, "no block given");
|
244
|
+
Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
|
245
|
+
vrv = Qnil;
|
246
|
+
tcrdbiterinit(db);
|
247
|
+
while((kxstr = tcrdbiternext2(db)) != NULL){
|
248
|
+
vrv = rb_yield_values(1, rb_str_new2(kxstr));
|
249
|
+
}
|
250
|
+
return vrv;
|
251
|
+
}
|
252
|
+
|
253
|
+
void init_mod(){
|
254
|
+
rb_define_const(mTokyoTyrant, "ESUCCESS", INT2NUM(TTESUCCESS));
|
255
|
+
rb_define_const(mTokyoTyrant, "EINVALID", INT2NUM(TTEINVALID));
|
256
|
+
rb_define_const(mTokyoTyrant, "ENOHOST", INT2NUM(TTENOHOST));
|
257
|
+
rb_define_const(mTokyoTyrant, "EREFUSED", INT2NUM(TTEREFUSED));
|
258
|
+
rb_define_const(mTokyoTyrant, "ESEND", INT2NUM(TTESEND));
|
259
|
+
rb_define_const(mTokyoTyrant, "ERECV", INT2NUM(TTERECV));
|
260
|
+
rb_define_const(mTokyoTyrant, "EKEEP", INT2NUM(TTEKEEP));
|
261
|
+
rb_define_const(mTokyoTyrant, "ENOREC", INT2NUM(TTENOREC));
|
262
|
+
rb_define_const(mTokyoTyrant, "EMISC", INT2NUM(TTEMISC));
|
263
|
+
|
264
|
+
rb_define_const(mTokyoTyrant, "ITLEXICAL", INT2NUM(RDBITLEXICAL));
|
265
|
+
rb_define_const(mTokyoTyrant, "ITDECIMAL", INT2NUM(RDBITDECIMAL));
|
266
|
+
rb_define_const(mTokyoTyrant, "ITVOID", INT2NUM(RDBITVOID));
|
267
|
+
rb_define_const(mTokyoTyrant, "ITKEEP", INT2NUM(RDBITKEEP));
|
268
|
+
|
269
|
+
rb_define_private_method(mTokyoTyrant, "initialize", mTokyoTyrant_initialize, -1);
|
270
|
+
rb_define_method(mTokyoTyrant, "close", mTokyoTyrant_close, 0);
|
271
|
+
rb_define_method(mTokyoTyrant, "errmsg", mTokyoTyrant_errmsg, -1);
|
272
|
+
rb_define_method(mTokyoTyrant, "ecode", mTokyoTyrant_ecode, 0);
|
273
|
+
rb_define_method(mTokyoTyrant, "out", mTokyoTyrant_out, 1);
|
274
|
+
rb_define_method(mTokyoTyrant, "check", mTokyoTyrant_check, 1);
|
275
|
+
rb_define_alias(mTokyoTyrant, "has_key?", "check");
|
276
|
+
rb_define_alias(mTokyoTyrant, "key?", "check");
|
277
|
+
rb_define_alias(mTokyoTyrant, "include?", "check");
|
278
|
+
rb_define_alias(mTokyoTyrant, "member?", "check");
|
279
|
+
rb_define_method(mTokyoTyrant, "iterinit", mTokyoTyrant_iterinit, 0);
|
280
|
+
rb_define_method(mTokyoTyrant, "iternext", mTokyoTyrant_iternext, 0);
|
281
|
+
rb_define_method(mTokyoTyrant, "fwmkeys", mTokyoTyrant_fwmkeys, -1);
|
282
|
+
rb_define_method(mTokyoTyrant, "keys", mTokyoTyrant_keys, 0);
|
283
|
+
rb_define_method(mTokyoTyrant, "ext", mTokyoTyrant_ext, -1);
|
284
|
+
rb_define_method(mTokyoTyrant, "sync", mTokyoTyrant_sync, 0);
|
285
|
+
rb_define_method(mTokyoTyrant, "vanish", mTokyoTyrant_vanish, 0);
|
286
|
+
rb_define_alias(mTokyoTyrant, "clear", "vanish");
|
287
|
+
rb_define_method(mTokyoTyrant, "copy", mTokyoTyrant_copy, 1);
|
288
|
+
rb_define_method(mTokyoTyrant, "restore", mTokyoTyrant_restore, 2);
|
289
|
+
rb_define_method(mTokyoTyrant, "setmst", mTokyoTyrant_setmst, 2);
|
290
|
+
rb_define_method(mTokyoTyrant, "rnum", mTokyoTyrant_rnum, 0);
|
291
|
+
rb_define_alias(mTokyoTyrant, "count", "rnum");
|
292
|
+
rb_define_method(mTokyoTyrant, "empty?", mTokyoTyrant_empty, 0);
|
293
|
+
rb_define_method(mTokyoTyrant, "size", mTokyoTyrant_size, 0);
|
294
|
+
rb_define_alias(mTokyoTyrant, "length", "size");
|
295
|
+
rb_define_method(mTokyoTyrant, "stat", mTokyoTyrant_stat, 0);
|
296
|
+
rb_define_method(mTokyoTyrant, "misc", mTokyoTyrant_misc, -1);
|
297
|
+
rb_define_method(mTokyoTyrant, "each_key", mTokyoTyrant_each_key, 0);
|
298
|
+
}
|