-
Notifications
You must be signed in to change notification settings - Fork 714
Expand file tree
/
Copy path_storage_instance_manager.py
More file actions
209 lines (166 loc) · 8.66 KB
/
_storage_instance_manager.py
File metadata and controls
209 lines (166 loc) · 8.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
from __future__ import annotations
from collections import defaultdict
from collections.abc import Coroutine, Hashable
from dataclasses import dataclass, field
from typing import TYPE_CHECKING, TypeVar
from crawlee.storage_clients._base import DatasetClient, KeyValueStoreClient, RequestQueueClient
if TYPE_CHECKING:
from crawlee.storage_clients import StorageClient
from ._base import Storage
T = TypeVar('T', bound='Storage')
@dataclass
class _StorageClientCache:
"""Cache for specific storage client.
Example:
Storage=Dataset, id='123', additional_cache_key="some_path" will be located in
storage = by_id[Dataset]['123'][some_path]
"""
by_id: defaultdict[type[Storage], defaultdict[str, defaultdict[Hashable, Storage]]] = field(
default_factory=lambda: defaultdict(lambda: defaultdict(lambda: defaultdict()))
)
"""Cache for storage instances by ID, separated by storage type and additional hash key."""
by_name: defaultdict[type[Storage], defaultdict[str, defaultdict[Hashable, Storage]]] = field(
default_factory=lambda: defaultdict(lambda: defaultdict(lambda: defaultdict()))
)
"""Cache for storage instances by name, separated by storage type and additional hash key."""
by_alias: defaultdict[type[Storage], defaultdict[str, defaultdict[Hashable, Storage]]] = field(
default_factory=lambda: defaultdict(lambda: defaultdict(lambda: defaultdict()))
)
"""Cache for storage instances by alias, separated by storage type and additional hash key."""
StorageClientType = DatasetClient | KeyValueStoreClient | RequestQueueClient
"""Type alias for the storage client types."""
ClientOpenerCoro = Coroutine[None, None, StorageClientType]
"""Type alias for the client opener function."""
class StorageInstanceManager:
"""Manager for caching and managing storage instances.
This class centralizes the caching logic for all storage types (Dataset, KeyValueStore, RequestQueue)
and provides a unified interface for opening and managing storage instances.
"""
_DEFAULT_STORAGE_ALIAS = '__default__'
"""Reserved alias for default unnamed storage."""
def __init__(self) -> None:
self._cache_by_storage_client: dict[type[StorageClient], _StorageClientCache] = defaultdict(_StorageClientCache)
async def open_storage_instance(
self,
cls: type[T],
*,
id: str | None,
name: str | None,
alias: str | None,
storage_client_type: type[StorageClient],
client_opener_coro: ClientOpenerCoro,
additional_cache_key: Hashable = '',
) -> T:
"""Open a storage instance with caching support.
Args:
cls: The storage class to instantiate.
id: Storage ID.
name: Storage name. (global scope, persists across runs).
alias: Storage alias (run scope, creates unnamed storage).
storage_client_type: Type of storage client to use.
client_opener_coro: Coroutine to open the storage client when storage instance not found in cache.
additional_cache_key: Additional optional key to differentiate cache entries.
Returns:
The storage instance.
Raises:
ValueError: If multiple parameters out of `id`, `name`, and `alias` are specified.
"""
try:
if name == self._DEFAULT_STORAGE_ALIAS:
raise ValueError(
f'Storage name cannot be "{self._DEFAULT_STORAGE_ALIAS}" as it is reserved for default alias.'
)
# Validate input parameters.
specified_params = sum(1 for param in [id, name, alias] if param is not None)
if specified_params > 1:
raise ValueError('Only one of "id", "name", or "alias" can be specified, not multiple.')
# Auto-set alias='default' when no parameters are specified.
# Default unnamed storage is equal to alias=default unnamed storage.
if specified_params == 0:
alias = self._DEFAULT_STORAGE_ALIAS
# Check cache
if id is not None and (
cached_instance := self._cache_by_storage_client[storage_client_type]
.by_id[cls][id]
.get(additional_cache_key)
):
if isinstance(cached_instance, cls):
return cached_instance
raise RuntimeError('Cached instance type mismatch.')
if name is not None and (
cached_instance := self._cache_by_storage_client[storage_client_type]
.by_name[cls][name]
.get(additional_cache_key)
):
if isinstance(cached_instance, cls):
return cached_instance
raise RuntimeError('Cached instance type mismatch.')
if alias is not None and (
cached_instance := self._cache_by_storage_client[storage_client_type]
.by_alias[cls][alias]
.get(additional_cache_key)
):
if isinstance(cached_instance, cls):
return cached_instance
raise RuntimeError('Cached instance type mismatch.')
# Check for conflicts between named and alias storages
if alias and (
self._cache_by_storage_client[storage_client_type].by_name[cls][alias].get(additional_cache_key)
):
raise ValueError(
f'Cannot create alias storage "{alias}" because a named storage with the same name already exists. '
f'Use a different alias or drop the existing named storage first.'
)
if name and (
self._cache_by_storage_client[storage_client_type].by_alias[cls][name].get(additional_cache_key)
):
raise ValueError(
f'Cannot create named storage "{name}" because an alias storage with the same name already exists. '
f'Use a different name or drop the existing alias storage first.'
)
# Create new instance
client: KeyValueStoreClient | DatasetClient | RequestQueueClient
client = await client_opener_coro
metadata = await client.get_metadata()
instance = cls(client, metadata.id, metadata.name) # type: ignore[call-arg]
instance_name = getattr(instance, 'name', None)
# Cache the instance.
# Always cache by id.
self._cache_by_storage_client[storage_client_type].by_id[cls][instance.id][additional_cache_key] = instance
# Cache named storage.
if instance_name is not None:
self._cache_by_storage_client[storage_client_type].by_name[cls][instance_name][additional_cache_key] = (
instance
)
# Cache unnamed storage.
if alias is not None:
self._cache_by_storage_client[storage_client_type].by_alias[cls][alias][additional_cache_key] = instance
return instance
finally:
# Make sure the client opener is closed.
# If it was awaited, then closing is no operation, if it was not awaited, this is the cleanup.
client_opener_coro.close()
def remove_from_cache(self, storage_instance: Storage) -> None:
"""Remove a storage instance from the cache.
Args:
storage_instance: The storage instance to remove.
"""
storage_type = type(storage_instance)
for storage_client_cache in self._cache_by_storage_client.values():
# Remove from ID cache
for additional_key in storage_client_cache.by_id[storage_type][storage_instance.id]:
del storage_client_cache.by_id[storage_type][storage_instance.id][additional_key]
break
# Remove from name cache or alias cache. It can never be in both.
if storage_instance.name is not None:
for additional_key in storage_client_cache.by_name[storage_type][storage_instance.name]:
del storage_client_cache.by_name[storage_type][storage_instance.name][additional_key]
break
else:
for alias_key in storage_client_cache.by_alias[storage_type]:
for additional_key in storage_client_cache.by_alias[storage_type][alias_key]:
del storage_client_cache.by_alias[storage_type][alias_key][additional_key]
break
def clear_cache(self) -> None:
"""Clear all cached storage instances."""
self._cache_by_storage_client = defaultdict(_StorageClientCache)