bitcask-dataset 0.2.0__tar.gz → 0.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {bitcask_dataset-0.2.0 → bitcask_dataset-0.3.0}/PKG-INFO +6 -5
- {bitcask_dataset-0.2.0 → bitcask_dataset-0.3.0}/README.md +5 -4
- {bitcask_dataset-0.2.0 → bitcask_dataset-0.3.0}/pyproject.toml +1 -1
- {bitcask_dataset-0.2.0 → bitcask_dataset-0.3.0}/pyproject.toml.orig +1 -1
- {bitcask_dataset-0.2.0 → bitcask_dataset-0.3.0}/src/bitcask_dataset/dataset.py +42 -37
- {bitcask_dataset-0.2.0 → bitcask_dataset-0.3.0}/src/bitcask_dataset/__init__.py +0 -0
- {bitcask_dataset-0.2.0 → bitcask_dataset-0.3.0}/src/bitcask_dataset/py.typed +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: bitcask-dataset
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3.0
|
|
4
4
|
Summary: A small, mmap-backed dataset of byte blobs, optionally keyed
|
|
5
5
|
Keywords: dataset,mmap,machine-learning,pytorch
|
|
6
6
|
License-Expression: BSD-3-Clause
|
|
@@ -73,9 +73,9 @@ array, ...).
|
|
|
73
73
|
## API
|
|
74
74
|
|
|
75
75
|
```python
|
|
76
|
-
BitcaskDataset.create(path, keyed=False, compression="lz4")
|
|
76
|
+
BitcaskDataset.create(path, keyed=False, compression="lz4", max_shard_size=1 << 30)
|
|
77
77
|
BitcaskDataset(path)
|
|
78
|
-
ds.extend(entries
|
|
78
|
+
ds.extend(entries)
|
|
79
79
|
ds[key]
|
|
80
80
|
ds.at(index)
|
|
81
81
|
ds.key_at(index)
|
|
@@ -84,7 +84,8 @@ key in ds
|
|
|
84
84
|
```
|
|
85
85
|
|
|
86
86
|
`create` needs a missing or empty directory. `BitcaskDataset(path)` opens an existing
|
|
87
|
-
dataset; `keyed` and `
|
|
87
|
+
dataset; `keyed`, `compression`, and `max_shard_size` are fixed at creation and read
|
|
88
|
+
from the manifest.
|
|
88
89
|
|
|
89
90
|
In keyed mode, `extend` takes `(key, value)` pairs and duplicate keys are errors. In
|
|
90
91
|
keyless mode it takes values directly. Each `extend` is all-or-nothing and only
|
|
@@ -112,7 +113,7 @@ my-dataset/
|
|
|
112
113
|
`manifest.json` holds the settings and how many shards are committed:
|
|
113
114
|
|
|
114
115
|
```json
|
|
115
|
-
{"keyed": true, "compression": "lz4", "shards": 2}
|
|
116
|
+
{"keyed": true, "compression": "lz4", "max_shard_size": 1073741824, "shards": 2}
|
|
116
117
|
```
|
|
117
118
|
|
|
118
119
|
Each `.idx` lists its shard's records as little-endian `uint32` sizes: `key_size,
|
|
@@ -58,9 +58,9 @@ array, ...).
|
|
|
58
58
|
## API
|
|
59
59
|
|
|
60
60
|
```python
|
|
61
|
-
BitcaskDataset.create(path, keyed=False, compression="lz4")
|
|
61
|
+
BitcaskDataset.create(path, keyed=False, compression="lz4", max_shard_size=1 << 30)
|
|
62
62
|
BitcaskDataset(path)
|
|
63
|
-
ds.extend(entries
|
|
63
|
+
ds.extend(entries)
|
|
64
64
|
ds[key]
|
|
65
65
|
ds.at(index)
|
|
66
66
|
ds.key_at(index)
|
|
@@ -69,7 +69,8 @@ key in ds
|
|
|
69
69
|
```
|
|
70
70
|
|
|
71
71
|
`create` needs a missing or empty directory. `BitcaskDataset(path)` opens an existing
|
|
72
|
-
dataset; `keyed` and `
|
|
72
|
+
dataset; `keyed`, `compression`, and `max_shard_size` are fixed at creation and read
|
|
73
|
+
from the manifest.
|
|
73
74
|
|
|
74
75
|
In keyed mode, `extend` takes `(key, value)` pairs and duplicate keys are errors. In
|
|
75
76
|
keyless mode it takes values directly. Each `extend` is all-or-nothing and only
|
|
@@ -97,7 +98,7 @@ my-dataset/
|
|
|
97
98
|
`manifest.json` holds the settings and how many shards are committed:
|
|
98
99
|
|
|
99
100
|
```json
|
|
100
|
-
{"keyed": true, "compression": "lz4", "shards": 2}
|
|
101
|
+
{"keyed": true, "compression": "lz4", "max_shard_size": 1073741824, "shards": 2}
|
|
101
102
|
```
|
|
102
103
|
|
|
103
104
|
Each `.idx` lists its shard's records as little-endian `uint32` sizes: `key_size,
|
|
@@ -17,7 +17,7 @@ import struct
|
|
|
17
17
|
from collections.abc import Buffer, Iterable, Iterator
|
|
18
18
|
from concurrent.futures import ThreadPoolExecutor
|
|
19
19
|
from pathlib import Path
|
|
20
|
-
from typing import BinaryIO, Literal, SupportsIndex
|
|
20
|
+
from typing import BinaryIO, Literal, NamedTuple, SupportsIndex
|
|
21
21
|
|
|
22
22
|
import numpy as np
|
|
23
23
|
from lz4 import block
|
|
@@ -32,25 +32,35 @@ type Record = tuple[bytes, bytes | memoryview] # (key, stored value)
|
|
|
32
32
|
type Compression = Literal["lz4"] | None
|
|
33
33
|
|
|
34
34
|
|
|
35
|
+
class Manifest(NamedTuple):
|
|
36
|
+
keyed: bool
|
|
37
|
+
compression: Compression
|
|
38
|
+
max_shard_size: int
|
|
39
|
+
shards: int # how many shards are committed
|
|
40
|
+
|
|
41
|
+
|
|
35
42
|
class BitcaskDataset:
|
|
36
43
|
"""An append-oriented, mmap-backed collection of byte blobs."""
|
|
37
44
|
|
|
38
45
|
@classmethod
|
|
39
46
|
def create(
|
|
40
|
-
cls,
|
|
47
|
+
cls,
|
|
48
|
+
path: str | Path,
|
|
49
|
+
*,
|
|
50
|
+
keyed: bool = False,
|
|
51
|
+
compression: Compression = "lz4",
|
|
52
|
+
max_shard_size: int = DEFAULT_MAX_SHARD_SIZE,
|
|
41
53
|
) -> BitcaskDataset:
|
|
42
54
|
"""Create an empty dataset in a new or empty directory."""
|
|
55
|
+
if max_shard_size <= 0:
|
|
56
|
+
raise ValueError("max_shard_size must be positive")
|
|
43
57
|
path = Path(path)
|
|
44
58
|
path.mkdir(parents=True, exist_ok=True)
|
|
45
59
|
if any(path.iterdir()):
|
|
46
60
|
raise FileExistsError(f"directory is not empty: {path}")
|
|
47
61
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
keyed=keyed,
|
|
51
|
-
compression=_validate_compression(compression),
|
|
52
|
-
shard_count=0,
|
|
53
|
-
)
|
|
62
|
+
compression = _validate_compression(compression)
|
|
63
|
+
_write_manifest(path, Manifest(keyed, compression, max_shard_size, shards=0))
|
|
54
64
|
return cls(path)
|
|
55
65
|
|
|
56
66
|
def __init__(self, path: str | Path) -> None:
|
|
@@ -58,22 +68,14 @@ class BitcaskDataset:
|
|
|
58
68
|
self.path = Path(path)
|
|
59
69
|
self._load()
|
|
60
70
|
|
|
61
|
-
def extend(
|
|
62
|
-
self,
|
|
63
|
-
entries: Iterable[Buffer | Entry],
|
|
64
|
-
*,
|
|
65
|
-
max_shard_size: int = DEFAULT_MAX_SHARD_SIZE,
|
|
66
|
-
) -> None:
|
|
71
|
+
def extend(self, entries: Iterable[Buffer | Entry]) -> None:
|
|
67
72
|
"""Add values, or unique key/value pairs for a keyed dataset, as new shards.
|
|
68
73
|
|
|
69
74
|
Keys and values must be C-contiguous buffers. Values are compressed in
|
|
70
75
|
parallel, so their buffers must not be modified after they are yielded.
|
|
71
76
|
"""
|
|
72
|
-
if max_shard_size <= 0:
|
|
73
|
-
raise ValueError("max_shard_size must be positive")
|
|
74
|
-
|
|
75
77
|
# Pick up shards committed through another handle, so they aren't overwritten.
|
|
76
|
-
self._add_shards(_read_manifest(self.path)
|
|
78
|
+
self._add_shards(_read_manifest(self.path).shards)
|
|
77
79
|
|
|
78
80
|
# Validation and duplicate checks run lazily in this thread, as the shard
|
|
79
81
|
# writer pulls records through the compression pool.
|
|
@@ -89,16 +91,14 @@ class BitcaskDataset:
|
|
|
89
91
|
records,
|
|
90
92
|
keyed=self.keyed,
|
|
91
93
|
first_shard_id=len(self._shards),
|
|
92
|
-
max_shard_size=max_shard_size,
|
|
94
|
+
max_shard_size=self.max_shard_size,
|
|
93
95
|
)
|
|
94
96
|
|
|
95
97
|
# Nothing is visible to readers until the manifest names the new shards.
|
|
96
|
-
|
|
97
|
-
self.
|
|
98
|
-
keyed=self.keyed,
|
|
99
|
-
compression=self.compression,
|
|
100
|
-
shard_count=shard_count,
|
|
98
|
+
manifest = Manifest(
|
|
99
|
+
self.keyed, self.compression, self.max_shard_size, shard_count
|
|
101
100
|
)
|
|
101
|
+
_write_manifest(self.path, manifest)
|
|
102
102
|
self._add_shards(shard_count)
|
|
103
103
|
|
|
104
104
|
def __len__(self) -> int:
|
|
@@ -142,7 +142,10 @@ class BitcaskDataset:
|
|
|
142
142
|
self._load()
|
|
143
143
|
|
|
144
144
|
def _load(self) -> None:
|
|
145
|
-
|
|
145
|
+
manifest = _read_manifest(self.path)
|
|
146
|
+
self.keyed = manifest.keyed
|
|
147
|
+
self.compression = manifest.compression
|
|
148
|
+
self.max_shard_size = manifest.max_shard_size
|
|
146
149
|
|
|
147
150
|
# One row per record, across all shards:
|
|
148
151
|
# _sizes (key_size, value_size), or (value_size,) when keyless
|
|
@@ -153,7 +156,7 @@ class BitcaskDataset:
|
|
|
153
156
|
self._shard_ids = np.empty(0, dtype=np.uint32)
|
|
154
157
|
self._record_offsets = np.empty(0, dtype=np.uint64)
|
|
155
158
|
self._keys: dict[bytes, int] = {}
|
|
156
|
-
self._add_shards(
|
|
159
|
+
self._add_shards(manifest.shards)
|
|
157
160
|
|
|
158
161
|
def _add_shards(self, shard_count: int) -> None:
|
|
159
162
|
"""Map shards from the current count up to shard_count and index them."""
|
|
@@ -211,13 +214,10 @@ def _validate_compression(compression: object) -> Compression:
|
|
|
211
214
|
raise ValueError(f"unsupported compression: {compression!r}")
|
|
212
215
|
|
|
213
216
|
|
|
214
|
-
def _write_manifest(
|
|
215
|
-
path: Path, *, keyed: bool, compression: Compression, shard_count: int
|
|
216
|
-
) -> None:
|
|
217
|
-
manifest = {"keyed": keyed, "compression": compression, "shards": shard_count}
|
|
217
|
+
def _write_manifest(path: Path, manifest: Manifest) -> None:
|
|
218
218
|
temporary = path / f".{MANIFEST_NAME}.tmp"
|
|
219
219
|
with temporary.open("wb") as file:
|
|
220
|
-
file.write(json.dumps(manifest).encode())
|
|
220
|
+
file.write(json.dumps(manifest._asdict()).encode())
|
|
221
221
|
_finish_files((file,))
|
|
222
222
|
|
|
223
223
|
# New shard files must be durable before the manifest rename, which commits them.
|
|
@@ -226,20 +226,25 @@ def _write_manifest(
|
|
|
226
226
|
_fsync_directory(path)
|
|
227
227
|
|
|
228
228
|
|
|
229
|
-
def _read_manifest(path: Path) ->
|
|
229
|
+
def _read_manifest(path: Path) -> Manifest:
|
|
230
230
|
manifest_path = path / MANIFEST_NAME
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
shard_count = manifest["shards"]
|
|
231
|
+
# Missing or unexpected fields fail here.
|
|
232
|
+
manifest = Manifest(**json.loads(manifest_path.read_text()))
|
|
234
233
|
|
|
235
234
|
# The manifest is read from disk, so check it rather than trust it: a
|
|
236
235
|
# hand-edited "keyed": "false" would otherwise misread every record.
|
|
236
|
+
keyed, compression, max_shard_size, shards = manifest
|
|
237
237
|
valid = (
|
|
238
|
-
isinstance(keyed, bool)
|
|
238
|
+
isinstance(keyed, bool)
|
|
239
|
+
and isinstance(max_shard_size, int)
|
|
240
|
+
and max_shard_size > 0
|
|
241
|
+
and isinstance(shards, int)
|
|
242
|
+
and shards >= 0
|
|
239
243
|
)
|
|
240
244
|
if not valid:
|
|
241
245
|
raise ValueError(f"invalid manifest: {manifest_path}")
|
|
242
|
-
|
|
246
|
+
_validate_compression(compression)
|
|
247
|
+
return manifest
|
|
243
248
|
|
|
244
249
|
|
|
245
250
|
def _records(
|
|
File without changes
|
|
File without changes
|