static mrb_value hdb_put(mrb_state *mrb, mrb_value self) { mrb_value key, value; mrb_bool sync; bool result = false; mrb_int args; void *kbuf, *vbuf; int ksize, vsize; mrb_int kint, vint; hdb_context *context = DATA_PTR(self); args = mrb_get_args(mrb, "oo|b", &key, &value, &sync); if (args == 2) { sync = true; } if (!get_content(key, &kint, &kbuf, &ksize)) { mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid type of key"); } if (!get_content(value, &vint, &vbuf, &vsize)) { mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid type of value"); } if (sync) { result = tchdbput(context->hdb, kbuf, ksize, vbuf, vsize); } else { result = tchdbputasync(context->hdb, kbuf, ksize, vbuf, vsize); } return mrb_bool_value(result); }
/* putasync */ JNIEXPORT jboolean JNICALL Java_tokyocabinet_HDB_putasync (JNIEnv *env, jobject self, jbyteArray key, jbyteArray val){ if(!key || !val){ throwillarg(env); return false; } TCHDB *hdb = (TCHDB *)(intptr_t)(*env)->GetLongField(env, self, hdb_fid_ptr); jboolean ick; jbyte *kbuf = (*env)->GetByteArrayElements(env, key, &ick); if(!kbuf){ throwoutmem(env); return false; } int ksiz = (*env)->GetArrayLength(env, key); jboolean icv; jbyte *vbuf = (*env)->GetByteArrayElements(env, val, &icv); if(!vbuf){ throwoutmem(env); return false; } int vsiz = (*env)->GetArrayLength(env, val); bool rv = tchdbputasync(hdb, kbuf, ksiz, vbuf, vsiz); if(icv) (*env)->ReleaseByteArrayElements(env, val, vbuf, JNI_ABORT); if(ick) (*env)->ReleaseByteArrayElements(env, key, kbuf, JNI_ABORT); return rv; }
/* perform write command */ int dowrite(char *name, int rnum){ TCHDB *hdb; int i, err, len; char buf[RECBUFSIZ]; if(showprgr) printf("<Writing Test of Hash>\n name=%s rnum=%d\n\n", name, rnum); /* open a database */ hdb = tchdbnew(); tchdbtune(hdb, rnum * 3, 0, 0, 0); tchdbsetxmsiz(hdb, rnum * 48); if(!tchdbopen(hdb, name, HDBOWRITER | HDBOCREAT | HDBOTRUNC)){ fprintf(stderr, "tchdbopen failed\n"); tchdbdel(hdb); return 1; } err = FALSE; /* loop for each record */ for(i = 1; i <= rnum; i++){ /* store a record */ len = sprintf(buf, "%08d", i); if(!tchdbputasync(hdb, buf, len, buf, len)){ fprintf(stderr, "tchdbputasync failed\n"); err = TRUE; break; } /* print progression */ if(showprgr && rnum > 250 && i % (rnum / 250) == 0){ putchar('.'); fflush(stdout); if(i == rnum || i % (rnum / 10) == 0){ printf(" (%08d)\n", i); fflush(stdout); } } } /* close the database */ if(!tchdbclose(hdb)){ fprintf(stderr, "tchdbclose failed\n"); tchdbdel(hdb); return 1; } tchdbdel(hdb); if(showprgr && !err) printf("ok\n\n"); return err ? 1 : 0; }