class ShelfDatabase(database.Database): """ShelfDatabase is a thread-safe implementation of the journal.database.Database interface which uses python Shelve for the underlying persistence. Attributes: lock (threading.RLock): A reentrant lock to ensure threadsafe access. shelf (shelve.Shelf): The underlying shelf database. """ def __init__(self, filename, flag): """Constructor for the ShelfDatabase class. Args: filename (str): The filename of the database file. flag (str): a flag indicating the mode for opening the database. Refer to the documentation for anydbm.open(). """ super(ShelfDatabase, self).__init__() self._lock = RLock() self._shelf = Shelf(anydbm.open(filename, flag)) def __len__(self): with self._lock: return len(self._shelf) def __contains__(self, key): with self._lock: return key in self._shelf def get(self, key): """Retrieves a value associated with a key from the database Args: key (str): The key to retrieve """ with self._lock: return self._shelf.get(key, default=None) def set(self, key, value): """Sets a value associated with a key in the database Args: key (str): The key to set. value (str): The value to associate with the key. """ with self._lock: self._shelf[key] = value def delete(self, key): """Removes a key:value from the database Args: key (str): The key to remove. """ with self._lock: del self._shelf[key] def sync(self): """Ensures that pending writes are flushed to disk """ with self._lock: self._shelf.sync() def close(self): """Closes the connection to the database """ with self._lock: self._shelf.close() def keys(self): """Returns a list of keys in the database """ with self._lock: return self._shelf.keys()
def keys(self): with self._lock: return Shelf.keys(self)
class ShelfDatabase(database.Database): """ShelfDatabase is a thread-safe implementation of the sawtooth_validator.database.Database interface which uses python Shelve for the underlying persistence. Attributes: lock (threading.RLock): A reentrant lock to ensure threadsafe access. shelf (shelve.Shelf): The underlying shelf database. """ def __init__(self, filename, flag): """Constructor for the ShelfDatabase class. Args: filename (str): The filename of the database file. flag (str): a flag indicating the mode for opening the database. Refer to the documentation for anydbm.open(). """ super(ShelfDatabase, self).__init__() self._lock = RLock() self._shelf = Shelf(dbm.open(filename, flag)) def __len__(self): with self._lock: return len(self._shelf) def __contains__(self, key): with self._lock: return key in self._shelf def get(self, key): """Retrieves a value associated with a key from the database Args: key (str): The key to retrieve """ with self._lock: return self._shelf.get(key, default=None) def set(self, key, value): """Sets a value associated with a key in the database Args: key (str): The key to set. value (str): The value to associate with the key. """ with self._lock: self._shelf[key] = value def delete(self, key): """Removes a key:value from the database Args: key (str): The key to remove. """ with self._lock: del self._shelf[key] def sync(self): """Ensures that pending writes are flushed to disk """ with self._lock: self._shelf.sync() def close(self): """Closes the connection to the database """ with self._lock: self._shelf.close() def keys(self): """Returns a list of keys in the database """ with self._lock: return self._shelf.keys()