def read_at_cursor(self, cursor, sskey=None, ssvalue=None, flags=bsddb.db.DB_NEXT): if flags == bsddb.db.DB_SET or flags == bsddb.db.DB_SET_RANGE \ or flags == bsddb.db.DB_GET_BOTH or flags == bsddb.db.DB_GET_BOTH_RANGE: sk = None elif sskey is not None: if not isinstance(sskey, serialize.Serializable): sskey = DataStream(sskey) sk = sskey.serialize(serialize.SerType.SER_DISK, nVersion=PyDB.Version) if flags == bsddb.db.DB_GET_BOTH or flags == bsddb.db.DB_GET_BOTH_RANGE: sv = None elif ssvalue is not None: if not isinstance(ssvalue, serialize.Serializable): ssvalue = DataStream(ssvalue) sv = ssvalue.serialize(serialize.SerType.SER_DISK, nVersion=PyDB.Version) ret = cursor.get(flags=flags) if ret is None: return None if sskey is not None: sskey.deserialize(ret[0], serialize.SerType.SER_DISK) else: sskey = ret[0] if ssvalue is not None: ssvalue.deserialize(ret[1], serialize.SerType.SER_DISK) else: ssvalue = ret[1] return sskey, ssvalue
def _read(self, key, value=None, raw=False): """ :param key: a Serializable class or basic build-in type :type key: serialize.Serializable | Any :param value: a Serializable class or class type, None return raw str :type value: serialize.Serializable | type :return: str or class instance (Serializable or DataStream) :rtype: str | serialize.Serializable | DataStream | any """ if not isinstance(key, serialize.Serializable): if not raw: key = DataStream(key) sk = key.serialize(serialize.SerType.SER_DISK, nVersion=PyDB.Version) ssvalue_data = self._db.get(sk, default=None, txn=self.get_txn()) if ssvalue_data is None: return None # 在没有传入value 的时候会直接返回原始的 raw 字符串,可以在函数外面再处理 if value is not None: if inspect.isclass(value): # auto new the class instance value = value() value.deserialize(ssvalue_data, nType=serialize.SerType.SER_DISK, nVersion=PyDB.Version) else: value = DataStream().deserialize(ssvalue_data, nType=serialize.SerType.SER_DISK, nVersion=PyDB.Version) return value
def _exists(self, key): """ :param key: a Serializable class or basic type :type key: Any | Serializable :return: True->success False->fail :rtype: bool """ if not isinstance(key, serialize.Serializable): key = DataStream(key) if self._db is None: return False s = key.serialize(serialize.SerType.SER_DISK, nVersion=PyDB.Version) return self._db.exists(s, txn=self.get_txn())
def _erase(self, key, raw=False): """ :param key: a Serializable class or basic type :type key: Any | Serializable :return: True->success False->fail :rtype: bool """ if not isinstance(key, serialize.Serializable): if not raw: key = DataStream(key) if self._db is None: return False s = key.serialize(serialize.SerType.SER_DISK, nVersion=PyDB.Version) ret = 0 try: self._db.delete(s, txn=self.get_txn()) except bsddb.db.DBNotFoundError, e: ret = bsddb.db.DB_NOTFOUND
def _write(self, key, value, f_overwrite=True, raw=False): """ :param key: a Serializable class or basic type :type key: Any | Serializable :param value: a Serializable class or basic type :type value: Serializable | Any :param f_overwrite: mark if orverwrite :type f_overwrite: bool :return: None """ if not isinstance(key, serialize.Serializable): if not raw: key = DataStream(key) if not isinstance(value, serialize.Serializable): if not raw: value = DataStream(value) if self._db is None: return sk = key.serialize(serialize.SerType.SER_DISK, nVersion=PyDB.Version) sv = value.serialize(serialize.SerType.SER_DISK, nVersion=PyDB.Version) self._db.put(sk, sv, txn=self.get_txn(), flags=(0 if f_overwrite else bsddb.db.DB_NOOVERWRITE)) self._db.sync()
def write_version(self, version): self._write("version", DataStream(version)) PyDB.Version = version
def read_version(self, fromdb=True): if fromdb is False: return PyDB.Version s = DataStream() self._read("version", s) return s.get_data()
def read_datas(self, key=None, value=None, keycls=None, valcls=None, types=None, flags=bsddb.db.DB_NEXT): if types is None: types = list() cursor = self._get_cursor() if cursor is None: return None if key: if not isinstance(key, serialize.Serializable): key = DataStream(key) sskey = key.serialize(serialize.SerType.SER_DISK) else: sskey = '' if value: if not isinstance(value, serialize.Serializable): value = DataStream(value) ssvalue = value.serialize(serialize.SerType.SER_DISK) else: ssvalue = '' if flags == bsddb.db.DB_SET: cursor.set(key=sskey, flags=bsddb.db.DB_SET) elif flags == bsddb.db.DB_SET_RANGE: cursor.set_range(key=sskey, flags=bsddb.db.DB_SET_RANGE, dlen=-1, doff=-1) elif flags == bsddb.db.DB_GET_BOTH: cursor.set_both(key=sskey, data=ssvalue, flags=bsddb.db.DB_GET_BOTH) elif flags == bsddb.db.DB_GET_BOTH_RANGE: cursor.set_both(key=sskey, data=ssvalue, flags=bsddb.db.DB_GET_BOTH_RANGE) key_types = dict() for t in types: if isinstance(t, tuple): key_types[t[0]] = t[1] else: key_types[t] = None keys = key_types.keys() ret_list = list() while True: ret = cursor.next() if ret is None: break sskey = ret[0] ssvalue = ret[1] if keycls is None and valcls is None: key = DataStream().deserialize(sskey) if types: if isinstance(key, list): k_type = key[0] else: k_type = key if k_type not in keys: continue cls_type = key_types[k_type] if cls_type is None: value = DataStream().deserialize( ssvalue, nType=serialize.SerType.SER_DISK) else: value = cls_type() value.deserialize(ssvalue, nType=serialize.SerType.SER_DISK) else: value = ssvalue elif issubclass(keycls, serialize.Serializable) and issubclass( valcls, serialize.Serializable): key = keycls() key.deserialize(sskey, nType=serialize.SerType.SER_DISK) value = valcls() value.deserialize(sskey, nType=serialize.SerType.SER_DISK) else: return None ret_list.append((key, value)) return ret_list