Class: TokyoTyrant::Table

Inherits:
Object
  • Object
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

Methods included from TokyoTyrant

#add_double, #add_int, #check, #close, #copy, #db_size, #delete_keys_with_prefix, #each_key, #ecode, #empty?, #errmsg, #ext, #fwmkeys, #get_double, #get_int, #iterinit, #iternext, #keys, #misc, #optimize, #outlist, #restore, #rnum, #server, #setmst, #stat, #sync, #vanish

Instance Method Details

#eachObject Also known as: each_pair



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'ext/tokyo_tyrant_table.c', line 183

static VALUE cTable_each(VALUE vself){
  VALUE vrv;
  TCMAP *cols;
  char *kbuf;
  int ksiz;
  if(rb_block_given_p() != Qtrue) rb_raise(rb_eArgError, "no block given");
  TCRDB *db = mTokyoTyrant_getdb(vself);
  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), maptovhash(cols));
      tcmapdel(cols);
    } else {
      vrv = rb_yield_values(2, rb_str_new(kbuf, ksiz), Qnil);
    }
    tcfree(kbuf);
  }
  return vrv;
}

#each_valueObject



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'ext/tokyo_tyrant_table.c', line 204

static VALUE cTable_each_value(VALUE vself){
  VALUE vrv;
  TCMAP *cols;
  char *kbuf;
  int ksiz;
  if(rb_block_given_p() != Qtrue) rb_raise(rb_eArgError, "no block given");
  TCRDB *db = mTokyoTyrant_getdb(vself);
  vrv = Qnil;
  tcrdbiterinit(db);
  while((kbuf = tcrdbiternext(db, &ksiz)) != NULL){
    if((cols = tcrdbtblget(db, kbuf, ksiz)) != NULL){
      vrv = rb_yield(maptovhash(cols));
      tcmapdel(cols);
    } else {
      vrv = rb_yield(Qnil);
    }
    tcfree(kbuf);
  }
  return vrv;
}

#fetch(*args) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'ext/tokyo_tyrant_table.c', line 168

static VALUE cTable_fetch(int argc, VALUE *argv, VALUE vself){
  VALUE vkey, vdef, vval;
  TCMAP *cols;
  TCRDB *db = mTokyoTyrant_getdb(vself);
  rb_scan_args(argc, argv, "11", &vkey, &vdef);
  vkey = StringValueEx(vkey);
  if((cols = tcrdbtblget(db, RSTRING_PTR(vkey), RSTRING_LEN(vkey))) != NULL){
    vval = maptovhash(cols);
    tcmapdel(cols);
  } else {
    vval = vdef;
  }
  return vval;
}

#findObject



262
263
264
265
266
267
268
# File 'ext/tokyo_tyrant_table.c', line 262

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;
}

#genuidObject Also known as: generate_unique_id

Rufus Compat



163
164
165
166
# File 'ext/tokyo_tyrant_table.c', line 163

static VALUE cTable_genuid(VALUE vself){
  TCRDB *db = mTokyoTyrant_getdb(vself);
  return LL2NUM(tcrdbtblgenuid(db));
}

#get(vkey) ⇒ Object Also known as: []

Rufus Compat



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'ext/tokyo_tyrant_table.c', line 88

static VALUE cTable_get(VALUE vself, VALUE vkey){
  VALUE vcols;
  int ecode;
  TCMAP *cols;
  TCRDB *db = mTokyoTyrant_getdb(vself);

  vkey = StringValueEx(vkey);
  if(!(cols = tcrdbtblget(db, RSTRING_PTR(vkey), RSTRING_LEN(vkey)))){
    if ((ecode = tcrdbecode(db))) {
      if (ecode != TTENOREC) mTokyoTyrant_exception(vself);
    }
    return Qnil;
  } else {
    vcols = maptovhash(cols);
  }

  tcmapdel(cols);
  return vcols;
}

#mget(*args) ⇒ Object Also known as: lget



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
# File 'ext/tokyo_tyrant_table.c', line 108

static VALUE cTable_mget(int argc, VALUE *argv, VALUE vself){
  const char *kbuf;
  int ksiz, vsiz;
  VALUE vkeys, vhash, vcols, vvalue;
  TCMAP *recs, *cols;
  TCRDB *db = mTokyoTyrant_getdb(vself);
  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_STRUCT: // range is not a T_STRUCT instead of a T_OBJECT in ruby1.9?
      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 = maptovhash(cols);
    tcmapdel(cols);
    rb_hash_aset(vhash, StringRaw(kbuf, ksiz), vcols);
  }
  tcmapdel(recs);
  return vhash;
}

#mput(vhash) ⇒ Object Also known as: lput



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
# File 'ext/tokyo_tyrant_table.c', line 37

static VALUE cTable_mput(VALUE vself, VALUE vhash){
  int i, num, j;
  VALUE vary, vkeys, vkey, vval;
  TCLIST *list, *result;
  TCRDB *db = mTokyoTyrant_getdb(vself);

  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



81
82
83
84
85
86
# File 'ext/tokyo_tyrant_table.c', line 81

static VALUE cTable_out(VALUE vself, VALUE vkey){
  TCRDB *db = mTokyoTyrant_getdb(vself);
  vkey = StringValueEx(vkey);

  return tcrdbtblout(db, RSTRING_PTR(vkey), RSTRING_LEN(vkey)) ? Qtrue : Qfalse;
}

#prepare_queryObject

Probably should dry these up



243
244
245
246
247
248
# File 'ext/tokyo_tyrant_table.c', line 243

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: []=

Rufus Compat



33
34
35
# File 'ext/tokyo_tyrant_table.c', line 33

static VALUE cTable_put(VALUE vself, VALUE vkey, VALUE vcols){
  return cTable_put_method(vself, vkey, vcols, TTPUT);
}

#putcat(vkey, vcols) ⇒ Object



77
78
79
# File 'ext/tokyo_tyrant_table.c', line 77

static VALUE cTable_putcat(VALUE vself, VALUE vkey, VALUE vcols){
  return cTable_put_method(vself, vkey, vcols, TTPUTCAT);
}

#putkeep(vkey, vcols) ⇒ Object



73
74
75
# File 'ext/tokyo_tyrant_table.c', line 73

static VALUE cTable_putkeep(VALUE vself, VALUE vkey, VALUE vcols){
  return cTable_put_method(vself, vkey, vcols, TTPUTKEEP);
}

#queryObject



250
251
252
253
254
255
256
257
258
259
260
# File 'ext/tokyo_tyrant_table.c', line 250

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;
  }
}

#search(*args) ⇒ Object



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
# File 'ext/tokyo_tyrant_table.c', line 270

static VALUE cTable_search(int argc, VALUE *argv, VALUE vself){
  VALUE vqrys, vkeys, vtype, vhash, vcols;
  TCMAP *recs, *cols;
  int qsiz, type, j;
  const char *kbuf;
  int ksiz, vsiz;
  TCRDB *db = mTokyoTyrant_getdb(vself);

//  rb_scan_args(argc, argv, "*", &vargs);
  rb_scan_args(argc, argv, "1*", &vtype, &vqrys);

//  vtype = rb_ary_pop(vargs);
  qsiz = argc - 1;
  RDBQRY *qrys[qsiz];

  vtype = StringValueEx(vtype);
  type = tctdbstrtometasearcytype(RSTRING_PTR(vtype));

  for(j = 0; j < qsiz; j++){
    VALUE vqry = rb_iv_get(rb_ary_entry(vqrys, j), RDBQRYVNDATA);
    Data_Get_Struct(vqry, RDBQRY, qrys[j]);
  }
  TCLIST *res = tcrdbmetasearch(qrys, qsiz, type);
  vkeys = listtovary(res);
  tclistdel(res);

  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 = maptovhash(cols);
    tcmapdel(cols);
    rb_hash_aset(vhash, StringRaw(kbuf, ksiz), vcols);
  }
  tcmapdel(recs);

  return vkeys;
}

#set_index(vname, vtype) ⇒ Object

Rufus Compat



149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'ext/tokyo_tyrant_table.c', line 149

static VALUE cTable_setindex(VALUE vself, VALUE vname, VALUE vtype){
  TCRDB *db = mTokyoTyrant_getdb(vself);
  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(vtype));
    vtype = INT2NUM(vtype);
  }

  return tcrdbtblsetindex(db, RSTRING_PTR(vname), NUM2INT(vtype)) ? Qtrue : Qfalse;
}

#valuesObject



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'ext/tokyo_tyrant_table.c', line 225

static VALUE cTable_values(VALUE vself){
  VALUE vary;
  TCMAP *cols;
  char *kxstr;
  int ksiz;
  TCRDB *db = mTokyoTyrant_getdb(vself);
  vary = rb_ary_new2(tcrdbrnum(db));
  tcrdbiterinit(db);
  while((kxstr = tcrdbiternext(db, &ksiz)) != NULL){
    cols = tcrdbtblget(db, kxstr, ksiz);
    rb_ary_push(vary, maptovhash(cols));
    tcmapdel(cols);
    tcfree(kxstr);
  }
  return vary;
}