Class: TokyoMessenger::Table

Inherits:
Object
  • Object
show all
Includes:
TokyoMessenger
Defined in:
ext/tokyo_messenger.c

Constant Summary

Constants included from TokyoMessenger

EINVALID, EKEEP, EMISC, ENOHOST, ENOREC, ERECV, EREFUSED, ESEND, ESUCCESS, ITDECIMAL, ITKEEP, ITLEXICAL, ITVOID

Instance Method Summary collapse

Methods included from TokyoMessenger

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

Instance Method Details

#eachObject Also known as: each_pair



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

static VALUE cTable_each(VALUE vself){
  VALUE vrv;
  TCMAP *cols;
  char *kbuf;
  int ksiz;
  if(rb_block_given_p() != Qtrue) rb_raise(rb_eArgError, "no block given");
  TCRDB *db = mTokyoMessenger_getdb(vself);
  vrv = Qnil;
  tcrdbiterinit(db);
  while((kbuf = tcrdbiternext(db, &ksiz)) != NULL){
    if((cols = tcrdbtblget(db, kbuf, ksiz)) != NULL){
      vrv = rb_yield_values(2, rb_str_new(kbuf, ksiz), maptovhash(cols));
      tcmapdel(cols);
    } else {
      vrv = rb_yield_values(2, rb_str_new(kbuf, ksiz), Qnil);
    }
    tcfree(kbuf);
  }
  return vrv;
}

#each_valueObject



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

static VALUE cTable_each_value(VALUE vself){
  VALUE vrv;
  TCMAP *cols;
  char *kbuf;
  int ksiz;
  if(rb_block_given_p() != Qtrue) rb_raise(rb_eArgError, "no block given");
  TCRDB *db = mTokyoMessenger_getdb(vself);
  vrv = Qnil;
  tcrdbiterinit(db);
  while((kbuf = tcrdbiternext(db, &ksiz)) != NULL){
    if((cols = tcrdbtblget(db, kbuf, ksiz)) != NULL){
      vrv = rb_yield(maptovhash(cols));
      tcmapdel(cols);
    } else {
      vrv = rb_yield(Qnil);
    }
    tcfree(kbuf);
  }
  return vrv;
}

#fetch(*args) ⇒ Object



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

static VALUE cTable_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;
  vkey = StringValueEx(vkey);

  if(vforce != Qfalse ||
     (vrv = cTable_get(vself, vkey)) == Qnil){
    vrv = rb_yield(vkey);
    cTable_put(vself, vkey, vrv);
  }

  return vrv;
}

#findObject



263
264
265
266
267
268
269
# File 'ext/tokyo_messenger_table.c', line 263

static VALUE cTable_find(VALUE vself){
  VALUE vqry, vary;
  vqry = rb_class_new_instance(1, &vself, rb_path2class("TokyoMessenger::Query"));
  if(rb_block_given_p()) rb_yield_values(1, vqry);
  vary = rb_funcall(vqry, rb_intern("get"), 0);
  return vary;
}

#genuidObject Also known as: generate_unique_id

Rufus Compat



163
164
165
166
# File 'ext/tokyo_messenger_table.c', line 163

static VALUE cTable_genuid(VALUE vself){
  TCRDB *db = mTokyoMessenger_getdb(vself);
  return LL2NUM(tcrdbtblgenuid(db));
}

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

Rufus Compat



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'ext/tokyo_messenger_table.c', line 88

static VALUE cTable_get(VALUE vself, VALUE vkey){
  VALUE vcols;
  int ecode;
  TCMAP *cols;
  TCRDB *db = mTokyoMessenger_getdb(vself);

  vkey = StringValueEx(vkey);
  if(!(cols = tcrdbtblget(db, RSTRING_PTR(vkey), RSTRING_LEN(vkey)))){
    if ((ecode = tcrdbecode(db))) {
      if (ecode != TTENOREC) mTokyoMessenger_exception(vself);
    }
    return Qnil;
  } else {
    vcols = maptovhash(cols);
  }

  tcmapdel(cols);
  return vcols;
}

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



108
109
110
111
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
143
144
145
146
147
# File 'ext/tokyo_messenger_table.c', line 108

static VALUE cTable_mget(int argc, VALUE *argv, VALUE vself){
  const char *kbuf;
  int ksiz, vsiz;
  VALUE vkeys, vhash, vcols, vvalue;
  TCMAP *recs, *cols;
  TCRDB *db = mTokyoMessenger_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 = rb_hash_new();
  tcmapiterinit(recs);
  while((kbuf = tcmapiternext(recs, &ksiz)) != NULL){
    const char *vbuf = tcmapiterval(kbuf, &vsiz);
    cols = tcstrsplit4(vbuf, vsiz);
    vcols = maptovhash(cols);
    tcmapdel(cols);
    rb_hash_aset(vhash, StringRaw(kbuf, ksiz), vcols);
  }
  tcmapdel(recs);
  return vhash;
}

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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'ext/tokyo_messenger_table.c', line 37

static VALUE cTable_mput(VALUE vself, VALUE vhash){
  int i, num, j;
  VALUE vary, vkeys, vkey, vval;
  TCLIST *list, *result;
  TCRDB *db = mTokyoMessenger_getdb(vself);

  vkeys = rb_funcall(vhash, rb_intern("keys"), 0);
  num = RARRAY_LEN(vkeys);
  list = tclistnew2(num * 2);
  for(i = 0; i < num; i++){
    vkey = rb_ary_entry(vkeys, i);
    vval = rb_hash_aref(vhash, vkey);

    vkey = StringValueEx(vkey);
    tclistpush2(list, RSTRING_PTR(vkey));

    TCLIST *cols = vhashtolist(vval);
    TCXSTR *xstr = tcxstrnew();

    for(j = 0; j < tclistnum(cols); j++){
      int rsiz;
      const char *rbuf = tclistval(cols, j, &rsiz);
      if (j > 0) tcxstrcat(xstr, "\0", 1);
      tcxstrcat(xstr, rbuf, rsiz);
    }
    tclistpush(list, tcxstrptr(xstr), tcxstrsize(xstr));
    tclistdel(cols);
    tcxstrdel(xstr);
  }
  result = tcrdbmisc(db, "putlist", 0, list);
  tclistdel(list);
  vary = listtovary(result);
  tclistdel(result);
  return vary;
}

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



81
82
83
84
85
86
# File 'ext/tokyo_messenger_table.c', line 81

static VALUE cTable_out(VALUE vself, VALUE vkey){
  TCRDB *db = mTokyoMessenger_getdb(vself);
  vkey = StringValueEx(vkey);

  return tcrdbtblout(db, RSTRING_PTR(vkey), RSTRING_LEN(vkey)) ? Qtrue : Qfalse;
}

#prepare_queryObject

Probably should dry these up



244
245
246
247
248
249
# File 'ext/tokyo_messenger_table.c', line 244

static VALUE cTable_prepare_query(VALUE vself){
  VALUE vqry;
  vqry = rb_class_new_instance(1, &vself, rb_path2class("TokyoMessenger::Query"));
  if(rb_block_given_p()) rb_yield_values(1, vqry);
  return vqry;
}

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

Rufus Compat



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

static VALUE cTable_put(VALUE vself, VALUE vkey, VALUE vcols){
  return cTable_put_method(vself, vkey, vcols, TTPUT);
}

#putcat(vkey, vcols) ⇒ Object



77
78
79
# File 'ext/tokyo_messenger_table.c', line 77

static VALUE cTable_putcat(VALUE vself, VALUE vkey, VALUE vcols){
  return cTable_put_method(vself, vkey, vcols, TTPUTCAT);
}

#putkeep(vkey, vcols) ⇒ Object



73
74
75
# File 'ext/tokyo_messenger_table.c', line 73

static VALUE cTable_putkeep(VALUE vself, VALUE vkey, VALUE vcols){
  return cTable_put_method(vself, vkey, vcols, TTPUTKEEP);
}

#queryObject



251
252
253
254
255
256
257
258
259
260
261
# File 'ext/tokyo_messenger_table.c', line 251

static VALUE cTable_query(VALUE vself){
  VALUE vqry, vary;
  vqry = rb_class_new_instance(1, &vself, rb_path2class("TokyoMessenger::Query"));
  if(rb_block_given_p()) {
    rb_yield_values(1, vqry);
    vary = rb_funcall(vqry, rb_intern("run"), 0);
    return vary;
  } else {
    return vqry;
  }
}

#search(*args) ⇒ Object



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'ext/tokyo_messenger_table.c', line 271

static VALUE cTable_search(int argc, VALUE *argv, VALUE vself){
  VALUE vqrys, vkeys, vtype;
  int qsiz, type, j;

  rb_scan_args(argc, argv, "1*", &vtype, &vqrys);

  qsiz = argc - 1;
  RDBQRY *qrys[qsiz];

  vtype = StringValueEx(vtype);
  type = tctdbstrtometasearcytype(RSTRING_PTR(vtype));

  for(j = 0; j < qsiz; j++){
    VALUE vqry = rb_iv_get(rb_ary_entry(vqrys, j), RDBQRYVNDATA);
    Data_Get_Struct(vqry, RDBQRY, qrys[j]);
  }
  TCLIST *res = tcrdbmetasearch(qrys, qsiz, type);
  vkeys = listtovary(res);
  tclistdel(res);

  return vkeys;
}

#set_index(vname, vtype) ⇒ Object

Rufus Compat



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

static VALUE cTable_setindex(VALUE vself, VALUE vname, VALUE vtype){
  TCRDB *db = mTokyoMessenger_getdb(vself);
  vname = StringValueEx(vname);
  if (TYPE(vtype) == T_SYMBOL) vtype = rb_str_new2(rb_id2name(SYM2ID(vtype)));

  if (TYPE(vtype) == T_STRING){
    vtype = StringValueEx(vtype);
    vtype = tctdbstrtoindextype(RSTRING_PTR(vtype));
    vtype = INT2NUM(vtype);
  }

  return tcrdbtblsetindex(db, RSTRING_PTR(vname), NUM2INT(vtype)) ? Qtrue : Qfalse;
}

#valuesObject



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'ext/tokyo_messenger_table.c', line 226

static VALUE cTable_values(VALUE vself){
  VALUE vary;
  TCMAP *cols;
  char *kxstr;
  int ksiz;
  TCRDB *db = mTokyoMessenger_getdb(vself);
  vary = rb_ary_new2(tcrdbrnum(db));
  tcrdbiterinit(db);
  while((kxstr = tcrdbiternext(db, &ksiz)) != NULL){
    cols = tcrdbtblget(db, kxstr, ksiz);
    rb_ary_push(vary, maptovhash(cols));
    tcmapdel(cols);
    tcfree(kxstr);
  }
  return vary;
}