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, #fwmkeys, #get_double, #get_int, #iterinit, #iternext, #keys, #misc, #out, #outlist, #restore, #rnum, #setmst, #stat, #sync, #vanish

Instance Method Details

#eachObject Also known as: each_pair



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

static VALUE cDB_each(VALUE vself){
  VALUE vrv;
  TCRDB *db;
  if(rb_block_given_p() != Qtrue) rb_raise(rb_eArgError, "no block given");
  Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
  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



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

static VALUE cDB_each_value(VALUE vself){
  VALUE vrv;
  TCRDB *db;
  if(rb_block_given_p() != Qtrue) rb_raise(rb_eArgError, "no block given");
  Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
  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");



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

static VALUE cDB_fetch(int argc, VALUE *argv, VALUE vself){
  VALUE vkey, vdef, vval;
  TCRDB *db;
  char *vbuf;
  int vsiz;
  rb_scan_args(argc, argv, "11", &vkey, &vdef);
  vkey = StringValueEx(vkey);
  Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
  if((vbuf = tcrdbget(db, RSTRING_PTR(vkey), RSTRING_LEN(vkey), &vsiz)) != NULL){
    vval = StringRaw(vbuf, vsiz);
    tcfree(vbuf);
  } else {
    vval = vdef;
  }
  return vval;
}

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



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'ext/tokyo_tyrant_db.c', line 84

static VALUE cDB_get(int argc, VALUE *argv, VALUE vself){
  VALUE vkey, vraw, vval;
  char *buf;
  int bsiz, ecode;
  bool raw;
  TCRDB *db;
  Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);

  rb_scan_args(argc, argv, "11", &vkey, &vraw);
  raw = (vraw == Qtrue);

  // this is ugly
  vkey = StringValueEx(vkey);
  if(!(buf = tcrdbget(db, RSTRING_PTR(vkey), RSTRING_LEN(vkey), &bsiz))){
    if ((ecode = tcrdbecode(db))) {
      if (ecode != TTENOREC) {
        rb_raise(eTokyoTyrantError, "get error: %s", tcrdberrmsg(ecode));
      }
    }
    return Qnil;
  } else {
    vval = StringRaw(buf, bsiz);
  }

  tcfree(buf);
  return vval;
}

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



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

static VALUE cDB_mget(int argc, VALUE *argv, VALUE vself){
  VALUE vkeys, vhash, vvalue;
  TCRDB *db;
  TCMAP *recs;
  Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
  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);

  recs = varytomap(vkeys);
  if(!tcrdbget3(db, recs)) return Qnil;
  vhash = maptovhash(recs);
  tcmapdel(recs);
  return vhash;
}

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



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'ext/tokyo_tyrant_db.c', line 42

static VALUE cDB_mput(VALUE vself, VALUE vhash){
  VALUE vary;
  TCRDB *db;
  TCLIST *list, *args;
  Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);

  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



38
39
40
# File 'ext/tokyo_tyrant_db.c', line 38

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

#putcat(vkey, vstr) ⇒ Object



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

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

#putkeep(vkey, vstr) ⇒ Object



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

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

#putnr(vkey, vstr) ⇒ Object



64
65
66
# File 'ext/tokyo_tyrant_db.c', line 64

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

#putshl(vkey, vstr, vwidth) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'ext/tokyo_tyrant_db.c', line 68

static VALUE cDB_putshl(VALUE vself, VALUE vkey, VALUE vstr, VALUE vwidth){
  int ecode;
  TCRDB *db;
  Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);

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

  if(!tcrdbputshl2(db, RSTRING_PTR(vkey), RSTRING_PTR(vstr), FIXNUM_P(vwidth))){
    ecode = tcrdbecode(db);
    rb_raise(eTokyoTyrantError, "put error: %s", tcrdberrmsg(ecode));
  }

  return Qtrue;
}

#valuesObject



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

static VALUE cDB_values(VALUE vself){
  VALUE vary;
  TCRDB *db;
  Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);
  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



144
145
146
147
148
149
150
# File 'ext/tokyo_tyrant_db.c', line 144

static VALUE cDB_vsiz(VALUE vself, VALUE vkey){
  TCRDB *db;
  Data_Get_Struct(rb_iv_get(vself, RDBVNDATA), TCRDB, db);

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