|
6 | 6 | from crawlee._utils.docs import docs_group
|
7 | 7 |
|
8 | 8 | if TYPE_CHECKING:
|
9 |
| - from contextlib import AbstractAsyncContextManager |
| 9 | + from datetime import datetime |
| 10 | + from pathlib import Path |
10 | 11 |
|
11 |
| - from httpx import Response |
| 12 | + from crawlee.storage_clients.models import KeyValueStoreListKeysPage, KeyValueStoreRecord |
12 | 13 |
|
13 |
| - from crawlee.storage_clients.models import KeyValueStoreListKeysPage, KeyValueStoreMetadata, KeyValueStoreRecord |
| 14 | +# Properties: |
| 15 | +# - id |
| 16 | +# - name |
| 17 | +# - created_at |
| 18 | +# - accessed_at |
| 19 | +# - modified_at |
| 20 | + |
| 21 | +# Methods: |
| 22 | +# - open |
| 23 | +# - drop |
| 24 | +# - get_value |
| 25 | +# - set_value |
| 26 | +# - delete_value |
| 27 | +# - iterate_keys |
| 28 | +# - get_public_url |
14 | 29 |
|
15 | 30 |
|
16 | 31 | @docs_group('Abstract classes')
|
17 | 32 | class KeyValueStoreClient(ABC):
|
18 |
| - """An abstract class for key-value store resource clients. |
| 33 | + """An abstract class for key-value store (KVS) resource clients. |
19 | 34 |
|
20 | 35 | These clients are specific to the type of resource they manage and operate under a designated storage
|
21 | 36 | client, like a memory storage client.
|
22 | 37 | """
|
23 | 38 |
|
| 39 | + @property |
24 | 40 | @abstractmethod
|
25 |
| - async def get(self) -> KeyValueStoreMetadata | None: |
26 |
| - """Get metadata about the key-value store being managed by this client. |
27 |
| -
|
28 |
| - Returns: |
29 |
| - An object containing the key-value store's details, or None if the key-value store does not exist. |
30 |
| - """ |
| 41 | + def id(self) -> str: |
| 42 | + """The ID of the key-value store.""" |
31 | 43 |
|
| 44 | + @property |
32 | 45 | @abstractmethod
|
33 |
| - async def delete(self) -> None: |
34 |
| - """Permanently delete the key-value store managed by this client.""" |
| 46 | + def name(self) -> str | None: |
| 47 | + """The name of the key-value store.""" |
35 | 48 |
|
| 49 | + @property |
36 | 50 | @abstractmethod
|
37 |
| - async def list_keys( |
38 |
| - self, |
39 |
| - *, |
40 |
| - limit: int = 1000, |
41 |
| - exclusive_start_key: str | None = None, |
42 |
| - ) -> KeyValueStoreListKeysPage: |
43 |
| - """List the keys in the key-value store. |
| 51 | + def created_at(self) -> datetime: |
| 52 | + """The time at which the key-value store was created.""" |
44 | 53 |
|
45 |
| - Args: |
46 |
| - limit: Number of keys to be returned. Maximum value is 1000. |
47 |
| - exclusive_start_key: All keys up to this one (including) are skipped from the result. |
| 54 | + @property |
| 55 | + @abstractmethod |
| 56 | + def accessed_at(self) -> datetime: |
| 57 | + """The time at which the key-value store was last accessed.""" |
48 | 58 |
|
49 |
| - Returns: |
50 |
| - The list of keys in the key-value store matching the given arguments. |
51 |
| - """ |
| 59 | + @property |
| 60 | + @abstractmethod |
| 61 | + def modified_at(self) -> datetime: |
| 62 | + """The time at which the key-value store was last modified.""" |
52 | 63 |
|
| 64 | + @classmethod |
53 | 65 | @abstractmethod
|
54 |
| - async def get_record(self, key: str) -> KeyValueStoreRecord | None: |
55 |
| - """Retrieve the given record from the key-value store. |
| 66 | + async def open( |
| 67 | + cls, |
| 68 | + *, |
| 69 | + id: str | None, |
| 70 | + name: str | None, |
| 71 | + storage_dir: Path, |
| 72 | + ) -> KeyValueStoreClient: |
| 73 | + """Open existing or create a new key-value store client. |
| 74 | +
|
| 75 | + If a key-value store with the given name already exists, the appropriate key-value store client is returned. |
| 76 | + Otherwise, a new key-value store is created and client for it is returned. |
56 | 77 |
|
57 | 78 | Args:
|
58 |
| - key: Key of the record to retrieve. |
| 79 | + id: The ID of the key-value store. |
| 80 | + name: The name of the key-value store. |
| 81 | + storage_dir: The path to the storage directory. If the client persists data, it should use this directory. |
59 | 82 |
|
60 | 83 | Returns:
|
61 |
| - The requested record, or None, if the record does not exist |
| 84 | + A key-value store client. |
62 | 85 | """
|
63 | 86 |
|
64 | 87 | @abstractmethod
|
65 |
| - async def get_record_as_bytes(self, key: str) -> KeyValueStoreRecord[bytes] | None: |
66 |
| - """Retrieve the given record from the key-value store, without parsing it. |
| 88 | + async def drop(self) -> None: |
| 89 | + """Drop the whole key-value store and remove all its items. |
67 | 90 |
|
68 |
| - Args: |
69 |
| - key: Key of the record to retrieve. |
70 |
| -
|
71 |
| - Returns: |
72 |
| - The requested record, or None, if the record does not exist |
| 91 | + The backend method for the `KeyValueStore.drop` call. |
73 | 92 | """
|
74 | 93 |
|
75 | 94 | @abstractmethod
|
76 |
| - async def stream_record(self, key: str) -> AbstractAsyncContextManager[KeyValueStoreRecord[Response] | None]: |
77 |
| - """Retrieve the given record from the key-value store, as a stream. |
| 95 | + async def get_value(self, *, key: str) -> KeyValueStoreRecord | None: |
| 96 | + """Retrieve the given record from the key-value store. |
78 | 97 |
|
79 | 98 | Args:
|
80 | 99 | key: Key of the record to retrieve.
|
81 | 100 |
|
82 | 101 | Returns:
|
83 |
| - The requested record as a context-managed streaming Response, or None, if the record does not exist |
| 102 | + The requested record, or None, if the record does not exist |
84 | 103 | """
|
85 | 104 |
|
86 | 105 | @abstractmethod
|
87 |
| - async def set_record(self, key: str, value: Any, content_type: str | None = None) -> None: |
| 106 | + async def set_value(self, *, key: str, value: Any, content_type: str | None = None) -> None: |
88 | 107 | """Set a value to the given record in the key-value store.
|
89 | 108 |
|
90 | 109 | Args:
|
91 | 110 | key: The key of the record to save the value to.
|
92 | 111 | value: The value to save into the record.
|
93 |
| - content_type: The content type of the saved value. |
| 112 | + content_type: The MIME content type string. If not provided, it is inferred from the value. |
94 | 113 | """
|
95 | 114 |
|
96 | 115 | @abstractmethod
|
97 |
| - async def delete_record(self, key: str) -> None: |
| 116 | + async def delete_value(self, *, key: str) -> None: |
98 | 117 | """Delete the specified record from the key-value store.
|
99 | 118 |
|
100 | 119 | Args:
|
101 | 120 | key: The key of the record which to delete.
|
102 | 121 | """
|
103 | 122 |
|
104 | 123 | @abstractmethod
|
105 |
| - async def get_public_url(self, key: str) -> str: |
| 124 | + async def iterate_keys( |
| 125 | + self, |
| 126 | + *, |
| 127 | + exclusive_start_key: str | None = None, |
| 128 | + limit: int = 1000, |
| 129 | + ) -> KeyValueStoreListKeysPage: |
| 130 | + """List the keys in the key-value store. |
| 131 | +
|
| 132 | + Args: |
| 133 | + exclusive_start_key: All keys up to this one (including) are skipped from the result. |
| 134 | + limit: Number of keys to be returned. |
| 135 | +
|
| 136 | + Returns: |
| 137 | + The list of keys in the key-value store matching the given arguments. |
| 138 | + """ |
| 139 | + |
| 140 | + @abstractmethod |
| 141 | + async def get_public_url(self, *, key: str) -> str: |
106 | 142 | """Get the public URL for the given key.
|
107 | 143 |
|
108 | 144 | Args:
|
|
0 commit comments