def vsiz(self, key): """Get the size of the value of a string record in a B+ tree database object.""" result = tc.bdb_vsiz2(self.db, key) if result == -1: raise tc.TCException(tc.bdb_errmsg(tc.bdb_ecode(self.db))) return result
def sync(self): """Synchronize updated contents of a B+ tree database object with the file and the device.""" result = tc.bdb_sync(self.db) if not result: raise tc.TCException(tc.bdb_errmsg(tc.bdb_ecode(self.db))) return result
def fsiz(self): """Get the size of the database file of a B+ tree database object.""" result = tc.bdb_fsiz(self.db) if not result: raise tc.TCException(tc.bdb_errmsg(tc.bdb_ecode(self.db))) return result
def putdupback(self, key, value): """Store a new string record into a B+ tree database object with backward duplication.""" result = tc.bdb_putdupback2(self.db, key, value) if not result: raise tc.TCException(tc.bdb_errmsg(tc.bdb_ecode(self.db))) return result
def outdup(self, key, as_raw=False): """Remove Python objects of a B+ tree database object.""" (c_key, c_key_len) = util.serialize(key, as_raw) result = tc.bdb_out3(self.db, c_key, c_key_len) if not result: raise tc.TCException(tc.bdb_errmsg(tc.bdb_ecode(self.db))) return result
def defrag(self, step): """Perform dynamic defragmentation of a B+ tree database object.""" result = tc.bdb_defrag(self.db, step) if not result: raise tc.TCException(tc.bdb_errmsg(tc.bdb_ecode(self.db))) return result
def putdup(self, key, value): """Store a string record into a B+ tree database object with allowing duplication of keys.""" result = tc.bdb_putdup2(self.db, key, value) if not result: raise tc.TCException(tc.bdb_errmsg(tc.bdb_ecode(self.db))) return result
def outdup(self, key): """Remove records of a B+ tree database object.""" (c_key, c_key_len) = util.serialize(key, str) result = tc.bdb_out3(self.db, c_key, c_key_len) if not result: raise tc.TCException(tc.bdb_errmsg(tc.bdb_ecode(self.db))) return result
def open(self, path, omode=OWRITER | OCREAT, lmemb=0, nmemb=0, bnum=0, apow=-1, fpow=-1, opts=0, lcnum=0, ncnum=0, xmsiz=0, dfunit=0): """Open a database file and connect a B+ tree database object.""" if lmemb or nmemb or bnum or apow >= 0 or fpow >= 0 or opts: self.tune(lmemb, nmemb, bnum, apow, fpow, opts) if lcnum or ncnum: self.setcache(lcnum, ncnum) if xmsiz: self.setxmsiz(xmsiz) if dfunit: self.setdfunit(dfunit) if not tc.bdb_open(self.db, path, omode): raise tc.TCException(tc.bdb_errmsg(tc.bdb_ecode(self.db)))
def putcat(self, key, value): """Concatenate a string value at the end of the existing record in a B+ tree database object.""" result = tc.bdb_putcat2(self.db, key, value) if not result: raise tc.TCException(tc.bdb_errmsg(tc.bdb_ecode(self.db))) return result
def value(self, as_type=None): """Get the value of the record where the cursor object is.""" c_value, c_value_len = tc.bdb_curval(self.cur) if not c_value: raise tc.TCException(tc.bdb_errmsg(tc.bdb_ecode(self.db))) value = util.deserialize(c_value, c_value_len, as_type) return value
def setcmpfunc(self, cmp_, cmpop, raw_key=False, value_type=None): """Set the custom comparison function of a B+ tree database object.""" def cmp_wraper(c_keya, c_keya_len, c_keyb, c_keyb_len, op): keya = util.deserialize(ctypes.cast(c_keya, ctypes.c_void_p), c_keya_len, raw_key) keyb = util.deserialize(ctypes.cast(c_keyb, ctypes.c_void_p), c_keyb_len, value_type) return cmp_(keya, keyb, ctypes.cast(op, ctypes.c_char_p).value) # If cmp_ is a string, it indicate a native tccmpxxx funcion. native = { None: tc.tccmplexical, 'default': tc.tccmplexical, 'tccmplexical': tc.tccmplexical, 'tccmpdecimal': tc.tccmpdecimal, 'tccmpint32': tc.tccmpint32, 'tccmpint64': tc.tccmpint64 } if cmp_ in native: result = tc.bdb_setcmpfunc(self.db, native[cmp_], cmpop) else: result = tc.bdb_setcmpfunc(self.db, tc.TCCMP(cmp_wraper), cmpop) if not result: raise tc.TCException(tc.bdb_errmsg(tc.bdb_ecode(self.db))) return result
def key(self, as_type=None): """Get the key of the record where the cursor object is.""" c_key, c_key_len = tc.bdb_curkey(self.cur) if not c_key: raise tc.TCException(tc.bdb_errmsg(tc.bdb_ecode(self.db))) key = util.deserialize(c_key, c_key_len, as_type) return key
def put(self, value, cpmode=CPCURRENT, as_raw=False): """Insert a record around a cursor object.""" (c_value, c_value_len) = util.serialize(value, as_raw) result = tc.bdb_curput(self.cur, c_value, c_value_len, cpmode) if not result: raise tc.TCException(tc.bdb_errmsg(tc.bdb_ecode(self.db))) return result
def memsync(self, phys): """Synchronize updating contents on memory of a B+ tree database object.""" result = tc.bdb_memsync(self.db, phys) if not result: raise tc.TCException(tc.bdb_errmsg(tc.bdb_ecode(self.db))) return result
def fwmkeys(self, prefix, max_=-1): """Get forward matching string keys in a B+ tree database object.""" tclist_objs = tc.bdb_fwmkeys2(self.db, prefix, max_) if not tclist_objs: raise tc.TCException(tc.bdb_errmsg(tc.bdb_ecode(self.db))) return util.deserialize_tclist(tclist_objs, str)
def vsiz(self, key, as_raw=False): """Get the size of the value of a record in a B+ tree database object.""" (c_key, c_key_len) = util.serialize(key, as_raw) result = tc.bdb_vsiz(self.db, c_key, c_key_len) if result == -1: raise tc.TCException(tc.bdb_errmsg(tc.bdb_ecode(self.db))) return result
def add_float(self, key, num, as_raw=False): """Add a real number to a record in a B+ tree database object.""" assert isinstance(num, float), 'Value is not a float' (c_key, c_key_len) = util.serialize(key, as_raw) result = tc.bdb_adddouble(self.db, c_key, c_key_len, num) if not result: raise tc.TCException(tc.bdb_errmsg(tc.bdb_ecode(self.db))) return result
def put(self, key, value, raw_key=False, raw_value=False): """Store any Python object into a B+ tree database object.""" (c_key, c_key_len) = util.serialize(key, raw_key) (c_value, c_value_len) = util.serialize(value, raw_value) result = tc.bdb_put(self.db, c_key, c_key_len, c_value, c_value_len) if not result: raise tc.TCException(tc.bdb_errmsg(tc.bdb_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 B+ tree database object.""" (c_key, c_key_len) = util.serialize(key, raw_key) (c_value, c_value_len) = util.serialize(value, raw_value) result = tc.bdb_putcat(self.db, c_key, c_key_len, c_value, c_value_len) if not result: raise tc.TCException(tc.bdb_errmsg(tc.bdb_ecode(self.db))) return result
def putdup_iter(self, key, values, raw_key=False, raw_value=False): """Store Python records into a B+ tree database object with allowing duplication of keys.""" (c_key, c_key_len) = util.serialize(key, raw_key) tclist_vals = util.serialize_tclist(values, raw_value) result = tc.bdb_putdup3(self.db, c_key, c_key_len, tclist_vals) if not result: raise tc.TCException(tc.bdb_errmsg(tc.bdb_ecode(self.db))) return result
def fwmkeys(self, prefix, max_=-1, as_raw=True): """Get forward matching string keys in a B+ tree database object.""" (c_prefix, c_prefix_len) = util.serialize(prefix, as_raw) tclist_objs = tc.bdb_fwmkeys(self.db, c_prefix, c_prefix_len, max_) if not tclist_objs: raise tc.TCException(tc.bdb_errmsg(tc.bdb_ecode(self.db))) as_type = util.get_type(prefix, as_raw) return util.deserialize_tclist(tclist_objs, as_type)
def record(self, key_type=None, value_type=None): """Get the key and the value of the record where the cursor object is.""" xstr_key = tc.tcxstrnew() xstr_value = tc.tcxstrnew() result = tc.bdb_currec(self.cur, xstr_key, xstr_value) if not result: raise tc.TCException(tc.bdb_errmsg(tc.bdb_ecode(self.db))) key = util.deserialize_xstr(xstr_key, key_type) value = util.deserialize_xstr(xstr_value, value_type) return (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 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 _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 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 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 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