Skip to content

Commit

Permalink
add putlist and getlist support for B+ tree
Browse files Browse the repository at this point in the history
  • Loading branch information
actsasflinn committed Mar 3, 2010
1 parent 7f053f0 commit bcc2606
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 4 deletions.
27 changes: 27 additions & 0 deletions ext/tokyo_tyrant.c
Expand Up @@ -123,6 +123,33 @@ extern TCLIST *vhashtolist(VALUE vhash){
return list;
}

extern TCLIST *vhashtoputlist(VALUE vhash){
VALUE vkey, vval, vkeys, vvals;
TCLIST *list;
int i, j;
vkeys = rb_funcall(vhash, rb_intern("keys"), 0);
list = tclistnew();
for(i = 0; i < RARRAY_LEN(vkeys); i++){
vkey = rb_ary_entry(vkeys, i);
vkey = StringValueEx(vkey);

vvals = rb_hash_aref(vhash, vkey);
if (TYPE(vvals) == T_ARRAY){
for(j = 0; j < RARRAY_LEN(vvals); j++){
vval = rb_ary_entry(vvals, j);
vval = StringValueEx(vval);
tclistpush(list, RSTRING_PTR(vkey), RSTRING_LEN(vkey));
tclistpush(list, RSTRING_PTR(vval), RSTRING_LEN(vval));
}
} else {
vval = StringValueEx(vvals);
tclistpush(list, RSTRING_PTR(vkey), RSTRING_LEN(vkey));
tclistpush(list, RSTRING_PTR(vval), RSTRING_LEN(vval));
}
}
return list;
}

VALUE mTokyoTyrant;
VALUE eTokyoTyrantError;
VALUE cDB;
Expand Down
2 changes: 1 addition & 1 deletion ext/tokyo_tyrant.h
Expand Up @@ -46,5 +46,5 @@ extern TCMAP *vhashtomap(VALUE vhash);
extern VALUE maptovhash(TCMAP *map);
extern TCMAP *varytomap(VALUE vhash);
extern TCLIST *vhashtolist(VALUE vhash);

extern TCLIST *vhashtoputlist(VALUE vhash);
#endif
67 changes: 67 additions & 0 deletions ext/tokyo_tyrant_db.c
Expand Up @@ -47,6 +47,25 @@ static VALUE cDB_mput(VALUE vself, VALUE vhash){
return vary;
}

static VALUE cDB_putlist(VALUE vself, VALUE vhash){
VALUE vary;
TCLIST *list, *result;
TCRDB *db = mTokyoTyrant_getdb(vself);
Check_Type(vhash, T_HASH);

// need a different vhash to putlist 'k v k v' instead of 'k v v v'
list = vhashtoputlist(vhash);
if ((result = tcrdbmisc(db, "putlist", 0, list)) != NULL){
vary = listtovary(result);
tclistdel(result);
} else {
vary = rb_ary_new();
}
tclistdel(list);

return vary;
}

static VALUE cDB_putkeep(VALUE vself, VALUE vkey, VALUE vstr){
return cDB_put_method(vself, vkey, vstr, TTPUTKEEP);
}
Expand Down Expand Up @@ -126,6 +145,52 @@ static VALUE cDB_mget(int argc, VALUE *argv, VALUE vself){
return vhash;
}

static VALUE cDB_getlist(int argc, VALUE *argv, VALUE vself){
VALUE vkeys, vvalue, vary, vhash, vkey, vval, vvals;
TCLIST *list, *result;
int i;
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);
result = tcrdbmisc(db, "getlist", RDBMONOULOG, list);
tclistdel(list);
vary = listtovary(result);
tclistdel(result);

vhash = rb_hash_new();
for(i = 0; i < RARRAY_LEN(vary); i += 2){
vkey = rb_ary_entry(vary, i);
vval = rb_ary_entry(vary, i + 1);
vvals = rb_hash_aref(vhash, vkey);
if (TYPE(vvals) == T_ARRAY){
vvals = rb_ary_push(vvals, vval);
} else {
vvals = rb_ary_new();
vvals = rb_ary_push(vvals, vval);
}
rb_hash_aset(vhash, vkey, vvals);
}
return vhash;
}

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

Expand Down Expand Up @@ -202,6 +267,7 @@ static VALUE cDB_values(VALUE vself){
void init_db(){
rb_define_method(cDB, "mput", cDB_mput, 1);
rb_define_alias(cDB, "lput", "mput"); // Rufus Compat
rb_define_method(cDB, "putlist", cDB_putlist, 1);
rb_define_method(cDB, "put", cDB_put, 2);
rb_define_alias(cDB, "[]=", "put");
rb_define_method(cDB, "putkeep", cDB_putkeep, 2);
Expand All @@ -212,6 +278,7 @@ void init_db(){
rb_define_alias(cDB, "[]", "get");
rb_define_method(cDB, "mget", cDB_mget, -1);
rb_define_alias(cDB, "lget", "mget"); // Rufus Compat
rb_define_method(cDB, "getlist", cDB_getlist, -1);
rb_define_method(cDB, "vsiz", cDB_vsiz, 1);
/*
rb_define_method(cDB, "check_value", cDB_check_value, 1);
Expand Down
11 changes: 8 additions & 3 deletions ext/tokyo_tyrant_module.c
Expand Up @@ -359,9 +359,14 @@ static VALUE mTokyoTyrant_misc(int argc, VALUE *argv, VALUE vself){
args = varytolist(vargs);
vname = StringValueEx(vname);

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

return vary;
}

Expand Down

0 comments on commit bcc2606

Please sign in to comment.