Skip to content

In memory (for dev)

In-Memory Storage is a simple key-value store that keeps data in memory. It is useful for testing and development purposes, but not suitable for production use due to its volatility.

InMemStorage

Bases: CipherStorageProtocol

An in-memory implementation of the CipherStorageProtocol.

This class provides a simple, non-persistent storage mechanism using a Python dictionary. It is suitable for testing or scenarios where data persistence across sessions is not required.

Attributes:

Name Type Description
data dict[Hash, tuple[Ciphertext, Metadata]]

A dictionary to store ciphertexts and their associated metadata, keyed by a hash.

Source code in topsecret/infra/storage/inmem.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
class InMemStorage(CipherStorageProtocol):
    """An in-memory implementation of the CipherStorageProtocol.

    This class provides a simple, non-persistent storage mechanism using a Python
    dictionary. It is suitable for testing or scenarios where data persistence
    across sessions is not required.

    Attributes:
        data (dict[Hash, tuple[Ciphertext, Metadata]]): A dictionary to store
            ciphertexts and their associated metadata, keyed by a hash.
    """

    def __init__(self) -> None:
        """Initializes the InMemStorage.

        Sets up an empty dictionary to hold the stored data.
        """
        self.data: dict[Hash, tuple[Ciphertext, Metadata]] = {}

    def store(self, key: str, value: tuple[Ciphertext, Metadata]) -> None:
        """Stores a ciphertext and its metadata associated with a key.

        Args:
            key (str): The key under which to store the value.
            value (tuple[Ciphertext, Metadata]): A tuple containing the
                ciphertext and its associated metadata.
        """

        self.data[key] = value

    def retrieve(self, key: Hash) -> tuple[Ciphertext, Metadata]:
        """Retrieves the ciphertext and metadata associated with a key.

        Args:
            key (Hash): The key for which to retrieve the data.

        Returns:
            tuple[Ciphertext, Metadata]: The stored ciphertext and metadata.

        Raises:
            KeyError: If the key is not found in the storage.
        """
        return self.data[key]

    def contains(self, key: Hash) -> bool:
        """Checks if a key exists in the storage.

        Args:
            key (Hash): The key to check for.

        Returns:
            bool: True if the key exists in the storage, False otherwise.
        """
        return key in self.data

__init__()

Initializes the InMemStorage.

Sets up an empty dictionary to hold the stored data.

Source code in topsecret/infra/storage/inmem.py
33
34
35
36
37
38
def __init__(self) -> None:
    """Initializes the InMemStorage.

    Sets up an empty dictionary to hold the stored data.
    """
    self.data: dict[Hash, tuple[Ciphertext, Metadata]] = {}

contains(key)

Checks if a key exists in the storage.

Parameters:

Name Type Description Default
key Hash

The key to check for.

required

Returns:

Name Type Description
bool bool

True if the key exists in the storage, False otherwise.

Source code in topsecret/infra/storage/inmem.py
65
66
67
68
69
70
71
72
73
74
def contains(self, key: Hash) -> bool:
    """Checks if a key exists in the storage.

    Args:
        key (Hash): The key to check for.

    Returns:
        bool: True if the key exists in the storage, False otherwise.
    """
    return key in self.data

retrieve(key)

Retrieves the ciphertext and metadata associated with a key.

Parameters:

Name Type Description Default
key Hash

The key for which to retrieve the data.

required

Returns:

Type Description
tuple[Ciphertext, Metadata]

tuple[Ciphertext, Metadata]: The stored ciphertext and metadata.

Raises:

Type Description
KeyError

If the key is not found in the storage.

Source code in topsecret/infra/storage/inmem.py
51
52
53
54
55
56
57
58
59
60
61
62
63
def retrieve(self, key: Hash) -> tuple[Ciphertext, Metadata]:
    """Retrieves the ciphertext and metadata associated with a key.

    Args:
        key (Hash): The key for which to retrieve the data.

    Returns:
        tuple[Ciphertext, Metadata]: The stored ciphertext and metadata.

    Raises:
        KeyError: If the key is not found in the storage.
    """
    return self.data[key]

store(key, value)

Stores a ciphertext and its metadata associated with a key.

Parameters:

Name Type Description Default
key str

The key under which to store the value.

required
value tuple[Ciphertext, Metadata]

A tuple containing the ciphertext and its associated metadata.

required
Source code in topsecret/infra/storage/inmem.py
40
41
42
43
44
45
46
47
48
49
def store(self, key: str, value: tuple[Ciphertext, Metadata]) -> None:
    """Stores a ciphertext and its metadata associated with a key.

    Args:
        key (str): The key under which to store the value.
        value (tuple[Ciphertext, Metadata]): A tuple containing the
            ciphertext and its associated metadata.
    """

    self.data[key] = value