def _get_item(self, value_key: str, local_db: Shelf) -> Optional[Any]:
        version_key = f"{value_key}_{constants.DB_ITEM_VERSION_SUFFIX}"
        default_version = Version("0.0.0")
        current_version = self._current_versions[version_key]
        actual_version = (checked_cast(Version, local_db[version_key])
                          if version_key in local_db else default_version)

        return local_db.get(
            value_key) if actual_version >= current_version else None
Example #2
0
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()
Example #3
0
 def get(self, key, default=None):
     with self._lock:
         return Shelf.get(self, key, default)
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()
Example #5
0
 def get(self, key, default=None):
     with self._lock:
         return Shelf.get(self, key, default)