def iteritems(self, key_type=None, schema=None): """Iterate for every key / value in a table database object.""" if not tc.tdb_iterinit(self.db): raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) while True: c_key, c_key_len = tc.tdb_iternext(self.db) if not c_key: break cols_tcmap = tc.tdb_get(self.db, c_key, c_key_len) key = util.deserialize(c_key, c_key_len, key_type) cols = util.deserialize_tcmap(cols_tcmap, schema) yield (key, cols)
def iteritems(self, as_type=None): """Iterate for every key / value in a fixed-length database object.""" if not tc.fdb_iterinit(self.db): raise tc.TCException(tc.fdb_errmsg(tc.fdb_ecode(self.db))) while True: key = tc.fdb_iternext(self.db) if not key: break (c_value, c_value_len) = tc.fdb_get(self.db, key) value = util.deserialize(c_value, c_value_len, as_type) yield (key, value)
def proc(self, proc, op): """Process each record corresponding to a query object.""" def proc_wraper(c_pkey, c_pkey_len, c_cols, op): pkey = util.deserialize(ctypes.cast(c_pkey, ctypes.c_void_p), c_pkey_len, as_type=int) cols = util.deserialize_tcmap(c_cols) return proc(pkey, cols, ctypes.cast(op, ctypes.c_char_p).value) result = tc.tdb_qryproc(self.qry, tc.TDBQRYPROC(proc_wraper), op) if not result: raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) return result
def iteritems(self, key_type=None, value_type=None): """Iterate for every key / value in a hash database object.""" if not tc.hdb_iterinit(self.db): raise tc.TCException(tc.hdb_errmsg(tc.hdb_ecode(self.db))) while True: xstr_key = tc.tcxstrnew() xstr_value = tc.tcxstrnew() result = tc.hdb_iternext3(self.db, xstr_key, xstr_value) if not result: break key = util.deserialize_xstr(xstr_key, key_type) value = util.deserialize_xstr(xstr_value, value_type) yield (key, value)
def foreach(self, proc, op): """Process each record atomically of a B+ tree database object.""" def proc_wraper(c_key, c_key_len, c_value, c_value_len, op): key = util.deserialize(ctypes.cast(c_key, ctypes.c_void_p), c_key_len, str) value = util.deserialize(ctypes.cast(c_value, ctypes.c_void_p), c_value_len, str) return proc(key, value, ctypes.cast(op, ctypes.c_char_p).value) result = tc.bdb_foreach(self.db, tc.TCITER(proc_wraper), op) if not result: raise tc.TCException(tc.bdb_errmsg(tc.bdb_ecode(self.db))) return result
def range(self, keya=None, inca=True, keyb=None, incb=True, max_=-1, as_raw=True): """Get keys of ranged records in a B+ tree database object.""" (c_keya, c_keya_len) = util.serialize(keya, as_raw) (c_keyb, c_keyb_len) = util.serialize(keyb, as_raw) tclist_objs = tc.bdb_range(self.db, c_keya, c_keya_len, inca, c_keyb, c_keyb_len, incb, max_) if not tclist_objs: raise tc.TCException(tc.bdb_errmsg(tc.bdb_ecode(self.db))) as_type = util.get_type(keya, as_raw) return util.deserialize_tclist(tclist_objs, as_type)
def _raise(self, msg=None): """Raise an exception based on the internal database object.""" mode = self.omode() if mode == OMDB: msg = 'Error in hash memory abstract database object.' elif mode == ONDB: msg = 'Error in B+ tree memory abstract database object.' elif mode == OHDB: msg = tc.hdb_errmsg(tc.hdb_ecode(self.reveal())) elif mode == OBDB: msg = tc.bdb_errmsg(tc.bdb_ecode(self.reveal())) elif mode == OFDB: msg = tc.fdb_errmsg(tc.fdb_ecode(self.reveal())) elif mode == OTDB: msg = tc.tdb_errmsg(tc.tdb_ecode(self.reveal())) raise tc.TCException(msg)
def optimize(self, lmemb=None, nmemb=None, bnum=None, apow=None, fpow=None, opts=None): """Optimize the file of a B+ tree database object.""" kwargs = dict([ x for x in (('lmemb', lmemb), ('nmemb', nmemb), ('bnum', bnum), ('apow', apow), ('fpow', fpow), ('opts', opts)) if x[1] ]) result = tc.bdb_optimize(self.db, **kwargs) if not result: raise tc.TCException(tc.bdb_errmsg(tc.bdb_ecode(self.db))) return result
def open(self, path, omode=OWRITER | OCREAT, bnum=0, apow=-1, fpow=-1, opts=0, rcnum=0, lcnum=0, ncnum=0, xmsiz=0, dfunit=0): """Open a database file and connect a table database object.""" self.setcache(rcnum, lcnum, ncnum) self.setxmsiz(xmsiz) self.setdfunit(dfunit) self.tune(bnum, apow, fpow, opts) if not tc.tdb_open(self.db, path, omode): raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db)))
def open(self, path, omode=OWRITER | OCREAT, bnum=0, apow=-1, fpow=-1, opts=0, rcnum=0, xmsiz=67108864, dfunit=0): """Open a database file and connect a hash database object.""" if bnum or apow >= 0 or fpow >= 0 or opts: self.tune(bnum, apow, fpow, opts) if rcnum: self.setcache(rcnum) if xmsiz != 67108864: self.setxmsiz(xmsiz) if dfunit: self.setdfunit(dfunit) if not tc.hdb_open(self.db, path, omode): raise tc.TCException(tc.hdb_errmsg(tc.hdb_ecode(self.db)))
def vanish(self): """Remove all records of a B+ tree database object.""" result = tc.bdb_vanish(self.db) if not result: raise tc.TCException(tc.bdb_errmsg(tc.bdb_ecode(self.db))) return result
def range(self, keya=None, inca=True, keyb=None, incb=True, max_=-1): """Get keys of ranged records in a B+ tree database object.""" tclist_objs = tc.bdb_range2(self.db, keya, inca, keyb, incb, max_) if not tclist_objs: raise tc.TCException(tc.bdb_errmsg(tc.bdb_ecode(self.db))) return util.deserialize_tclist(tclist_objs, str)
def out(self, key): """Remove a string record of a B+ tree database object.""" result = tc.bdb_out2(self.db, key) if not result: raise tc.TCException(tc.bdb_errmsg(tc.bdb_ecode(self.db))) return result
def put(self, key, value): """Store a string record into a B+ tree database object.""" result = tc.bdb_put2(self.db, key, value) if not result: raise tc.TCException(tc.bdb_errmsg(tc.bdb_ecode(self.db))) return result
def close(self): """Close a B+ tree database object.""" result = tc.bdb_close(self.db) if not result: raise tc.TCException(tc.bdb_errmsg(tc.bdb_ecode(self.db))) return result
def trancommit(self): """Commit the transaction of a fixed-length database object.""" result = tc.fdb_trancommit(self.db) if not result: raise tc.TCException(tc.fdb_errmsg(tc.fdb_ecode(self.db))) return result
def setcapnum(self, capnum): """Set the capacity number of records.""" result = tc.bdb_setcapnum(self.db, capnum) if not result: raise tc.TCException(tc.bdb_errmsg(tc.bdb_ecode(self.db))) return result
def path(self): """Get the file path of a B+ tree database object.""" result = tc.bdb_path(self.db) if not result: raise tc.TCException(tc.bdb_errmsg(tc.bdb_ecode(self.db))) return result
def key(self): """Get the key of the record where the cursor object is.""" key = tc.bdb_curkey2(self.cur) if not key: raise tc.TCException(tc.bdb_errmsg(tc.bdb_ecode(self.db))) return key.value
def put(self, value, cpmode=CPCURRENT): """Insert a record around a cursor object.""" result = tc.bdb_curput(self.cur, value, cpmode) if not result: raise tc.TCException(tc.bdb_errmsg(tc.bdb_ecode(self.db))) return result
def close(self): """Close a fixed-length database object.""" result = tc.fdb_close(self.db) if not result: raise tc.TCException(tc.fdb_errmsg(tc.fdb_ecode(self.db))) return result
def copy(self, path): """Copy the database file of a B+ tree database object.""" result = tc.bdb_copy(self.db, path) if not result: raise tc.TCException(tc.bdb_errmsg(tc.bdb_ecode(self.db))) return result
def tranabort(self): """Abort the transaction of a B+ tree database object.""" result = tc.bdb_tranabort(self.db) if not result: raise tc.TCException(tc.bdb_errmsg(tc.bdb_ecode(self.db))) return result
def out(self, key): """Remove a Python object of a fixed-length database object.""" result = tc.fdb_out(self.db, key) if not result: raise tc.TCException(tc.fdb_errmsg(tc.fdb_ecode(self.db))) return result
def setlsmax(self, lsmax): """Set the maximum size of each leaf node.""" result = tc.bdb_setlsmax(self.db, lsmax) if not result: raise tc.TCException(tc.bdb_errmsg(tc.bdb_ecode(self.db))) return result
def value(self): """Get the value of the record where the cursor object is.""" value = tc.bdb_curval2(self.cur) if not value: raise tc.TCException(tc.bdb_errmsg(tc.bdb_ecode(self.db))) return value.value
def cacheclear(self): """Clear the cache of a B+ tree database object.""" result = tc.bdb_cacheclear(self.db) if not result: raise tc.TCException(tc.bdb_errmsg(tc.bdb_ecode(self.db))) return result
def tune(self, lmemb=0, nmemb=0, bnum=0, apow=-1, fpow=-1, opts=0): """Set the tuning parameters of a B+ tree database object.""" result = tc.bdb_tune(self.db, lmemb, nmemb, bnum, apow, fpow, opts) if not result: raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) return result
def next(self): """Move a cursor object to the next record.""" result = tc.bdb_curnext(self.cur) if not result: raise tc.TCException(tc.bdb_errmsg(tc.bdb_ecode(self.db))) return result
def setcache(self, lcnum=0, ncnum=0): """Set the caching parameters of a B+ tree database object.""" result = tc.bdb_setcache(self.db, lcnum, ncnum) if not result: raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) return result