Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

added async api for BDB Cursor, need test

  • Loading branch information...
commit 88be005b5e7ae699f26bbb63ee95e7b36a98d60e 1 parent b548fae
authored May 29, 2010

Showing 1 changed file with 234 additions and 131 deletions. Show diff stats Hide diff stats

  1. 365  src/tokyocabinet.cc
365  src/tokyocabinet.cc
@@ -250,6 +250,16 @@ class TCWrap : public ObjectWrap {
250 250
     virtual const char* Path () { assert(false); }
251 251
     virtual uint64_t Rnum () { assert(false); }
252 252
     virtual uint64_t Fsiz () { assert(false); }
  253
+    // for BDB Cursor
  254
+    virtual bool First () { assert(false); } // for CUR
  255
+    virtual bool Last () { assert(false); } // for CUR
  256
+    virtual bool Jump (char *kbuf, int ksiz) { assert(false); } // for CUR
  257
+    virtual bool Prev () { assert(false); } // for CUR
  258
+    virtual bool Next () { assert(false); } // for CUR
  259
+    virtual bool Put (char *kbuf, int ksiz, int cpmode) { assert(false); } // for CUR
  260
+    virtual bool Out () { assert(false); } // for CUR
  261
+    virtual char * Key (int *vsiz_p) { assert(false); } // for CUR
  262
+    virtual char * Val (int *vsiz_p) { assert(false); } // for CUR
253 263
 
254 264
   protected:
255 265
     class ArgsData {
@@ -1249,13 +1259,13 @@ class HDB : public TCWrap {
1249 1259
 
1250 1260
       public:
1251 1261
         OpenData (const Arguments& args) : FilenameData(args) {
1252  
-          omode = ARG1->IsUndefined() ? HDBOREADER : ARG1->Int32Value();
  1262
+          omode = NOU(ARG1) ? HDBOREADER : ARG1->Int32Value();
1253 1263
         }
1254 1264
 
1255 1265
         static bool
1256 1266
         checkArgs (const Arguments& args) {
1257 1267
           return FilenameData::checkArgs(args) &&
1258  
-            (ARG1->IsUndefined() || ARG1->IsNumber());
  1268
+            (NOU(ARG1) || ARG1->IsNumber());
1259 1269
         }
1260 1270
 
1261 1271
         bool
@@ -1662,13 +1672,13 @@ class BDB : public TCWrap {
1662 1672
 
1663 1673
       public:
1664 1674
         OpenData (const Arguments& args) : FilenameData(args) {
1665  
-          omode = ARG1->IsUndefined() ? BDBOREADER : ARG1->Int32Value();
  1675
+          omode = NOU(ARG1) ? BDBOREADER : ARG1->Int32Value();
1666 1676
         }
1667 1677
 
1668 1678
         static bool
1669 1679
         checkArgs (const Arguments& args) {
1670 1680
           return FilenameData::checkArgs(args) &&
1671  
-            (ARG1->IsUndefined() || ARG1->IsNumber());
  1681
+            (NOU(ARG1) || ARG1->IsNumber());
1672 1682
         }
1673 1683
 
1674 1684
         bool
@@ -1888,7 +1898,7 @@ class BDB : public TCWrap {
1888 1898
 const Persistent<FunctionTemplate> BDB::Tmpl =
1889 1899
   Persistent<FunctionTemplate>::New(FunctionTemplate::New(BDB::New));
1890 1900
 
1891  
-class CUR : ObjectWrap {
  1901
+class CUR : TCWrap {
1892 1902
   public:
1893 1903
     CUR (TCBDB *bdb) {
1894 1904
       cur = tcbdbcurnew(bdb);
@@ -1920,15 +1930,24 @@ class CUR : ObjectWrap {
1920 1930
       DEFINE_PREFIXED_CONSTANT(tmpl, BDB, CPBEFORE);
1921 1931
       DEFINE_PREFIXED_CONSTANT(tmpl, BDB, CPAFTER);
1922 1932
 
1923  
-      NODE_SET_PROTOTYPE_METHOD(tmpl, "first", First);
1924  
-      NODE_SET_PROTOTYPE_METHOD(tmpl, "last", Last);
1925  
-      NODE_SET_PROTOTYPE_METHOD(tmpl, "jump", Jump);
1926  
-      NODE_SET_PROTOTYPE_METHOD(tmpl, "prev", Prev);
1927  
-      NODE_SET_PROTOTYPE_METHOD(tmpl, "next", Next);
1928  
-      NODE_SET_PROTOTYPE_METHOD(tmpl, "put", Put);
1929  
-      NODE_SET_PROTOTYPE_METHOD(tmpl, "out", Out);
1930  
-      NODE_SET_PROTOTYPE_METHOD(tmpl, "key", Key);
1931  
-      NODE_SET_PROTOTYPE_METHOD(tmpl, "val", Val);
  1933
+      NODE_SET_PROTOTYPE_METHOD(tmpl, "first", FirstSync);
  1934
+      NODE_SET_PROTOTYPE_METHOD(tmpl, "firstAsync", FirstAsync);
  1935
+      NODE_SET_PROTOTYPE_METHOD(tmpl, "last", LastSync);
  1936
+      NODE_SET_PROTOTYPE_METHOD(tmpl, "lastAsync", LastAsync);
  1937
+      NODE_SET_PROTOTYPE_METHOD(tmpl, "jump", JumpSync);
  1938
+      NODE_SET_PROTOTYPE_METHOD(tmpl, "jumpAsync", JumpAsync);
  1939
+      NODE_SET_PROTOTYPE_METHOD(tmpl, "prev", PrevSync);
  1940
+      NODE_SET_PROTOTYPE_METHOD(tmpl, "prevAsync", PrevAsync);
  1941
+      NODE_SET_PROTOTYPE_METHOD(tmpl, "next", NextSync);
  1942
+      NODE_SET_PROTOTYPE_METHOD(tmpl, "nextAsync", NextAsync);
  1943
+      NODE_SET_PROTOTYPE_METHOD(tmpl, "put", PutSync);
  1944
+      NODE_SET_PROTOTYPE_METHOD(tmpl, "putAsync", PutAsync);
  1945
+      NODE_SET_PROTOTYPE_METHOD(tmpl, "out", OutSync);
  1946
+      NODE_SET_PROTOTYPE_METHOD(tmpl, "outAsync", OutAsync);
  1947
+      NODE_SET_PROTOTYPE_METHOD(tmpl, "key", KeySync);
  1948
+      NODE_SET_PROTOTYPE_METHOD(tmpl, "keyAsync", KeyAsync);
  1949
+      NODE_SET_PROTOTYPE_METHOD(tmpl, "val", ValSync);
  1950
+      NODE_SET_PROTOTYPE_METHOD(tmpl, "valAsync", ValAsync);
1932 1951
 
1933 1952
       target->Set(String::New("BDBCUR"), tmpl->GetFunction());
1934 1953
     }
@@ -1948,144 +1967,228 @@ class CUR : ObjectWrap {
1948 1967
       return THIS;
1949 1968
     }
1950 1969
 
1951  
-    static Handle<Value>
1952  
-    First (const Arguments& args) {
1953  
-      HandleScope scope;
1954  
-      bool success = tcbdbcurfirst(Backend(THIS));
1955  
-      return Boolean::New(success);
  1970
+    int Ecode () {
  1971
+      return tcbdbecode(cur->bdb);
1956 1972
     }
1957 1973
 
1958  
-    /*
1959  
-    struct AsyncData {
1960  
-      CUR *cur;
1961  
-      Persistent<Function> cb;
1962  
-    };
1963  
-
1964  
-    static Handle<Value>
1965  
-    FirstAsync (const Arguments& args) {
1966  
-      HandleScope scope;
1967  
-      AsyncData data = new AsyncData;
1968  
-      data->cur = Unwrap(THIS);
1969  
-      data->cur->Ref();
1970  
-      data->cb = args[0]->IsFunction() ? 
1971  
-        Persistent<Function>::New(Function::Cast(args[0])) :
1972  
-        Persistent<Function>();
1973  
-      eio_custom(ExecFirst, EIO_PRI_DEFAULT, AfterFirst, data);
1974  
-      ev_ref(EV_DEFAULT_UC);
1975  
-      return Undefined();
  1974
+    bool First () {
  1975
+      return tcbdbcurfirst(cur);
1976 1976
     }
1977 1977
 
1978  
-    static int
1979  
-    Exec##name (eio_req *req) {
1980  
-      name##AsyncData *data = static_cast<name##AsyncData *>(req->data);
1981  
-      req->result = data->run() ? TCESUCCESS : data->ecode();
1982  
-      return 0;
  1978
+    bool Last () {
  1979
+      return tcbdbcurlast(cur);
1983 1980
     }
1984 1981
 
1985  
-    static int
1986  
-    After##name (eio_req *req) {
1987  
-      HandleScope scope;
1988  
-      name##AsyncData *data = static_cast<name##AsyncData *>(req->data);
1989  
-      if (data->hasCallback) {
1990  
-        data->callCallback(Integer::New(req->result));
1991  
-      }
1992  
-      ev_unref(EV_DEFAULT_UC);
1993  
-      delete data;
1994  
-      return 0;
  1982
+    bool Jump (char *kbuf, int ksiz) {
  1983
+      return tcbdbcurjump(cur, kbuf, ksiz);
1995 1984
     }
1996  
-    */
1997 1985
 
1998  
-    static Handle<Value>
1999  
-    Last (const Arguments& args) {
2000  
-      HandleScope scope;
2001  
-      bool success = tcbdbcurlast(
2002  
-          Backend(THIS));
2003  
-      return Boolean::New(success);
  1986
+    bool Prev () {
  1987
+      return tcbdbcurprev(cur);
2004 1988
     }
2005 1989
 
2006  
-    static Handle<Value>
2007  
-    Jump (const Arguments& args) {
2008  
-      HandleScope scope;
2009  
-      if (args.Length() < 1) {
2010  
-        return THROW_BAD_ARGS;
2011  
-      }
2012  
-      bool success = tcbdbcurjump(
2013  
-          Backend(THIS),
2014  
-          VSTRPTR(ARG0),
2015  
-          VSTRSIZ(ARG0));
2016  
-      return Boolean::New(success);
  1990
+    bool Next () {
  1991
+      return tcbdbcurnext(cur);
2017 1992
     }
2018 1993
 
2019  
-    static Handle<Value>
2020  
-    Prev (const Arguments& args) {
2021  
-      HandleScope scope;
2022  
-      bool success = tcbdbcurprev(
2023  
-          Backend(THIS));
2024  
-      return Boolean::New(success);
  1994
+    bool Put (char *kbuf, int ksiz, int cpmode) {
  1995
+      return tcbdbcurput(cur, kbuf, ksiz, cpmode);
2025 1996
     }
2026 1997
 
2027  
-    static Handle<Value>
2028  
-    Next (const Arguments& args) {
2029  
-      HandleScope scope;
2030  
-      bool success = tcbdbcurnext(
2031  
-          Backend(THIS));
2032  
-      return Boolean::New(success);
  1998
+    bool Out () {
  1999
+      return tcbdbcurout(cur);
2033 2000
     }
2034 2001
 
2035  
-    static Handle<Value>
2036  
-    Put (const Arguments& args) {
2037  
-      HandleScope scope;
2038  
-      if (args.Length() < 1 ||
2039  
-          !ARG2->IsNumber()) {
2040  
-        return THROW_BAD_ARGS;
2041  
-      }
2042  
-      bool success = tcbdbcurput(
2043  
-          Backend(THIS),
2044  
-          VSTRPTR(ARG0),
2045  
-          VSTRSIZ(ARG0),
2046  
-          NOU(ARG1) ? BDBCPCURRENT : VINT32(ARG1));
2047  
-      return Boolean::New(success);
  2002
+    char * Key (int *vsiz_p) {
  2003
+      return static_cast<char *>(tcbdbcurkey(cur, vsiz_p));
2048 2004
     }
2049 2005
 
2050  
-    static Handle<Value>
2051  
-    Out (const Arguments& args) {
2052  
-      HandleScope scope;
2053  
-      bool success = tcbdbcurout(
2054  
-          Backend(THIS));
2055  
-      return Boolean::New(success);
  2006
+    char * Val (int *vsiz_p) {
  2007
+      return static_cast<char *>(tcbdbcurval(cur, vsiz_p));
2056 2008
     }
2057 2009
 
2058  
-    static Handle<Value>
2059  
-    Key (const Arguments& args) {
2060  
-      HandleScope scope;
2061  
-      int ksiz;
2062  
-      char *key = static_cast<char *>(tcbdbcurkey(
2063  
-          Backend(THIS),
2064  
-          &ksiz));
2065  
-      if (key == NULL) {
2066  
-        return Null();
2067  
-      } else {
2068  
-        Local<String> ret = String::New(key, ksiz);
2069  
-        tcfree(key);
2070  
-        return ret;
2071  
-      }
2072  
-    }
  2010
+    class FirstData : public virtual ArgsData {
  2011
+      public:
  2012
+        FirstData(const Arguments& args) : ArgsData(args) {}
2073 2013
 
2074  
-    static Handle<Value>
2075  
-    Val (const Arguments& args) {
2076  
-      HandleScope scope;
2077  
-      int vsiz;
2078  
-      char *val = static_cast<char *>(tcbdbcurval(
2079  
-          Backend(THIS),
2080  
-          &vsiz));
2081  
-      if (val == NULL) {
2082  
-        return Null();
2083  
-      } else {
2084  
-        Local<String> ret = String::New(val, vsiz);
2085  
-        tcfree(val);
2086  
-        return ret;
2087  
-      }
2088  
-    }
  2014
+        bool run () {
  2015
+          return tcw->First();
  2016
+        }
  2017
+    };
  2018
+
  2019
+    DEFINE_SYNC(First)
  2020
+
  2021
+    class FirstAsyncData : public FirstData, public AsyncData {
  2022
+      public:
  2023
+        FirstAsyncData(const Arguments& args)
  2024
+          : AsyncData(args[0]), FirstData(args) {}
  2025
+    };
  2026
+
  2027
+    DEFINE_ASYNC(First)
  2028
+
  2029
+    class LastData : public virtual ArgsData {
  2030
+      public:
  2031
+        LastData(const Arguments& args) : ArgsData(args) {}
  2032
+
  2033
+        bool run () {
  2034
+          return tcw->Last();
  2035
+        }
  2036
+    };
  2037
+
  2038
+    DEFINE_SYNC(Last)
  2039
+
  2040
+    class LastAsyncData : public LastData, public AsyncData {
  2041
+      public:
  2042
+        LastAsyncData(const Arguments& args)
  2043
+          : AsyncData(args[0]), LastData(args) {}
  2044
+    };
  2045
+
  2046
+    DEFINE_ASYNC(Last)
  2047
+
  2048
+    class JumpData : public KeyData {
  2049
+      public:
  2050
+        JumpData(const Arguments& args) : KeyData(args) {}
  2051
+
  2052
+        bool run () {
  2053
+          return tcw->Jump(*kbuf, ksiz);
  2054
+        }
  2055
+    };
  2056
+
  2057
+    DEFINE_SYNC(Jump)
  2058
+
  2059
+    class JumpAsyncData : public JumpData, public AsyncData {
  2060
+      public:
  2061
+        JumpAsyncData(const Arguments& args)
  2062
+          : AsyncData(args[1]), JumpData(args) {}
  2063
+    };
  2064
+
  2065
+    DEFINE_ASYNC(Jump)
  2066
+
  2067
+    class PrevData : public virtual ArgsData {
  2068
+      public:
  2069
+        PrevData(const Arguments& args) : ArgsData(args) {}
  2070
+
  2071
+        bool run () {
  2072
+          return tcw->Prev();
  2073
+        }
  2074
+    };
  2075
+
  2076
+    DEFINE_SYNC(Prev)
  2077
+
  2078
+    class PrevAsyncData : public PrevData, public AsyncData {
  2079
+      public:
  2080
+        PrevAsyncData(const Arguments& args)
  2081
+          : AsyncData(args[0]), PrevData(args) {}
  2082
+    };
  2083
+
  2084
+    DEFINE_ASYNC(Prev)
  2085
+
  2086
+    class NextData : public virtual ArgsData {
  2087
+      public:
  2088
+        NextData(const Arguments& args) : ArgsData(args) {}
  2089
+
  2090
+        bool run () {
  2091
+          return tcw->Next();
  2092
+        }
  2093
+    };
  2094
+
  2095
+    DEFINE_SYNC(Next)
  2096
+
  2097
+    class NextAsyncData : public NextData, public AsyncData {
  2098
+      public:
  2099
+        NextAsyncData(const Arguments& args)
  2100
+          : AsyncData(args[0]), NextData(args) {}
  2101
+    };
  2102
+
  2103
+    DEFINE_ASYNC(Next)
  2104
+
  2105
+    class PutData : public KeyData {
  2106
+      private:
  2107
+        int cpmode;
  2108
+
  2109
+      public:
  2110
+        PutData(const Arguments& args) : KeyData(args) {
  2111
+          cpmode = NOU(args[1]) ?
  2112
+            BDBCPCURRENT : args[1]->Int32Value();
  2113
+        }
  2114
+
  2115
+        static bool checkArgs (const Arguments& args) {
  2116
+          return NOU(args[1]) || args[1]->IsNumber();
  2117
+        }
  2118
+
  2119
+        bool run () {
  2120
+          return tcw->Put(*kbuf, ksiz, cpmode);
  2121
+        }
  2122
+    };
  2123
+
  2124
+    DEFINE_SYNC(Put)
  2125
+
  2126
+    class PutAsyncData : public PutData, public AsyncData {
  2127
+      public:
  2128
+        PutAsyncData(const Arguments& args)
  2129
+          : AsyncData(args[2]), PutData(args) {}
  2130
+    };
  2131
+
  2132
+    DEFINE_ASYNC(Put)
  2133
+
  2134
+    class OutData : public virtual ArgsData {
  2135
+      public:
  2136
+        OutData(const Arguments& args) : ArgsData(args) {}
  2137
+
  2138
+        bool run () {
  2139
+          return tcw->Out();
  2140
+        }
  2141
+    };
  2142
+
  2143
+    DEFINE_SYNC(Out)
  2144
+
  2145
+    class OutAsyncData : public OutData, public AsyncData {
  2146
+      public:
  2147
+        OutAsyncData(const Arguments& args)
  2148
+          : AsyncData(args[0]), OutData(args) {}
  2149
+    };
  2150
+
  2151
+    DEFINE_ASYNC(Out)
  2152
+
  2153
+    class KeyData : public ValueData {
  2154
+      public:
  2155
+        KeyData(const Arguments& args) {}
  2156
+
  2157
+        bool run () {
  2158
+          vbuf = tcw->Key(&vsiz);
  2159
+          return vbuf != NULL;
  2160
+        }
  2161
+    };
  2162
+
  2163
+    DEFINE_SYNC2(Key)
  2164
+
  2165
+    class KeyAsyncData : public KeyData, public AsyncData {
  2166
+      public:
  2167
+        KeyAsyncData(const Arguments& args)
  2168
+          : AsyncData(args[1]), KeyData(args) {}
  2169
+    };
  2170
+
  2171
+    DEFINE_ASYNC2(Key)
  2172
+
  2173
+    class ValData : public ValueData {
  2174
+      public:
  2175
+        ValData(const Arguments& args) {}
  2176
+
  2177
+        bool run () {
  2178
+          vbuf = tcw->Val(&vsiz);
  2179
+          return vbuf != NULL;
  2180
+        }
  2181
+    };
  2182
+
  2183
+    DEFINE_SYNC2(Val)
  2184
+
  2185
+    class ValAsyncData : public ValData, public AsyncData {
  2186
+      public:
  2187
+        ValAsyncData(const Arguments& args)
  2188
+          : AsyncData(args[1]), ValData(args) {}
  2189
+    };
  2190
+
  2191
+    DEFINE_ASYNC2(Val)
2089 2192
 };
2090 2193
 
2091 2194
 class FDB : ObjectWrap {

0 notes on commit 88be005

Please sign in to comment.
Something went wrong with that request. Please try again.