-
-
Notifications
You must be signed in to change notification settings - Fork 398
Expand file tree
/
Copy pathstore.py
More file actions
713 lines (582 loc) · 22.2 KB
/
store.py
File metadata and controls
713 lines (582 loc) · 22.2 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
from __future__ import annotations
import asyncio
import json
from abc import ABC, abstractmethod
from dataclasses import dataclass
from itertools import starmap
from typing import TYPE_CHECKING, Literal, Protocol, runtime_checkable
from zarr.core.sync import sync
if TYPE_CHECKING:
from collections.abc import AsyncGenerator, AsyncIterator, Iterable
from types import TracebackType
from typing import Any, Self, TypeAlias
from zarr.core.buffer import Buffer, BufferPrototype
__all__ = ["ByteGetter", "ByteSetter", "Store", "set_or_delete"]
@dataclass
class RangeByteRequest:
"""Request a specific byte range"""
start: int
"""The start of the byte range request (inclusive)."""
end: int
"""The end of the byte range request (exclusive)."""
@dataclass
class OffsetByteRequest:
"""Request all bytes starting from a given byte offset"""
offset: int
"""The byte offset for the offset range request."""
@dataclass
class SuffixByteRequest:
"""Request up to the last `n` bytes"""
suffix: int
"""The number of bytes from the suffix to request."""
ByteRequest: TypeAlias = RangeByteRequest | OffsetByteRequest | SuffixByteRequest
class Store(ABC):
"""
Abstract base class for Zarr stores.
"""
_read_only: bool
_is_open: bool
def __init__(self, *, read_only: bool = False) -> None:
self._is_open = False
self._read_only = read_only
@classmethod
async def open(cls, *args: Any, **kwargs: Any) -> Self:
"""
Create and open the store.
Parameters
----------
*args : Any
Positional arguments to pass to the store constructor.
**kwargs : Any
Keyword arguments to pass to the store constructor.
Returns
-------
Store
The opened store instance.
"""
store = cls(*args, **kwargs)
await store._open()
return store
def with_read_only(self, read_only: bool = False) -> Store:
"""
Return a new store with a new read_only setting.
The new store points to the same location with the specified new read_only state.
The returned Store is not automatically opened, and this store is
not automatically closed.
Parameters
----------
read_only
If True, the store will be created in read-only mode. Defaults to False.
Returns
-------
A new store of the same type with the new read only attribute.
"""
raise NotImplementedError(
f"with_read_only is not implemented for the {type(self)} store type."
)
def __enter__(self) -> Self:
"""Enter a context manager that will close the store upon exiting."""
return self
def __exit__(
self,
exc_type: type[BaseException] | None,
exc_value: BaseException | None,
traceback: TracebackType | None,
) -> None:
"""Close the store."""
self.close()
async def _open(self) -> None:
"""
Open the store.
Raises
------
ValueError
If the store is already open.
"""
if self._is_open:
raise ValueError("store is already open")
self._is_open = True
async def _ensure_open(self) -> None:
"""Open the store if it is not already open."""
if not self._is_open:
await self._open()
async def is_empty(self, prefix: str) -> bool:
"""
Check if the directory is empty.
Parameters
----------
prefix : str
Prefix of keys to check.
Returns
-------
bool
True if the store is empty, False otherwise.
"""
if not self.supports_listing:
raise NotImplementedError
if prefix != "" and not prefix.endswith("/"):
prefix += "/"
async for _ in self.list_prefix(prefix):
return False
return True
async def clear(self) -> None:
"""
Clear the store.
Remove all keys and values from the store.
"""
if not self.supports_deletes:
raise NotImplementedError
if not self.supports_listing:
raise NotImplementedError
self._check_writable()
await self.delete_dir("")
@property
def read_only(self) -> bool:
"""Is the store read-only?"""
return self._read_only
def _check_writable(self) -> None:
"""Raise an exception if the store is not writable."""
if self.read_only:
raise ValueError("store was opened in read-only mode and does not support writing")
@abstractmethod
def __eq__(self, value: object) -> bool:
"""Equality comparison."""
...
@abstractmethod
async def get(
self,
key: str,
prototype: BufferPrototype,
byte_range: ByteRequest | None = None,
) -> Buffer | None:
"""Retrieve the value associated with a given key.
Parameters
----------
key : str
prototype : BufferPrototype
The prototype of the output buffer. Stores may support a default buffer prototype.
byte_range : ByteRequest, optional
ByteRequest may be one of the following. If not provided, all data associated with the key is retrieved.
- RangeByteRequest(int, int): Request a specific range of bytes in the form (start, end). The end is exclusive. If the given range is zero-length or starts after the end of the object, an error will be returned. Additionally, if the range ends after the end of the object, the entire remainder of the object will be returned. Otherwise, the exact requested range will be returned.
- OffsetByteRequest(int): Request all bytes starting from a given byte offset. This is equivalent to bytes={int}- as an HTTP header.
- SuffixByteRequest(int): Request the last int bytes. Note that here, int is the size of the request, not the byte offset. This is equivalent to bytes=-{int} as an HTTP header.
Returns
-------
Buffer
"""
...
async def _get_bytes(
self, key: str, *, prototype: BufferPrototype, byte_range: ByteRequest | None = None
) -> bytes:
"""
Retrieve raw bytes from the store asynchronously.
This is a convenience method that wraps ``get()`` and converts the result
to bytes. Use this when you need the raw byte content of a stored value.
Parameters
----------
key : str
The key identifying the data to retrieve.
prototype : BufferPrototype
The buffer prototype to use for reading the data.
byte_range : ByteRequest, optional
If specified, only retrieve a portion of the stored data.
Can be a ``RangeByteRequest``, ``OffsetByteRequest``, or ``SuffixByteRequest``.
Returns
-------
bytes
The raw bytes stored at the given key.
Raises
------
FileNotFoundError
If the key does not exist in the store.
See Also
--------
get : Lower-level method that returns a Buffer object.
get_bytes : Synchronous version of this method.
get_json : Asynchronous method for retrieving and parsing JSON data.
Examples
--------
>>> store = await MemoryStore.open()
>>> await store.set("data", Buffer.from_bytes(b"hello world"))
>>> data = await store.get_bytes("data", prototype=default_buffer_prototype())
>>> print(data)
b'hello world'
"""
buffer = await self.get(key, prototype, byte_range)
if buffer is None:
raise FileNotFoundError(key)
return buffer.to_bytes()
def _get_bytes_sync(
self, key: str = "", *, prototype: BufferPrototype, byte_range: ByteRequest | None = None
) -> bytes:
"""
Retrieve raw bytes from the store synchronously.
This is a synchronous wrapper around ``get_bytes()``. It should only
be called from non-async code. For async contexts, use ``get_bytes()``
instead.
Parameters
----------
key : str, optional
The key identifying the data to retrieve. Defaults to an empty string.
prototype : BufferPrototype
The buffer prototype to use for reading the data.
byte_range : ByteRequest, optional
If specified, only retrieve a portion of the stored data.
Can be a ``RangeByteRequest``, ``OffsetByteRequest``, or ``SuffixByteRequest``.
Returns
-------
bytes
The raw bytes stored at the given key.
Raises
------
FileNotFoundError
If the key does not exist in the store.
Warnings
--------
Do not call this method from async functions. Use ``get_bytes()`` instead
to avoid blocking the event loop.
See Also
--------
get_bytes : Asynchronous version of this method.
get_json_sync : Synchronous method for retrieving and parsing JSON data.
Examples
--------
>>> store = MemoryStore()
>>> await store.set("data", Buffer.from_bytes(b"hello world"))
>>> data = store.get_bytes_sync("data", prototype=default_buffer_prototype())
>>> print(data)
b'hello world'
"""
return sync(self._get_bytes(key, prototype=prototype, byte_range=byte_range))
async def _get_json(
self, key: str, *, prototype: BufferPrototype, byte_range: ByteRequest | None = None
) -> Any:
"""
Retrieve and parse JSON data from the store asynchronously.
This is a convenience method that retrieves bytes from the store and
parses them as JSON.
Parameters
----------
key : str
The key identifying the JSON data to retrieve.
prototype : BufferPrototype
The buffer prototype to use for reading the data.
byte_range : ByteRequest, optional
If specified, only retrieve a portion of the stored data.
Can be a ``RangeByteRequest``, ``OffsetByteRequest``, or ``SuffixByteRequest``.
Note: Using byte ranges with JSON may result in invalid JSON.
Returns
-------
Any
The parsed JSON data. This follows the behavior of ``json.loads()`` and
can be any JSON-serializable type: dict, list, str, int, float, bool, or None.
Raises
------
FileNotFoundError
If the key does not exist in the store.
json.JSONDecodeError
If the stored data is not valid JSON.
See Also
--------
get_bytes : Method for retrieving raw bytes.
get_json_sync : Synchronous version of this method.
Examples
--------
>>> store = await MemoryStore.open()
>>> metadata = {"zarr_format": 3, "node_type": "array"}
>>> await store.set("zarr.json", Buffer.from_bytes(json.dumps(metadata).encode()))
>>> data = await store.get_json("zarr.json", prototype=default_buffer_prototype())
>>> print(data)
{'zarr_format': 3, 'node_type': 'array'}
"""
return json.loads(await self._get_bytes(key, prototype=prototype, byte_range=byte_range))
def _get_json_sync(
self, key: str = "", *, prototype: BufferPrototype, byte_range: ByteRequest | None = None
) -> Any:
"""
Retrieve and parse JSON data from the store synchronously.
This is a synchronous wrapper around ``get_json()``. It should only
be called from non-async code. For async contexts, use ``get_json()``
instead.
Parameters
----------
key : str, optional
The key identifying the JSON data to retrieve. Defaults to an empty string.
prototype : BufferPrototype
The buffer prototype to use for reading the data.
byte_range : ByteRequest, optional
If specified, only retrieve a portion of the stored data.
Can be a ``RangeByteRequest``, ``OffsetByteRequest``, or ``SuffixByteRequest``.
Note: Using byte ranges with JSON may result in invalid JSON.
Returns
-------
Any
The parsed JSON data. This follows the behavior of ``json.loads()`` and
can be any JSON-serializable type: dict, list, str, int, float, bool, or None.
Raises
------
FileNotFoundError
If the key does not exist in the store.
json.JSONDecodeError
If the stored data is not valid JSON.
Warnings
--------
Do not call this method from async functions. Use ``get_json()`` instead
to avoid blocking the event loop.
See Also
--------
get_json : Asynchronous version of this method.
get_bytes_sync : Synchronous method for retrieving raw bytes without parsing.
Examples
--------
>>> store = MemoryStore()
>>> metadata = {"zarr_format": 3, "node_type": "array"}
>>> store.set("zarr.json", Buffer.from_bytes(json.dumps(metadata).encode()))
>>> data = store.get_json_sync("zarr.json", prototype=default_buffer_prototype())
>>> print(data)
{'zarr_format': 3, 'node_type': 'array'}
"""
return sync(self._get_json(key, prototype=prototype, byte_range=byte_range))
@abstractmethod
async def get_partial_values(
self,
prototype: BufferPrototype,
key_ranges: Iterable[tuple[str, ByteRequest | None]],
) -> list[Buffer | None]:
"""Retrieve possibly partial values from given key_ranges.
Parameters
----------
prototype : BufferPrototype
The prototype of the output buffer. Stores may support a default buffer prototype.
key_ranges : Iterable[tuple[str, tuple[int | None, int | None]]]
Ordered set of key, range pairs, a key may occur multiple times with different ranges
Returns
-------
list of values, in the order of the key_ranges, may contain null/none for missing keys
"""
...
@abstractmethod
async def exists(self, key: str) -> bool:
"""Check if a key exists in the store.
Parameters
----------
key : str
Returns
-------
bool
"""
...
@property
@abstractmethod
def supports_writes(self) -> bool:
"""Does the store support writes?"""
...
@abstractmethod
async def set(self, key: str, value: Buffer) -> None:
"""Store a (key, value) pair.
Parameters
----------
key : str
value : Buffer
"""
...
async def set_if_not_exists(self, key: str, value: Buffer) -> None:
"""
Store a key to ``value`` if the key is not already present.
Parameters
----------
key : str
value : Buffer
"""
# Note for implementers: the default implementation provided here
# is not safe for concurrent writers. There's a race condition between
# the `exists` check and the `set` where another writer could set some
# value at `key` or delete `key`.
if not await self.exists(key):
await self.set(key, value)
async def _set_many(self, values: Iterable[tuple[str, Buffer]]) -> None:
"""
Insert multiple (key, value) pairs into storage.
"""
await asyncio.gather(*starmap(self.set, values))
@property
def supports_consolidated_metadata(self) -> bool:
"""
Does the store support consolidated metadata?.
If it doesn't an error will be raised on requests to consolidate the metadata.
Returning `False` can be useful for stores which implement their own
consolidation mechanism outside of the zarr-python implementation.
"""
return True
@property
@abstractmethod
def supports_deletes(self) -> bool:
"""Does the store support deletes?"""
...
@abstractmethod
async def delete(self, key: str) -> None:
"""Remove a key from the store
Parameters
----------
key : str
"""
...
@property
def supports_partial_writes(self) -> Literal[False]:
"""Does the store support partial writes?
Partial writes are no longer used by Zarr, so this is always false.
"""
return False
@property
@abstractmethod
def supports_listing(self) -> bool:
"""Does the store support listing?"""
...
@abstractmethod
def list(self) -> AsyncIterator[str]:
"""Retrieve all keys in the store.
Returns
-------
AsyncIterator[str]
"""
# This method should be async, like overridden methods in child classes.
# However, that's not straightforward:
# https://stackoverflow.com/questions/68905848
@abstractmethod
def list_prefix(self, prefix: str) -> AsyncIterator[str]:
"""
Retrieve all keys in the store that begin with a given prefix. Keys are returned relative
to the root of the store.
Parameters
----------
prefix : str
Returns
-------
AsyncIterator[str]
"""
# This method should be async, like overridden methods in child classes.
# However, that's not straightforward:
# https://stackoverflow.com/questions/68905848
@abstractmethod
def list_dir(self, prefix: str) -> AsyncIterator[str]:
"""
Retrieve all keys and prefixes with a given prefix and which do not contain the character
“/” after the given prefix.
Parameters
----------
prefix : str
Returns
-------
AsyncIterator[str]
"""
# This method should be async, like overridden methods in child classes.
# However, that's not straightforward:
# https://stackoverflow.com/questions/68905848
async def delete_dir(self, prefix: str) -> None:
"""
Remove all keys and prefixes in the store that begin with a given prefix.
"""
if not self.supports_deletes:
raise NotImplementedError
if not self.supports_listing:
raise NotImplementedError
self._check_writable()
if prefix != "" and not prefix.endswith("/"):
prefix += "/"
async for key in self.list_prefix(prefix):
await self.delete(key)
def close(self) -> None:
"""Close the store."""
self._is_open = False
async def _get_many(
self, requests: Iterable[tuple[str, BufferPrototype, ByteRequest | None]]
) -> AsyncGenerator[tuple[str, Buffer | None], None]:
"""
Retrieve a collection of objects from storage. In general this method does not guarantee
that objects will be retrieved in the order in which they were requested, so this method
yields tuple[str, Buffer | None] instead of just Buffer | None
"""
for req in requests:
yield (req[0], await self.get(*req))
async def getsize(self, key: str) -> int:
"""
Return the size, in bytes, of a value in a Store.
Parameters
----------
key : str
Returns
-------
nbytes : int
The size of the value (in bytes).
Raises
------
FileNotFoundError
When the given key does not exist in the store.
"""
# Note to implementers: this default implementation is very inefficient since
# it requires reading the entire object. Many systems will have ways to get the
# size of an object without reading it.
# avoid circular import
from zarr.core.buffer.core import default_buffer_prototype
value = await self.get(key, prototype=default_buffer_prototype())
if value is None:
raise FileNotFoundError(key)
return len(value)
async def getsize_prefix(self, prefix: str) -> int:
"""
Return the size, in bytes, of all values under a prefix.
Parameters
----------
prefix : str
The prefix of the directory to measure.
Returns
-------
nbytes : int
The sum of the sizes of the values in the directory (in bytes).
See Also
--------
zarr.Array.nbytes_stored
Store.getsize
Notes
-----
``getsize_prefix`` is just provided as a potentially faster alternative to
listing all the keys under a prefix calling [`Store.getsize`][zarr.abc.store.Store.getsize] on each.
In general, ``prefix`` should be the path of an Array or Group in the Store.
Implementations may differ on the behavior when some other ``prefix``
is provided.
"""
# TODO: Overlap listing keys with getsize calls.
# Currently, we load the list of keys into memory and only then move
# on to getting sizes. Ideally we would overlap those two, which should
# improve tail latency and might reduce memory pressure (since not all keys
# would be in memory at once).
keys = [x async for x in self.list_prefix(prefix)]
sizes = await asyncio.gather(*[self.getsize(key) for key in keys])
return sum(sizes)
@runtime_checkable
class ByteGetter(Protocol):
async def get(
self, prototype: BufferPrototype, byte_range: ByteRequest | None = None
) -> Buffer | None: ...
@runtime_checkable
class ByteSetter(Protocol):
async def get(
self, prototype: BufferPrototype, byte_range: ByteRequest | None = None
) -> Buffer | None: ...
async def set(self, value: Buffer) -> None: ...
async def delete(self) -> None: ...
async def set_if_not_exists(self, default: Buffer) -> None: ...
async def set_or_delete(byte_setter: ByteSetter, value: Buffer | None) -> None:
"""Set or delete a value in a byte setter
Parameters
----------
byte_setter : ByteSetter
value : Buffer | None
Notes
-----
If value is None, the key will be deleted.
"""
if value is None:
await byte_setter.delete()
else:
await byte_setter.set(value)