Module: TokyoTyrant

Included in:
DB, Table
Defined in:
ext/tokyo_tyrant.c

Defined Under Namespace

Classes: DB, Query, Table

Constant Summary collapse

ESUCCESS =
INT2NUM(TTESUCCESS)
EINVALID =
INT2NUM(TTEINVALID)
ENOHOST =
INT2NUM(TTENOHOST)
EREFUSED =
INT2NUM(TTEREFUSED)
ESEND =
INT2NUM(TTESEND)
ERECV =
INT2NUM(TTERECV)
EKEEP =
INT2NUM(TTEKEEP)
ENOREC =
INT2NUM(TTENOREC)
EMISC =
INT2NUM(TTEMISC)
ITLEXICAL =
INT2NUM(RDBITLEXICAL)
ITDECIMAL =
INT2NUM(RDBITDECIMAL)
ITVOID =
INT2NUM(RDBITVOID)
ITKEEP =
INT2NUM(RDBITKEEP)

Instance Method Summary collapse

Instance Method Details

#add_double(*args) ⇒ Object Also known as: adddouble



234
235
236
237
238
239
240
241
242
243
# File 'ext/tokyo_tyrant_module.c', line 234

static VALUE mTokyoTyrant_add_double(int argc, VALUE *argv, VALUE vself){
  VALUE vkey, vnum;
  double dnum = 1.0;

  rb_scan_args(argc, argv, "11", &vkey, &vnum);
  vkey = StringValueEx(vkey);
  if(NIL_P(vnum)) vnum = rb_float_new(dnum);

  return mTokyoTyrant_adddouble(vself, vkey, NUM2DBL(vnum));
}

#add_int(*args) ⇒ Object Also known as: addint, increment



211
212
213
214
215
216
217
218
219
220
# File 'ext/tokyo_tyrant_module.c', line 211

static VALUE mTokyoTyrant_add_int(int argc, VALUE *argv, VALUE vself){
  VALUE vkey, vnum;
  int inum = 1;

  rb_scan_args(argc, argv, "11", &vkey, &vnum);
  vkey = StringValueEx(vkey);
  if(NIL_P(vnum)) vnum = INT2NUM(inum);

  return mTokyoTyrant_addint(vself, vkey, NUM2INT(vnum));
}

#check(vkey) ⇒ Object Also known as: has_key?, key?, include?, member?

Rufus Compat



123
124
125
126
127
128
# File 'ext/tokyo_tyrant_module.c', line 123

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

  vkey = StringValueEx(vkey);
  return tcrdbvsiz(db, RSTRING_PTR(vkey), RSTRING_LEN(vkey)) >= 0 ? Qtrue : Qfalse;
}

#closeObject



25
26
27
28
29
30
31
32
33
34
# File 'ext/tokyo_tyrant_module.c', line 25

static VALUE mTokyoTyrant_close(VALUE vself){
  int ecode;
  TCRDB *db = mTokyoTyrant_getdb(vself);

  if(!tcrdbclose(db)){
    ecode = tcrdbecode(db);
    rb_raise(eTokyoTyrantError, "close error: %s", tcrdberrmsg(ecode));
  }
  return Qtrue;
}

#copy(path) ⇒ Object



272
273
274
275
276
277
# File 'ext/tokyo_tyrant_module.c', line 272

static VALUE mTokyoTyrant_copy(VALUE vself, VALUE path){
  TCRDB *db = mTokyoTyrant_getdb(vself);

  Check_Type(path, T_STRING);
  return tcrdbcopy(db, RSTRING_PTR(path)) ? Qtrue : Qfalse;
}

#db_sizeObject

Rufus Compat



312
313
314
315
316
# File 'ext/tokyo_tyrant_module.c', line 312

static VALUE mTokyoTyrant_size(VALUE vself){
  TCRDB *db = mTokyoTyrant_getdb(vself);

  return LL2NUM(tcrdbsize(db));
}

#delete_keys_with_prefix(*args) ⇒ Object Also known as: dfwmkeys

Rufus Compat



163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'ext/tokyo_tyrant_module.c', line 163

static VALUE mTokyoTyrant_delete_keys_with_prefix(int argc, VALUE *argv, VALUE vself){
  VALUE vprefix, vmax;
  TCLIST *keys;
  int max;
  TCRDB *db = mTokyoTyrant_getdb(vself);
  rb_scan_args(argc, argv, "11", &vprefix, &vmax);

  vprefix = StringValueEx(vprefix);
  max = (vmax == Qnil) ? -1 : NUM2INT(vmax);
  keys = tcrdbfwmkeys(db, RSTRING_PTR(vprefix), RSTRING_LEN(vprefix), max);
  tcrdbmisc(db, "outlist", 0, keys);
  tclistdel(keys);
  return Qnil;
}

#each_keyObject



354
355
356
357
358
359
360
361
362
363
364
365
# File 'ext/tokyo_tyrant_module.c', line 354

static VALUE mTokyoTyrant_each_key(VALUE vself){
  VALUE vrv;
  char *kxstr;
  if(rb_block_given_p() != Qtrue) rb_raise(rb_eArgError, "no block given");
  TCRDB *db = mTokyoTyrant_getdb(vself);
  vrv = Qnil;
  tcrdbiterinit(db);
  while((kxstr = tcrdbiternext2(db)) != NULL){
    vrv = rb_yield_values(1, rb_str_new2(kxstr));
  }
  return vrv;
}

#ecodeObject



78
79
80
81
82
# File 'ext/tokyo_tyrant_module.c', line 78

static VALUE mTokyoTyrant_ecode(VALUE vself){
  TCRDB *db = mTokyoTyrant_getdb(vself);

  return INT2NUM(tcrdbecode(db));
}

#empty?Boolean

Returns:

  • (Boolean)


306
307
308
309
310
# File 'ext/tokyo_tyrant_module.c', line 306

static VALUE mTokyoTyrant_empty(VALUE vself){
  TCRDB *db = mTokyoTyrant_getdb(vself);

  return tcrdbrnum(db) < 1 ? Qtrue : Qfalse;
}

#errmsg(*args) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
# File 'ext/tokyo_tyrant_module.c', line 66

static VALUE mTokyoTyrant_errmsg(int argc, VALUE *argv, VALUE vself){
  VALUE vecode;
  const char *msg;
  int ecode;
  TCRDB *db = mTokyoTyrant_getdb(vself);
  rb_scan_args(argc, argv, "01", &vecode);

  ecode = (vecode == Qnil) ? tcrdbecode(db) : NUM2INT(vecode);
  msg = tcrdberrmsg(ecode);
  return rb_str_new2(msg);
}

#ext(vext, vkey, vvalue) ⇒ Object Also known as: run



339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'ext/tokyo_tyrant_module.c', line 339

static VALUE mTokyoTyrant_ext(VALUE vself, VALUE vext, VALUE vkey, VALUE vvalue){
  int vsiz;
  char *vbuf;
  TCRDB *db = mTokyoTyrant_getdb(vself);
  vext = StringValueEx(vext);
  vkey = StringValueEx(vkey);
  vvalue = StringValueEx(vvalue);

  if(!(vbuf = tcrdbext(db, RSTRING_PTR(vext), 0, RSTRING_PTR(vkey), RSTRING_LEN(vkey), RSTRING_PTR(vvalue), RSTRING_LEN(vvalue), &vsiz))){
    return Qnil;
  } else {
    return rb_str_new(vbuf, vsiz);
  }
}

#fwmkeys(*args) ⇒ Object



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

static VALUE mTokyoTyrant_fwmkeys(int argc, VALUE *argv, VALUE vself){
  VALUE vprefix, vmax, vary;
  TCLIST *keys;
  int max;
  TCRDB *db = mTokyoTyrant_getdb(vself);
  rb_scan_args(argc, argv, "11", &vprefix, &vmax);

  vprefix = StringValueEx(vprefix);
  max = (vmax == Qnil) ? -1 : NUM2INT(vmax);
  keys = tcrdbfwmkeys(db, RSTRING_PTR(vprefix), RSTRING_LEN(vprefix), max);
  vary = listtovary(keys);
  tclistdel(keys);
  return vary;
}

#get_double(vkey) ⇒ Object



245
246
247
# File 'ext/tokyo_tyrant_module.c', line 245

static VALUE mTokyoTyrant_get_double(VALUE vself, VALUE vkey){
  return mTokyoTyrant_adddouble(vself, vkey, 0.0);
}

#get_int(vkey) ⇒ Object



222
223
224
# File 'ext/tokyo_tyrant_module.c', line 222

static VALUE mTokyoTyrant_get_int(VALUE vself, VALUE vkey){
  return mTokyoTyrant_addint(vself, vkey, 0);
}

#iterinitObject



130
131
132
133
134
# File 'ext/tokyo_tyrant_module.c', line 130

static VALUE mTokyoTyrant_iterinit(VALUE vself){
  TCRDB *db = mTokyoTyrant_getdb(vself);

  return tcrdbiterinit(db) ? Qtrue : Qfalse;
}

#iternextObject



136
137
138
139
140
141
142
143
144
145
146
# File 'ext/tokyo_tyrant_module.c', line 136

static VALUE mTokyoTyrant_iternext(VALUE vself){
  VALUE vval;
  char *vbuf;
  TCRDB *db = mTokyoTyrant_getdb(vself);

  if(!(vbuf = tcrdbiternext2(db))) return Qnil;
  vval = rb_str_new2(vbuf);
  tcfree(vbuf);

  return vval;
}

#keysObject



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'ext/tokyo_tyrant_module.c', line 178

static VALUE mTokyoTyrant_keys(VALUE vself){
  /*
  VALUE vary;
  char *kxstr;
  TCRDB *db = mTokyoTyrant_getdb(vself);
  vary = rb_ary_new2(tcrdbrnum(db));
  tcrdbiterinit(db);
  while((kxstr = tcrdbiternext2(db)) != NULL){
    rb_ary_push(vary, rb_str_new2(kxstr));
  }
  return vary;
  */

  // Using forward matching keys with an empty string is 100x faster than iternext+get
  VALUE vary;
  TCLIST *keys;
  char *prefix;
  TCRDB *db = mTokyoTyrant_getdb(vself);
  prefix = "";
  keys = tcrdbfwmkeys2(db, prefix, -1);
  vary = listtovary(keys);
  tclistdel(keys);
  return vary;
}

#misc(*args) ⇒ Object



324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'ext/tokyo_tyrant_module.c', line 324

static VALUE mTokyoTyrant_misc(int argc, VALUE *argv, VALUE vself){
  VALUE vname, vopts, vargs, vary;
  TCLIST *list, *args;
  TCRDB *db = mTokyoTyrant_getdb(vself);
  rb_scan_args(argc, argv, "13", &vname, &vopts, &vargs);

  args = varytolist(vargs);
  vname = StringValueEx(vname);

  list = tcrdbmisc(db, RSTRING_PTR(vname), NUM2INT(vopts), args);
  vary = listtovary(list);
  tclistdel(list);
  return vary;
}

#optimize(*args) ⇒ Object



255
256
257
258
259
260
261
262
263
264
# File 'ext/tokyo_tyrant_module.c', line 255

static VALUE mTokyoTyrant_optimize(int argc, VALUE *argv, VALUE vself){
  VALUE vparams;
  const char *params = NULL;
  TCRDB *db = mTokyoTyrant_getdb(vself);
  rb_scan_args(argc, argv, "01", &vparams);
  if(NIL_P(vparams)) vparams = Qnil;
  if(vparams != Qnil) params = RSTRING_PTR(vparams);

  return tcrdboptimize(db, params) ? Qtrue : Qfalse;
}

#out(vkey) ⇒ Object Also known as: delete



84
85
86
87
88
89
# File 'ext/tokyo_tyrant_module.c', line 84

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

  vkey = StringValueEx(vkey);
  return tcrdbout(db, RSTRING_PTR(vkey), RSTRING_LEN(vkey)) ? Qtrue : Qfalse;
}

#outlist(*args) ⇒ Object Also known as: mdelete, ldelete

TODO: merge out and mout?



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

static VALUE mTokyoTyrant_outlist(int argc, VALUE *argv, VALUE vself){
  VALUE vkeys, vary, vvalue;
  TCLIST *list, *result;
  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_OBJECT:
        vkeys = rb_convert_type(vvalue, T_ARRAY, "Array", "to_a");
        break;
    }
  }
  Check_Type(vkeys, T_ARRAY);

  list = varytolist(vkeys);
  result = tcrdbmisc(db, "outlist", 0, list);
  tclistdel(list);
  vary = listtovary(result);
  tclistdel(result);
  return vary;
}

#restore(vpath, vts, vopts) ⇒ Object



279
280
281
282
283
284
285
286
287
288
# File 'ext/tokyo_tyrant_module.c', line 279

static VALUE mTokyoTyrant_restore(VALUE vself, VALUE vpath, VALUE vts, VALUE vopts){
  uint64_t ts;
  int opts;
  TCRDB *db = mTokyoTyrant_getdb(vself);

  Check_Type(vpath, T_STRING);
  ts = (uint64_t) FIX2INT(vts);
  opts = FIX2INT(vopts);
  return tcrdbrestore(db, RSTRING_PTR(vpath), ts, opts) ? Qtrue : Qfalse;
}

#rnumObject Also known as: count, size



300
301
302
303
304
# File 'ext/tokyo_tyrant_module.c', line 300

static VALUE mTokyoTyrant_rnum(VALUE vself){
  TCRDB *db = mTokyoTyrant_getdb(vself);

  return LL2NUM(tcrdbrnum(db));
}

#serverObject



21
22
23
# File 'ext/tokyo_tyrant_module.c', line 21

static VALUE mTokyoTyrant_server(VALUE vself){
  return rb_iv_get(vself, "@server");
}

#setmst(vhost, vport, vts, vopts) ⇒ Object



290
291
292
293
294
295
296
297
298
# File 'ext/tokyo_tyrant_module.c', line 290

static VALUE mTokyoTyrant_setmst(VALUE vself, VALUE vhost, VALUE vport, VALUE vts, VALUE vopts){
  uint64_t ts;
  int opts;
  TCRDB *db = mTokyoTyrant_getdb(vself);

  ts = (uint64_t) FIX2INT(vts);
  opts = FIX2INT(vopts);
  return tcrdbsetmst(db, RSTRING_PTR(vhost), FIX2INT(vport), ts, opts) ? Qtrue : Qfalse;
}

#statObject



318
319
320
321
322
# File 'ext/tokyo_tyrant_module.c', line 318

static VALUE mTokyoTyrant_stat(VALUE vself){
  TCRDB *db = mTokyoTyrant_getdb(vself);

  return rb_str_new2(tcrdbstat(db));
}

#syncObject



249
250
251
252
253
# File 'ext/tokyo_tyrant_module.c', line 249

static VALUE mTokyoTyrant_sync(VALUE vself){
  TCRDB *db = mTokyoTyrant_getdb(vself);

  return tcrdbsync(db) ? Qtrue : Qfalse;
}

#vanishObject Also known as: clear



266
267
268
269
270
# File 'ext/tokyo_tyrant_module.c', line 266

static VALUE mTokyoTyrant_vanish(VALUE vself){
  TCRDB *db = mTokyoTyrant_getdb(vself);

  return tcrdbvanish(db) ? Qtrue : Qfalse;
}