class DBDB(object): ''' defines a class (DBDB) which implements the Python dictionary API using the concrete BinaryTree implementation. This is how you'd use DBDB inside a Python program. ''' def __init__(self, f): self._storage = Storage(f) self._tree = BinaryTree(self._storage) def _assert_not_closed(self): if self._storage.closed: raise ValueError('Database closed.') def __getitem__(self, key): self._assert_not_closed() return self._tree.get(key) def __setitem__(self, key, value): self._assert_not_closed() return self._tree.set(key, value) def __delitem__(self, key): self._assert_not_closed() return self._tree.pop(key) def __contains__(self, key): try: self[key] except KeyError: return False else: return True def close(self): self._storage.close() def commit(self): self._assert_not_closed() self._tree.commit() def __len__(self): return len(self._tree) def find_max(self): return self._tree.find_max()
class DBDB(object): """ implements the python dictionary API using the concrete BinaryTree implementation """ def __init__(self, f): self._storage = Storage(f) # Data stores tend to use more complex types of search trees such as # B-trees, B+ trees, and others to improve the performance. self._tree = BinaryTree(self._storage) def _assert_not_closed(self): if self._storage.closed: raise ValueError('Database closed!') def commit(self): self._assert_not_closed() self._tree.commit() def close(self): self._storage.close() def __getitem__(self, key): self._assert_not_closed() return self._tree.get(key) def __setitem__(self, key, value): self._assert_not_closed() return self._tree.set(key, value) def __delitem__(self, key): self._assert_not_closed() return self._tree.pop(key) def __contains__(self, key): try: self[key] except KeyError: return False else: return True def __len__(self): return len(self._tree)
class DB(object): """将二叉树的操作封装成dict键值对操作""" def __init__(self, f): self._storage = Storage(f) self._tree = BinaryTree(self._storage) def _assert_not_closed(self): """检查文件是否关闭""" if self._storage.closed: raise ValueError('Database closed.') def close(self): self._storage.close() def commit(self): """提交后才会更新数据库""" self._assert_not_closed() self._tree.commit() def __getitem__(self, key): """通过 db[key] 获取值""" pass def __setitem__(self, key, value): """通过 db[key] = value 设置值""" pass def __delitem__(self, key): """通过 del db[key] 删除值""" pass def __contains__(self, key): """通过 key in db 判断键是否在数据库""" try: self[key] except KeyError: return False else: return True def __len__(self): return len(self._tree)
class DBDB(object): def __init__(self, f): self._storage = Storage(f) self._tree = BinaryTree(self._storage) def _assert_not_closed(self): if self._storage.closed: raise ValueError('Database closed.') def close(self): self._storage.close() def commit(self): self._assert_not_closed() self._tree.commit() def __getitem__(self, key): self._assert_not_closed() return self._tree.get(key) def __setitem__(self, key, value): self._assert_not_closed() return self._tree.set(key, value) def __delitem__(self, key): self._assert_not_closed() return self._tree.pop(key) def __contains__(self, key): try: self[key] except KeyError: return False else: return True def __len__(self): return len(self._tree)
def __init__(self, f): self._storage = Storage(f) self._tree = BinaryTree(self._storage)
def __init__(self, f): self._storage = Storage(f) # Data stores tend to use more complex types of search trees such as # B-trees, B+ trees, and others to improve the performance. self._tree = BinaryTree(self._storage)