Module: TokyoTyrant

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

Defined Under Namespace

Modules: Balancer Classes: BDB, ConstistentHash, 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



292
293
294
295
296
297
298
299
300
301
# File 'ext/tokyo_tyrant_module.c', line 292

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



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

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



181
182
183
184
185
186
# File 'ext/tokyo_tyrant_module.c', line 181

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



57
58
59
60
61
62
# File 'ext/tokyo_tyrant_module.c', line 57

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

  if(!tcrdbclose(db)) mTokyoTyrant_exception(vself, "close error: %s");
  return Qtrue;
}

#copy(path) ⇒ Object



330
331
332
333
334
335
# File 'ext/tokyo_tyrant_module.c', line 330

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



370
371
372
373
374
# File 'ext/tokyo_tyrant_module.c', line 370

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



221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'ext/tokyo_tyrant_module.c', line 221

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



432
433
434
435
436
437
438
439
440
441
442
443
444
# File 'ext/tokyo_tyrant_module.c', line 432

static VALUE mTokyoTyrant_each_key(VALUE vself){
  VALUE vrv = Qnil;
  char *kbuf;
  if(!rb_block_given_p()) rb_raise(rb_eArgError, "no block given");
  TCRDB *db = mTokyoTyrant_getdb(vself);

  tcrdbiterinit(db);
  while((kbuf = tcrdbiternext2(db)) != NULL){
    vrv = rb_yield_values(1, rb_str_new2(kbuf));
    tcfree(kbuf);
  }
  return vrv;
}

#ecodeObject



134
135
136
137
138
# File 'ext/tokyo_tyrant_module.c', line 134

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

  return INT2NUM(tcrdbecode(db));
}

#empty?Boolean

Returns:

  • (Boolean)


364
365
366
367
368
# File 'ext/tokyo_tyrant_module.c', line 364

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

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

#errmsg(*args) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
# File 'ext/tokyo_tyrant_module.c', line 122

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, vval) ⇒ Object Also known as: run



413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# File 'ext/tokyo_tyrant_module.c', line 413

static VALUE mTokyoTyrant_ext(VALUE vself, VALUE vext, VALUE vkey, VALUE vval){
  int rsiz;
  char *rbuf, *kbuf, *vbuf, *xbuf;
  TCRDB *db = mTokyoTyrant_getdb(vself);
  VALUE vres = Qnil;
  vext = StringValueEx(vext);
  vkey = StringValueEx(vkey);
  vval = StringValueEx(vval);
  xbuf = RSTRING_PTR(vext);
  kbuf = RSTRING_PTR(vkey);
  vbuf = RSTRING_PTR(vval);

  if((rbuf = tcrdbext(db, xbuf, 0, kbuf, RSTRING_LEN(vkey), vbuf, RSTRING_LEN(vval), &rsiz)) != NULL){
    vres = rb_str_new(rbuf, rsiz);
    tcfree(rbuf);
  }
  return vres;
}

#fwmkeys(*args) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'ext/tokyo_tyrant_module.c', line 206

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



303
304
305
# File 'ext/tokyo_tyrant_module.c', line 303

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

#get_int(vkey) ⇒ Object



280
281
282
# File 'ext/tokyo_tyrant_module.c', line 280

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

#iterinitObject



188
189
190
191
192
# File 'ext/tokyo_tyrant_module.c', line 188

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

  return tcrdbiterinit(db) ? Qtrue : Qfalse;
}

#iternextObject



194
195
196
197
198
199
200
201
202
203
204
# File 'ext/tokyo_tyrant_module.c', line 194

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

  if((vbuf = tcrdbiternext2(db)) != NULL){
    vval = rb_str_new2(vbuf);
    tcfree(vbuf);
  }
  return vval;
}

#keysObject



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'ext/tokyo_tyrant_module.c', line 236

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



391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# File 'ext/tokyo_tyrant_module.c', line 391

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

  Check_Type(vargs, T_ARRAY);
  args = varytolist(vargs);
  vname = StringValueEx(vname);

  if ((list = tcrdbmisc(db, RSTRING_PTR(vname), NUM2INT(vopts), args)) != NULL){
    vary = listtovary(list);
    tclistdel(list);
  }
  tclistdel(args);

  return vary;
}

#optimize(*args) ⇒ Object



313
314
315
316
317
318
319
320
321
322
# File 'ext/tokyo_tyrant_module.c', line 313

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



140
141
142
143
144
145
# File 'ext/tokyo_tyrant_module.c', line 140

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?



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

static VALUE mTokyoTyrant_outlist(int argc, VALUE *argv, VALUE vself){
  VALUE vkeys, vvalue;
  VALUE vary = rb_ary_new();
  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);
  if ((result = tcrdbmisc(db, "outlist", 0, list)) != NULL){
    vary = listtovary(result);
    tclistdel(result);
  }
  tclistdel(list);
  return vary;
}

#reconnectObject



92
93
94
95
96
97
98
99
# File 'ext/tokyo_tyrant_module.c', line 92

static VALUE mTokyoTyrant_reconnect(VALUE vself){
  TCRDB *db = mTokyoTyrant_getdb(vself);
  if(db->fd != -1) mTokyoTyrant_close(vself);
  db = tcrdbnew();
  rb_iv_set(vself, RDBVNDATA, Data_Wrap_Struct(rb_cObject, 0, mTokyoTyrant_free, db));

  return mTokyoTyrant_connect(vself);
}

#restore(vpath, vts, vopts) ⇒ Object



337
338
339
340
341
342
343
344
345
346
# File 'ext/tokyo_tyrant_module.c', line 337

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



358
359
360
361
362
# File 'ext/tokyo_tyrant_module.c', line 358

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

  return LL2NUM(tcrdbrnum(db));
}

#serverObject



53
54
55
# File 'ext/tokyo_tyrant_module.c', line 53

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

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



348
349
350
351
352
353
354
355
356
# File 'ext/tokyo_tyrant_module.c', line 348

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



376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'ext/tokyo_tyrant_module.c', line 376

static VALUE mTokyoTyrant_stat(VALUE vself){
  VALUE vhash;
  char *stats;
  TCMAP *map;
  TCRDB *db = mTokyoTyrant_getdb(vself);
  vhash = rb_hash_new();

  if ((stats = tcrdbstat(db)) != NULL){
    map = tcstrsplit3(stats, "\t\n");
    vhash = maptovhash(map);
    tcmapdel(map);
  }
  return vhash;
}

#syncObject



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

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

  return tcrdbsync(db) ? Qtrue : Qfalse;
}

#vanishObject Also known as: clear



324
325
326
327
328
# File 'ext/tokyo_tyrant_module.c', line 324

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

  return tcrdbvanish(db) ? Qtrue : Qfalse;
}