Class: TokyoTyrant::DB

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, #out, #outlist, #reconnect, #restore, #rnum, #server, #setmst, #stat, #sync, #vanish

Instance Method Details

#eachObject Also known as: each_pair



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'ext/tokyo_tyrant_db.c', line 149

static VALUE cDB_each(VALUE vself){
  VALUE vrv;
  if(rb_block_given_p() != Qtrue) rb_raise(rb_eArgError, "no block given");
  TCRDB *db = mTokyoTyrant_getdb(vself);
  vrv = Qnil;
  tcrdbiterinit(db);
  int ksiz;
  char *kbuf;
  while((kbuf = tcrdbiternext(db, &ksiz)) != NULL){
    int vsiz;
    char *vbuf = tcrdbget(db, kbuf, ksiz, &vsiz);
    vrv = rb_yield_values(2, rb_str_new2(kbuf), StringRaw(vbuf, vsiz));
    tcfree(vbuf);
    tcfree(kbuf);
  }
  return vrv;
}

#each_valueObject



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

static VALUE cDB_each_value(VALUE vself){
  VALUE vrv;
  if(rb_block_given_p() != Qtrue) rb_raise(rb_eArgError, "no block given");
  TCRDB *db = mTokyoTyrant_getdb(vself);
  vrv = Qnil;
  tcrdbiterinit(db);
  int ksiz;
  char *kbuf;
  while((kbuf = tcrdbiternext(db, &ksiz)) != NULL){
    int vsiz;
    char *vbuf = tcrdbget(db, kbuf, ksiz, &vsiz);
    vrv = rb_yield_values(1, StringRaw(vbuf, vsiz));
    tcfree(vbuf);
    tcfree(kbuf);
  }
  return vrv;
}

#fetch(*args) ⇒ Object

rb_define_method(cDB, “check_value”, cDB_check_value, 1); rb_define_alias(cDB, “has_value?”, “check_value”); rb_define_alias(cDB, “value?”, “check_value”);



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

static VALUE cDB_fetch(int argc, VALUE *argv, VALUE vself){
  VALUE vkey, vrv, vforce;
  rb_scan_args(argc, argv, "11", &vkey, &vforce);
  if(rb_block_given_p() != Qtrue) rb_raise(rb_eArgError, "no block given");
  if(vforce == Qnil) vforce = Qfalse;

  if(vforce != Qfalse || (vrv = cDB_get(vself, vkey)) == Qnil){
    vrv = rb_yield(vkey);
    cDB_put(vself, vkey, vrv);
  }
  return vrv;
}

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



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'ext/tokyo_tyrant_db.c', line 76

static VALUE cDB_get(VALUE vself, VALUE vkey){
  VALUE vval;
  char *buf;
  int bsiz, ecode;
  TCRDB *db = mTokyoTyrant_getdb(vself);

  // this is ugly
  vkey = StringValueEx(vkey);
  if(!(buf = tcrdbget(db, RSTRING_PTR(vkey), RSTRING_LEN(vkey), &bsiz))){
    if ((ecode = tcrdbecode(db))) {
      if (ecode != TTENOREC) mTokyoTyrant_exception(vself);
    }
    return Qnil;
  } else {
    vval = StringRaw(buf, bsiz);
  }

  tcfree(buf);
  return vval;
}

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



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
122
123
124
125
126
127
# File 'ext/tokyo_tyrant_db.c', line 97

static VALUE cDB_mget(int argc, VALUE *argv, VALUE vself){
  VALUE vkeys, vhash, vvalue;
  TCMAP *recs;
  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 = maptovhash(recs);
  tcmapdel(recs);
  return vhash;
}

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



37
38
39
40
41
42
43
44
45
46
47
48
# File 'ext/tokyo_tyrant_db.c', line 37

static VALUE cDB_mput(VALUE vself, VALUE vhash){
  VALUE vary;
  TCLIST *list, *args;
  TCRDB *db = mTokyoTyrant_getdb(vself);

  args = vhashtolist(vhash);
  list = tcrdbmisc(db, "putlist", 0, args);
  vary = listtovary(list);
  tclistdel(args);
  tclistdel(list);
  return vary;
}

#put(vkey, vstr) ⇒ Object Also known as: []=

Rufus Compat



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

static VALUE cDB_put(VALUE vself, VALUE vkey, VALUE vstr){
  return cDB_put_method(vself, vkey, vstr, TTPUT);
}

#putcat(vkey, vstr) ⇒ Object



54
55
56
# File 'ext/tokyo_tyrant_db.c', line 54

static VALUE cDB_putcat(VALUE vself, VALUE vkey, VALUE vstr){
  return cDB_put_method(vself, vkey, vstr, TTPUTCAT);
}

#putkeep(vkey, vstr) ⇒ Object



50
51
52
# File 'ext/tokyo_tyrant_db.c', line 50

static VALUE cDB_putkeep(VALUE vself, VALUE vkey, VALUE vstr){
  return cDB_put_method(vself, vkey, vstr, TTPUTKEEP);  
}

#putnr(vkey, vstr) ⇒ Object



58
59
60
# File 'ext/tokyo_tyrant_db.c', line 58

static VALUE cDB_putnr(VALUE vself, VALUE vkey, VALUE vstr){
  return cDB_put_method(vself, vkey, vstr, TTPUTNR);
}

#putshl(vkey, vstr, vwidth) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'ext/tokyo_tyrant_db.c', line 62

static VALUE cDB_putshl(VALUE vself, VALUE vkey, VALUE vstr, VALUE vwidth){
  bool res;
  TCRDB *db = mTokyoTyrant_getdb(vself);

  vkey = StringValueEx(vkey);
  vstr = StringValueEx(vstr);

  res = tcrdbputshl(db, RSTRING_PTR(vkey), RSTRING_LEN(vkey), RSTRING_PTR(vstr), RSTRING_LEN(vstr), FIXNUM_P(vwidth));

  if(!res) mTokyoTyrant_exception(vself);

  return Qtrue;
}

#valuesObject



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

static VALUE cDB_values(VALUE vself){
  VALUE vary;
  TCRDB *db = mTokyoTyrant_getdb(vself);
  vary = rb_ary_new2(tcrdbrnum(db));
  tcrdbiterinit(db);
  int ksiz;
  char *kbuf;
  while((kbuf = tcrdbiternext(db, &ksiz)) != NULL){
    int vsiz;
    char *vbuf = tcrdbget(db, kbuf, ksiz, &vsiz);
    rb_ary_push(vary, StringRaw(vbuf, vsiz));
    tcfree(vbuf);
    tcfree(kbuf);
  }
  return vary;
}

#vsiz(vkey) ⇒ Object

Rufus Compat



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

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

  vkey = StringValueEx(vkey);
  return INT2NUM(tcrdbvsiz(db, RSTRING_PTR(vkey), RSTRING_LEN(vkey)));
}