def defrag(self, step): """Perform dynamic defragmentation of a hash database object.""" result = tc.hdb_defrag(self.db, step) if not result: raise tc.TCException(tc.hdb_errmsg(tc.hdb_ecode(self.db))) return result
def memsync(self, phys): """Synchronize updating contents on memory of a hash database object.""" result = tc.hdb_memsync(self.db, phys) if not result: raise tc.TCException(tc.hdb_errmsg(tc.hdb_ecode(self.db))) return result
def memsync(self, phys): """Synchronize updating contents on memory of a hash database object.""" result = tc.hdb_memsync(self.db, phys) if not result: raise tc.TCException(tc.hdb_errmsg(tc.hdb_ecode(self.db))) return result
def out(self, key, as_raw=False): """Remove a Python object of a hash database object.""" (c_key, c_key_len) = util.serialize(key, as_raw) result = tc.hdb_out(self.db, c_key, c_key_len) if not result: raise tc.TCException(tc.hdb_errmsg(tc.hdb_ecode(self.db))) return result
def fsiz(self): """Get the size of the database file of a hash database object.""" result = tc.hdb_fsiz(self.db) if not result: raise tc.TCException(tc.hdb_errmsg(tc.hdb_ecode(self.db))) return result
def sync(self): """Synchronize updated contents of a hash database object with the file and the device.""" result = tc.hdb_sync(self.db) if not result: raise tc.TCException(tc.hdb_errmsg(tc.hdb_ecode(self.db))) return result
def out(self, key): """Remove a string record of a hash database object.""" # assert isinstance(key, str), 'Key is not a string' result = tc.hdb_out2(self.db, key) if not result: raise tc.TCException(tc.hdb_errmsg(tc.hdb_ecode(self.db))) return result
def vsiz(self, key): """Get the size of the value of a Python object in a hash database object.""" result = tc.hdb_vsiz2(self.db, key) if result == -1: raise tc.TCException(tc.hdb_errmsg(tc.hdb_ecode(self.db))) return result
def defrag(self, step): """Perform dynamic defragmentation of a hash database object.""" result = tc.hdb_defrag(self.db, step) if not result: raise tc.TCException(tc.hdb_errmsg(tc.hdb_ecode(self.db))) return result
def sync(self): """Synchronize updated contents of a hash database object with the file and the device.""" result = tc.hdb_sync(self.db) if not result: raise tc.TCException(tc.hdb_errmsg(tc.hdb_ecode(self.db))) return result
def vsiz(self, key): """Get the size of the value of a Python object in a hash database object.""" result = tc.hdb_vsiz2(self.db, key) if result == -1: raise tc.TCException(tc.hdb_errmsg(tc.hdb_ecode(self.db))) return result
def out(self, key): """Remove a string record of a hash database object.""" # assert isinstance(key, str), 'Key is not a string' result = tc.hdb_out2(self.db, key) if not result: raise tc.TCException(tc.hdb_errmsg(tc.hdb_ecode(self.db))) return result
def fsiz(self): """Get the size of the database file of a hash database object.""" result = tc.hdb_fsiz(self.db) if not result: raise tc.TCException(tc.hdb_errmsg(tc.hdb_ecode(self.db))) return result
def fwmkeys(self, prefix, max_=-1, as_raw=True): """Get forward matching string keys in a hash database object.""" (c_prefix, c_prefix_len) = util.serialize(prefix, as_raw) tclist_objs = tc.hdb_fwmkeys(self.db, c_prefix, c_prefix_len, max_) if not tclist_objs: raise tc.TCException(tc.hdb_errmsg(tc.hdb_ecode(self.db))) as_type = util.get_type(prefix, as_raw) return util.deserialize_tclist(tclist_objs, as_type)
def add_float(self, key, num, as_raw=False): """Add a real number to a record in a hash database object.""" assert isinstance(num, float), 'Value is not a float' (c_key, c_key_len) = util.serialize(key, as_raw) result = tc.hdb_adddouble(self.db, c_key, c_key_len, num) if not result: raise tc.TCException(tc.hdb_errmsg(tc.hdb_ecode(self.db))) return result
def vsiz(self, key, as_raw=False): """Get the size of the value of a Python object in a hash database object.""" (c_key, c_key_len) = util.serialize(key, as_raw) result = tc.hdb_vsiz(self.db, c_key, c_key_len) if result == -1: raise tc.TCException(tc.hdb_errmsg(tc.hdb_ecode(self.db))) return result
def put(self, key, value): """Store a string record into a hash database object.""" # assert isinstance(key, str), 'Key is not a string' # assert isinstance(value, str), 'Value is not a string' result = tc.hdb_put2(self.db, key, value) if not result: raise tc.TCException(tc.hdb_errmsg(tc.hdb_ecode(self.db))) return result
def put(self, key, value): """Store a string record into a hash database object.""" # assert isinstance(key, str), 'Key is not a string' # assert isinstance(value, str), 'Value is not a string' result = tc.hdb_put2(self.db, key, value) if not result: raise tc.TCException(tc.hdb_errmsg(tc.hdb_ecode(self.db))) return result
def put(self, key, value, raw_key=False, raw_value=False): """Store any Python object into a hash database object.""" (c_key, c_key_len) = util.serialize(key, raw_key) (c_value, c_value_len) = util.serialize(value, raw_value) result = tc.hdb_put(self.db, c_key, c_key_len, c_value, c_value_len) if not result: raise tc.TCException(tc.hdb_errmsg(tc.hdb_ecode(self.db))) return result
def put(self, key, value, raw_key=False, raw_value=False): """Store any Python object into a hash database object.""" (c_key, c_key_len) = util.serialize(key, raw_key) (c_value, c_value_len) = util.serialize(value, raw_value) result = tc.hdb_put(self.db, c_key, c_key_len, c_value, c_value_len) if not result: raise tc.TCException(tc.hdb_errmsg(tc.hdb_ecode(self.db))) return result
def putcat(self, key, value, raw_key=False, raw_value=False): """Concatenate an object value at the end of the existing record in a hash database object.""" (c_key, c_key_len) = util.serialize(key, raw_key) (c_value, c_value_len) = util.serialize(value, raw_value) result = tc.hdb_putcat(self.db, c_key, c_key_len, c_value, c_value_len) if not result: raise tc.TCException(tc.hdb_errmsg(tc.hdb_ecode(self.db))) return result
def putcat(self, key, value): """Concatenate a string value at the end of the existing record in a hash database object.""" # assert isinstance(key, str), 'Key is not a string' # assert isinstance(value, str), 'Value is not a string' result = tc.hdb_putcat2(self.db, key, value) if not result: raise tc.TCException(tc.hdb_errmsg(tc.hdb_ecode(self.db))) return result
def iterkeys(self): """Iterate for every key 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: key = tc.hdb_iternext2(self.db) if not key: break yield key.value
def putcat(self, key, value): """Concatenate a string value at the end of the existing record in a hash database object.""" # assert isinstance(key, str), 'Key is not a string' # assert isinstance(value, str), 'Value is not a string' result = tc.hdb_putcat2(self.db, key, value) if not result: raise tc.TCException(tc.hdb_errmsg(tc.hdb_ecode(self.db))) return result
def iterkeys(self): """Iterate for every key 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: key = tc.hdb_iternext2(self.db) if not key: break yield key.value
def putcat(self, key, value, raw_key=False, raw_value=False): """Concatenate an object value at the end of the existing record in a hash database object.""" (c_key, c_key_len) = util.serialize(key, raw_key) (c_value, c_value_len) = util.serialize(value, raw_value) result = tc.hdb_putcat(self.db, c_key, c_key_len, c_value, c_value_len) if not result: raise tc.TCException(tc.hdb_errmsg(tc.hdb_ecode(self.db))) return result
def iterkeys(self, as_type=None): """Iterate for every key 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: c_key, c_key_len = tc.hdb_iternext(self.db) if not c_key: break key = util.deserialize(c_key, c_key_len, as_type) yield key
def optimize(self, bnum=None, apow=None, fpow=None, opts=None): """Optimize the file of a hash database object.""" kwargs = dict([x for x in (('bnum', bnum), ('apow', apow), ('fpow', fpow), ('opts', opts)) if x[1]]) result = tc.hdb_optimize(self.db, **kwargs) if not result: raise tc.TCException(tc.hdb_errmsg(tc.hdb_ecode(self.db))) return result
def itervalues(self): """Iterate for every 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: key = tc.hdb_iternext2(self.db) if not key: break value = tc.hdb_get2(self.db, key) yield value.value
def itervalues(self): """Iterate for every 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: key = tc.hdb_iternext2(self.db) if not key: break value = tc.hdb_get2(self.db, key) yield value.value
def optimize(self, bnum=None, apow=None, fpow=None, opts=None): """Optimize the file of a hash database object.""" kwargs = dict([ x for x in (('bnum', bnum), ('apow', apow), ('fpow', fpow), ('opts', opts)) if x[1] ]) result = tc.hdb_optimize(self.db, **kwargs) if not result: raise tc.TCException(tc.hdb_errmsg(tc.hdb_ecode(self.db))) return result
def itervalues(self, as_type=None): """Iterate for every 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: c_key, c_key_len = tc.hdb_iternext(self.db) if not c_key: break (c_value, c_value_len) = tc.hdb_get(self.db, c_key, c_key_len) value = util.deserialize(c_value, c_value_len, as_type) yield value
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 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 foreach(self, proc, op, key_type=None, value_type=None): """Process each record atomically of a hash 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, key_type) value = util.deserialize(ctypes.cast(c_value, ctypes.c_void_p), c_value_len, value_type) return proc(key, value, ctypes.cast(op, ctypes.c_char_p).value) result = tc.hdb_foreach(self.db, tc.TCITER(proc_wraper), op) if not result: raise tc.TCException(tc.hdb_errmsg(tc.hdb_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 fwmkeys(self, prefix, max_=-1): """Get forward matching string keys in a hash database object.""" tclist_objs = tc.hdb_fwmkeys2(self.db, prefix, max_) if not tclist_objs: raise tc.TCException(tc.hdb_errmsg(tc.hdb_ecode(self.db))) return util.deserialize_tclist(tclist_objs, str)
def vanish(self): """Remove all records of a hash database object.""" result = tc.hdb_vanish(self.db) if not result: raise tc.TCException(tc.hdb_errmsg(tc.hdb_ecode(self.db))) return result
def copy(self, path): """Copy the database file of a hash database object.""" result = tc.hdb_copy(self.db, path) if not result: raise tc.TCException(tc.hdb_errmsg(tc.hdb_ecode(self.db))) return result
def path(self): """Get the file path of a hash database object.""" result = tc.hdb_path(self.db) if not result: raise tc.TCException(tc.hdb_errmsg(tc.hdb_ecode(self.db))) return result
def cacheclear(self): """Clear the cache of a hash tree database object.""" result = tc.hdb_cacheclear(self.db) if not result: raise tc.TCException(tc.hdb_errmsg(tc.hdb_ecode(self.db))) return result
def tranvoid(self): """Void the transaction of a hash database object.""" result = tc.hdb_tranvoid(self.db) if not result: raise tc.TCException(tc.hdb_errmsg(tc.hdb_ecode(self.db))) return result
def close(self): """Close a hash database object.""" result = tc.hdb_close(self.db) if not result: raise tc.TCException(tc.hdb_errmsg(tc.hdb_ecode(self.db))) return result
def tranvoid(self): """Void the transaction of a hash database object.""" result = tc.hdb_tranvoid(self.db) if not result: raise tc.TCException(tc.hdb_errmsg(tc.hdb_ecode(self.db))) return result
def close(self): """Close a hash database object.""" result = tc.hdb_close(self.db) if not result: raise tc.TCException(tc.hdb_errmsg(tc.hdb_ecode(self.db))) return result
def fwmkeys(self, prefix, max_=-1): """Get forward matching string keys in a hash database object.""" tclist_objs = tc.hdb_fwmkeys2(self.db, prefix, max_) if not tclist_objs: raise tc.TCException(tc.hdb_errmsg(tc.hdb_ecode(self.db))) return util.deserialize_tclist(tclist_objs, str)
def cacheclear(self): """Clear the cache of a hash tree database object.""" result = tc.hdb_cacheclear(self.db) if not result: raise tc.TCException(tc.hdb_errmsg(tc.hdb_ecode(self.db))) return result
def vanish(self): """Remove all records of a hash database object.""" result = tc.hdb_vanish(self.db) if not result: raise tc.TCException(tc.hdb_errmsg(tc.hdb_ecode(self.db))) return result
def copy(self, path): """Copy the database file of a hash database object.""" result = tc.hdb_copy(self.db, path) if not result: raise tc.TCException(tc.hdb_errmsg(tc.hdb_ecode(self.db))) return result
def path(self): """Get the file path of a hash database object.""" result = tc.hdb_path(self.db) if not result: raise tc.TCException(tc.hdb_errmsg(tc.hdb_ecode(self.db))) return result