feat: Remove immediate persistence in delete operation

- Enhance delete implementation in JsonKVStorage by removing immediate persistence in delete operation
- Update documentation for drop method to clarify persistence behavior
- Add abstract delete method to BaseKVStorage
This commit is contained in:
yangdx
2025-03-31 14:14:32 +08:00
parent bbc770d1ed
commit 81f887ebab
2 changed files with 31 additions and 1 deletions

View File

@@ -123,6 +123,7 @@ class StorageNameSpace(ABC):
3. Reset the storage to its initial state 3. Reset the storage to its initial state
4. Handle cleanup of any resources 4. Handle cleanup of any resources
5. Notify other processes if necessary 5. Notify other processes if necessary
6. This action should persistent the data to disk immediately.
Returns: Returns:
dict[str, str]: Operation status and message with the following format: dict[str, str]: Operation status and message with the following format:
@@ -207,6 +208,22 @@ class BaseKVStorage(StorageNameSpace, ABC):
async def upsert(self, data: dict[str, dict[str, Any]]) -> None: async def upsert(self, data: dict[str, dict[str, Any]]) -> None:
"""Upsert data""" """Upsert data"""
@abstractmethod
async def delete(self, ids: list[str]) -> None:
"""Delete specific records from storage by their IDs
This method will:
1. Remove the specified records from in-memory storage
2. For in-memory DB, update flags to notify other processes that data persistence is needed
3. For in-memory DB, changes will be persisted to disk during the next index_done_callback
Args:
ids (list[str]): List of document IDs to be deleted from storage
Returns:
None
"""
@dataclass @dataclass
class BaseGraphStorage(StorageNameSpace, ABC): class BaseGraphStorage(StorageNameSpace, ABC):

View File

@@ -122,14 +122,27 @@ class JsonKVStorage(BaseKVStorage):
await set_all_update_flags(self.namespace) await set_all_update_flags(self.namespace)
async def delete(self, ids: list[str]) -> None: async def delete(self, ids: list[str]) -> None:
"""Delete specific records from storage by their IDs
This method will:
1. Remove the specified records from in-memory storage
2. Update flags to notify other processes that data persistence is needed
3. The changes will be persisted to disk during the next index_done_callback
Args:
ids (list[str]): List of document IDs to be deleted from storage
Returns:
None
"""
async with self._storage_lock: async with self._storage_lock:
for doc_id in ids: for doc_id in ids:
self._data.pop(doc_id, None) self._data.pop(doc_id, None)
await set_all_update_flags(self.namespace) await set_all_update_flags(self.namespace)
await self.index_done_callback()
async def drop(self) -> dict[str, str]: async def drop(self) -> dict[str, str]:
"""Drop all data from storage and clean up resources """Drop all data from storage and clean up resources
This action will persistent the data to disk immediately.
This method will: This method will:
1. Clear all data from memory 1. Clear all data from memory