Skip to content
Browse files

added database abstraction class

  • Loading branch information...
1 parent 4a698b3 commit b6843ec46af46e253735efaab73e7b9d986929f7 Atsushi Takayama committed May 26, 2010
Showing with 1,392 additions and 1,135 deletions.
  1. +1,392 −1,135 src/tokyocabinet.cc
View
2,527 src/tokyocabinet.cc
@@ -37,10 +37,68 @@
// null or undefined
#define NOU(obj) ((obj)->IsNull() || (obj)->IsUndefined())
+using namespace v8;
+using namespace node;
+
+// conversion between Tokyo Cabinet list/map to V8 Arrya/Object and vice versa
+inline TCLIST* arytotclist (const Handle<Array> ary) {
+ HandleScope scope;
+ int len = ary->Length();
+ TCLIST *list = tclistnew2(len);
+ Handle<Value> val;
+ for (int i = 0; i < len; i++) {
+ val = ary->Get(Integer::New(i));
+ if (val->IsString()) {
+ tclistpush2(list, VSTRPTR(val));
+ }
+ }
+ return list;
+}
+
+inline Local<Array> tclisttoary (TCLIST *list) {
+ HandleScope scope;
+ int len = tclistnum(list);
+ Local<Array> ary = Array::New(len);
+ for (int i = 0; i < len; i++) {
+ ary->Set(Integer::New(i), String::New(tclistval2(list, i)));
+ }
+ return ary;
+}
+
+inline TCMAP* objtotcmap (const Handle<Object> obj) {
+ HandleScope scope;
+ TCMAP *map = tcmapnew2(31);
+ Local<Array> keys = obj->GetPropertyNames();
+ int len = keys->Length();
+ Local<Value> key, val;
+ for (int i = 0; i < len; i++) {
+ key = keys->Get(Integer::New(i));
+ val = obj->Get(key);
+ if (NOU(val)) continue;
+ tcmapput(map, VSTRPTR(key), VSTRSIZ(key), VSTRPTR(val), VSTRSIZ(val));
+ }
+ return map;
+}
+
+inline Local<Object> tcmaptoobj (TCMAP *map) {
+ const char *kbuf, *vbuf;
+ int ksiz, vsiz;
+ HandleScope scope;
+ Local<Object> obj = Object::New();
+ tcmapiterinit(map);
+ for (;;) {
+ kbuf = static_cast<const char*>(tcmapiternext(map, &ksiz));
+ if (kbuf == NULL) break;
+ vbuf = static_cast<const char*>(tcmapiterval(kbuf, &vsiz));
+ obj->Set(String::New(kbuf, ksiz), String::New(vbuf, vsiz));
+ }
+ return obj;
+}
+
/* sync method blueprint */
#define DEFINE_SYNC(name) \
static Handle<Value> \
- name (const Arguments& args) { \
+ name##Sync (const Arguments& args) { \
HandleScope scope; \
if (!name##Data::checkArgs(args)) { \
return THROW_BAD_ARGS; \
@@ -51,7 +109,7 @@
/* when there is an extra value to return */
#define DEFINE_SYNC2(name) \
static Handle<Value> \
- name (const Arguments& args) { \
+ name##Sync (const Arguments& args) { \
HandleScope scope; \
if (!name##Data::checkArgs(args)) { \
return THROW_BAD_ARGS; \
@@ -120,1000 +178,1284 @@
DEFINE_ASYNC_EXEC(name) \
DEFINE_ASYNC_AFTER2(name) \
-using namespace v8;
-using namespace node;
-
-// conversion between Tokyo Cabinet list/map to V8 Arrya/Object and vice versa
-inline TCLIST* arytotclist (const Handle<Array> ary) {
- HandleScope scope;
- int len = ary->Length();
- TCLIST *list = tclistnew2(len);
- Handle<Value> val;
- for (int i = 0; i < len; i++) {
- val = ary->Get(Integer::New(i));
- if (val->IsString()) {
- tclistpush2(list, VSTRPTR(val));
- }
- }
- return list;
+// Tokyo Cabinet error codes
+inline void set_ecodes (const Handle<FunctionTemplate> tmpl) {
+ DEFINE_PREFIXED_CONSTANT(tmpl, TC, ESUCCESS);
+ DEFINE_PREFIXED_CONSTANT(tmpl, TC, ETHREAD);
+ DEFINE_PREFIXED_CONSTANT(tmpl, TC, EINVALID);
+ DEFINE_PREFIXED_CONSTANT(tmpl, TC, ENOFILE);
+ DEFINE_PREFIXED_CONSTANT(tmpl, TC, ENOPERM);
+ DEFINE_PREFIXED_CONSTANT(tmpl, TC, EMETA);
+ DEFINE_PREFIXED_CONSTANT(tmpl, TC, ERHEAD);
+ DEFINE_PREFIXED_CONSTANT(tmpl, TC, EOPEN);
+ DEFINE_PREFIXED_CONSTANT(tmpl, TC, ECLOSE);
+ DEFINE_PREFIXED_CONSTANT(tmpl, TC, ETRUNC);
+ DEFINE_PREFIXED_CONSTANT(tmpl, TC, ESYNC);
+ DEFINE_PREFIXED_CONSTANT(tmpl, TC, ESTAT);
+ DEFINE_PREFIXED_CONSTANT(tmpl, TC, ESEEK);
+ DEFINE_PREFIXED_CONSTANT(tmpl, TC, EREAD);
+ DEFINE_PREFIXED_CONSTANT(tmpl, TC, EWRITE);
+ DEFINE_PREFIXED_CONSTANT(tmpl, TC, EMMAP);
+ DEFINE_PREFIXED_CONSTANT(tmpl, TC, ELOCK);
+ DEFINE_PREFIXED_CONSTANT(tmpl, TC, EUNLINK);
+ DEFINE_PREFIXED_CONSTANT(tmpl, TC, ERENAME);
+ DEFINE_PREFIXED_CONSTANT(tmpl, TC, EMKDIR);
+ DEFINE_PREFIXED_CONSTANT(tmpl, TC, ERMDIR);
+ DEFINE_PREFIXED_CONSTANT(tmpl, TC, EKEEP);
+ DEFINE_PREFIXED_CONSTANT(tmpl, TC, ENOREC);
+ DEFINE_PREFIXED_CONSTANT(tmpl, TC, EMISC);
}
-inline Local<Array> tclisttoary (TCLIST *list) {
- HandleScope scope;
- int len = tclistnum(list);
- Local<Array> ary = Array::New(len);
- for (int i = 0; i < len; i++) {
- ary->Set(Integer::New(i), String::New(tclistval2(list, i)));
- }
- return ary;
-}
+// Database wrapper (interfaces for database objects, all included)
+class DBWrap : public ObjectWrap {
+ public:
+ // these methods must be overridden in individual DB classes
+ virtual int Ecode () { assert(false); }
+ virtual const char * Errmsg (int ecode) { assert(false); }
+ virtual bool Setmutex () { assert(false); }
+ virtual bool Tune (int64_t bnum, int8_t apow, int8_t fpow, uint8_t opts) { assert(false); } // for HDB
+ virtual bool Tune (int32_t lmemb, int32_t nmemb, int64_t bnum, int8_t apow, int8_t fpow, uint8_t opts) { assert(false); } // for BDB
+ virtual bool Setcache (int32_t rcnum) { assert(false); } // for HDB
+ virtual bool Setcache (int32_t lcnum, int32_t ncnum) { assert(false); } // for BDB
+ virtual bool Setxmsiz (int64_t xmsiz) { assert(false); }
+ virtual bool Setdfunit (int32_t dfunit) { assert(false); }
+ virtual bool Open (char *path, int omode) { assert(false); }
+ virtual bool Close () { assert(false); }
+ virtual bool Put(char *kbuf, int ksiz, char *vbuf, int vsiz) { assert(false); }
+ virtual bool Putkeep(char *kbuf, int ksiz, char *vbuf, int vsiz) { assert(false); }
+ virtual bool Putcat(char *kbuf, int ksiz, char *vbuf, int vsiz) { assert(false); }
+ virtual bool Putasync(char *kbuf, int ksiz, char *vbuf, int vsiz) { assert(false); }
+ virtual bool Putdup(char *kbuf, int ksiz, char *vbuf, int vsiz) { assert(false); } // for BDB
+ virtual bool Putlist(char *kbuf, int ksiz, TCLIST *list) { assert(false); } // for BDB
+ virtual bool Out(char *kbuf, int ksiz) { assert(false); }
+ virtual bool Outlist(char *kbuf, int ksiz) { assert(false); } // for BDB
+ virtual char * Get(char *kbuf, int ksiz, int *vsiz_p) { assert(false); }
+ virtual TCLIST * Getlist(char *kbuf, int ksiz) { assert(false); } // for BDB
+ virtual int Vnum (char *kbuf, int ksiz) { assert(false); }
+ virtual int Vsiz(char *kbuf, int ksiz) { assert(false); }
+ virtual TCLIST * Range(void *bkbuf, int bksiz, bool binc, char *ekbuf, int eksiz, bool einc, int max) {} // for BDB
+ virtual bool Iterinit () { assert(false); }
+ virtual char * Iternext (int *vsiz_p) { assert(false); }
+ virtual TCLIST * Fwmkeys(char *kbuf, int ksiz, int max) { assert(false); }
+ virtual int Addint(char *kbuf, int ksiz, int num) { assert(false); }
+ virtual double Adddouble(char *kbuf, int ksiz, double num) { assert(false); }
+ virtual bool Sync () { assert(false); }
+ virtual bool Optimize (int64_t bnum, int8_t apow, int8_t fpow, uint8_t opts) { assert(false); } // for HDB
+ virtual bool Optimize (int32_t lmemb, int32_t nmemb, int64_t bnum, int8_t apow, int8_t fpow, uint8_t opts) { assert(false); } // for HDB
+ virtual bool Vanish () { assert(false); }
+ virtual bool Copy (char *path) { assert(false); }
+ virtual bool Tranbegin () { assert(false); }
+ virtual bool Trancommit () { assert(false); }
+ virtual bool Tranabort () { assert(false); }
+ virtual const char* Path () { assert(false); }
+ virtual uint64_t Rnum () { assert(false); }
+ virtual uint64_t Fsiz () { assert(false); }
-inline TCMAP* objtotcmap (const Handle<Object> obj) {
- HandleScope scope;
- TCMAP *map = tcmapnew2(31);
- Local<Array> keys = obj->GetPropertyNames();
- int len = keys->Length();
- Local<Value> key, val;
- for (int i = 0; i < len; i++) {
- key = keys->Get(Integer::New(i));
- val = obj->Get(key);
- if (NOU(val)) continue;
- tcmapput(map, VSTRPTR(key), VSTRSIZ(key), VSTRPTR(val), VSTRSIZ(val));
- }
- return map;
-}
+ protected:
+ class ArgsData {
+ protected:
+ DBWrap *db;
-inline Local<Object> tcmaptoobj (TCMAP *map) {
- const char *kbuf, *vbuf;
- int ksiz, vsiz;
- HandleScope scope;
- Local<Object> obj = Object::New();
- tcmapiterinit(map);
- for (;;) {
- kbuf = static_cast<const char*>(tcmapiternext(map, &ksiz));
- if (kbuf == NULL) break;
- vbuf = static_cast<const char*>(tcmapiterval(kbuf, &vsiz));
- obj->Set(String::New(kbuf, ksiz), String::New(vbuf, vsiz));
- }
- return obj;
-}
+ /* This is defined only because AsyncData(Handle<Value>) constructor
+ * gives compile error without this, but in fact AsyncData does not
+ * use it. See OpenAsyncData or GetData, for example. */
+ ArgsData () {
+ assert(false);
+ }
-// object that gets passed to libeio async functions
-class AsyncDataCore {
- public:
- Persistent<Function> cb;
- bool hasCallback;
+ public:
+ static bool
+ checkArgs (const Arguments& args) {
+ return true;
+ }
- AsyncDataCore (Handle<Value> cb_) {
- HandleScope scope;
- if (cb_->IsFunction()) {
- hasCallback = true;
- cb = Persistent<Function>::New(Handle<Function>::Cast(cb_));
- } else {
- hasCallback = false;
- }
- }
+ ArgsData (const Arguments& args) {
+ db = Unwrap<DBWrap>(args.This());
+ }
- ~AsyncDataCore () {
- if (hasCallback) cb.Dispose();
- }
+ int
+ ecode () {
+ return db->Ecode();
+ }
+ };
- inline void
- callback (int argc, Handle<Value> argv[]) {
- TryCatch try_catch;
- cb->Call(Context::GetCurrent()->Global(), argc, argv);
- if (try_catch.HasCaught()) {
- FatalException(try_catch);
- }
- }
+ class AsyncData : public virtual ArgsData {
+ public:
+ Persistent<Function> cb;
+ bool hasCallback;
+
+ AsyncData (Handle<Value> cb_) {
+ HandleScope scope;
+ assert(db); // make sure ArgsData is already initialized with This value
+ db->Ref();
+ if (cb_->IsFunction()) {
+ hasCallback = true;
+ cb = Persistent<Function>::New(Handle<Function>::Cast(cb_));
+ } else {
+ hasCallback = false;
+ }
+ }
- inline void
- callCallback (Handle<Value> arg0) {
- HandleScope scope;
- Handle<Value> args[1] = {arg0};
- callback(1, args);
- }
+ virtual
+ ~AsyncData () {
+ db->Unref();
+ cb.Dispose();
+ }
- inline void
- callCallback (Handle<Value> arg0, Handle<Value>arg1) {
- HandleScope scope;
- Handle<Value> args[2] = {arg0, arg1};
- callback(2, args);
- }
-};
+ inline void
+ callback (int argc, Handle<Value> argv[]) {
+ TryCatch try_catch;
+ cb->Call(Context::GetCurrent()->Global(), argc, argv);
+ if (try_catch.HasCaught()) {
+ FatalException(try_catch);
+ }
+ }
-// arguments data that will be multiply inherited to make actual async objects
-class ArgsData {
- public:
- static bool
- checkArgs (const Arguments& args) {
- return true;
- }
-};
+ inline void
+ callCallback (Handle<Value> arg0) {
+ HandleScope scope;
+ Handle<Value> args[1] = {arg0};
+ callback(1, args);
+ }
-class PathDataCore : public ArgsData {
- protected:
- String::Utf8Value path;
+ inline void
+ callCallback (Handle<Value> arg0, Handle<Value>arg1) {
+ HandleScope scope;
+ Handle<Value> args[2] = {arg0, arg1};
+ callback(2, args);
+ }
+ };
- public:
- PathDataCore (Handle<Value> path_) : path(path_) {}
+ class EcodeData : public ArgsData {
+ private:
+ int ecode;
- static bool
- checkArgs (const Arguments& args) {
- return ARG0->IsString();
- }
-};
+ public:
+ EcodeData (const Arguments& args) : ArgsData(args) {}
-class OpenDataCore : public PathDataCore {
- protected:
- int omode;
+ bool run () {
+ ecode = db->Ecode();
+ return true;
+ }
- public:
- OpenDataCore (Handle<Value> path_) : PathDataCore(path_) {}
+ Handle<Value>
+ returnValue () {
+ HandleScope scope;
+ return Integer::New(ecode);
+ }
+ };
- static bool
- checkArgs (const Arguments& args) {
- return PathDataCore::checkArgs(args) &&
- (ARG1->IsUndefined() || ARG1->IsNumber());
- }
-};
+ class ErrmsgData : public ArgsData {
+ public:
+ int ecode;
+ const char *msg;
-// virtual inheritance of KeyDataCore and ValueDataCore from ArgsData
-// together solves ambiguity of checkArgs method of GetDataCore
-class KeyDataCore : public virtual ArgsData {
- protected:
- String::Utf8Value kbuf;
- int ksiz;
+ ErrmsgData (const Arguments& args) : ArgsData(args) {
+ ecode = ARG0->IsNumber() ? ARG0->Int32Value() : db->Ecode();
+ }
- public:
- KeyDataCore (Handle<Value> key) : kbuf(key) {
- ksiz = kbuf.length();
- }
-};
+ bool
+ run () {
+ msg = db->Errmsg(ecode);
+ return true;
+ }
-class VsizDataCore : public KeyDataCore {
- protected:
- int vsiz;
+ Handle<Value>
+ returnValue () {
+ return String::New(msg);
+ }
- public:
- VsizDataCore (Handle<Value> key) : KeyDataCore(key) {}
+ static bool
+ checkArgs (const Arguments& args) {
+ return ARG0->IsNumber() || ARG0->IsUndefined();
+ }
+ };
- Handle<Value>
- returnValue () {
- return Number::New(vsiz);
- }
-};
+ class SetmutexData : public ArgsData {
+ public:
+ SetmutexData (const Arguments& args) : ArgsData(args) {}
-class PutDataCore : public KeyDataCore {
- protected:
- String::Utf8Value vbuf;
- int vsiz;
+ bool run () {
+ return db->Setmutex();
+ }
+ };
- public:
- PutDataCore (Handle<Value> key, Handle<Value> val)
- : vbuf(val), KeyDataCore(key) {
- vsiz = vbuf.length();
- }
-};
+ class SetxmsizData : public ArgsData {
+ private:
+ int64_t xmsiz;
-class ValueDataCore : public virtual ArgsData {
- protected:
- char *vbuf;
- int vsiz;
+ public:
+ SetxmsizData (const Arguments& args) : ArgsData(args) {
+ xmsiz = ARG0->IsUndefined() ? -1 : ARG0->IntegerValue();
+ }
- public:
- ~ValueDataCore () {
- tcfree(vbuf);
- }
+ static bool
+ checkArgs (const Arguments& args) {
+ return ARG0->IsNumber() || ARG0->IsUndefined();
+ }
- Handle<Value>
- returnValue () {
- return vbuf == NULL ? Null() : String::New(vbuf, vsiz);
- }
-};
+ bool
+ run () {
+ return db->Setxmsiz(xmsiz);
+ }
+ };
-class GetDataCore : public KeyDataCore, public ValueDataCore {
- public:
- GetDataCore (Handle<Value> key) : KeyDataCore(key) {}
-};
+ class SetdfunitData : public ArgsData {
+ private:
+ int32_t dfunit;
-class GetListDataCore : public KeyDataCore {
- protected:
- TCLIST *list;
+ public:
+ SetdfunitData (const Arguments& args) : ArgsData(args) {
+ dfunit = ARG0->IsUndefined() ? -1 : ARG0->Int32Value();
+ }
- public:
- GetListDataCore (Handle<Value> key) : KeyDataCore(key) {}
+ static bool
+ checkArgs (const Arguments& args) {
+ return ARG0->IsNumber() || ARG0->IsUndefined();
+ }
- ~GetListDataCore () {
- tclistdel(list);
- }
+ bool
+ run () {
+ return db->Setdfunit(dfunit);
+ }
+ };
- Handle<Value>
- returnValue () {
- return tclisttoary(list);
- }
-};
+ class FilenameData : public virtual ArgsData {
+ protected:
+ String::Utf8Value path;
-class FwmkeysDataCore : public GetListDataCore {
- protected:
- int max;
+ public:
+ FilenameData (const Arguments& args) : path(args[0]), ArgsData(args) {}
- public:
- FwmkeysDataCore (Handle<Value> key, Handle<Value> max_)
- : GetListDataCore(key) {
- max = max_->Int32Value();
- }
+ static bool
+ checkArgs (const Arguments& args) {
+ return ARG0->IsString();
+ }
+ };
- static bool
- checkArgs (const Arguments& args) {
- return ARG1->IsNumber();
- }
-};
+ class CloseData : public virtual ArgsData {
+ public:
+ CloseData (const Arguments& args) : ArgsData(args) {}
-class AddintDataCore : public KeyDataCore {
- protected:
- int num;
+ bool run () {
+ return db->Close();
+ }
+ };
- public:
- AddintDataCore (Handle<Value> key, Handle<Value> num_)
- : KeyDataCore(key) {
- num = num_->Int32Value();
- }
+ class CloseAsyncData : public CloseData, public AsyncData {
+ public:
+ CloseAsyncData (const Arguments& args)
+ : CloseData(args), AsyncData(ARG0), ArgsData(args) {}
+ };
- static bool
- checkArgs (const Arguments& args) {
- return ARG1->IsNumber();
- }
+ class KeyData : public virtual ArgsData {
+ protected:
+ String::Utf8Value kbuf;
+ int ksiz;
- Handle<Value>
- returnValue () {
- return num == INT_MIN ? Null() : Integer::New(num);
- }
-};
-
-class AdddoubleDataCore : public KeyDataCore {
- protected:
- double num;
-
- public:
- AdddoubleDataCore (Handle<Value> key, Handle<Value> num_)
- : KeyDataCore(key) {
- num = num_->NumberValue();
- }
-
- static bool
- checkArgs (const Arguments& args) {
- return ARG1->IsNumber();
- }
-
- Handle<Value>
- returnValue () {
- return isnan(num) ? Null() : Number::New(num);
- }
-};
-
-
-// Tokyo Cabinet
-inline void set_ecodes (const Handle<FunctionTemplate> tmpl) {
- DEFINE_PREFIXED_CONSTANT(tmpl, TC, ESUCCESS);
- DEFINE_PREFIXED_CONSTANT(tmpl, TC, ETHREAD);
- DEFINE_PREFIXED_CONSTANT(tmpl, TC, EINVALID);
- DEFINE_PREFIXED_CONSTANT(tmpl, TC, ENOFILE);
- DEFINE_PREFIXED_CONSTANT(tmpl, TC, ENOPERM);
- DEFINE_PREFIXED_CONSTANT(tmpl, TC, EMETA);
- DEFINE_PREFIXED_CONSTANT(tmpl, TC, ERHEAD);
- DEFINE_PREFIXED_CONSTANT(tmpl, TC, EOPEN);
- DEFINE_PREFIXED_CONSTANT(tmpl, TC, ECLOSE);
- DEFINE_PREFIXED_CONSTANT(tmpl, TC, ETRUNC);
- DEFINE_PREFIXED_CONSTANT(tmpl, TC, ESYNC);
- DEFINE_PREFIXED_CONSTANT(tmpl, TC, ESTAT);
- DEFINE_PREFIXED_CONSTANT(tmpl, TC, ESEEK);
- DEFINE_PREFIXED_CONSTANT(tmpl, TC, EREAD);
- DEFINE_PREFIXED_CONSTANT(tmpl, TC, EWRITE);
- DEFINE_PREFIXED_CONSTANT(tmpl, TC, EMMAP);
- DEFINE_PREFIXED_CONSTANT(tmpl, TC, ELOCK);
- DEFINE_PREFIXED_CONSTANT(tmpl, TC, EUNLINK);
- DEFINE_PREFIXED_CONSTANT(tmpl, TC, ERENAME);
- DEFINE_PREFIXED_CONSTANT(tmpl, TC, EMKDIR);
- DEFINE_PREFIXED_CONSTANT(tmpl, TC, ERMDIR);
- DEFINE_PREFIXED_CONSTANT(tmpl, TC, EKEEP);
- DEFINE_PREFIXED_CONSTANT(tmpl, TC, ENOREC);
- DEFINE_PREFIXED_CONSTANT(tmpl, TC, EMISC);
-}
-
-class HDB : ObjectWrap {
- public:
- HDB () : ObjectWrap () {
- db = tchdbnew();
- }
-
- ~HDB () {
- tchdbdel(db);
- }
-
- static HDB *
- Unwrap (const Handle<Object> obj) {
- return ObjectWrap::Unwrap<HDB>(obj);
- }
-
- static TCHDB *
- Backend (const Handle<Object> obj) {
- return Unwrap(obj)->db;
- }
+ public:
+ KeyData (const Arguments& args) : kbuf(args[0]), ArgsData(args) {
+ ksiz = kbuf.length();
+ }
+ };
- static void
- Initialize (const Handle<Object> target) {
- HandleScope scope;
- Local<FunctionTemplate> tmpl = FunctionTemplate::New(New);
- tmpl->InstanceTemplate()->SetInternalFieldCount(1);
- set_ecodes(tmpl);
+ class PutData : public KeyData {
+ protected:
+ String::Utf8Value vbuf;
+ int vsiz;
- DEFINE_PREFIXED_CONSTANT(tmpl, HDB, TLARGE);
- DEFINE_PREFIXED_CONSTANT(tmpl, HDB, TDEFLATE);
- DEFINE_PREFIXED_CONSTANT(tmpl, HDB, TBZIP);
- DEFINE_PREFIXED_CONSTANT(tmpl, HDB, TTCBS);
- DEFINE_PREFIXED_CONSTANT(tmpl, HDB, OREADER);
- DEFINE_PREFIXED_CONSTANT(tmpl, HDB, OWRITER);
- DEFINE_PREFIXED_CONSTANT(tmpl, HDB, OCREAT);
- DEFINE_PREFIXED_CONSTANT(tmpl, HDB, OTRUNC);
- DEFINE_PREFIXED_CONSTANT(tmpl, HDB, ONOLCK);
- DEFINE_PREFIXED_CONSTANT(tmpl, HDB, OLCKNB);
- DEFINE_PREFIXED_CONSTANT(tmpl, HDB, OTSYNC);
+ public:
+ PutData (const Arguments& args) : vbuf(args[1]), KeyData(args) {
+ vsiz = vbuf.length();
+ }
- NODE_SET_PROTOTYPE_METHOD(tmpl, "errmsg", Errmsg);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "ecode", Ecode);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "tune", Tune);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "setcache", Setcache);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "setxmsiz", Setxmsiz);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "setdfunit", Setdfunit);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "setmutex", Setmutex);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "open", Open);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "openAsync", OpenAsync);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "close", Close);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "closeAsync", CloseAsync);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "put", Put);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "putAsync", PutAsync);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "putkeep", Putkeep);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "putkeepAsync", PutkeepAsync);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "putcat", Putcat);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "putcatAsync", PutcatAsync);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "putasync", Putasync);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "putasyncAsync", PutasyncAsync);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "out", Out);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "outAsync", OutAsync);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "get", Get);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "getAsync", GetAsync);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "vsiz", Vsiz);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "vsizAsync", VsizAsync);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "iterinit", Iterinit);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "iterinitAsync", IterinitAsync);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "iternext", Iternext);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "iternextAsync", IternextAsync);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "fwmkeys", Fwmkeys);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "fwmkeysAsync", FwmkeysAsync);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "addint", Addint);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "addintAsync", AddintAsync);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "adddouble", Adddouble);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "adddoubleAsync", AdddoubleAsync);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "sync", Sync);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "syncAsync", SyncAsync);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "optimize", Optimize);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "optimizeAsync", OptimizeAsync);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "vanish", Vanish);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "vanishAsync", VanishAsync);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "copy", Copy);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "copyAsync", CopyAsync);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "tranbegin", Tranbegin);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "tranbeginAsync", TranbeginAsync);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "trancommit", Trancommit);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "trancommitAsync", TrancommitAsync);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "tranabort", Tranabort);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "tranabortAsync", TranabortAsync);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "path", Path);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "rnum", Rnum);
- NODE_SET_PROTOTYPE_METHOD(tmpl, "fsiz", Fsiz);
+ bool
+ run () {
+ return db->Put(*kbuf, ksiz, *vbuf, vsiz);
+ }
+ };
- target->Set(String::New("HDB"), tmpl->GetFunction());
- }
+ class PutAsyncData : public PutData, public AsyncData {
+ public:
+ PutAsyncData (const Arguments& args)
+ : PutData(args), AsyncData(args[2]), ArgsData(args) {}
+ };
- private:
- TCHDB *db;
+ class PutkeepData : public PutData {
+ public:
+ PutkeepData (const Arguments& args) : PutData(args) {}
- class ThisData {
- protected:
- HDB *hdb;
+ bool
+ run () {
+ return db->Putkeep(*kbuf, ksiz, *vbuf, vsiz);
+ }
+ };
- /* only used from AsyncData(Handle<Value>) constructor */
- ThisData () {}
+ class PutkeepAsyncData : public PutkeepData, public AsyncData {
+ public:
+ PutkeepAsyncData (const Arguments& args)
+ : PutkeepData(args), AsyncData(args[2]), ArgsData(args) {}
+ };
+ class PutcatData : public PutData {
public:
- ThisData (Handle<Object> This) {
- hdb = Unwrap(This);
- }
+ PutcatData (const Arguments& args) : PutData(args) {}
- int
- ecode () {
- return tchdbecode(hdb->db);
+ bool
+ run () {
+ return db->Putcat(*kbuf, ksiz, *vbuf, vsiz);
}
};
- class AsyncData : public virtual ThisData, public AsyncDataCore {
+ class PutcatAsyncData : public PutcatData, public AsyncData {
+ public:
+ PutcatAsyncData (const Arguments& args)
+ : PutcatData(args), AsyncData(args[2]), ArgsData(args) {}
+ };
+
+ class PutasyncData : public PutData {
public:
- /* make sure ThisData is already initialized with This arg.
- * see, for example, OpenAsyncData constructor
- * note: it implicitly calls ArgsData(void) constructor */
- AsyncData (Handle<Value> cb_) : AsyncDataCore (cb_) {
- assert(hdb);
- hdb->Ref();
- }
+ PutasyncData (const Arguments& args) : PutData(args) {}
- virtual
- ~AsyncData () {
- hdb->Unref();
+ bool
+ run () {
+ return db->Putasync(*kbuf, ksiz, *vbuf, vsiz);
}
};
- static Handle<Value>
- New (const Arguments& args) {
- HandleScope scope;
- if (!args.IsConstructCall()) return args.Callee()->NewInstance();
- (new HDB)->Wrap(THIS);
- return THIS;
- }
+ class PutasyncAsyncData : public PutasyncData, public AsyncData {
+ public:
+ PutasyncAsyncData (const Arguments& args)
+ : PutasyncData(args), AsyncData(args[2]), ArgsData(args) {}
+ };
- static Handle<Value>
- Errmsg (const Arguments& args) {
- HandleScope scope;
- if (!(ARG0->IsUndefined() || ARG0->IsNumber())) {
- return THROW_BAD_ARGS;
- }
- const char *msg = tchdberrmsg(
- ARG0->IsUndefined() ? tchdbecode(Backend(THIS)) : VINT32(ARG0));
- return String::New(msg);
- }
+ class PutdupData : public PutData {
+ public:
+ PutdupData (const Arguments& args) : PutData(args) {}
- static Handle<Value>
- Ecode (const Arguments& args) {
- HandleScope scope;
- int ecode = tchdbecode(Backend(THIS));
- return Integer::New(ecode);
- }
+ bool
+ run () {
+ return db->Putdup(*kbuf, ksiz, *vbuf, vsiz);
+ }
+ };
- static Handle<Value>
- Setmutex (const Arguments& args) {
- HandleScope scope;
- bool success = tchdbsetmutex(
- Backend(THIS));
- return Boolean::New(success);
- }
+ class PutdupAsyncData : public PutdupData, public AsyncData {
+ public:
+ PutdupAsyncData (const Arguments& args)
+ : PutdupData(args), AsyncData(args[2]), ArgsData(args) {}
+ };
- class TuneData : public ArgsData, public virtual ThisData {
- protected:
- int64_t bnum;
- int8_t apow;
- int8_t fpow;
- uint8_t opts;
+ class PutlistData : public KeyData {
+ private:
+ TCLIST *list;
public:
- static bool checkArgs (const Arguments& args) {
- return (ARG0->IsNumber() || NOU(ARG0)) &&
- (ARG1->IsNumber() || NOU(ARG1)) &&
- (ARG2->IsNumber() || NOU(ARG2)) &&
- (ARG3->IsNumber() || NOU(ARG3));
+ PutlistData (const Arguments& args) : KeyData(args) {
+ HandleScope scope;
+ list = arytotclist(Handle<Array>::Cast(ARG0));
}
- TuneData (const Arguments& args) : ThisData(THIS) {
- bnum = NOU(ARG0) ? -1 : ARG0->IntegerValue();
- apow = NOU(ARG1) ? -1 : ARG1->Int32Value();
- fpow = NOU(ARG2) ? -1 : ARG2->Int32Value();
- opts = NOU(ARG3) ? UINT8_MAX : ARG3->Int32Value();
+ ~PutlistData () {
+ tclistdel(list);
}
- bool run () {
- return tchdbtune(hdb->db, bnum, apow, fpow, opts);
+ bool
+ run () {
+ return db->Putlist(*kbuf, ksiz, list);
}
};
- DEFINE_SYNC(Tune)
-
- static Handle<Value>
- Setcache (const Arguments& args) {
- HandleScope scope;
- if (!(ARG0->IsNumber() || ARG0->IsUndefined())) {
- return THROW_BAD_ARGS;
- }
- bool success = tchdbsetcache(
- Backend(THIS),
- ARG0->IsUndefined() ? -1 : VINT32(ARG0));
- return Boolean::New(success);
- }
-
- static Handle<Value>
- Setxmsiz (const Arguments& args) {
- HandleScope scope;
- if (!(ARG0->IsNumber() || ARG0->IsUndefined())) {
- return THROW_BAD_ARGS;
- }
- bool success = tchdbsetxmsiz(
- Backend(THIS),
- ARG0->IsUndefined() ? -1 : VINT64(ARG0));
- return Boolean::New(success);
- }
+ class PutlistAsyncData : public PutlistData, public AsyncData {
+ public:
+ PutlistAsyncData (const Arguments& args)
+ : PutlistData(args), AsyncData(args[2]), ArgsData(args) {}
+ };
- static Handle<Value>
- Setdfunit (const Arguments& args) {
- HandleScope scope;
- if (!(ARG0->IsNumber() || ARG0->IsUndefined())) {
- return THROW_BAD_ARGS;
- }
- bool success = tchdbsetdfunit(
- Backend(THIS),
- ARG0->IsUndefined() ? -1 : VINT32(ARG0));
- return Boolean::New(success);
- }
+ class ValueData : public virtual ArgsData {
+ protected:
+ char *vbuf;
+ int vsiz;
- class OpenData : public OpenDataCore, public virtual ThisData {
public:
- OpenData (const Arguments& args)
- : OpenDataCore(ARG0), ThisData(THIS) {
- omode = ARG1->IsUndefined() ? HDBOREADER : VINT32(ARG1);
+ ~ValueData () {
+ tcfree(vbuf);
}
- bool run () {
- return tchdbopen(hdb->db, *path, omode);
+ Handle<Value>
+ returnValue () {
+ return vbuf == NULL ? Null() : String::New(vbuf, vsiz);
}
};
- DEFINE_SYNC(Open)
-
- class OpenAsyncData : public OpenData, public AsyncData {
+ class OutData : public KeyData {
public:
- OpenAsyncData (const Arguments& args)
- : OpenData(args), AsyncData(ARG2), ThisData(THIS) {}
+ OutData (const Arguments& args) : KeyData(args) {}
+
+ bool
+ run () {
+ return db->Out(*kbuf, ksiz);
+ }
};
- DEFINE_ASYNC(Open)
+ class OutAsyncData : public OutData, public AsyncData {
+ public:
+ OutAsyncData (const Arguments& args)
+ : OutData(args), AsyncData(args[1]), ArgsData(args) {}
+ };
- class CloseData : public ArgsData, public virtual ThisData {
+ class OutlistData : public KeyData {
public:
- CloseData (const Arguments& args) : ThisData(THIS) {}
+ OutlistData (const Arguments& args) : KeyData(args) {}
- bool run () {
- return tchdbclose(hdb->db);
+ bool
+ run () {
+ return db->Outlist(*kbuf, ksiz);
}
};
- DEFINE_SYNC(Close)
-
- class CloseAsyncData : public CloseData, public AsyncData {
+ class OutlistAsyncData : public OutlistData, public AsyncData {
public:
- CloseAsyncData (const Arguments& args)
- : CloseData(args), AsyncData(ARG0), ThisData(THIS) {}
+ OutlistAsyncData (const Arguments& args)
+ : OutlistData(args), AsyncData(args[1]), ArgsData(args) {}
};
- DEFINE_ASYNC(Close)
-
- class PutData : public PutDataCore, public virtual ThisData {
+ class GetData : public KeyData, public ValueData {
public:
- PutData (const Arguments& args)
- : PutDataCore(ARG0, ARG1), ThisData(THIS) {}
+ GetData (const Arguments& args) : KeyData(args), ArgsData(args) {}
- bool run () {
- return tchdbput(hdb->db, *kbuf, ksiz, *vbuf, vsiz);
+ bool
+ run () {
+ vbuf = db->Get(*kbuf, ksiz, &vsiz);
+ return vbuf != NULL;
}
};
- DEFINE_SYNC(Put)
-
- class PutAsyncData : public PutData, public AsyncData {
+ class GetAsyncData : public GetData, public AsyncData {
public:
- PutAsyncData (const Arguments& args)
- : PutData(args), AsyncData(ARG2), ThisData(THIS) {}
+ GetAsyncData (const Arguments& args)
+ : GetData(args), AsyncData(args[1]), ArgsData(args) {}
};
- DEFINE_ASYNC(Put)
+ class GetlistData : public KeyData {
+ protected:
+ TCLIST *list;
- class PutkeepData : public PutData {
public:
- PutkeepData (const Arguments &args) : PutData(args) {}
+ GetlistData (const Arguments& args) : KeyData(args) {}
- bool run () {
- return tchdbputkeep(hdb->db, *kbuf, ksiz, *vbuf, vsiz);
+ ~GetlistData () {
+ tclistdel(list);
}
- };
- DEFINE_SYNC(Putkeep)
+ bool
+ run () {
+ list = db->Getlist(*kbuf, ksiz);
+ return true;
+ }
- class PutkeepAsyncData : public PutkeepData, public AsyncData {
+ Handle<Value>
+ returnValue () {
+ return tclisttoary(list);
+ }
+ };
+
+ class GetlistAsyncData : public GetlistData, public AsyncData {
public:
- PutkeepAsyncData (const Arguments& args)
- : PutkeepData(args), AsyncData(ARG2), ThisData(THIS) {}
+ GetlistAsyncData (const Arguments& args)
+ : GetlistData(args), AsyncData(args[1]), ArgsData(args) {}
};
- DEFINE_ASYNC(Putkeep)
+ class FwmkeysData : public GetlistData {
+ protected:
+ int max;
- class PutcatData : public PutData {
public:
- PutcatData (const Arguments &args) : PutData(args) {}
+ FwmkeysData (const Arguments& args) : GetlistData(args) {
+ max = args[1]->Int32Value();
+ }
- bool run () {
- return tchdbputcat(hdb->db, *kbuf, ksiz, *vbuf, vsiz);
+ bool
+ run () {
+ list = db->Fwmkeys(*kbuf, ksiz, max);
+ return true;
}
- };
- DEFINE_SYNC(Putcat)
+ static bool
+ checkArgs (const Arguments& args) {
+ return ARG1->IsNumber();
+ }
+ };
- class PutcatAsyncData : public PutcatData, public AsyncData {
+ class FwmkeysAsyncData : public FwmkeysData, public AsyncData {
public:
- PutcatAsyncData (const Arguments& args)
- : PutcatData(args), AsyncData(ARG2), ThisData(THIS) {}
+ FwmkeysAsyncData (const Arguments& args)
+ : FwmkeysData(args), AsyncData(args[1]), ArgsData(args) {}
};
- DEFINE_ASYNC(Putcat)
+ class VnumData : public KeyData {
+ protected:
+ int vnum;
- class PutasyncData : public PutData {
public:
- PutasyncData (const Arguments &args) : PutData(args) {}
+ VnumData (const Arguments& args) : KeyData(args) {}
- bool run () {
- return tchdbputasync(hdb->db, *kbuf, ksiz, *vbuf, vsiz);
+ bool
+ run () {
+ vnum = db->Vnum(*kbuf, ksiz);
+ return vnum != 0;
}
- };
- DEFINE_SYNC(Putasync)
+ Handle<Value>
+ returnValue () {
+ HandleScope scope; // really needed?
+ return Number::New(vnum);
+ }
+ };
- class PutasyncAsyncData : public PutasyncData, public AsyncData {
+ class VnumAsyncData : public VnumData, public AsyncData {
public:
- PutasyncAsyncData (const Arguments& args)
- : PutasyncData(args), AsyncData(ARG2), ThisData(THIS) {}
+ VnumAsyncData (const Arguments& args)
+ : VnumData(args), AsyncData(args[1]), ArgsData(args) {}
};
- DEFINE_ASYNC(Putasync)
+ class VsizData : public KeyData {
+ protected:
+ int vsiz;
- class OutData : public KeyDataCore, public virtual ThisData {
public:
- OutData (const Arguments &args)
- : KeyDataCore(ARG0), ThisData(THIS) {}
+ VsizData (const Arguments& args) : KeyData(args) {}
- bool run () {
- return tchdbout(hdb->db, *kbuf, ksiz);
+ bool
+ run () {
+ vsiz = db->Vsiz(*kbuf, ksiz);
+ return vsiz != -1;
}
- };
- DEFINE_SYNC(Out)
+ Handle<Value>
+ returnValue () {
+ HandleScope scope; // really needed?
+ return Number::New(vsiz);
+ }
+ };
- class OutAsyncData : public OutData, public AsyncData {
+ class VsizAsyncData : public VsizData, public AsyncData {
public:
- OutAsyncData (const Arguments& args)
- : OutData(args), AsyncData(ARG1), ThisData(THIS) {}
+ VsizAsyncData (const Arguments& args)
+ : VsizData(args), AsyncData(args[1]), ArgsData(args) {}
};
- DEFINE_ASYNC(Out)
+ class RangeData : public virtual ArgsData {
+ protected:
+ String::Utf8Value bkbuf;
+ int bksiz;
+ bool binc;
+ String::Utf8Value ekbuf;
+ int eksiz;
+ bool einc;
+ int max;
+ TCLIST *list;
- class GetData : public GetDataCore, public virtual ThisData {
public:
- GetData (const Arguments &args)
- : GetDataCore(ARG0), ThisData(THIS) {}
+ static bool
+ checkArgs (const Arguments& args) {
+ return (NOU(ARG1) || ARG1->IsBoolean()) &&
+ (NOU(ARG3) || ARG3->IsBoolean()) &&
+ (NOU(ARG4) || ARG4->IsNumber());
+ }
- bool run () {
- vbuf = static_cast<char *>(tchdbget(hdb->db, *kbuf, ksiz, &vsiz));
- return vbuf != NULL;
+ RangeData (const Arguments& args)
+ : bkbuf(ARG0), ekbuf(ARG2), ArgsData(args) {
+ bksiz = ARG0->IsNull() ? -1 : bkbuf.length();
+ binc = ARG1->BooleanValue();
+ eksiz = ARG2->IsNull() ? -1 : ekbuf.length();
+ einc = ARG3->BooleanValue();
+ max = NOU(ARG4) ? -1 : ARG4->Int32Value();
}
- };
- DEFINE_SYNC2(Get)
+ ~RangeData () {
+ tclistdel(list);
+ }
- class GetAsyncData : public GetData, public AsyncData {
+ bool
+ run () {
+ list = db->Range(bksiz == -1 ? NULL : *bkbuf, bksiz, binc,
+ eksiz == -1 ? NULL : *ekbuf, eksiz, einc, max);
+ return true;
+ }
+
+ Handle<Value>
+ returnValue () {
+ HandleScope scope;
+ return tclisttoary(list);
+ }
+ };
+
+ class RangeAsyncData : public RangeData, public AsyncData {
public:
- GetAsyncData (const Arguments& args)
- : GetData(args), AsyncData(ARG1), ThisData(THIS) {}
+ RangeAsyncData (const Arguments& args)
+ : RangeData(args), AsyncData(args[5]), ArgsData(args) {}
};
- DEFINE_ASYNC2(Get)
+ class AddintData : public KeyData {
+ protected:
+ int num;
- class VsizData : public VsizDataCore, public virtual ThisData {
public:
- VsizData (const Arguments &args)
- : VsizDataCore(ARG0), ThisData(THIS) {}
+ AddintData (const Arguments& args) : KeyData(args) {
+ num = args[1]->Int32Value();
+ }
- bool run () {
- vsiz = tchdbvsiz(hdb->db, *kbuf, ksiz);
- return vsiz != -1;
+ static bool
+ checkArgs (const Arguments& args) {
+ return ARG1->IsNumber();
}
- };
- DEFINE_SYNC2(Vsiz)
+ bool
+ run () {
+ num = db->Addint(*kbuf, ksiz, num);
+ return num != INT_MIN;
+ }
- class VsizAsyncData : public VsizData, public AsyncData {
- public:
- VsizAsyncData (const Arguments& args)
- : VsizData(args), AsyncData(ARG1), ThisData(THIS) {}
+ Handle<Value>
+ returnValue () {
+ return num == INT_MIN ? Null() : Integer::New(num);
+ }
};
- DEFINE_ASYNC2(Vsiz)
+ class AddintAsyncData : public AddintData, public AsyncData {
+ public:
+ AddintAsyncData (const Arguments& args)
+ : AddintData(args), AsyncData(args[2]), ArgsData(args) {}
+ };
- class IterinitData : public ArgsData, public virtual ThisData {
+ class IterinitData : public virtual ArgsData {
public:
- IterinitData (const Arguments& args) : ThisData(THIS) {}
+ IterinitData (const Arguments& args) : ArgsData(args) {}
bool run () {
- return tchdbiterinit(hdb->db);
+ return db->Iterinit();
}
};
- DEFINE_SYNC(Iterinit)
-
class IterinitAsyncData : public IterinitData, public AsyncData {
public:
IterinitAsyncData (const Arguments& args)
- : IterinitData(args), AsyncData(ARG0), ThisData(THIS) {}
+ : IterinitData(args), AsyncData(ARG0), ArgsData(args) {}
};
- DEFINE_ASYNC(Iterinit)
-
- class IternextData : public ValueDataCore, public virtual ThisData {
+ class IternextData : public ValueData {
public:
- IternextData (const Arguments& args) : ThisData(THIS) {}
+ IternextData (const Arguments& args) {}
bool run () {
- vbuf = static_cast<char *>(tchdbiternext(hdb->db, &vsiz));
+ vbuf = db->Iternext(&vsiz);
return vbuf != NULL;
}
};
- DEFINE_SYNC2(Iternext)
-
class IternextAsyncData : public IternextData, public AsyncData {
public:
IternextAsyncData (const Arguments& args)
- : IternextData(args), AsyncData(ARG0), ThisData(THIS) {}
+ : IternextData(args), AsyncData(ARG0), ArgsData(args) {}
};
- DEFINE_ASYNC2(Iternext)
+ class AdddoubleData : public KeyData {
+ protected:
+ double num;
- class FwmkeysData : public FwmkeysDataCore, public virtual ThisData {
public:
- FwmkeysData (const Arguments& args)
- : FwmkeysDataCore(ARG0, ARG1), ThisData(THIS) {}
+ AdddoubleData (const Arguments& args) : KeyData(args) {
+ num = args[1]->NumberValue();
+ }
- bool run () {
- list = tchdbfwmkeys(hdb->db, *kbuf, ksiz, max);
- return true;
+ static bool
+ checkArgs (const Arguments& args) {
+ return ARG1->IsNumber();
}
- };
- DEFINE_SYNC2(Fwmkeys)
+ bool
+ run () {
+ num = db->Adddouble(*kbuf, ksiz, num);
+ return isnan(num);
+ }
- class FwmkeysAsyncData : public FwmkeysData, public AsyncData {
- public:
- FwmkeysAsyncData (const Arguments& args)
- : FwmkeysData(args), AsyncData(ARG2), ThisData(THIS) {}
+ Handle<Value>
+ returnValue () {
+ return isnan(num) ? Null() : Number::New(num);
+ }
};
- DEFINE_ASYNC2(Fwmkeys)
+ class AdddoubleAsyncData : public AdddoubleData, public AsyncData {
+ public:
+ AdddoubleAsyncData (const Arguments& args)
+ : AdddoubleData(args), AsyncData(args[2]), ArgsData(args) {}
+ };
- class AddintData : public AddintDataCore, public virtual ThisData {
+ class SyncData : public virtual ArgsData {
public:
- AddintData (const Arguments& args)
- : AddintDataCore(ARG0, ARG1), ThisData(THIS) {}
+ SyncData (const Arguments& args) : ArgsData(args) {}
bool run () {
- num = tchdbaddint(hdb->db, *kbuf, ksiz, num);
- return num != INT_MIN;
+ return db->Sync();
}
};
- DEFINE_SYNC2(Addint)
-
- class AddintAsyncData : public AddintData, public AsyncData {
+ class SyncAsyncData : public SyncData, public AsyncData {
public:
- AddintAsyncData (const Arguments& args)
- : AddintData(args), AsyncData(ARG2), ThisData(THIS) {}
+ SyncAsyncData (const Arguments& args)
+ : SyncData(args), AsyncData(ARG0), ArgsData(args) {}
};
- DEFINE_ASYNC2(Addint)
-
- class AdddoubleData : public AdddoubleDataCore, public virtual ThisData {
+ class VanishData : public virtual ArgsData {
public:
- AdddoubleData (const Arguments& args)
- : AdddoubleDataCore(ARG0, ARG1), ThisData(THIS) {}
+ VanishData (const Arguments& args) : ArgsData(args) {}
bool run () {
- num = tchdbadddouble(hdb->db, *kbuf, ksiz, num);
- return !isnan(num);
+ return db->Vanish();
}
};
- DEFINE_SYNC2(Adddouble)
-
- class AdddoubleAsyncData : public AdddoubleData, public AsyncData {
+ class VanishAsyncData : public VanishData, public AsyncData {
public:
- AdddoubleAsyncData (const Arguments& args)
- : AdddoubleData(args), AsyncData(ARG2), ThisData(THIS) {}
+ VanishAsyncData (const Arguments& args)
+ : VanishData(args), AsyncData(ARG0), ArgsData(args) {}
};
- DEFINE_ASYNC2(Adddouble)
-
- class SyncData : public ArgsData, public virtual ThisData {
+ class CopyData : public FilenameData {
public:
- SyncData (const Arguments& args) : ThisData(THIS) {}
+ CopyData (const Arguments& args) : FilenameData(args) {}
- bool run () {
- return tchdbsync(hdb->db);
+ bool
+ run () {
+ return db->Copy(*path);
}
};
- DEFINE_SYNC(Sync)
-
- class SyncAsyncData : public SyncData, public AsyncData {
+ class CopyAsyncData : public CopyData, public AsyncData {
public:
- SyncAsyncData (const Arguments& args)
- : SyncData(args), AsyncData(ARG0), ThisData(THIS) {}
+ CopyAsyncData (const Arguments& args)
+ : CopyData(args), AsyncData(args[2]), ArgsData(args) {}
};
- DEFINE_ASYNC(Sync)
-
- class OptimizeData : public TuneData {
+ class TranbeginData : public virtual ArgsData {
public:
- OptimizeData (const Arguments& args) : TuneData(args) {}
+ TranbeginData (const Arguments& args) : ArgsData(args) {}
bool run () {
- return tchdboptimize(hdb->db, bnum, apow, fpow, opts);
+ return db->Tranbegin();
}
};
- DEFINE_SYNC(Optimize)
-
- class OptimizeAsyncData : public OptimizeData, public AsyncData {
+ class TranbeginAsyncData : public TranbeginData, public AsyncData {
public:
- OptimizeAsyncData (const Arguments& args)
- : OptimizeData(args), AsyncData(ARG4), ThisData(THIS) {}
+ TranbeginAsyncData (const Arguments& args)
+ : TranbeginData(args), AsyncData(ARG0), ArgsData(args) {}
};
- DEFINE_ASYNC(Optimize)
-
- class VanishData : public ArgsData, public virtual ThisData {
+ class TrancommitData : public virtual ArgsData {
public:
- VanishData (const Arguments& args) : ThisData(THIS) {}
+ TrancommitData (const Arguments& args) : ArgsData(args) {}
bool run () {
- return tchdbvanish(hdb->db);
+ return db->Trancommit();
}
};
- DEFINE_SYNC(Vanish)
-
- class VanishAsyncData : public VanishData, public AsyncData {
+ class TrancommitAsyncData : public TrancommitData, public AsyncData {
public:
- VanishAsyncData (const Arguments& args)
- : VanishData(args), AsyncData(ARG0), ThisData(THIS) {}
+ TrancommitAsyncData (const Arguments& args)
+ : TrancommitData(args), AsyncData(ARG0), ArgsData(args) {}
};
- DEFINE_ASYNC(Vanish)
-
- class CopyData : public PathDataCore, public virtual ThisData {
+ class TranabortData : public virtual ArgsData {
public:
- CopyData (const Arguments& args)
- : PathDataCore(ARG0), ThisData(THIS) {
- }
+ TranabortData (const Arguments& args) : ArgsData(args) {}
bool run () {
- return tchdbcopy(hdb->db, *path);
+ return db->Tranabort();
}
};
- DEFINE_SYNC(Copy)
-
- class CopyAsyncData : public CopyData, public AsyncData {
+ class TranabortAsyncData : public TranabortData, public AsyncData {
public:
- CopyAsyncData (const Arguments& args)
- : CopyData(args), AsyncData(ARG1), ThisData(THIS) {}
+ TranabortAsyncData (const Arguments& args)
+ : TranabortData(args), AsyncData(ARG0), ArgsData(args) {}
};
- DEFINE_ASYNC(Copy)
+ class PathData : public ArgsData {
+ private:
+ const char *path;
- class TranbeginData : public ArgsData, public virtual ThisData {
public:
- TranbeginData (const Arguments& args) : ThisData(THIS) {}
+ PathData (const Arguments& args) : ArgsData(args) {}
- bool run () {
- return tchdbtranbegin(hdb->db);
+ bool
+ run () {
+ path = db->Path();
+ return path != NULL;
}
- };
- DEFINE_SYNC(Tranbegin)
-
- class TranbeginAsyncData : public TranbeginData, public AsyncData {
- public:
- TranbeginAsyncData (const Arguments& args)
- : TranbeginData(args), AsyncData(ARG0), ThisData(THIS) {}
+ Handle<Value>
+ returnValue () {
+ HandleScope scope;
+ return path == NULL ? Null() : String::New(path);
+ }
};
- DEFINE_ASYNC(Tranbegin)
+ class RnumData : public ArgsData {
+ private:
+ uint64_t rnum;
- class TrancommitData : public ArgsData, public virtual ThisData {
public:
- TrancommitData (const Arguments& args) : ThisData(THIS) {}
+ RnumData (const Arguments& args) : ArgsData(args) {}
- bool run () {
- return tchdbtrancommit(hdb->db);
+ bool
+ run () {
+ rnum = db->Rnum();
+ // rnum == 0 when not connected to any database file
+ return rnum != 0;
}
- };
-
- DEFINE_SYNC(Trancommit)
- class TrancommitAsyncData : public TrancommitData, public AsyncData {
- public:
- TrancommitAsyncData (const Arguments& args)
- : TrancommitData(args), AsyncData(ARG0), ThisData(THIS) {}
+ Handle<Value>
+ returnValue () {
+ HandleScope scope;
+ // JavaScript integer cannot express uint64
+ // so this can't handle too large number
+ return Integer::New(rnum);
+ }
};
- DEFINE_ASYNC(Trancommit)
+ class FsizData : public ArgsData {
+ private:
+ uint64_t fsiz;
- class TranabortData : public ArgsData, public virtual ThisData {
public:
- TranabortData (const Arguments& args) : ThisData(THIS) {}
+ FsizData (const Arguments& args) : ArgsData(args) {}
- bool run () {
- return tchdbtranabort(hdb->db);
+ bool
+ run () {
+ fsiz = db->Fsiz();
+ // fsiz == 0 when not connected to any database file
+ return fsiz != 0;
}
- };
-
- DEFINE_SYNC(Tranabort)
- class TranabortAsyncData : public TranabortData, public AsyncData {
- public:
- TranabortAsyncData (const Arguments& args)
- : TranabortData(args), AsyncData(ARG0), ThisData(THIS) {}
+ Handle<Value>
+ returnValue () {
+ HandleScope scope;
+ // JavaScript integer cannot express uint64
+ // so this can't handle too large number
+ return Integer::New(fsiz);
+ }
};
+};
- DEFINE_ASYNC(Tranabort)
+class HDB : public DBWrap {
+ public:
+ HDB () {
+ hdb = tchdbnew();
+ }
- static Handle<Value>
- Path (const Arguments& args) {
- HandleScope scope;
- const char *path = tchdbpath(
- Backend(THIS));
- return path == NULL ? Null() : String::New(path);
+ ~HDB () {
+ tchdbdel(hdb);
}
- static Handle<Value>
- Rnum (const Arguments& args) {
+ static void
+ Initialize (const Handle<Object> target) {
HandleScope scope;
- int64_t num = tchdbrnum(
- Backend(THIS));
- return Integer::New(num);
+ Local<FunctionTemplate> tmpl = FunctionTemplate::New(New);
+ tmpl->InstanceTemplate()->SetInternalFieldCount(1);
+ set_ecodes(tmpl);
+
+ DEFINE_PREFIXED_CONSTANT(tmpl, HDB, TLARGE);
+ DEFINE_PREFIXED_CONSTANT(tmpl, HDB, TDEFLATE);
+ DEFINE_PREFIXED_CONSTANT(tmpl, HDB, TBZIP);
+ DEFINE_PREFIXED_CONSTANT(tmpl, HDB, TTCBS);
+ DEFINE_PREFIXED_CONSTANT(tmpl, HDB, OREADER);
+ DEFINE_PREFIXED_CONSTANT(tmpl, HDB, OWRITER);
+ DEFINE_PREFIXED_CONSTANT(tmpl, HDB, OCREAT);
+ DEFINE_PREFIXED_CONSTANT(tmpl, HDB, OTRUNC);
+ DEFINE_PREFIXED_CONSTANT(tmpl, HDB, ONOLCK);
+ DEFINE_PREFIXED_CONSTANT(tmpl, HDB, OLCKNB);
+ DEFINE_PREFIXED_CONSTANT(tmpl, HDB, OTSYNC);
+
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "errmsg", ErrmsgSync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "ecode", EcodeSync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "setmutex", SetmutexSync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "tune", TuneSync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "setcache", SetcacheSync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "setxmsiz", SetxmsizSync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "setdfunit", SetdfunitSync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "open", OpenSync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "openAsync", OpenAsync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "close", CloseSync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "closeAsync", CloseAsync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "put", PutSync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "putAsync", PutAsync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "putkeep", PutkeepSync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "putkeepAsync", PutkeepAsync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "putcat", PutcatSync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "putcatAsync", PutcatAsync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "putasync", PutasyncSync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "putasyncAsync", PutasyncAsync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "out", OutSync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "outAsync", OutAsync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "get", GetSync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "getAsync", GetAsync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "vsiz", VsizSync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "vsizAsync", VsizAsync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "iterinit", IterinitSync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "iterinitAsync", IterinitAsync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "iternext", IternextSync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "iternextAsync", IternextAsync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "fwmkeys", FwmkeysSync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "fwmkeysAsync", FwmkeysAsync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "addint", AddintSync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "addintAsync", AddintAsync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "adddouble", AdddoubleSync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "adddoubleAsync", AdddoubleAsync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "sync", SyncSync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "syncAsync", SyncAsync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "optimize", OptimizeSync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "optimizeAsync", OptimizeAsync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "vanish", VanishSync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "vanishAsync", VanishAsync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "copy", CopySync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "copyAsync", CopyAsync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "tranbegin", TranbeginSync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "tranbeginAsync", TranbeginAsync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "trancommit", TrancommitSync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "trancommitAsync", TrancommitAsync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "tranabort", TranabortSync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "tranabortAsync", TranabortAsync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "path", PathSync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "rnum", RnumSync);
+ NODE_SET_PROTOTYPE_METHOD(tmpl, "fsiz", FsizSync);
+
+ target->Set(String::New("HDB"), tmpl->GetFunction());
}
+ private:
+ TCHDB *hdb;
+
static Handle<Value>
- Fsiz (const Arguments& args) {
+ New (const Arguments& args) {
HandleScope scope;
- int64_t siz = tchdbfsiz(
- Backend(THIS));
- return Integer::New(siz);
+ if (!args.IsConstructCall()) return args.Callee()->NewInstance();
+ (new HDB)->Wrap(THIS);
+ return THIS;
}
-};
-
-class BDB : ObjectWrap {
- public:
- const static Persistent<FunctionTemplate> Tmpl;
- BDB () : ObjectWrap () {
- db = tcbdbnew();
+ int Ecode () {
+ return tchdbecode(hdb);
}
- ~BDB () {
- tcbdbdel(db);
+ DEFINE_SYNC2(Ecode)
+
+ const char * Errmsg (int ecode) {
+ return tchdberrmsg(ecode);
}
- static BDB *
- Unwrap (const Handle<Object> obj) {
- return ObjectWrap::Unwrap<BDB>(obj);
+ DEFINE_SYNC2(Errmsg)
+
+ bool Setmutex () {
+ return tchdbsetmutex(hdb);
}
- static TCBDB *
- Backend (const Handle<Object> obj) {
- return Unwrap(obj)->db;
+ DEFINE_SYNC(Setmutex)
+
+ bool Setcache (int32_t rcnum) {
+ return tchdbsetcache(hdb, rcnum);
+ }
+
+ class SetcacheData : public ArgsData {
+ private:
+ int32_t rcnum;
+
+ public:
+ SetcacheData (const Arguments& args) : ArgsData(args) {
+ rcnum = ARG0->IsUndefined() ? -1 : ARG0->Int32Value();
+ }
+
+ static bool
+ checkArgs (const Arguments& args) {
+ return ARG0->IsNumber() || ARG0->IsUndefined();
+ }
+
+ bool
+ run () {
+ return db->Setcache(rcnum);
+ }
+ };
+
+ DEFINE_SYNC(Setcache)
+
+ bool Setxmsiz (int64_t xmsiz) {
+ return tchdbsetxmsiz(hdb, xmsiz);
+ }
+
+ DEFINE_SYNC(Setxmsiz)
+
+ bool Setdfunit (int32_t dfunit) {
+ return tchdbsetdfunit(hdb, dfunit);
+ }
+
+ DEFINE_SYNC(Setdfunit)
+
+ bool Tune (int64_t bnum, int8_t apow, int8_t fpow, uint8_t opts) {
+ return tchdbtune(hdb, bnum, apow, fpow, opts);
+ }
+
+ class TuneData : public virtual ArgsData {
+ protected:
+ int64_t bnum;
+ int8_t apow;
+ int8_t fpow;
+ uint8_t opts;
+
+ public:
+ static bool checkArgs (const Arguments& args) {
+ return (ARG0->IsNumber() || NOU(ARG0)) &&
+ (ARG1->IsNumber() || NOU(ARG1)) &&
+ (ARG2->IsNumber() || NOU(ARG2)) &&
+ (ARG3->IsNumber() || NOU(ARG3));
+ }
+
+ TuneData (const Arguments& args) : ArgsData(args) {
+ bnum = NOU(ARG0) ? -1 : ARG0->IntegerValue();
+ apow = NOU(ARG1) ? -1 : ARG1->Int32Value();
+ fpow = NOU(ARG2) ? -1 : ARG2->Int32Value();
+ opts = NOU(ARG3) ? UINT8_MAX : ARG3->Int32Value();
+ }
+
+ bool run () {
+ return db->Tune(bnum, apow, fpow, opts);
+ }
+ };
+
+ DEFINE_SYNC(Tune)
+
+ bool Open (char *path, int omode) {
+ return tchdbopen(hdb, path, omode);
+ }
+
+ class OpenData : public FilenameData {
+ protected:
+ int omode;
+
+ public:
+ OpenData (const Arguments& args) : FilenameData(args) {
+ omode = ARG1->IsUndefined() ? HDBOREADER : ARG1->Int32Value();
+ }
+
+ static bool
+ checkArgs (const Arguments& args) {
+ return FilenameData::checkArgs(args) &&
+ (ARG1->IsUndefined() || ARG1->IsNumber());
+ }
+
+ bool
+ run () {
+ return db->Open(*path, omode);
+ }
+ };
+
+ DEFINE_SYNC(Open)
+
+ class OpenAsyncData : public OpenData, public AsyncData {
+ public:
+ OpenAsyncData (const Arguments& args)
+ : OpenData(args), AsyncData(args[2]), ArgsData(args) {}
+ };
+
+ DEFINE_ASYNC(Open)
+
+ bool Close () {
+ return tchdbclose(hdb);
+ }
+
+ DEFINE_SYNC(Close)
+ DEFINE_ASYNC(Close)
+
+ bool Put(char *kbuf, int ksiz, char *vbuf, int vsiz) {
+ return tchdbput(hdb, kbuf, ksiz, vbuf, vsiz);
+ }
+
+ DEFINE_SYNC(Put)
+ DEFINE_ASYNC(Put)
+
+ bool Putkeep(char *kbuf, int ksiz, char *vbuf, int vsiz) {
+ return tchdbputkeep(hdb, kbuf, ksiz, vbuf, vsiz);
+ }
+
+ DEFINE_SYNC(Putkeep)
+ DEFINE_ASYNC(Putkeep)
+
+ bool Putcat(char *kbuf, int ksiz, char *vbuf, int vsiz) {
+ return tchdbputcat(hdb, kbuf, ksiz, vbuf, vsiz);
+ }
+
+ DEFINE_SYNC(Putcat)
+ DEFINE_ASYNC(Putcat)
+
+ bool Putasync(char *kbuf, int ksiz, char *vbuf, int vsiz) {
+ return tchdbputasync(hdb, kbuf, ksiz, vbuf, vsiz);
+ }
+
+ DEFINE_SYNC(Putasync)
+ DEFINE_ASYNC(Putasync)
+
+ bool Out(char *kbuf, int ksiz) {
+ return tchdbout(hdb, kbuf, ksiz);
+ }
+
+ DEFINE_SYNC(Out)
+ DEFINE_ASYNC(Out)
+
+ char * Get(char *kbuf, int ksiz, int *vsiz_p) {
+ return static_cast<char *>(tchdbget(hdb, kbuf, ksiz, vsiz_p));
+ }
+
+ DEFINE_SYNC2(Get)
+ DEFINE_ASYNC2(Get)
+
+ int Vsiz(char *kbuf, int ksiz) {
+ return tchdbvsiz(hdb, kbuf, ksiz);
+ }
+
+ DEFINE_SYNC2(Vsiz)
+ DEFINE_ASYNC2(Vsiz)
+
+ bool Iterinit () {
+ return tchdbiterinit(hdb);
+ }
+
+ DEFINE_SYNC(Iterinit)
+ DEFINE_ASYNC(Iterinit)
+
+ char * Iternext (int *vsiz_p) {
+ return static_cast<char *>(tchdbiternext(hdb, vsiz_p));
+ }
+
+ DEFINE_SYNC2(Iternext)
+ DEFINE_ASYNC2(Iternext)
+
+ TCLIST * Fwmkeys(char *kbuf, int ksiz, int max) {
+ return tchdbfwmkeys(hdb, kbuf, ksiz, max);
+ }
+
+ DEFINE_SYNC2(Fwmkeys)
+ DEFINE_ASYNC2(Fwmkeys)
+
+ int Addint(char *kbuf, int ksiz, int num) {
+ return tchdbaddint(hdb, kbuf, ksiz, num);
+ }
+
+ DEFINE_SYNC2(Addint)
+ DEFINE_ASYNC2(Addint)
+
+ double Adddouble(char *kbuf, int ksiz, double num) {
+ return tchdbadddouble(hdb, kbuf, ksiz, num);
+ }
+
+ DEFINE_SYNC2(Adddouble)
+ DEFINE_ASYNC2(Adddouble)
+
+ bool Sync () {
+ return tchdbsync(hdb);
+ }
+
+ DEFINE_SYNC(Sync)
+ DEFINE_ASYNC(Sync)
+
+ bool Optimize (int64_t bnum, int8_t apow, int8_t fpow, uint8_t opts) {
+ return tchdboptimize(hdb, bnum, apow, fpow, opts);
+ }
+
+ class OptimizeData : public TuneData {
+ public:
+ OptimizeData (const Arguments& args) : TuneData(args) {}
+
+ bool run () {
+ return db->Optimize(bnum, apow, fpow, opts);
+ }
+ };
+
+ DEFINE_SYNC(Optimize)
+
+ class OptimizeAsyncData : public OptimizeData, public AsyncData {
+ public:
+ OptimizeAsyncData (const Arguments& args)
+ : OptimizeData(args), AsyncData(ARG4) {}
+ };
+
+ DEFINE_ASYNC(Optimize)
+
+ bool Vanish () {
+ return tchdbvanish(hdb);
+ }
+
+ DEFINE_SYNC(Vanish)
+ DEFINE_ASYNC(Vanish)
+
+ bool Copy (char *path) {
+ return tchdbcopy(hdb, path);
+ }
+
+ DEFINE_SYNC(Copy)
+ DEFINE_ASYNC(Copy)
+
+ bool Tranbegin () {
+ return tchdbtranbegin(hdb);
+ }
+
+ DEFINE_SYNC(Tranbegin)
+ DEFINE_ASYNC(Tranbegin)
+
+ bool Trancommit () {
+ return tchdbtrancommit(hdb);
+ }
+
+ DEFINE_SYNC(Trancommit)
+ DEFINE_ASYNC(Trancommit)
+
+ bool Tranabort () {
+ return tchdbtranabort(hdb);
+ }
+
+ DEFINE_SYNC(Tranabort)
+ DEFINE_ASYNC(Tranabort)
+
+ const char * Path () {
+ return tchdbpath(hdb);
+ }
+
+ DEFINE_SYNC2(Path)
+
+ uint64_t Rnum () {
+ return tchdbrnum(hdb);
+ }
+
+ DEFINE_SYNC2(Rnum)
+
+ uint64_t Fsiz () {
+ return tchdbfsiz(hdb);
+ }
+
+ DEFINE_SYNC2(Fsiz)
+};
+
+class BDB : public DBWrap {
+ public:
+ TCBDB *bdb;
+
+ const static Persistent<FunctionTemplate> Tmpl;
+
+ BDB () {
+ bdb = tcbdbnew();
+ }
+
+ ~BDB () {
+ tcbdbdel(bdb);
}
static void
@@ -1134,493 +1476,408 @@ class BDB : ObjectWrap {
DEFINE_PREFIXED_CONSTANT(Tmpl, BDB, OLCKNB);
DEFINE_PREFIXED_CONSTANT(Tmpl, BDB, OTSYNC);
- NODE_SET_PROTOTYPE_METHOD(Tmpl, "errmsg", Errmsg);
- NODE_SET_PROTOTYPE_METHOD(Tmpl, "ecode", Ecode);
- NODE_SET_PROTOTYPE_METHOD(Tmpl, "tune", Tune);
- NODE_SET_PROTOTYPE_METHOD(Tmpl, "setcache", Setcache);
- NODE_SET_PROTOTYPE_METHOD(Tmpl, "setxmsiz", Setxmsiz);
- NODE_SET_PROTOTYPE_METHOD(Tmpl, "setdfunit", Setdfunit);
- NODE_SET_PROTOTYPE_METHOD(Tmpl, "open", Open);
- NODE_SET_PROTOTYPE_METHOD(Tmpl, "close", Close);
- NODE_SET_PROTOTYPE_METHOD(Tmpl, "put", Put);
- NODE_SET_PROTOTYPE_METHOD(Tmpl, "putkeep", Putkeep);
- NODE_SET_PROTOTYPE_METHOD(Tmpl, "putcat", Putcat);
- NODE_SET_PROTOTYPE_METHOD(Tmpl, "putdup", Putdup);
- NODE_SET_PROTOTYPE_METHOD(Tmpl, "putlist", Putlist);
- NODE_SET_PROTOTYPE_METHOD(Tmpl, "out", Out);
- NODE_SET_PROTOTYPE_METHOD(Tmpl, "outlist", Outlist);
- NODE_SET_PROTOTYPE_METHOD(Tmpl, "get", Get);
- NODE_SET_PROTOTYPE_METHOD(Tmpl, "getlist", Getlist);
- NODE_SET_PROTOTYPE_METHOD(Tmpl, "vnum", Vnum);
- NODE_SET_PROTOTYPE_METHOD(Tmpl, "vsiz", Vsiz);
- NODE_SET_PROTOTYPE_METHOD(Tmpl, "range", Range);
- NODE_SET_PROTOTYPE_METHOD(Tmpl, "fwmkeys", Fwmkeys);
- NODE_SET_PROTOTYPE_METHOD(Tmpl, "addint", Addint);
- NODE_SET_PROTOTYPE_METHOD(Tmpl, "adddouble", Adddouble);
- NODE_SET_PROTOTYPE_METHOD(Tmpl, "sync", Sync);
- NODE_SET_PROTOTYPE_METHOD(Tmpl, "optimize", Optimize);
- NODE_SET_PROTOTYPE_METHOD(Tmpl, "vanish", Vanish);
- NODE_SET_PROTOTYPE_METHOD(Tmpl, "copy", Copy);
- NODE_SET_PROTOTYPE_METHOD(Tmpl, "tranbegin", Tranbegin);
- NODE_SET_PROTOTYPE_METHOD(Tmpl, "trancommit", Trancommit);
- NODE_SET_PROTOTYPE_METHOD(Tmpl, "tranabort", Tranabort);
- NODE_SET_PROTOTYPE_METHOD(Tmpl, "path", Path);
- NODE_SET_PROTOTYPE_METHOD(Tmpl, "rnum", Rnum);
- NODE_SET_PROTOTYPE_METHOD(Tmpl, "fsiz", Fsiz);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "errmsg", ErrmsgSync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "ecode", EcodeSync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "setmutex", SetmutexSync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "tune", TuneSync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "setcache", SetcacheSync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "setxmsiz", SetxmsizSync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "setdfunit", SetdfunitSync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "open", OpenSync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "openAsync", OpenAsync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "close", CloseSync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "closeAsync", CloseAsync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "put", PutSync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "putAsync", PutAsync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "putkeep", PutkeepSync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "putkeepAsync", PutkeepAsync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "putcat", PutcatSync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "putcatAsync", PutcatAsync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "putdup", PutdupSync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "putdupAsync", PutdupAsync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "putlist", PutlistSync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "putlistAsync", PutlistAsync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "out", OutSync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "outAsync", OutAsync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "outlist", OutlistSync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "outlistAsync", OutlistAsync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "get", GetSync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "getAsync", GetAsync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "getlist", GetlistSync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "getlistAsync", GetlistAsync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "vnum", VnumSync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "vnumAsync", VnumAsync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "vsiz", VsizSync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "vsizAsync", VsizAsync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "range", RangeSync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "rangeAsync", RangeAsync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "fwmkeys", FwmkeysSync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "fwmkeysAsync", FwmkeysAsync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "addint", AddintSync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "addintAsync", AddintAsync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "adddouble", AdddoubleSync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "adddoubleAsync", AdddoubleAsync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "sync", SyncSync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "syncAsync", SyncAsync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "optimize", OptimizeSync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "optimizeAsync", OptimizeAsync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "vanish", VanishSync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "vanishAsync", VanishAsync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "copy", CopySync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "copyAsync", CopyAsync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "tranbegin", TranbeginSync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "tranbeginAsync", TranbeginAsync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "trancommit", TrancommitSync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "trancommitAsync", TrancommitAsync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "tranabort", TranabortSync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "tranabortAsync", TranabortAsync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "path", PathSync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "rnum", RnumSync);
+ NODE_SET_PROTOTYPE_METHOD(Tmpl, "fsiz", FsizSync);
target->Set(String::New("BDB"), Tmpl->GetFunction());
}
+ private:
+
static Handle<Value>
New (const Arguments& args) {
HandleScope scope;
+ if (!args.IsConstructCall()) return args.Callee()->NewInstance();
(new BDB)->Wrap(THIS);
return THIS;
}
- private:
- TCBDB *db;
-
- static Handle<Value>
- Errmsg (const Arguments& args) {
- HandleScope scope;
- if (!(NOU(ARG0) || ARG0->IsNumber())) {
- return THROW_BAD_ARGS;
- }
- const char *msg = tchdberrmsg(
- NOU(ARG0) ? tcbdbecode(Backend(THIS)) : VINT32(ARG0));
- return String::New(msg);
+ int Ecode () {
+ return tcbdbecode(bdb);
}
- static Handle<Value>
- Ecode (const Arguments& args) {
- HandleScope scope;
- int ecode = tcbdbecode(
- Backend(THIS));
- return Integer::New(ecode);
+ DEFINE_SYNC2(Ecode)
+
+ const char * Errmsg (int ecode) {
+ return tcbdberrmsg(ecode);
}
- static Handle<Value>
- Tune (const Arguments& args) {
- HandleScope scope;
- if (!(ARG0->IsNumber() || NOU(ARG0)) ||
- !(ARG1->IsNumber() || NOU(ARG1)) ||
- !(ARG2->IsNumber() || NOU(ARG2)) ||
- !(ARG3->IsNumber() || NOU(ARG3)) ||
- !(ARG4->IsNumber() || NOU(ARG4)) ||
- !(ARG5->IsNumber() || NOU(ARG5))) {
- return THROW_BAD_ARGS;
- }
- bool success = tcbdbtune(
- Backend(THIS),
- NOU(ARG0) ? -1 : VINT32(ARG0),
- NOU(ARG1) ? -1 : VINT32(ARG1),
- NOU(ARG2) ? -1 : VINT64(ARG2),
- NOU(ARG3) ? -1 : VINT32(ARG3),
- NOU(ARG4) ? -1 : VINT32(ARG4),
- NOU(ARG5) ? 0 : VINT32(ARG5));
- return Boolean::New(success);
+ DEFINE_SYNC2(Errmsg)
+
+ bool Setmutex () {
+ return tcbdbsetmutex(bdb);
}
- static Handle<Value>
- Setcache (const Arguments& args) {
- HandleScope scope;
- if (!(ARG0->IsNumber() || NOU(ARG0)) ||
- !(ARG1->IsNumber() || NOU(ARG1))) {
- return THROW_BAD_ARGS;
- }
- bool success = tcbdbsetcache(
- Backend(THIS),
- NOU(ARG0) ? -1 : VINT32(ARG0),
- NOU(ARG1) ? -1 : VINT32(ARG1));
- return Boolean::New(success);
+ DEFINE_SYNC(Setmutex)
+
+ virtual bool Tune (int32_t lmemb, int32_t nmemb, int64_t bnum, int8_t apow,
+ int8_t fpow, uint8_t opts) {
+ return tcbdbtune(bdb, lmemb, nmemb, bnum, apow, fpow, opts);
}
- static Handle<Value>
- Setxmsiz (const Arguments& args) {
- HandleScope scope;
- if (!(ARG0->IsNumber() || NOU(ARG0))) {
- return THROW_BAD_ARGS;
- }
- bool success = tcbdbsetxmsiz(
- Backend(THIS),
- NOU(ARG0) ? -1 : VINT64(ARG0));
- return Boolean::New(success);
+ class TuneData : public virtual ArgsData {
+ protected:
+ int32_t lmemb;
+ int32_t nmemb;
+ int64_t bnum;
+ int8_t apow;
+ int8_t fpow;
+ uint8_t opts;
+
+ public:
+ static bool checkArgs (const Arguments& args) {
+ return (ARG0->IsNumber() || NOU(ARG0)) &&
+ (ARG1->IsNumber() || NOU(ARG1)) &&
+ (ARG2->IsNumber() || NOU(ARG2)) &&
+ (ARG3->IsNumber() || NOU(ARG3)) &&
+ (ARG4->IsNumber() || NOU(ARG4)) &&
+ (ARG5->IsNumber() || NOU(ARG5));
+ }
+
+ TuneData (const Arguments& args) : ArgsData(args) {
+ lmemb = NOU(ARG0) ? -1 : ARG0->Int32Value();
+ nmemb = NOU(ARG1) ? -1 : ARG1->Int32Value();
+ bnum = NOU(ARG2) ? -1 : ARG2->IntegerValue();
+ apow = NOU(ARG3) ? -1 : ARG3->Int32Value();
+ fpow = NOU(ARG4) ? -1 : ARG4->Int32Value();
+ opts = NOU(ARG5) ? UINT8_MAX : ARG5->Int32Value();
+ }
+
+ bool run () {
+ return db->Tune(lmemb, nmemb, bnum, apow, fpow, opts);
+ }
+ };
+
+ DEFINE_SYNC(Tune)
+
+ bool Setcache (int32_t lcnum, int32_t ncnum) {
+ return tcbdbsetcache(bdb, lcnum, ncnum);
}
- static Handle<Value>
- Setdfunit (const Arguments& args) {
- HandleScope scope;
- if (!(ARG0->IsNumber() || NOU(ARG0))) {
- return THROW_BAD_ARGS;
- }
- bool success = tcbdbsetdfunit(
- Backend(THIS),
- NOU(ARG0) ? -1 : VINT32(ARG0));
- return Boolean::New(success);
+ class SetcacheData : public ArgsData {
+ private:
+ int32_t lcnum;
+ int32_t ncnum;
+
+ public:
+ SetcacheData (const Arguments& args) : ArgsData(args) {
+ lcnum = ARG0->IsUndefined() ? -1 : ARG0->Int32Value();
+ ncnum = ARG1->IsUndefined() ? -1 : ARG1->Int32Value();
+ }
+
+ static bool
+ checkArgs (const Arguments& args) {
+ return (ARG0->IsNumber() || ARG0->IsUndefined()) &&
+ (ARG1->IsNumber() || ARG1->IsUndefined());
+ }
+
+ bool
+ run () {
+ return db->Setcache(lcnum, ncnum);
+ }
+ };
+
+ DEFINE_SYNC(Setcache)
+
+ bool Setxmsiz (int64_t xmsiz) {
+ return tcbdbsetxmsiz(bdb, xmsiz);
}
- static Handle<Value>
- Open (const Arguments& args) {
- HandleScope scope;
- if (!ARG0->IsString()) {
- return THROW_BAD_ARGS;
- }
- bool success = tcbdbopen(
- Backend(THIS),
- VSTRPTR(ARG0),
- NOU(ARG1) ? BDBOREADER : VINT32(ARG1));
- return Boolean::New(success);
+ DEFINE_SYNC(Setxmsiz)
+
+ bool Setdfunit (int32_t dfunit) {
+ return tcbdbsetdfunit(bdb, dfunit);
}
- static Handle<Value>
- Close (const Arguments& args) {
- HandleScope scope;
- bool success = tcbdbclose(Backend(THIS));
- return Boolean::New(success);
+ DEFINE_SYNC(Setdfunit)
+
+ bool Open (char *path, int omode) {
+ return tcbdbopen(bdb, path, omode);
}
- static Handle<Value>
- Put (const Arguments& args) {
- HandleScope scope;
- if (args.Length() < 2 ||
- !ARG0->IsString() ||
- !ARG1->IsString()) {
- return THROW_BAD_ARGS;
- }
- bool success = tcbdbput(
- Backend(THIS),
- VSTRPTR(ARG0),
- VSTRSIZ(ARG0),
- VSTRPTR(ARG1),
- VSTRSIZ(ARG1));
- return Boolean::New(success);
+ class OpenData : public FilenameData {
+ protected:
+ int omode;
+
+ public:
+ OpenData (const Arguments& args) : FilenameData(args) {
+ omode = ARG1->IsUndefined() ? BDBOREADER : ARG1->Int32Value();
+ }
+
+ static bool
+ checkArgs (const Arguments& args) {
+ return FilenameData::checkArgs(args) &&
+ (ARG1->IsUndefined() || ARG1->IsNumber());
+ }
+
+ bool
+ run () {
+ return db->Open(*path, omode);
+ }
+ };
+
+ DEFINE_SYNC(Open)
+
+ class OpenAsyncData : public OpenData, public AsyncData {
+ public:
+ OpenAsyncData (const Arguments& args)
+ : OpenData(args), AsyncData(args[2]), ArgsData(args) {}
+ };
+</