Skip to content

Simple

SimpleKVStore #

Bases: BaseInMemoryKVStore

Simple in-memory Key-Value store.

Parameters:

Name Type Description Default
data Optional[DATA_TYPE]

data to initialize the store with

None
Source code in llama-index-core/llama_index/core/storage/kvstore/simple_kvstore.py
 17
 18
 19
 20
 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
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
class SimpleKVStore(BaseInMemoryKVStore):
    """Simple in-memory Key-Value store.

    Args:
        data (Optional[DATA_TYPE]): data to initialize the store with
    """

    def __init__(
        self,
        data: Optional[DATA_TYPE] = None,
    ) -> None:
        """Init a SimpleKVStore."""
        self._data: DATA_TYPE = data or {}

    def put(self, key: str, val: dict, collection: str = DEFAULT_COLLECTION) -> None:
        """Put a key-value pair into the store."""
        if collection not in self._data:
            self._data[collection] = {}
        self._data[collection][key] = val.copy()

    async def aput(
        self, key: str, val: dict, collection: str = DEFAULT_COLLECTION
    ) -> None:
        """Put a key-value pair into the store."""
        self.put(key, val, collection)

    def get(self, key: str, collection: str = DEFAULT_COLLECTION) -> Optional[dict]:
        """Get a value from the store."""
        collection_data = self._data.get(collection, None)
        if not collection_data:
            return None
        if key not in collection_data:
            return None
        return collection_data[key].copy()

    async def aget(
        self, key: str, collection: str = DEFAULT_COLLECTION
    ) -> Optional[dict]:
        """Get a value from the store."""
        return self.get(key, collection)

    def get_all(self, collection: str = DEFAULT_COLLECTION) -> Dict[str, dict]:
        """Get all values from the store."""
        return self._data.get(collection, {}).copy()

    async def aget_all(self, collection: str = DEFAULT_COLLECTION) -> Dict[str, dict]:
        """Get all values from the store."""
        return self.get_all(collection)

    def delete(self, key: str, collection: str = DEFAULT_COLLECTION) -> bool:
        """Delete a value from the store."""
        try:
            self._data[collection].pop(key)
            return True
        except KeyError:
            return False

    async def adelete(self, key: str, collection: str = DEFAULT_COLLECTION) -> bool:
        """Delete a value from the store."""
        return self.delete(key, collection)

    def persist(
        self, persist_path: str, fs: Optional[fsspec.AbstractFileSystem] = None
    ) -> None:
        """Persist the store."""
        fs = fs or fsspec.filesystem("file")
        dirpath = os.path.dirname(persist_path)
        if not fs.exists(dirpath):
            fs.makedirs(dirpath)

        with fs.open(persist_path, "w") as f:
            f.write(json.dumps(self._data))

    @classmethod
    def from_persist_path(
        cls, persist_path: str, fs: Optional[fsspec.AbstractFileSystem] = None
    ) -> "SimpleKVStore":
        """Load a SimpleKVStore from a persist path and filesystem."""
        fs = fs or fsspec.filesystem("file")
        logger.debug(f"Loading {__name__} from {persist_path}.")
        with fs.open(persist_path, "rb") as f:
            data = json.load(f)
        return cls(data)

    def to_dict(self) -> dict:
        """Save the store as dict."""
        return self._data

    @classmethod
    def from_dict(cls, save_dict: dict) -> "SimpleKVStore":
        """Load a SimpleKVStore from dict."""
        return cls(save_dict)

put #

put(key: str, val: dict, collection: str = DEFAULT_COLLECTION) -> None

Put a key-value pair into the store.

Source code in llama-index-core/llama_index/core/storage/kvstore/simple_kvstore.py
31
32
33
34
35
def put(self, key: str, val: dict, collection: str = DEFAULT_COLLECTION) -> None:
    """Put a key-value pair into the store."""
    if collection not in self._data:
        self._data[collection] = {}
    self._data[collection][key] = val.copy()

aput async #

aput(key: str, val: dict, collection: str = DEFAULT_COLLECTION) -> None

Put a key-value pair into the store.

Source code in llama-index-core/llama_index/core/storage/kvstore/simple_kvstore.py
37
38
39
40
41
async def aput(
    self, key: str, val: dict, collection: str = DEFAULT_COLLECTION
) -> None:
    """Put a key-value pair into the store."""
    self.put(key, val, collection)

get #

get(key: str, collection: str = DEFAULT_COLLECTION) -> Optional[dict]

Get a value from the store.

Source code in llama-index-core/llama_index/core/storage/kvstore/simple_kvstore.py
43
44
45
46
47
48
49
50
def get(self, key: str, collection: str = DEFAULT_COLLECTION) -> Optional[dict]:
    """Get a value from the store."""
    collection_data = self._data.get(collection, None)
    if not collection_data:
        return None
    if key not in collection_data:
        return None
    return collection_data[key].copy()

aget async #

aget(key: str, collection: str = DEFAULT_COLLECTION) -> Optional[dict]

Get a value from the store.

Source code in llama-index-core/llama_index/core/storage/kvstore/simple_kvstore.py
52
53
54
55
56
async def aget(
    self, key: str, collection: str = DEFAULT_COLLECTION
) -> Optional[dict]:
    """Get a value from the store."""
    return self.get(key, collection)

get_all #

get_all(collection: str = DEFAULT_COLLECTION) -> Dict[str, dict]

Get all values from the store.

Source code in llama-index-core/llama_index/core/storage/kvstore/simple_kvstore.py
58
59
60
def get_all(self, collection: str = DEFAULT_COLLECTION) -> Dict[str, dict]:
    """Get all values from the store."""
    return self._data.get(collection, {}).copy()

aget_all async #

aget_all(collection: str = DEFAULT_COLLECTION) -> Dict[str, dict]

Get all values from the store.

Source code in llama-index-core/llama_index/core/storage/kvstore/simple_kvstore.py
62
63
64
async def aget_all(self, collection: str = DEFAULT_COLLECTION) -> Dict[str, dict]:
    """Get all values from the store."""
    return self.get_all(collection)

delete #

delete(key: str, collection: str = DEFAULT_COLLECTION) -> bool

Delete a value from the store.

Source code in llama-index-core/llama_index/core/storage/kvstore/simple_kvstore.py
66
67
68
69
70
71
72
def delete(self, key: str, collection: str = DEFAULT_COLLECTION) -> bool:
    """Delete a value from the store."""
    try:
        self._data[collection].pop(key)
        return True
    except KeyError:
        return False

adelete async #

adelete(key: str, collection: str = DEFAULT_COLLECTION) -> bool

Delete a value from the store.

Source code in llama-index-core/llama_index/core/storage/kvstore/simple_kvstore.py
74
75
76
async def adelete(self, key: str, collection: str = DEFAULT_COLLECTION) -> bool:
    """Delete a value from the store."""
    return self.delete(key, collection)

persist #

persist(persist_path: str, fs: Optional[AbstractFileSystem] = None) -> None

Persist the store.

Source code in llama-index-core/llama_index/core/storage/kvstore/simple_kvstore.py
78
79
80
81
82
83
84
85
86
87
88
def persist(
    self, persist_path: str, fs: Optional[fsspec.AbstractFileSystem] = None
) -> None:
    """Persist the store."""
    fs = fs or fsspec.filesystem("file")
    dirpath = os.path.dirname(persist_path)
    if not fs.exists(dirpath):
        fs.makedirs(dirpath)

    with fs.open(persist_path, "w") as f:
        f.write(json.dumps(self._data))

from_persist_path classmethod #

from_persist_path(persist_path: str, fs: Optional[AbstractFileSystem] = None) -> SimpleKVStore

Load a SimpleKVStore from a persist path and filesystem.

Source code in llama-index-core/llama_index/core/storage/kvstore/simple_kvstore.py
90
91
92
93
94
95
96
97
98
99
@classmethod
def from_persist_path(
    cls, persist_path: str, fs: Optional[fsspec.AbstractFileSystem] = None
) -> "SimpleKVStore":
    """Load a SimpleKVStore from a persist path and filesystem."""
    fs = fs or fsspec.filesystem("file")
    logger.debug(f"Loading {__name__} from {persist_path}.")
    with fs.open(persist_path, "rb") as f:
        data = json.load(f)
    return cls(data)

to_dict #

to_dict() -> dict

Save the store as dict.

Source code in llama-index-core/llama_index/core/storage/kvstore/simple_kvstore.py
101
102
103
def to_dict(self) -> dict:
    """Save the store as dict."""
    return self._data

from_dict classmethod #

from_dict(save_dict: dict) -> SimpleKVStore

Load a SimpleKVStore from dict.

Source code in llama-index-core/llama_index/core/storage/kvstore/simple_kvstore.py
105
106
107
108
@classmethod
def from_dict(cls, save_dict: dict) -> "SimpleKVStore":
    """Load a SimpleKVStore from dict."""
    return cls(save_dict)