*/
-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);
}
}
-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;
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;
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;
setParams (arg, 1, ¶ms, 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);
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) {
*/
//#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:
*/
//#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:
*/
//#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);
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;
*/
//#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);
v.append (values[i]);
}
}
- obj->db.put (key, v);
+ obj->db->put (key, v);
return NULL;
}
*/
//#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);
if (arg)
throw (uErrorWrongNumber);
- obj->db.del (key);
+ obj->db->del (key);
return NULL;
}
*/
//#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);
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);
*/
//#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);
}
}
}
- obj->db.put (key, val);
+ obj->db->put (key, val);
return NULL;
}
*/
//#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);
bool ans = false;
ustring key, val;
MNodePtr h;
-// MNodePtr ag;
int i, n;
if (!arg)
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 ();
}
h = execDefun (mlenv, lambda (), ag (), uEmpty);
-// ag ()->unset_car ();
if (to_bool (h ())) {
ans = true;
if (vars.size () > 0) {
//#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;
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;
//#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;
}
}
- obj.db.put (key, v);
+ obj.db->put (key, v);
return NULL;
}
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);