def vsiz(self, key): """Get the size of the string value with a decimal key in a fixed-length database object.""" result = tc.fdb_vsiz3(self.db, key) if result == -1: raise tc.TCException(tc.fdb_errmsg(tc.fdb_ecode(self.db))) return result
def out(self, key): """Remove a record with a decimal key of a fixed-length database object.""" result = tc.fdb_out3(self.db, key) if not result: raise tc.TCException(tc.fdb_errmsg(tc.fdb_ecode(self.db))) return result
def vsiz(self, key): """Get the size of the value of a Python object in a fixed-length database object.""" result = tc.fdb_vsiz(self.db, key) if result == -1: raise tc.TCException(tc.fdb_errmsg(tc.fdb_ecode(self.db))) return result
def memsync(self, phys): """Synchronize updating contents on memory of a fixed-length database object.""" result = tc.fdb_memsync(self.db, phys) if not result: raise tc.TCException(tc.fdb_errmsg(tc.fdb_ecode(self.db))) return result
def putcat(self, key, value): """Concatenate a string value with a decimal key in a fixed-length database object.""" result = tc.fdb_putcat3(self.db, key, value) if not result: raise tc.TCException(tc.fdb_errmsg(tc.fdb_ecode(self.db))) return result
def add_int(self, key, num): """Add an integer to a record in a fixed-length database object.""" assert isinstance(num, int), 'Value is not an integer' result = tc.fdb_addint(self.db, key, num) if not result: raise tc.TCException(tc.fdb_errmsg(tc.fdb_ecode(self.db))) return result
def fsiz(self): """Get the size of the database file of a fixed-length database object.""" result = tc.fdb_fsiz(self.db) if not result: raise tc.TCException(tc.fdb_errmsg(tc.fdb_ecode(self.db))) return result
def open(self, path, omode=OWRITER|OCREAT, width=0, limsiz=0): """Open a database file and connect a fixed-length database object.""" self.tune(width, limsiz) if not tc.fdb_open(self.db, path, omode): raise tc.TCException(tc.fdb_errmsg(tc.fdb_ecode(self.db)))
def add_float(self, key, num): """Add a real number to a record in a fixed-length database object.""" assert isinstance(num, float), 'Value is not a float' result = tc.fdb_adddouble(self.db, key, num) if not result: raise tc.TCException(tc.fdb_errmsg(tc.fdb_ecode(self.db))) return result
def sync(self): """Synchronize updated contents of a fixed-length database object with the file and the device.""" result = tc.fdb_sync(self.db) if not result: raise tc.TCException(tc.fdb_errmsg(tc.fdb_ecode(self.db))) return result
def range(self, lower, upper, max_=-1): """Get range matching ID numbers in a fixed-length database object.""" tclist_objs = tc.fdb_range3(self.db, lower, upper, max_) if not tclist_objs: raise tc.TCException(tc.fdb_errmsg(tc.fdb_ecode(self.db))) return util.deserialize_tclist(tclist_objs, str)
def putcat(self, key, value, as_raw=False): """Concatenate a Python object value at the end of the existing record in a fixed-length database object.""" (c_value, c_value_len) = util.serialize(value, as_raw) result = tc.fdb_putcat(self.db, key, c_value, c_value_len) if not result: raise tc.TCException(tc.fdb_errmsg(tc.fdb_ecode(self.db))) return result
def put(self, key, value, as_raw=False): """Store any Python object into a fixed-length database object.""" (c_value, c_value_len) = util.serialize(value, as_raw) result = tc.fdb_put(self.db, key, c_value, c_value_len) if not result: raise tc.TCException(tc.fdb_errmsg(tc.fdb_ecode(self.db))) return result
def optimize(self, width=None, limsiz=None): """Optimize the file of a fixed-length database object.""" kwargs = dict([x for x in (('width', width), ('limsiz', limsiz)) if x[1]]) result = tc.fdb_optimize(self.db, **kwargs) if not result: raise tc.TCException(tc.fdb_errmsg(tc.fdb_ecode(self.db))) return result
def iterkeys(self): """Iterate for every key 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 yield key
def itervalues(self): """Iterate for every 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_iternext3(self.db) if not key: break value = tc.fdb_get3(self.db, key) yield value.value
def itervalues(self, as_type=None): """Iterate for every 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 value
def foreach(self, proc, op, as_type=None): """Process each record atomically of a fixed-length 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, as_type) return proc(int(key), value, ctypes.cast(op, ctypes.c_char_p).value) result = tc.fdb_foreach(self.db, tc.TCITER(proc_wraper), op) if not result: raise tc.TCException(tc.fdb_errmsg(tc.fdb_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 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 put(self, key, value): """Store a string record into a fixed-length database object.""" result = tc.fdb_put3(self.db, key, value) if not result: raise tc.TCException(tc.fdb_errmsg(tc.fdb_ecode(self.db))) return result
def vanish(self): """Remove all records of a fixed-length database object.""" result = tc.fdb_vanish(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 fixed-length database object.""" result = tc.fdb_copy(self.db, path) if not result: raise tc.TCException(tc.fdb_errmsg(tc.fdb_ecode(self.db))) return result
def tranabort(self): """Abort the transaction of a fixed-length database object.""" result = tc.fdb_tranabort(self.db) if not result: raise tc.TCException(tc.fdb_errmsg(tc.fdb_ecode(self.db))) return result
def path(self): """Get the file path of a fixed-length database object.""" result = tc.fdb_path(self.db) if not result: raise tc.TCException(tc.fdb_errmsg(tc.fdb_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