OSDN Git Service

update bdb functions.
authorvisor <visor@users.sourceforge.jp>
Thu, 16 Feb 2012 14:39:06 +0000 (23:39 +0900)
committervisor <visor@users.sourceforge.jp>
Thu, 16 Feb 2012 14:39:06 +0000 (23:39 +0900)
modules/ml-db.cc
modules/ml-db.h

index dbc8469..3d32bb4 100644 (file)
 
 */
 
-void  MLDb::dbPath (const ustring& name, bool fxserial, ustring& dbpath, ustring& lockpath) {
+void  MLDb::closedb () {
+    if (db) {
+       db->close ();
+       lock.close ();
+       delete db;
+       db = NULL;
+    }
+}
+
+void  MLDbBtree::dbPath (const ustring& name, bool fxserial, ustring& dbpath, ustring& lockpath) {
     if (fxserial) {
        dbpath = mlenv->env->path_store_file (name, kEXT_BTREE);
        lockpath = mlenv->env->path_store_file (name, kEXT_LOCK);
@@ -26,7 +35,70 @@ void  MLDb::dbPath (const ustring& name, bool fxserial, ustring& dbpath, ustring
     }
 }
 
-void  MLDb::openRead (const ustring& name, bool fxserial) {
+void  MLDbBtree::openRead (const ustring& name, bool fxserial) {
+    if (! db) {
+       ustring  dbpath;
+       ustring  lockpath;
+       BDBBtree*  o = new BDBBtree;
+
+       db = o;;
+       dbPath (name, fxserial, dbpath, lockpath);
+       lock.openReadLock (lockpath.c_str ());
+       o->openRead (dbpath.c_str ());
+    }
+}
+
+void  MLDbBtree::openRW (const ustring& name, bool fxserial) {
+    if (! db) {
+       ustring  dbpath;
+       ustring  lockpath;
+       BDBBtree*  o = new BDBBtree;
+
+       db = o;
+       dbPath (name, fxserial, dbpath, lockpath);
+       lock.openAppendLock (lockpath.c_str ());
+       o->openRW (dbpath.c_str ());
+    }
+}
+
+void  MLDbHash::dbPath (const ustring& name, bool fxserial, ustring& dbpath, ustring& lockpath) {
+    if (fxserial) {
+       dbpath = mlenv->env->path_store_file (name, kEXT_HASH);
+       lockpath = mlenv->env->path_store_file (name, kEXT_LOCK);
+    } else {
+       dbpath = mlenv->env->path_db (name, kEXT_HASH);
+       lockpath = mlenv->env->path_db (name, kEXT_LOCK);
+    }
+}
+
+void  MLDbHash::openRead (const ustring& name, bool fxserial) {
+    if (! db) {
+       ustring  dbpath;
+       ustring  lockpath;
+       BDBHashRDOnly*  o = new BDBHashRDOnly;
+
+       db = o;
+       dbPath (name, fxserial, dbpath, lockpath);
+       lock.openReadLock (lockpath.c_str ());
+       o->open (dbpath.c_str ());
+    }
+}
+
+void  MLDbHash::openRW (const ustring& name, bool fxserial) {
+    if (! db) {
+       ustring  dbpath;
+       ustring  lockpath;
+       BDBHash*  o = new BDBHash;
+
+       db = o;
+       dbPath (name, fxserial, dbpath, lockpath);
+       lock.openAppendLock (lockpath.c_str ());
+       o->open (dbpath.c_str ());
+    }
+}
+
+#if 0
+void  MLDbB::openRead (const ustring& name, bool fxserial) {
     ustring  dbpath;
     ustring  lockpath;
 
@@ -34,8 +106,9 @@ void  MLDb::openRead (const ustring& name, bool fxserial) {
     lock.openReadLock (lockpath.c_str ());
     db.openRead (dbpath.c_str ());
 }
-
-void  MLDb::openRW (const ustring& name, bool fxserial) {
+#endif
+#if 0
+void  MLDbB::openRW (const ustring& name, bool fxserial) {
     ustring  dbpath;
     ustring  lockpath;
 
@@ -43,20 +116,16 @@ void  MLDb::openRW (const ustring& name, bool fxserial) {
     lock.openAppendLock (lockpath.c_str ());
     db.openRW (dbpath.c_str ());
 }
-
-void  MLDb::closedb () {
-    db.close ();
-    lock.close ();
-}
+#endif
 
 typedef enum {
     FN_READ,
     FN_RW,
 }  DBFUNC;
 
-static MNode*  ml_db_sub (MNode* cell, MlEnv* mlenv, DBFUNC fn) {
+static MNode*  ml_db_sub (MNode* cell, MlEnv* mlenv, MLDb* obj, DBFUNC fn) {
     MNode*  arg = cell->cdr ();
-    MLDb  obj (mlenv);
+//    MLDbBtree  obj (mlenv);
     MNodePtr  ans;
     ustring  name;
     bool  fxserial = false;
@@ -73,11 +142,11 @@ static MNode*  ml_db_sub (MNode* cell, MlEnv* mlenv, DBFUNC fn) {
 
     setParams (arg, 1, &params, kwlist, &keywords, &rest);
     name = eval_str (params[0], mlenv);
-    obj.limit = eval_int (keywords[0], mlenv);
-    if (obj.limit <= 0)
-       obj.limit = 1;
-    if (obj.limit > kARRAYMAX)
-       obj.limit = kARRAYMAX;
+    obj->limit = eval_int (keywords[0], mlenv);
+    if (obj->limit <= 0)
+       obj->limit = 1;
+    if (obj->limit > kARRAYMAX)
+       obj->limit = kARRAYMAX;
     fxserial = eval_bool (keywords[1], mlenv);
     if (keywords[2])
        errfn = eval (keywords[2], mlenv);
@@ -89,16 +158,16 @@ static MNode*  ml_db_sub (MNode* cell, MlEnv* mlenv, DBFUNC fn) {
 
     switch (fn) {
     case FN_READ:
-       obj.openRead (name, fxserial);
+       obj->openRead (name, fxserial);
        break;
     case FN_RW:
-       obj.openRW (name, fxserial);
+       obj->openRW (name, fxserial);
        break;
     default:
        assert (0);
     }
 
-    mlenv->setMStack (&obj);
+    mlenv->setMStack (obj);
     try {
        ans = progn (rest, mlenv);
     } catch (ustring& msg) {
@@ -123,7 +192,8 @@ static MNode*  ml_db_sub (MNode* cell, MlEnv* mlenv, DBFUNC fn) {
 */
 //#MFUNC       $db-read        ml_db_read      cMLDbID
 MNode*  ml_db_read (MNode* cell, MlEnv* mlenv) {
-    return ml_db_sub (cell, mlenv, FN_READ);
+    MLDbBtree  obj (mlenv);
+    return ml_db_sub (cell, mlenv, &obj, FN_READ);
 }
 
 /*DOC:
@@ -134,7 +204,32 @@ MNode*  ml_db_read (MNode* cell, MlEnv* mlenv) {
 */
 //#MFUNC       $db-rw  ml_db_rw        cMLDbID
 MNode*  ml_db_rw (MNode* cell, MlEnv* mlenv) {
-    return ml_db_sub (cell, mlenv, FN_RW);
+    MLDbBtree  obj (mlenv);
+    return ml_db_sub (cell, mlenv, &obj, FN_RW);
+}
+
+/*DOC:
+===$hash-read===
+ ($hash-read NAME [:limit NUM] [#xserial | :xserial BOOL] [:on-error FUNCTION]
+       [SUBFUNCTION...])
+
+*/
+//#MFUNC       $hash-read      ml_hash_read    cMLDbID
+MNode*  ml_hash_read (MNode* cell, MlEnv* mlenv) {
+    MLDbHash  obj (mlenv);
+    return ml_db_sub (cell, mlenv, &obj, FN_READ);
+}
+
+/*DOC:
+===$hash-rw===
+ ($hash-rw NAME [:limit NUM] [#xserial | :xserial BOOL] [:on-error FUNCTION]
+       [SUBFUNCTION...])
+
+*/
+//#MFUNC       $hash-rw        ml_hash_rw      cMLDbID
+MNode*  ml_hash_rw (MNode* cell, MlEnv* mlenv) {
+    MLDbHash  obj (mlenv);
+    return ml_db_sub (cell, mlenv, &obj, FN_RW);
 }
 
 /*DOC:
@@ -148,6 +243,8 @@ MNode*  ml_db_rw (MNode* cell, MlEnv* mlenv) {
 */
 //#SFUNC       read    ml_db_x_read    $db-read
 //#SFUNC       read    ml_db_x_read    $db-rw
+//#SFUNC       read    ml_db_x_read    $hash-read
+//#SFUNC       read    ml_db_x_read    $hash-rw
 MNode*  ml_db_x_read (MNode* cell, MlEnv* mlenv, MLFunc* mobj) {
     MNode*  arg = cell->cdr ();
     MLDb*  obj = MObjRef<MLDb> (mobj, cMLDbID);
@@ -161,7 +258,7 @@ MNode*  ml_db_x_read (MNode* cell, MlEnv* mlenv, MLFunc* mobj) {
     if (arg) 
        throw (uErrorWrongNumber);
 
-    if (obj->db.get (key, val)) {
+    if (obj->db->get (key, val)) {
        uiterator  b = val.begin ();
        uiterator  e = val.end ();
        uiterator  it;
@@ -187,6 +284,7 @@ MNode*  ml_db_x_read (MNode* cell, MlEnv* mlenv, MLFunc* mobj) {
 
 */
 //#SFUNC       write   ml_db_x_write   $db-rw
+//#SFUNC       write   ml_db_x_write   $hash-rw
 MNode*  ml_db_x_write (MNode* cell, MlEnv* mlenv, MLFunc* mobj) {
     MNode*  arg = cell->cdr ();
     MLDb*  obj = MObjRef<MLDb> (mobj, cMLDbID);
@@ -214,7 +312,7 @@ MNode*  ml_db_x_write (MNode* cell, MlEnv* mlenv, MLFunc* mobj) {
            v.append (values[i]);
        }
     }
-    obj->db.put (key, v);
+    obj->db->put (key, v);
 
     return NULL;
 }
@@ -225,6 +323,7 @@ MNode*  ml_db_x_write (MNode* cell, MlEnv* mlenv, MLFunc* mobj) {
 
 */
 //#SFUNC       delete  ml_db_x_delete  $db-rw
+//#SFUNC       delete  ml_db_x_delete  $hash-rw
 MNode*  ml_db_x_delete (MNode* cell, MlEnv* mlenv, MLFunc* mobj) {
     MNode*  arg = cell->cdr ();
     MLDb*  obj = MObjRef<MLDb> (mobj, cMLDbID);
@@ -237,7 +336,7 @@ MNode*  ml_db_x_delete (MNode* cell, MlEnv* mlenv, MLFunc* mobj) {
     if (arg) 
        throw (uErrorWrongNumber);
 
-    obj->db.del (key);
+    obj->db->del (key);
 
     return NULL;
 }
@@ -327,6 +426,8 @@ static void  set_read_arys (ustring& val, std::vector<ustring>& vars, std::vecto
 */
 //#SFUNC       read-array      ml_db_x_read_array      $db-read
 //#SFUNC       read-array      ml_db_x_read_array      $db-rw
+//#SFUNC       read-array      ml_db_x_read_array      $hash-read
+//#SFUNC       read-array      ml_db_x_read_array      $hash-rw
 MNode*  ml_db_x_read_array (MNode* cell, MlEnv* mlenv, MLFunc* mobj) {
     MNode*  arg = cell->cdr ();
     MLDb*  obj = MObjRef<MLDb> (mobj, cMLDbID);
@@ -355,7 +456,7 @@ MNode*  ml_db_x_read_array (MNode* cell, MlEnv* mlenv, MLFunc* mobj) {
        nextNode (arg);
     }
 
-    if (obj->db.get (key, val))
+    if (obj->db->get (key, val))
        ans = true;
     if (s == 0) {
        set_read_vars (val, vars, type, mlenv);
@@ -372,6 +473,7 @@ MNode*  ml_db_x_read_array (MNode* cell, MlEnv* mlenv, MLFunc* mobj) {
 
 */
 //#SFUNC       write-array     ml_db_x_write_array     $db-rw
+//#SFUNC       write-array     ml_db_x_write_array     $hash-rw
 MNode*  ml_db_x_write_array (MNode* cell, MlEnv* mlenv, MLFunc* mobj) {
     MNode*  arg = cell->cdr ();
     MLDb*  obj = MObjRef<MLDb> (mobj, cMLDbID);
@@ -433,7 +535,7 @@ MNode*  ml_db_x_write_array (MNode* cell, MlEnv* mlenv, MLFunc* mobj) {
            }
        }
     }
-    obj->db.put (key, val);
+    obj->db->put (key, val);
 
     return NULL;
 }
@@ -445,6 +547,8 @@ MNode*  ml_db_x_write_array (MNode* cell, MlEnv* mlenv, MLFunc* mobj) {
 */
 //#SFUNC       read-select     ml_db_x_select  $db-read
 //#SFUNC       read-select     ml_db_x_select  $db-rw
+//#SFUNC       read-select     ml_db_x_select  $hash-read
+//#SFUNC       read-select     ml_db_x_select  $hash-rw
 MNode*  ml_db_x_select (MNode* cell, MlEnv* mlenv, MLFunc* mobj) {
     MNode*  arg = cell->cdr ();
     MLDb*  obj = MObjRef<MLDb> (mobj, cMLDbID);
@@ -454,7 +558,6 @@ MNode*  ml_db_x_select (MNode* cell, MlEnv* mlenv, MLFunc* mobj) {
     bool  ans = false;
     ustring  key, val;
     MNodePtr  h;
-//    MNodePtr  ag;
     int  i, n;
 
     if (!arg) 
@@ -471,11 +574,9 @@ MNode*  ml_db_x_select (MNode* cell, MlEnv* mlenv, MLFunc* mobj) {
        nextNode (arg);
     }
 
-    obj->db.initeach ();
-//    ag = new MNode;
+    obj->db->initeach ();
     n = 0;
-    while (obj->db.each (key, val)) {
-//     ag ()->set_car (newMNode_str (new ustring (key)));
+    while (obj->db->each (key, val)) {
        MNodeList  ag;
        uiterator  b = val.begin ();
        uiterator  e = val.end ();
@@ -491,7 +592,6 @@ MNode*  ml_db_x_select (MNode* cell, MlEnv* mlenv, MLFunc* mobj) {
        }
 
        h = execDefun (mlenv, lambda (), ag (), uEmpty);
-//     ag ()->unset_car ();
        if (to_bool (h ())) {
            ans = true;
            if (vars.size () > 0) {
@@ -537,7 +637,7 @@ static ustring  TempRWDB (CharConst ("temprwdb"));
 //#AFUNC       temp-read       ml_temp_read
 MNode*  ml_temp_read (MNode* cell, MlEnv* mlenv) {
     MNode*  arg = cell->cdr ();
-    MLDb  obj (mlenv);
+    MLDbBtree  obj (mlenv);
     ustring  key;
     ustring  val;
 
@@ -553,7 +653,7 @@ MNode*  ml_temp_read (MNode* cell, MlEnv* mlenv) {
 
     obj.openRead (TempRWDB, true);
 
-    if (obj.db.get (key, val)) {
+    if (obj.db->get (key, val)) {
        uiterator  b = val.begin ();
        uiterator  e = val.end ();
        uiterator  it;
@@ -581,7 +681,7 @@ MNode*  ml_temp_read (MNode* cell, MlEnv* mlenv) {
 //#AFUNC       temp-write      ml_temp_write
 MNode*  ml_temp_write (MNode* cell, MlEnv* mlenv) {
     MNode*  arg = cell->cdr ();
-    MLDb  obj (mlenv);
+    MLDbBtree  obj (mlenv);
     ustring  key;
     std::vector<ustring>  values;
     ustring  v;
@@ -613,7 +713,7 @@ MNode*  ml_temp_write (MNode* cell, MlEnv* mlenv) {
        }
     }
 
-    obj.db.put (key, v);
+    obj.db->put (key, v);
 
     return NULL;
 }
index 742a3e0..75e5865 100644 (file)
@@ -12,24 +12,47 @@ class  MlEnv;
 
 class  MLDb: public MLFunc {
  public:
-    BDBBtree  db;
+    BDB*  db;
     FileMacro  lock;
     int  limit;
 
     MLDb (MlEnv* _mlenv): MLFunc (cMLDbID, _mlenv) {
+       db = NULL;
        limit = 10000;
     };
     virtual  ~MLDb () {
        closedb ();
     };
+    virtual void  dbPath (const ustring& name, bool fxserial, ustring& dbpath, ustring& lockpath) = 0;
+    virtual void  openRead (const ustring& name, bool fxserial) = 0;
+    virtual void  openRW (const ustring& name, bool fxserial) = 0;
+    virtual void  closedb ();
+};
+
+class  MLDbBtree: public MLDb {
+ public:
+    MLDbBtree (MlEnv* _mlenv): MLDb (_mlenv) {};
+    virtual  ~MLDbBtree () {};
+
+    virtual void  dbPath (const ustring& name, bool fxserial, ustring& dbpath, ustring& lockpath);
+    virtual void  openRead (const ustring& name, bool fxserial);
+    virtual void  openRW (const ustring& name, bool fxserial);
+};
+
+class  MLDbHash: public MLDb {
+ public:
+    MLDbHash (MlEnv* _mlenv): MLDb (_mlenv) {};
+    virtual  ~MLDbHash () {};
+
     virtual void  dbPath (const ustring& name, bool fxserial, ustring& dbpath, ustring& lockpath);
     virtual void  openRead (const ustring& name, bool fxserial);
     virtual void  openRW (const ustring& name, bool fxserial);
-    virtual void  closedb ();
 };
 
 MNode*  ml_db_read (MNode* cell, MlEnv* mlenv);
 MNode*  ml_db_rw (MNode* cell, MlEnv* mlenv);
+MNode*  ml_hash_read (MNode* cell, MlEnv* mlenv);
+MNode*  ml_hash_rw (MNode* cell, MlEnv* mlenv);
 MNode*  ml_db_x_read (MNode* cell, MlEnv* mlenv, MLFunc* mobj);
 MNode*  ml_db_x_write (MNode* cell, MlEnv* mlenv, MLFunc* mobj);
 MNode*  ml_db_x_delete (MNode* cell, MlEnv* mlenv, MLFunc* mobj);