Skip to content

Commit 1bb30e5

Browse files
refactor: move MemoryStore seeding to setup_collection for lazy initialization
- Store seed data as instance variable instead of seeding synchronously in __init__ - Move seeding logic into _setup_collection which is called once per collection - Remove _seed_store_sync method entirely - Update docstring to clarify seeding occurs lazily when collection is first accessed This provides cleaner separation of concerns and leverages the existing setup infrastructure, ensuring collections are only seeded when actually used. Co-authored-by: William Easton <strawgate@users.noreply.github.com>
1 parent 3a3da4e commit 1bb30e5

File tree

1 file changed

+12
-16
lines changed
  • key-value/key-value-aio/src/key_value/aio/stores/memory

1 file changed

+12
-16
lines changed

key-value/key-value-aio/src/key_value/aio/stores/memory/store.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ class MemoryStore(BaseDestroyStore, BaseDestroyCollectionStore, BaseEnumerateCol
109109
max_entries_per_collection: int
110110

111111
_cache: dict[str, MemoryCollection]
112+
_seed: Mapping[str, Mapping[str, Mapping[str, Any]]]
112113

113114
def __init__(
114115
self,
@@ -124,20 +125,18 @@ def __init__(
124125
default_collection: The default collection to use if no collection is provided.
125126
seed: Optional seed data to pre-populate the store. Format: {collection: {key: {field: value, ...}}}.
126127
Each value must be a mapping (dict) that will be stored as the entry's value.
128+
Seeding occurs lazily when each collection is first accessed.
127129
"""
128130

129131
self.max_entries_per_collection = max_entries_per_collection
130132

131133
self._cache = {}
134+
self._seed = seed or {}
132135

133136
self._stable_api = True
134137

135138
super().__init__(default_collection=default_collection)
136139

137-
# Seed the store if seed data is provided
138-
if seed:
139-
self._seed_store_sync(seed)
140-
141140
def _create_collection(self, collection: str) -> MemoryCollection:
142141
"""Create a new collection.
143142
@@ -151,26 +150,23 @@ def _create_collection(self, collection: str) -> MemoryCollection:
151150
self._cache[collection] = collection_cache
152151
return collection_cache
153152

154-
def _seed_store_sync(self, seed: Mapping[str, Mapping[str, Mapping[str, Any]]]) -> None:
155-
"""Seed the store with initial data synchronously.
153+
@override
154+
async def _setup_collection(self, *, collection: str) -> None:
155+
"""Set up a collection, creating it and seeding it if seed data is available.
156156
157157
Args:
158-
seed: Seed data in format {collection: {key: {field: value, ...}}}.
159-
Each value must be a mapping (dict) that will be stored as the entry's value.
158+
collection: The collection name.
160159
"""
161-
for collection, items in seed.items():
162-
# Ensure collection exists
163-
collection_cache = self._create_collection(collection) if collection not in self._cache else self._cache[collection]
160+
# Create the collection
161+
collection_cache = self._create_collection(collection)
164162

165-
# Add items using the same logic as put
163+
# Seed the collection if seed data is available for it
164+
if collection in self._seed:
165+
items = self._seed[collection]
166166
for key, value in items.items():
167167
managed_entry = ManagedEntry(value=value)
168168
collection_cache.put(key=key, value=managed_entry)
169169

170-
@override
171-
async def _setup_collection(self, *, collection: str) -> None:
172-
self._create_collection(collection)
173-
174170
@override
175171
async def _get_managed_entry(self, *, key: str, collection: str) -> ManagedEntry | None:
176172
collection_cache: MemoryCollection = self._cache[collection]

0 commit comments

Comments
 (0)