Class: TokyoTyrant::DB

Inherits:
Object
  • Object
show all
Includes:
TokyoTyrant
Defined in:
ext/tokyo_tyrant.c

Direct Known Subclasses

BDB

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



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

static VALUE cDB_each(VALUE vself){
  VALUE vrv;
  if(!rb_block_given_p()) 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){
    VALUE vkey = rb_str_new(kbuf, ksiz);
    VALUE vval = cDB_get(vself, vkey);

    vrv = rb_yield_values(2, vkey, vval);
    tcfree(kbuf);
  }
  return vrv;
}

#each_valueObject



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'ext/tokyo_tyrant_db.c', line 170

static VALUE cDB_each_value(VALUE vself){
  VALUE vrv;
  if(!rb_block_given_p()) 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){
    VALUE vkey = rb_str_new(kbuf, ksiz);
    VALUE vval = cDB_get(vself, vkey);

    vrv = rb_yield_values(1, vval);
    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”);



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

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()) 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: []



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 77

static VALUE cDB_get(VALUE vself, VALUE vkey){
  VALUE vval = Qnil;
  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)) == NULL){
    if ((ecode = tcrdbecode(db))) {
      if (ecode != TTENOREC) mTokyoTyrant_exception(vself, NULL);
    }
  } 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
128
129
# File 'ext/tokyo_tyrant_db.c', line 97

static VALUE cDB_mget(int argc, VALUE *argv, VALUE vself){
  VALUE vkeys, vval;
  VALUE vhash = Qnil;
  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) {
    vval = rb_ary_entry(vkeys, 0);
    switch (TYPE(vval)){
      case T_STRING:
      case T_FIXNUM:
        break;
      case T_ARRAY:
        vkeys = vval;
        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(vval, T_ARRAY, "Array", "to_a");
        break;
    }
  }

  Check_Type(vkeys, T_ARRAY);

  recs = varytomap(vkeys);
  if(tcrdbget3(db, recs)){
    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
49
# File 'ext/tokyo_tyrant_db.c', line 37

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

  args = vhashtolist(vhash);
  if ((list = tcrdbmisc(db, "putlist", 0, args)) != NULL){
    vary = listtovary(list);
    tclistdel(list);
  }
  tclistdel(args);
  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



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

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

#putkeep(vkey, vstr) ⇒ Object



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

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

#putnr(vkey, vstr) ⇒ Object



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

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

#putshl(vkey, vstr, vwidth) ⇒ Object



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

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, NULL);

  return Qtrue;
}

#valuesObject



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'ext/tokyo_tyrant_db.c', line 189

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){
    VALUE vkey = rb_str_new(kbuf, ksiz);
    VALUE vval = cDB_get(vself, vkey);

    rb_ary_push(vary, vval);
    tcfree(kbuf);
  }
  return vary;
}

#vsiz(vkey) ⇒ Object

Rufus Compat



131
132
133
134
135
136
# File 'ext/tokyo_tyrant_db.c', line 131

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