Class: TokyoTyrant::Table
- Inherits:
-
Object
- Object
- TokyoTyrant::Table
show all
- Includes:
- TokyoTyrant
- Defined in:
- ext/tokyo_tyrant.c
Constant Summary
Constants included
from TokyoTyrant
EINVALID, EKEEP, EMISC, ENOHOST, ENOREC, ERECV, EREFUSED, ESEND, ESUCCESS, ITDECIMAL, ITKEEP, ITLEXICAL, ITVOID
Instance Method Summary
collapse
#add_double, #add_int, #check, #close, #copy, #db_size, #delete_keys_with_prefix, #each_key, #ecode, #empty?, #errmsg, #fwmkeys, #get_double, #get_int, #iterinit, #iternext, #keys, #misc, #outlist, #restore, #rnum, #setmst, #stat, #sync, #vanish
Instance Method Details
#each ⇒ Object
Also known as:
each_pair
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
# File 'ext/tokyo_tyrant_table.c', line 187
static VALUE cTable_each(VALUE vself){
VALUE vrv;
TCRDB *db;
TCMAP *cols;
char *kbuf;
int ksiz;
if(rb_block_given_p() != Qtrue) rb_raise(rb_eArgError, "no block given");
Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
vrv = Qnil;
tcrdbiterinit(db);
while((kbuf = tcrdbiternext(db, &ksiz)) != NULL){
if((cols = tcrdbtblget(db, kbuf, ksiz)) != NULL){
vrv = rb_yield_values(2, rb_str_new(kbuf, ksiz), maptovhashsym(cols));
tcmapdel(cols);
} else {
vrv = rb_yield_values(2, rb_str_new(kbuf, ksiz), Qnil);
}
tcfree(kbuf);
}
return vrv;
}
|
#each_value ⇒ Object
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
# File 'ext/tokyo_tyrant_table.c', line 209
static VALUE cTable_each_value(VALUE vself){
VALUE vrv;
TCRDB *db;
TCMAP *cols;
char *kbuf;
int ksiz;
if(rb_block_given_p() != Qtrue) rb_raise(rb_eArgError, "no block given");
Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
vrv = Qnil;
tcrdbiterinit(db);
while((kbuf = tcrdbiternext(db, &ksiz)) != NULL){
if((cols = tcrdbtblget(db, kbuf, ksiz)) != NULL){
vrv = rb_yield(maptovhashsym(cols));
tcmapdel(cols);
} else {
vrv = rb_yield(Qnil);
}
tcfree(kbuf);
}
return vrv;
}
|
#fetch(*args) ⇒ Object
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
# File 'ext/tokyo_tyrant_table.c', line 171
static VALUE cTable_fetch(int argc, VALUE *argv, VALUE vself){
VALUE vkey, vdef, vval;
TCRDB *db;
TCMAP *cols;
rb_scan_args(argc, argv, "11", &vkey, &vdef);
vkey = StringValueEx(vkey);
Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
if((cols = tcrdbtblget(db, RSTRING_PTR(vkey), RSTRING_LEN(vkey))) != NULL){
vval = maptovhashsym(cols);
tcmapdel(cols);
} else {
vval = vdef;
}
return vval;
}
|
#find ⇒ Object
269
270
271
272
273
274
275
|
# File 'ext/tokyo_tyrant_table.c', line 269
static VALUE cTable_find(VALUE vself){
VALUE vqry, vary;
vqry = rb_class_new_instance(1, &vself, rb_path2class("TokyoTyrant::Query"));
if(rb_block_given_p()) rb_yield_values(1, vqry);
vary = rb_funcall(vqry, rb_intern("get"), 0);
return vary;
}
|
#genuid ⇒ Object
Also known as:
generate_unique_id
165
166
167
168
169
|
# File 'ext/tokyo_tyrant_table.c', line 165
static VALUE cTable_genuid(VALUE vself){
TCRDB *db;
Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
return LL2NUM(tcrdbtblgenuid(db));
}
|
#get(vkey) ⇒ Object
Also known as:
[]
96
97
98
99
100
101
102
103
104
105
106
107
|
# File 'ext/tokyo_tyrant_table.c', line 96
static VALUE cTable_get(VALUE vself, VALUE vkey){
VALUE vcols;
TCRDB *db;
TCMAP *cols;
Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
vkey = StringValueEx(vkey);
if(!(cols = tcrdbtblget(db, RSTRING_PTR(vkey), RSTRING_LEN(vkey)))) return Qnil;
vcols = maptovhashsym(cols);
tcmapdel(cols);
return vcols;
}
|
#mget(*args) ⇒ Object
Also known as:
lget
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
|
# File 'ext/tokyo_tyrant_table.c', line 109
static VALUE cTable_mget(int argc, VALUE *argv, VALUE vself){
const char *kbuf;
int ksiz, vsiz;
VALUE vkeys, vhash, vcols, vvalue;
TCRDB *db;
TCMAP *recs, *cols;
Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
rb_scan_args(argc, argv, "*", &vkeys);
// I really hope there is a better way to do this
if (RARRAY_LEN(vkeys) == 1) {
vvalue = rb_ary_entry(vkeys, 0);
switch (TYPE(vvalue)){
case T_STRING:
case T_FIXNUM:
break;
case T_ARRAY:
vkeys = vvalue;
break;
case T_OBJECT:
vkeys = rb_convert_type(vvalue, T_ARRAY, "Array", "to_a");
break;
}
}
Check_Type(vkeys, T_ARRAY);
recs = varytomap(vkeys);
if(!tcrdbget3(db, recs)) return Qnil;
vhash = rb_hash_new();
tcmapiterinit(recs);
while((kbuf = tcmapiternext(recs, &ksiz)) != NULL){
const char *vbuf = tcmapiterval(kbuf, &vsiz);
cols = tcstrsplit4(vbuf, vsiz);
vcols = maptovhashsym(cols);
tcmapdel(cols);
rb_hash_aset(vhash, StringRaw(kbuf, ksiz), vcols);
}
tcmapdel(recs);
return vhash;
}
|
#mput(vhash) ⇒ Object
Also known as:
lput
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
|
# File 'ext/tokyo_tyrant_table.c', line 43
static VALUE cTable_mput(VALUE vself, VALUE vhash){
int i, num, j;
VALUE vary, vkeys, vkey, vval;
TCRDB *db;
TCLIST *list, *result;
Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
vkeys = rb_funcall(vhash, rb_intern("keys"), 0);
num = RARRAY_LEN(vkeys);
list = tclistnew2(num * 2);
for(i = 0; i < num; i++){
vkey = rb_ary_entry(vkeys, i);
vval = rb_hash_aref(vhash, vkey);
vkey = StringValueEx(vkey);
tclistpush2(list, RSTRING_PTR(vkey));
TCLIST *cols = vhashtolist(vval);
TCXSTR *xstr = tcxstrnew();
for(j = 0; j < tclistnum(cols); j++){
int rsiz;
const char *rbuf = tclistval(cols, j, &rsiz);
if (j > 0) tcxstrcat(xstr, "\0", 1);
tcxstrcat(xstr, rbuf, rsiz);
}
tclistpush(list, tcxstrptr(xstr), tcxstrsize(xstr));
tclistdel(cols);
tcxstrdel(xstr);
}
result = tcrdbmisc(db, "putlist", 0, list);
tclistdel(list);
vary = listtovary(result);
tclistdel(result);
return vary;
}
|
#out(vkey) ⇒ Object
Also known as:
delete
88
89
90
91
92
93
94
|
# File 'ext/tokyo_tyrant_table.c', line 88
static VALUE cTable_out(VALUE vself, VALUE vkey){
TCRDB *db;
Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
vkey = StringValueEx(vkey);
return tcrdbtblout(db, RSTRING_PTR(vkey), RSTRING_LEN(vkey)) ? Qtrue : Qfalse;
}
|
#prepare_query ⇒ Object
Probably should dry these up
250
251
252
253
254
255
|
# File 'ext/tokyo_tyrant_table.c', line 250
static VALUE cTable_prepare_query(VALUE vself){
VALUE vqry;
vqry = rb_class_new_instance(1, &vself, rb_path2class("TokyoTyrant::Query"));
if(rb_block_given_p()) rb_yield_values(1, vqry);
return vqry;
}
|
#put(vkey, vcols) ⇒ Object
Also known as:
[]=
39
40
41
|
# File 'ext/tokyo_tyrant_table.c', line 39
static VALUE cTable_put(VALUE vself, VALUE vkey, VALUE vcols){
return cTable_put_method(vself, vkey, vcols, TTPUT);
}
|
#putcat(vkey, vcols) ⇒ Object
84
85
86
|
# File 'ext/tokyo_tyrant_table.c', line 84
static VALUE cTable_putcat(VALUE vself, VALUE vkey, VALUE vcols){
return cTable_put_method(vself, vkey, vcols, TTPUTCAT);
}
|
#putkeep(vkey, vcols) ⇒ Object
80
81
82
|
# File 'ext/tokyo_tyrant_table.c', line 80
static VALUE cTable_putkeep(VALUE vself, VALUE vkey, VALUE vcols){
return cTable_put_method(vself, vkey, vcols, TTPUTKEEP);
}
|
#query ⇒ Object
257
258
259
260
261
262
263
264
265
266
267
|
# File 'ext/tokyo_tyrant_table.c', line 257
static VALUE cTable_query(VALUE vself){
VALUE vqry, vary;
vqry = rb_class_new_instance(1, &vself, rb_path2class("TokyoTyrant::Query"));
if(rb_block_given_p()) {
rb_yield_values(1, vqry);
vary = rb_funcall(vqry, rb_intern("run"), 0);
return vary;
} else {
return vqry;
}
}
|
#set_index(vname, vtype) ⇒ Object
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
# File 'ext/tokyo_tyrant_table.c', line 150
static VALUE cTable_setindex(VALUE vself, VALUE vname, VALUE vtype){
TCRDB *db;
Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
vname = StringValueEx(vname);
if (TYPE(vtype) == T_SYMBOL) vtype = rb_str_new2(rb_id2name(SYM2ID(vtype)));
if (TYPE(vtype) == T_STRING){
vtype = StringValueEx(vtype);
vtype = tctdbstrtoindextype(RSTRING_PTR(toupper(vtype)));
vtype = INT2NUM(vtype);
}
return tcrdbtblsetindex(db, RSTRING_PTR(vname), NUM2INT(vtype)) ? Qtrue : Qfalse;
}
|
#values ⇒ Object
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
# File 'ext/tokyo_tyrant_table.c', line 231
static VALUE cTable_values(VALUE vself){
VALUE vary;
TCRDB *db;
TCMAP *cols;
char *kxstr;
int ksiz;
Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
vary = rb_ary_new2(tcrdbrnum(db));
tcrdbiterinit(db);
while((kxstr = tcrdbiternext(db, &ksiz)) != NULL){
cols = tcrdbtblget(db, kxstr, ksiz);
rb_ary_push(vary, maptovhashsym(cols));
tcmapdel(cols);
tcfree(kxstr);
}
return vary;
}
|