def setdfunit(self, dfunit=0): """Set the unit step number of auto defragmentation of a B+ tree database object.""" result = tc.bdb_setdfunit(self.db, dfunit) if not result: raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) return result
def setdfunit(self, dfunit=0): """Set the unit step number of auto defragmentation of a hash database object.""" result = tc.hdb_setdfunit(self.db, dfunit) if not result: raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) return result
def setinvcache(self, iccmax, iccsync): """Set the parameters of the inverted cache of a table database object.""" result = tc.tdb_setinvcache(self.db, iccmax, iccsync) if not result: raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) return result
def searchout_non_atomic(self): """Remove each record corresponding to a query object with non-atomic fashion.""" result = tc.tdb_qrysearchout2(self.qry) if not result: raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) return result
def out(self, key, as_raw=False): """Remove a record of a table database object.""" (c_key, c_key_len) = util.serialize(key, as_raw) result = tc.tdb_out(self.db, c_key, c_key_len) if not result: raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) return result
def setxmsiz(self, xmsiz=0): """Set the size of the extra mapped memory of a hash database object.""" result = tc.hdb_setxmsiz(self.db, xmsiz) if not result: raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) return result
def fsiz(self): """Get the size of the database file of a table database object.""" result = tc.tdb_fsiz(self.db) if not result: raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) return result
def sync(self): """Synchronize updated contents of a table database object with the file and the device.""" result = tc.tdb_sync(self.db) if not result: raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) return result
def out(self, key, as_raw=False): """Remove a record of a table database object.""" (c_key, c_key_len) = util.serialize(key, as_raw) result = tc.tdb_out(self.db, c_key, c_key_len) if not result: raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) return result
def defrag(self, step): """Perform dynamic defragmentation of a table database object.""" result = tc.tdb_defrag(self.db, step) if not result: raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) return result
def defrag(self, step): """Perform dynamic defragmentation of a table database object.""" result = tc.tdb_defrag(self.db, step) if not result: raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) return result
def memsync(self, phys): """Synchronize updating contents on memory of a table database object.""" result = tc.tdb_memsync(self.db, phys) if not result: raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) return result
def tune(self, width=0, limsiz=0): """Set the tuning parameters of a fixed-length database object.""" result = tc.fdb_tune(self.db, width, limsiz) if not result: raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) return result
def setuidseed(self, seed): """Set the seed of unique ID unumbers of a table database object.""" result = tc.tdb_setuidseed(self.db, seed) if not result: raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) return result
def setuidseed(self, seed): """Set the seed of unique ID unumbers of a table database object.""" result = tc.tdb_setuidseed(self.db, seed) if not result: raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) return result
def setinvcache(self, iccmax, iccsync): """Set the parameters of the inverted cache of a table database object.""" result = tc.tdb_setinvcache(self.db, iccmax, iccsync) if not result: raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) return result
def memsync(self, phys): """Synchronize updating contents on memory of a table database object.""" result = tc.tdb_memsync(self.db, phys) if not result: raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) return result
def fsiz(self): """Get the size of the database file of a table database object.""" result = tc.tdb_fsiz(self.db) if not result: raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) return result
def setxmsiz(self, xmsiz=0): """Set the size of the extra mapped memory of a B+ tree database object.""" result = tc.bdb_setxmsiz(self.db, xmsiz) if not result: raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) return result
def searchout_non_atomic(self): """Remove each record corresponding to a query object with non-atomic fashion.""" result = tc.tdb_qrysearchout2(self.qry) if not result: raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) return result
def sync(self): """Synchronize updated contents of a table database object with the file and the device.""" result = tc.tdb_sync(self.db) if not result: raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) return result
def vsiz(self, key, as_raw=False): """Get the size of the value of a Python object in a table database object.""" (c_key, c_key_len) = util.serialize(key, as_raw) result = tc.tdb_vsiz(self.db, c_key, c_key_len) if result == -1: raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) return result
def vsiz(self, key, as_raw=False): """Get the size of the value of a Python object in a table database object.""" (c_key, c_key_len) = util.serialize(key, as_raw) result = tc.tdb_vsiz(self.db, c_key, c_key_len) if result == -1: raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) return result
def putcat(self, key, cols, raw_key=False, raw_cols=False): """Concatenate columns of the existing record in a table database object.""" (c_key, c_key_len) = util.serialize(key, raw_key) cols_tcmap = util.serialize_tcmap(cols, raw_cols) result = tc.tdb_putcat(self.db, c_key, c_key_len, cols_tcmap) if not result: raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) return result
def add_float(self, key, num, as_raw=False): """Add a real number to a column of a record in a table database object.""" assert isinstance(num, float), 'Value is not a float' (c_key, c_key_len) = util.serialize(key, as_raw) result = tc.tdb_adddouble(self.db, c_key, c_key_len, num) if not result: raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) return result
def add_float(self, key, num, as_raw=False): """Add a real number to a column of a record in a table database object.""" assert isinstance(num, float), 'Value is not a float' (c_key, c_key_len) = util.serialize(key, as_raw) result = tc.tdb_adddouble(self.db, c_key, c_key_len, num) if not result: raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) return result
def fwmkeys(self, prefix, as_raw=True): """Get forward matching primary keys in a table database object.""" (c_prefix, c_prefix_len) = util.serialize(prefix, as_raw) tclist_objs = tc.tdb_fwmkeys(self.db, c_prefix, c_prefix_len) if not tclist_objs: raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) as_type = util.get_type(prefix, as_raw) return util.deserialize_tclist(tclist_objs, as_type)
def fwmkeys(self, prefix, as_raw=True): """Get forward matching primary keys in a table database object.""" (c_prefix, c_prefix_len) = util.serialize(prefix, as_raw) tclist_objs = tc.tdb_fwmkeys(self.db, c_prefix, c_prefix_len) if not tclist_objs: raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) as_type = util.get_type(prefix, as_raw) return util.deserialize_tclist(tclist_objs, as_type)
def put(self, key, cols, raw_key=False, raw_cols=False): """Store a record into a table database object.""" assert isinstance(cols, dict) (c_key, c_key_len) = util.serialize(key, raw_key) cols_tcmap = util.serialize_tcmap(cols, raw_cols) result = tc.tdb_put(self.db, c_key, c_key_len, cols_tcmap) if not result: raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) return result
def put(self, key, cols, raw_key=False, raw_cols=False): """Store a record into a table database object.""" assert isinstance(cols, dict) (c_key, c_key_len) = util.serialize(key, raw_key) cols_tcmap = util.serialize_tcmap(cols, raw_cols) result = tc.tdb_put(self.db, c_key, c_key_len, cols_tcmap) if not result: raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) return result
def putcat(self, key, cols, raw_key=False, raw_cols=False): """Concatenate columns of the existing record in a table database object.""" (c_key, c_key_len) = util.serialize(key, raw_key) cols_tcmap = util.serialize_tcmap(cols, raw_cols) result = tc.tdb_putcat(self.db, c_key, c_key_len, cols_tcmap) if not result: raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) return result
def itervalues(self, schema=None): """Iterate for every 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: cols_tcmap = tc.tdb_iternext3(self.db) if not cols_tcmap: break cols = util.deserialize_tcmap(cols_tcmap, schema) yield cols
def iterkeys(self, as_type=None): """Iterate for every key 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 key = util.deserialize(c_key, c_key_len, as_type) yield key
def iterkeys(self, as_type=None): """Iterate for every key 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 key = util.deserialize(c_key, c_key_len, as_type) yield key
def itervalues(self, schema=None): """Iterate for every 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: cols_tcmap = tc.tdb_iternext3(self.db) if not cols_tcmap: break cols = util.deserialize_tcmap(cols_tcmap, schema) yield cols
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 optimize(self, bnum=None, apow=None, fpow=None, opts=None): """Optimize the file of a table database object.""" kwargs = dict([ x for x in (('bnum', bnum), ('apow', apow), ('fpow', fpow), ('opts', opts)) if x[1] ]) result = tc.tdb_optimize(self.db, **kwargs) if not result: raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) return result
def optimize(self, bnum=None, apow=None, fpow=None, opts=None): """Optimize the file of a table database object.""" kwargs = dict([x for x in (('bnum', bnum), ('apow', apow), ('fpow', fpow), ('opts', opts)) if x[1]]) result = tc.tdb_optimize(self.db, **kwargs) if not result: raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) return result
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 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, 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 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 foreach(self, proc, op, key_type=None, schema=None): """Process each record atomically of a table database object.""" def proc_wraper(c_key, c_key_len, c_cols, c_cols_len, op): key = util.deserialize(ctypes.cast(c_key, ctypes.c_void_p), c_key_len, key_type) cols = util.deserialize(ctypes.cast(c_cols, ctypes.c_void_p), c_cols_len, str) return proc(key, cols, ctypes.cast(op, ctypes.c_char_p).value) result = tc.tdb_foreach(self.db, tc.TCITER(proc_wraper), op) if not result: raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) return result
def foreach(self, proc, op, key_type=None, schema=None): """Process each record atomically of a table database object.""" def proc_wraper(c_key, c_key_len, c_cols, c_cols_len, op): key = util.deserialize(ctypes.cast(c_key, ctypes.c_void_p), c_key_len, key_type) cols = util.deserialize(ctypes.cast(c_cols, ctypes.c_void_p), c_cols_len, str) return proc(key, cols, ctypes.cast(op, ctypes.c_char_p).value) result = tc.tdb_foreach(self.db, tc.TCITER(proc_wraper), op) if not result: raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) return result
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 _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 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 genuid(self): """Generate a unique ID number of a table database object.""" result = tc.tdb_genuid(self.db) if result == -1: raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) return result
def setcache(self, rcnum=0): """Set the caching parameters of a hash database object.""" result = tc.hdb_setcache(self.db, rcnum) if not result: raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) return result
def path(self): """Get the file path of a table database object.""" result = tc.tdb_path(self.db) if not result: raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) return result
def path(self): """Get the file path of a table database object.""" result = tc.tdb_path(self.db) if not result: raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) return result
def tune(self, bnum=0, apow=-1, fpow=-1, opts=0): """Set the tuning parameters of a hash database object.""" result = tc.hdb_tune(self.db, bnum, apow, fpow, opts) if not result: raise tc.TCException(tc.tdb_errmsg(tc.tdb_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 strtometasearchtype(self, str_): """Convert a string into the meta search type number.""" result = tc.tdb_strtometasearchtype(str_) if result == -1: raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) return result
def setindex(self, name, type_): """Set a column index to a table database object.""" result = tc.tdb_setindex(self.db, name, type_) if not result: raise tc.TCException(tc.tdb_errmsg(tc.tdb_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
def setindex(self, name, type_): """Set a column index to a table database object.""" result = tc.tdb_setindex(self.db, name, type_) if not result: raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) return result
def strtometasearchtype(self, str_): """Convert a string into the meta search type number.""" result = tc.tdb_strtometasearchtype(str_) if result == -1: raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) return result
def genuid(self): """Generate a unique ID number of a table database object.""" result = tc.tdb_genuid(self.db) if result == -1: raise tc.TCException(tc.tdb_errmsg(tc.tdb_ecode(self.db))) return result