bitcask-dataset 0.2.0__tar.gz → 0.3.1__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.3.1/PKG-INFO +142 -0
- bitcask_dataset-0.3.1/README.md +126 -0
- {bitcask_dataset-0.2.0 → bitcask_dataset-0.3.1}/pyproject.toml +2 -2
- {bitcask_dataset-0.2.0 → bitcask_dataset-0.3.1}/pyproject.toml.orig +2 -2
- {bitcask_dataset-0.2.0 → bitcask_dataset-0.3.1}/src/bitcask_dataset/dataset.py +76 -72
- bitcask_dataset-0.2.0/PKG-INFO +0 -145
- bitcask_dataset-0.2.0/README.md +0 -130
- {bitcask_dataset-0.2.0 → bitcask_dataset-0.3.1}/src/bitcask_dataset/__init__.py +0 -0
- {bitcask_dataset-0.2.0 → bitcask_dataset-0.3.1}/src/bitcask_dataset/py.typed +0 -0
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: bitcask-dataset
|
|
3
|
+
Version: 0.3.1
|
|
4
|
+
Summary: A small, mmap-backed dataset of byte blobs, optionally keyed
|
|
5
|
+
Keywords: dataset,mmap,machine-learning,pytorch
|
|
6
|
+
License-Expression: BSD-3-Clause
|
|
7
|
+
Classifier: Development Status :: 3 - Alpha
|
|
8
|
+
Classifier: Operating System :: POSIX
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
11
|
+
Classifier: Typing :: Typed
|
|
12
|
+
Requires-Dist: lz4>=4.4.5
|
|
13
|
+
Requires-Dist: numpy>=2.1
|
|
14
|
+
Requires-Python: >=3.14
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
|
|
17
|
+
# bitcask-dataset
|
|
18
|
+
|
|
19
|
+
A small, append-only blob store on disk. Good fit if you want:
|
|
20
|
+
|
|
21
|
+
- random-access reads by index or optional key, without a million loose files on disk
|
|
22
|
+
- read-only `memoryview` reads: zero-copy when uncompressed, or LZ4-compressed by
|
|
23
|
+
default
|
|
24
|
+
- immutable shards: each `extend` only writes new files and updates `manifest.json`, so
|
|
25
|
+
unlike a single HDF5 or LMDB file, syncing to S3 only uploads what's new
|
|
26
|
+
- easy multiprocess reads (handles pickle down to their path and reopen on their own)
|
|
27
|
+
|
|
28
|
+
Works best for biggish blobs (KiB to MiB), small keys, and batch appends.
|
|
29
|
+
|
|
30
|
+
Not a good fit if you need deletes, overwrites, compaction, concurrent writers, Windows
|
|
31
|
+
support, or more keys than fit in RAM per process.
|
|
32
|
+
|
|
33
|
+
## Install
|
|
34
|
+
|
|
35
|
+
Requires Python 3.14+ on POSIX.
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
pip install bitcask-dataset
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Example
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
from bitcask_dataset import BitcaskDataset
|
|
45
|
+
|
|
46
|
+
ds = BitcaskDataset.create("my-dataset", keyed=True)
|
|
47
|
+
ds.extend([
|
|
48
|
+
(b"image/1", image_bytes),
|
|
49
|
+
(b"image/2", more_image_bytes),
|
|
50
|
+
])
|
|
51
|
+
|
|
52
|
+
ds = BitcaskDataset("my-dataset") # settings come from the manifest
|
|
53
|
+
value = ds[b"image/1"] # read-only memoryview
|
|
54
|
+
|
|
55
|
+
# insertion order works too
|
|
56
|
+
value = ds.at(0)
|
|
57
|
+
key = ds.key_at(0)
|
|
58
|
+
|
|
59
|
+
len(ds)
|
|
60
|
+
b"image/1" in ds
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Keys are optional (keyless is the default):
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
ds = BitcaskDataset.create("my-keyless-dataset")
|
|
67
|
+
ds.extend([image_bytes, more_image_bytes])
|
|
68
|
+
value = ds.at(0)
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Keys and values can be any C-contiguous buffer (`bytes`, `bytearray`, a contiguous NumPy
|
|
72
|
+
array, ...).
|
|
73
|
+
|
|
74
|
+
## API
|
|
75
|
+
|
|
76
|
+
```python
|
|
77
|
+
BitcaskDataset.create(path, *, keyed=False, compression="lz4", max_shard_size=1 << 30)
|
|
78
|
+
BitcaskDataset(path)
|
|
79
|
+
ds.extend(entries)
|
|
80
|
+
ds[key]
|
|
81
|
+
ds.at(index)
|
|
82
|
+
ds.key_at(index)
|
|
83
|
+
len(ds)
|
|
84
|
+
key in ds
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
- `create` needs a missing or empty directory. `BitcaskDataset(path)` opens an existing
|
|
88
|
+
dataset and reads `keyed`, `compression`, and `max_shard_size` from the manifest.
|
|
89
|
+
- `extend` takes `(key, value)` pairs when keyed (duplicate keys are errors) and values
|
|
90
|
+
directly when keyless. Each `extend` is all-or-nothing and only writes new shards.
|
|
91
|
+
Records aren't split, so a record over `max_shard_size` gets a shard to itself.
|
|
92
|
+
- Values are compressed on a thread pool, so don't modify a value's buffer after
|
|
93
|
+
yielding it to `extend`.
|
|
94
|
+
- Keys and values come back as read-only `memoryview`s, which `np.frombuffer` and
|
|
95
|
+
`torch.frombuffer` can use without copying. With `compression=None`, values are views
|
|
96
|
+
straight into the mmap with zero copies; non-empty compressed values are views of
|
|
97
|
+
freshly decompressed `bytes` (`value.obj`). Keys are always uncompressed.
|
|
98
|
+
- `at` and `key_at` support negative indexes. Key lookup (`ds[key]`) and `key_at` raise
|
|
99
|
+
`TypeError` in keyless mode. Datasets aren't iterable.
|
|
100
|
+
|
|
101
|
+
## Format
|
|
102
|
+
|
|
103
|
+
```text
|
|
104
|
+
my-dataset/
|
|
105
|
+
manifest.json
|
|
106
|
+
000000.bin
|
|
107
|
+
000000.idx
|
|
108
|
+
000001.bin
|
|
109
|
+
000001.idx
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
`manifest.json` holds the settings and how many shards are committed:
|
|
113
|
+
|
|
114
|
+
```json
|
|
115
|
+
{"keyed": true, "compression": "lz4", "max_shard_size": 1073741824, "shards": 2}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
Each `.idx` lists its shard's records as little-endian `uint32` sizes: `key_size,
|
|
119
|
+
value_size` pairs when keyed, one stored-value size per record when keyless. Keyed
|
|
120
|
+
shards are `key || value || key || value ...`; keyless shards only contain values.
|
|
121
|
+
There are no record headers or padding. Values are independent LZ4 blocks using
|
|
122
|
+
high-compression mode, with the original size in each block. Empty values stay empty.
|
|
123
|
+
|
|
124
|
+
Rewriting the manifest commits an `extend`, so shard files it doesn't list are
|
|
125
|
+
leftovers from an interrupted `extend`; they're ignored and overwritten later. When
|
|
126
|
+
copying a dataset, copy the manifest last.
|
|
127
|
+
|
|
128
|
+
Keyed datasets copy keys into a Python dict in each process that opens them. Each key
|
|
129
|
+
and stored value is limited to `2**32 - 1` bytes; with LZ4, uncompressed values are also
|
|
130
|
+
limited to about 2 GiB, the LZ4 block limit.
|
|
131
|
+
|
|
132
|
+
Not actually an implementation of the Bitcask paper, though the per-shard index files
|
|
133
|
+
are close to its hint files.
|
|
134
|
+
|
|
135
|
+
## Dev
|
|
136
|
+
|
|
137
|
+
```bash
|
|
138
|
+
uv sync
|
|
139
|
+
make format
|
|
140
|
+
make lint
|
|
141
|
+
make test
|
|
142
|
+
```
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
# bitcask-dataset
|
|
2
|
+
|
|
3
|
+
A small, append-only blob store on disk. Good fit if you want:
|
|
4
|
+
|
|
5
|
+
- random-access reads by index or optional key, without a million loose files on disk
|
|
6
|
+
- read-only `memoryview` reads: zero-copy when uncompressed, or LZ4-compressed by
|
|
7
|
+
default
|
|
8
|
+
- immutable shards: each `extend` only writes new files and updates `manifest.json`, so
|
|
9
|
+
unlike a single HDF5 or LMDB file, syncing to S3 only uploads what's new
|
|
10
|
+
- easy multiprocess reads (handles pickle down to their path and reopen on their own)
|
|
11
|
+
|
|
12
|
+
Works best for biggish blobs (KiB to MiB), small keys, and batch appends.
|
|
13
|
+
|
|
14
|
+
Not a good fit if you need deletes, overwrites, compaction, concurrent writers, Windows
|
|
15
|
+
support, or more keys than fit in RAM per process.
|
|
16
|
+
|
|
17
|
+
## Install
|
|
18
|
+
|
|
19
|
+
Requires Python 3.14+ on POSIX.
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pip install bitcask-dataset
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Example
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
from bitcask_dataset import BitcaskDataset
|
|
29
|
+
|
|
30
|
+
ds = BitcaskDataset.create("my-dataset", keyed=True)
|
|
31
|
+
ds.extend([
|
|
32
|
+
(b"image/1", image_bytes),
|
|
33
|
+
(b"image/2", more_image_bytes),
|
|
34
|
+
])
|
|
35
|
+
|
|
36
|
+
ds = BitcaskDataset("my-dataset") # settings come from the manifest
|
|
37
|
+
value = ds[b"image/1"] # read-only memoryview
|
|
38
|
+
|
|
39
|
+
# insertion order works too
|
|
40
|
+
value = ds.at(0)
|
|
41
|
+
key = ds.key_at(0)
|
|
42
|
+
|
|
43
|
+
len(ds)
|
|
44
|
+
b"image/1" in ds
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Keys are optional (keyless is the default):
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
ds = BitcaskDataset.create("my-keyless-dataset")
|
|
51
|
+
ds.extend([image_bytes, more_image_bytes])
|
|
52
|
+
value = ds.at(0)
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Keys and values can be any C-contiguous buffer (`bytes`, `bytearray`, a contiguous NumPy
|
|
56
|
+
array, ...).
|
|
57
|
+
|
|
58
|
+
## API
|
|
59
|
+
|
|
60
|
+
```python
|
|
61
|
+
BitcaskDataset.create(path, *, keyed=False, compression="lz4", max_shard_size=1 << 30)
|
|
62
|
+
BitcaskDataset(path)
|
|
63
|
+
ds.extend(entries)
|
|
64
|
+
ds[key]
|
|
65
|
+
ds.at(index)
|
|
66
|
+
ds.key_at(index)
|
|
67
|
+
len(ds)
|
|
68
|
+
key in ds
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
- `create` needs a missing or empty directory. `BitcaskDataset(path)` opens an existing
|
|
72
|
+
dataset and reads `keyed`, `compression`, and `max_shard_size` from the manifest.
|
|
73
|
+
- `extend` takes `(key, value)` pairs when keyed (duplicate keys are errors) and values
|
|
74
|
+
directly when keyless. Each `extend` is all-or-nothing and only writes new shards.
|
|
75
|
+
Records aren't split, so a record over `max_shard_size` gets a shard to itself.
|
|
76
|
+
- Values are compressed on a thread pool, so don't modify a value's buffer after
|
|
77
|
+
yielding it to `extend`.
|
|
78
|
+
- Keys and values come back as read-only `memoryview`s, which `np.frombuffer` and
|
|
79
|
+
`torch.frombuffer` can use without copying. With `compression=None`, values are views
|
|
80
|
+
straight into the mmap with zero copies; non-empty compressed values are views of
|
|
81
|
+
freshly decompressed `bytes` (`value.obj`). Keys are always uncompressed.
|
|
82
|
+
- `at` and `key_at` support negative indexes. Key lookup (`ds[key]`) and `key_at` raise
|
|
83
|
+
`TypeError` in keyless mode. Datasets aren't iterable.
|
|
84
|
+
|
|
85
|
+
## Format
|
|
86
|
+
|
|
87
|
+
```text
|
|
88
|
+
my-dataset/
|
|
89
|
+
manifest.json
|
|
90
|
+
000000.bin
|
|
91
|
+
000000.idx
|
|
92
|
+
000001.bin
|
|
93
|
+
000001.idx
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
`manifest.json` holds the settings and how many shards are committed:
|
|
97
|
+
|
|
98
|
+
```json
|
|
99
|
+
{"keyed": true, "compression": "lz4", "max_shard_size": 1073741824, "shards": 2}
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Each `.idx` lists its shard's records as little-endian `uint32` sizes: `key_size,
|
|
103
|
+
value_size` pairs when keyed, one stored-value size per record when keyless. Keyed
|
|
104
|
+
shards are `key || value || key || value ...`; keyless shards only contain values.
|
|
105
|
+
There are no record headers or padding. Values are independent LZ4 blocks using
|
|
106
|
+
high-compression mode, with the original size in each block. Empty values stay empty.
|
|
107
|
+
|
|
108
|
+
Rewriting the manifest commits an `extend`, so shard files it doesn't list are
|
|
109
|
+
leftovers from an interrupted `extend`; they're ignored and overwritten later. When
|
|
110
|
+
copying a dataset, copy the manifest last.
|
|
111
|
+
|
|
112
|
+
Keyed datasets copy keys into a Python dict in each process that opens them. Each key
|
|
113
|
+
and stored value is limited to `2**32 - 1` bytes; with LZ4, uncompressed values are also
|
|
114
|
+
limited to about 2 GiB, the LZ4 block limit.
|
|
115
|
+
|
|
116
|
+
Not actually an implementation of the Bitcask paper, though the per-shard index files
|
|
117
|
+
are close to its hint files.
|
|
118
|
+
|
|
119
|
+
## Dev
|
|
120
|
+
|
|
121
|
+
```bash
|
|
122
|
+
uv sync
|
|
123
|
+
make format
|
|
124
|
+
make lint
|
|
125
|
+
make test
|
|
126
|
+
```
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "bitcask-dataset"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.3.1"
|
|
4
4
|
description = "A small, mmap-backed dataset of byte blobs, optionally keyed"
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
requires-python = ">=3.14"
|
|
@@ -17,6 +17,7 @@ keywords = [
|
|
|
17
17
|
]
|
|
18
18
|
classifiers = [
|
|
19
19
|
"Development Status :: 3 - Alpha",
|
|
20
|
+
"Operating System :: POSIX",
|
|
20
21
|
"Programming Language :: Python :: 3",
|
|
21
22
|
"Programming Language :: Python :: 3.14",
|
|
22
23
|
"Typing :: Typed",
|
|
@@ -64,7 +65,6 @@ ignore = [
|
|
|
64
65
|
"SIM102",
|
|
65
66
|
"SIM108",
|
|
66
67
|
]
|
|
67
|
-
preview = true
|
|
68
68
|
select = [
|
|
69
69
|
"B",
|
|
70
70
|
"C4",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "bitcask-dataset"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.3.1"
|
|
4
4
|
description = "A small, mmap-backed dataset of byte blobs, optionally keyed"
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
requires-python = ">=3.14"
|
|
@@ -12,6 +12,7 @@ license = "BSD-3-Clause"
|
|
|
12
12
|
keywords = ["dataset", "mmap", "machine-learning", "pytorch"]
|
|
13
13
|
classifiers = [
|
|
14
14
|
"Development Status :: 3 - Alpha",
|
|
15
|
+
"Operating System :: POSIX",
|
|
15
16
|
"Programming Language :: Python :: 3",
|
|
16
17
|
"Programming Language :: Python :: 3.14",
|
|
17
18
|
"Typing :: Typed",
|
|
@@ -48,5 +49,4 @@ target-version = "py314"
|
|
|
48
49
|
|
|
49
50
|
[tool.ruff.lint]
|
|
50
51
|
ignore = ["B028", "B905", "C408", "E203", "FURB118", "SIM102", "SIM108"]
|
|
51
|
-
preview = true
|
|
52
52
|
select = ["B", "C4", "E", "F", "FURB", "I", "PT", "Q", "SIM", "U", "YTT"]
|
|
@@ -12,12 +12,13 @@ extend. Files the manifest doesn't count are leftovers and get overwritten.
|
|
|
12
12
|
|
|
13
13
|
import json
|
|
14
14
|
import mmap
|
|
15
|
+
import operator
|
|
15
16
|
import os
|
|
16
17
|
import struct
|
|
17
18
|
from collections.abc import Buffer, Iterable, Iterator
|
|
18
19
|
from concurrent.futures import ThreadPoolExecutor
|
|
19
20
|
from pathlib import Path
|
|
20
|
-
from typing import BinaryIO, Literal, SupportsIndex
|
|
21
|
+
from typing import BinaryIO, Literal, NamedTuple, SupportsIndex
|
|
21
22
|
|
|
22
23
|
import numpy as np
|
|
23
24
|
from lz4 import block
|
|
@@ -32,48 +33,64 @@ type Record = tuple[bytes, bytes | memoryview] # (key, stored value)
|
|
|
32
33
|
type Compression = Literal["lz4"] | None
|
|
33
34
|
|
|
34
35
|
|
|
36
|
+
class Manifest(NamedTuple):
|
|
37
|
+
keyed: bool
|
|
38
|
+
compression: Compression
|
|
39
|
+
max_shard_size: int
|
|
40
|
+
shards: int # how many shards are committed
|
|
41
|
+
|
|
42
|
+
|
|
35
43
|
class BitcaskDataset:
|
|
36
44
|
"""An append-oriented, mmap-backed collection of byte blobs."""
|
|
37
45
|
|
|
38
46
|
@classmethod
|
|
39
47
|
def create(
|
|
40
|
-
cls,
|
|
48
|
+
cls,
|
|
49
|
+
path: str | Path,
|
|
50
|
+
*,
|
|
51
|
+
keyed: bool = False,
|
|
52
|
+
compression: Compression = "lz4",
|
|
53
|
+
max_shard_size: int = DEFAULT_MAX_SHARD_SIZE,
|
|
41
54
|
) -> BitcaskDataset:
|
|
42
55
|
"""Create an empty dataset in a new or empty directory."""
|
|
56
|
+
if type(max_shard_size) is not int or max_shard_size <= 0:
|
|
57
|
+
raise ValueError("max_shard_size must be a positive int")
|
|
58
|
+
compression = _validate_compression(compression)
|
|
43
59
|
path = Path(path)
|
|
44
60
|
path.mkdir(parents=True, exist_ok=True)
|
|
45
61
|
if any(path.iterdir()):
|
|
46
62
|
raise FileExistsError(f"directory is not empty: {path}")
|
|
47
63
|
|
|
48
|
-
_write_manifest(
|
|
49
|
-
path,
|
|
50
|
-
keyed=keyed,
|
|
51
|
-
compression=_validate_compression(compression),
|
|
52
|
-
shard_count=0,
|
|
53
|
-
)
|
|
64
|
+
_write_manifest(path, Manifest(keyed, compression, max_shard_size, shards=0))
|
|
54
65
|
return cls(path)
|
|
55
66
|
|
|
56
67
|
def __init__(self, path: str | Path) -> None:
|
|
57
68
|
"""Open an existing dataset."""
|
|
58
69
|
self.path = Path(path)
|
|
59
|
-
self.
|
|
70
|
+
manifest = _read_manifest(self.path)
|
|
71
|
+
self.keyed = manifest.keyed
|
|
72
|
+
self.compression = manifest.compression
|
|
73
|
+
self.max_shard_size = manifest.max_shard_size
|
|
60
74
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
75
|
+
# One row per record, across all shards:
|
|
76
|
+
# _sizes (key_size, value_size), or (value_size,) when keyless
|
|
77
|
+
# _shard_ids which shard holds the record
|
|
78
|
+
# _record_offsets where the record starts in that shard
|
|
79
|
+
self._shards: list[memoryview] = []
|
|
80
|
+
self._sizes = np.empty((0, 2 if self.keyed else 1), dtype=INDEX_DTYPE)
|
|
81
|
+
self._shard_ids = np.empty(0, dtype=np.uint32)
|
|
82
|
+
self._record_offsets = np.empty(0, dtype=np.uint64)
|
|
83
|
+
self._keys: dict[bytes, int] = {}
|
|
84
|
+
self._add_shards(manifest.shards)
|
|
85
|
+
|
|
86
|
+
def extend(self, entries: Iterable[Buffer | Entry]) -> None:
|
|
67
87
|
"""Add values, or unique key/value pairs for a keyed dataset, as new shards.
|
|
68
88
|
|
|
69
89
|
Keys and values must be C-contiguous buffers. Values are compressed in
|
|
70
90
|
parallel, so their buffers must not be modified after they are yielded.
|
|
71
91
|
"""
|
|
72
|
-
if max_shard_size <= 0:
|
|
73
|
-
raise ValueError("max_shard_size must be positive")
|
|
74
|
-
|
|
75
92
|
# Pick up shards committed through another handle, so they aren't overwritten.
|
|
76
|
-
self._add_shards(_read_manifest(self.path)
|
|
93
|
+
self._add_shards(_read_manifest(self.path).shards)
|
|
77
94
|
|
|
78
95
|
# Validation and duplicate checks run lazily in this thread, as the shard
|
|
79
96
|
# writer pulls records through the compression pool.
|
|
@@ -89,18 +106,19 @@ class BitcaskDataset:
|
|
|
89
106
|
records,
|
|
90
107
|
keyed=self.keyed,
|
|
91
108
|
first_shard_id=len(self._shards),
|
|
92
|
-
max_shard_size=max_shard_size,
|
|
109
|
+
max_shard_size=self.max_shard_size,
|
|
93
110
|
)
|
|
94
111
|
|
|
95
112
|
# 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,
|
|
113
|
+
manifest = Manifest(
|
|
114
|
+
self.keyed, self.compression, self.max_shard_size, shard_count
|
|
101
115
|
)
|
|
116
|
+
_write_manifest(self.path, manifest)
|
|
102
117
|
self._add_shards(shard_count)
|
|
103
118
|
|
|
119
|
+
# Not iterable: iter() would otherwise call __getitem__ with 0, 1, 2, ...
|
|
120
|
+
__iter__ = None
|
|
121
|
+
|
|
104
122
|
def __len__(self) -> int:
|
|
105
123
|
return self._sizes.shape[0]
|
|
106
124
|
|
|
@@ -115,6 +133,7 @@ class BitcaskDataset:
|
|
|
115
133
|
def at(self, index: SupportsIndex) -> memoryview:
|
|
116
134
|
"""Return a value by insertion position."""
|
|
117
135
|
# A record is key || value, so the value starts after the key.
|
|
136
|
+
index = operator.index(index)
|
|
118
137
|
key_size = int(self._sizes[index, 0]) if self.keyed else 0
|
|
119
138
|
value_size = int(self._sizes[index, -1])
|
|
120
139
|
offset = int(self._record_offsets[index]) + key_size
|
|
@@ -129,31 +148,14 @@ class BitcaskDataset:
|
|
|
129
148
|
"""Return a key by insertion position."""
|
|
130
149
|
if not self.keyed:
|
|
131
150
|
raise TypeError("keys are disabled for this dataset")
|
|
151
|
+
index = operator.index(index)
|
|
132
152
|
offset = int(self._record_offsets[index])
|
|
133
153
|
key_size = int(self._sizes[index, 0])
|
|
134
154
|
return self._shards[int(self._shard_ids[index])][offset : offset + key_size]
|
|
135
155
|
|
|
136
156
|
# Pickle only the path: mmaps can't be pickled, so workers reopen the dataset.
|
|
137
|
-
def
|
|
138
|
-
return self.path
|
|
139
|
-
|
|
140
|
-
def __setstate__(self, state: Path) -> None:
|
|
141
|
-
self.path = state
|
|
142
|
-
self._load()
|
|
143
|
-
|
|
144
|
-
def _load(self) -> None:
|
|
145
|
-
self.keyed, self.compression, shard_count = _read_manifest(self.path)
|
|
146
|
-
|
|
147
|
-
# One row per record, across all shards:
|
|
148
|
-
# _sizes (key_size, value_size), or (value_size,) when keyless
|
|
149
|
-
# _shard_ids which shard holds the record
|
|
150
|
-
# _record_offsets where the record starts in that shard
|
|
151
|
-
self._shards: list[memoryview] = []
|
|
152
|
-
self._sizes = np.empty((0, 2 if self.keyed else 1), dtype=INDEX_DTYPE)
|
|
153
|
-
self._shard_ids = np.empty(0, dtype=np.uint32)
|
|
154
|
-
self._record_offsets = np.empty(0, dtype=np.uint64)
|
|
155
|
-
self._keys: dict[bytes, int] = {}
|
|
156
|
-
self._add_shards(shard_count)
|
|
157
|
+
def __reduce__(self) -> tuple[type[BitcaskDataset], tuple[Path]]:
|
|
158
|
+
return type(self), (self.path,)
|
|
157
159
|
|
|
158
160
|
def _add_shards(self, shard_count: int) -> None:
|
|
159
161
|
"""Map shards from the current count up to shard_count and index them."""
|
|
@@ -164,7 +166,8 @@ class BitcaskDataset:
|
|
|
164
166
|
sizes = [self._sizes]
|
|
165
167
|
shard_ids = [self._shard_ids]
|
|
166
168
|
record_offsets = [self._record_offsets]
|
|
167
|
-
|
|
169
|
+
keys: dict[bytes, int] = {}
|
|
170
|
+
next_index = len(self)
|
|
168
171
|
for shard_id in range(len(self._shards), shard_count):
|
|
169
172
|
data_path, index_path = _shard_paths(self.path, shard_id)
|
|
170
173
|
shard_sizes = np.fromfile(index_path, dtype=INDEX_DTYPE).reshape(
|
|
@@ -179,11 +182,11 @@ class BitcaskDataset:
|
|
|
179
182
|
# previous one ended.
|
|
180
183
|
offsets = np.cumsum(record_sizes, dtype=np.uint64) - record_sizes
|
|
181
184
|
if self.keyed:
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
185
|
+
for offset, key_size in zip(
|
|
186
|
+
offsets.tolist(), shard_sizes[:, 0].tolist()
|
|
187
|
+
):
|
|
188
|
+
keys[bytes(shard[offset : offset + key_size])] = next_index
|
|
189
|
+
next_index += 1
|
|
187
190
|
|
|
188
191
|
shards.append(shard)
|
|
189
192
|
sizes.append(shard_sizes)
|
|
@@ -192,15 +195,18 @@ class BitcaskDataset:
|
|
|
192
195
|
|
|
193
196
|
# Validate everything before changing any state, so a failure leaves the
|
|
194
197
|
# handle as it was.
|
|
195
|
-
|
|
196
|
-
if len(keys) !=
|
|
198
|
+
added = next_index - len(self)
|
|
199
|
+
if len(keys) != added or not self._keys.keys().isdisjoint(keys):
|
|
197
200
|
raise ValueError("duplicate keys in dataset")
|
|
198
201
|
|
|
199
202
|
self._shards += shards
|
|
200
203
|
self._sizes = np.concatenate(sizes)
|
|
201
204
|
self._shard_ids = np.concatenate(shard_ids)
|
|
202
205
|
self._record_offsets = np.concatenate(record_offsets)
|
|
203
|
-
self._keys
|
|
206
|
+
if self._keys:
|
|
207
|
+
self._keys.update(keys)
|
|
208
|
+
else:
|
|
209
|
+
self._keys = keys
|
|
204
210
|
|
|
205
211
|
|
|
206
212
|
def _validate_compression(compression: object) -> Compression:
|
|
@@ -211,14 +217,12 @@ def _validate_compression(compression: object) -> Compression:
|
|
|
211
217
|
raise ValueError(f"unsupported compression: {compression!r}")
|
|
212
218
|
|
|
213
219
|
|
|
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}
|
|
220
|
+
def _write_manifest(path: Path, manifest: Manifest) -> None:
|
|
218
221
|
temporary = path / f".{MANIFEST_NAME}.tmp"
|
|
219
222
|
with temporary.open("wb") as file:
|
|
220
|
-
file.write(json.dumps(manifest).encode())
|
|
221
|
-
|
|
223
|
+
file.write(json.dumps(manifest._asdict()).encode())
|
|
224
|
+
file.flush()
|
|
225
|
+
os.fsync(file.fileno())
|
|
222
226
|
|
|
223
227
|
# New shard files must be durable before the manifest rename, which commits them.
|
|
224
228
|
_fsync_directory(path)
|
|
@@ -226,20 +230,25 @@ def _write_manifest(
|
|
|
226
230
|
_fsync_directory(path)
|
|
227
231
|
|
|
228
232
|
|
|
229
|
-
def _read_manifest(path: Path) ->
|
|
233
|
+
def _read_manifest(path: Path) -> Manifest:
|
|
230
234
|
manifest_path = path / MANIFEST_NAME
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
shard_count = manifest["shards"]
|
|
235
|
+
# Missing or unexpected fields fail here.
|
|
236
|
+
manifest = Manifest(**json.loads(manifest_path.read_text()))
|
|
234
237
|
|
|
235
238
|
# The manifest is read from disk, so check it rather than trust it: a
|
|
236
239
|
# hand-edited "keyed": "false" would otherwise misread every record.
|
|
240
|
+
keyed, compression, max_shard_size, shards = manifest
|
|
237
241
|
valid = (
|
|
238
|
-
isinstance(keyed, bool)
|
|
242
|
+
isinstance(keyed, bool)
|
|
243
|
+
and type(max_shard_size) is int
|
|
244
|
+
and max_shard_size > 0
|
|
245
|
+
and type(shards) is int
|
|
246
|
+
and shards >= 0
|
|
239
247
|
)
|
|
240
248
|
if not valid:
|
|
241
249
|
raise ValueError(f"invalid manifest: {manifest_path}")
|
|
242
|
-
|
|
250
|
+
_validate_compression(compression)
|
|
251
|
+
return manifest
|
|
243
252
|
|
|
244
253
|
|
|
245
254
|
def _records(
|
|
@@ -345,13 +354,8 @@ def _as_key(value: Buffer) -> bytes:
|
|
|
345
354
|
|
|
346
355
|
|
|
347
356
|
def _as_bytes_view(value: Buffer) -> memoryview:
|
|
348
|
-
try:
|
|
349
|
-
view = memoryview(value)
|
|
350
|
-
except TypeError:
|
|
351
|
-
raise TypeError("keys and values must support the buffer protocol") from None
|
|
352
|
-
|
|
353
357
|
# A flat byte view, so len() counts bytes; raises for non-C-contiguous buffers.
|
|
354
|
-
return
|
|
358
|
+
return memoryview(value).cast("B")
|
|
355
359
|
|
|
356
360
|
|
|
357
361
|
def _finish_files(files: Iterable[BinaryIO]) -> None:
|
bitcask_dataset-0.2.0/PKG-INFO
DELETED
|
@@ -1,145 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: bitcask-dataset
|
|
3
|
-
Version: 0.2.0
|
|
4
|
-
Summary: A small, mmap-backed dataset of byte blobs, optionally keyed
|
|
5
|
-
Keywords: dataset,mmap,machine-learning,pytorch
|
|
6
|
-
License-Expression: BSD-3-Clause
|
|
7
|
-
Classifier: Development Status :: 3 - Alpha
|
|
8
|
-
Classifier: Programming Language :: Python :: 3
|
|
9
|
-
Classifier: Programming Language :: Python :: 3.14
|
|
10
|
-
Classifier: Typing :: Typed
|
|
11
|
-
Requires-Dist: lz4>=4.4.5
|
|
12
|
-
Requires-Dist: numpy>=2.1
|
|
13
|
-
Requires-Python: >=3.14
|
|
14
|
-
Description-Content-Type: text/markdown
|
|
15
|
-
|
|
16
|
-
# bitcask-dataset
|
|
17
|
-
|
|
18
|
-
A disk-backed blob store that's a good fit if:
|
|
19
|
-
|
|
20
|
-
- you mostly do random-access reads, e.g. ML training data,
|
|
21
|
-
- values are biggish blobs (KiB to MiB) and keys are small,
|
|
22
|
-
- you add data in batches and never update or delete.
|
|
23
|
-
|
|
24
|
-
Each `extend` writes new shard files. Opening a dataset mmaps the shards as one logical
|
|
25
|
-
file, so lookups are fast and efficient.
|
|
26
|
-
Values are optionally LZ4-compressed (on by default).
|
|
27
|
-
|
|
28
|
-
Because old shards never change, keeping a copy elsewhere (say, S3) after an `extend`
|
|
29
|
-
means uploading only the new shards and the manifest, and never the whole dataset.
|
|
30
|
-
|
|
31
|
-
Not a good fit if you need deletes, overwrites, concurrent writers, or more keys
|
|
32
|
-
than what your RAM can fit.
|
|
33
|
-
|
|
34
|
-
## Install
|
|
35
|
-
|
|
36
|
-
```bash
|
|
37
|
-
pip install bitcask-dataset
|
|
38
|
-
```
|
|
39
|
-
|
|
40
|
-
## Example
|
|
41
|
-
|
|
42
|
-
```python
|
|
43
|
-
from bitcask_dataset import BitcaskDataset
|
|
44
|
-
|
|
45
|
-
ds = BitcaskDataset.create("my-dataset", keyed=True)
|
|
46
|
-
ds.extend([
|
|
47
|
-
(b"image/1", image_bytes),
|
|
48
|
-
(b"image/2", more_image_bytes),
|
|
49
|
-
])
|
|
50
|
-
|
|
51
|
-
ds = BitcaskDataset("my-dataset") # settings come from the manifest
|
|
52
|
-
value = ds[b"image/1"] # read-only memoryview of the decompressed bytes
|
|
53
|
-
|
|
54
|
-
# insertion order is available too
|
|
55
|
-
value = ds.at(0)
|
|
56
|
-
key = ds.key_at(0)
|
|
57
|
-
|
|
58
|
-
len(ds)
|
|
59
|
-
b"image/1" in ds
|
|
60
|
-
```
|
|
61
|
-
|
|
62
|
-
Keys are optional:
|
|
63
|
-
|
|
64
|
-
```python
|
|
65
|
-
ds = BitcaskDataset.create("my-keyless-dataset")
|
|
66
|
-
ds.extend([image_bytes, more_image_bytes])
|
|
67
|
-
value = ds.at(0)
|
|
68
|
-
```
|
|
69
|
-
|
|
70
|
-
Keys and values can be any C-contiguous buffer (`bytes`, `bytearray`, a contiguous NumPy
|
|
71
|
-
array, ...).
|
|
72
|
-
|
|
73
|
-
## API
|
|
74
|
-
|
|
75
|
-
```python
|
|
76
|
-
BitcaskDataset.create(path, keyed=False, compression="lz4")
|
|
77
|
-
BitcaskDataset(path)
|
|
78
|
-
ds.extend(entries, max_shard_size=1 << 30)
|
|
79
|
-
ds[key]
|
|
80
|
-
ds.at(index)
|
|
81
|
-
ds.key_at(index)
|
|
82
|
-
len(ds)
|
|
83
|
-
key in ds
|
|
84
|
-
```
|
|
85
|
-
|
|
86
|
-
`create` needs a missing or empty directory. `BitcaskDataset(path)` opens an existing
|
|
87
|
-
dataset; `keyed` and `compression` are fixed at creation and read from the manifest.
|
|
88
|
-
|
|
89
|
-
In keyed mode, `extend` takes `(key, value)` pairs and duplicate keys are errors. In
|
|
90
|
-
keyless mode it takes values directly. Each `extend` is all-or-nothing and only
|
|
91
|
-
creates new shards. Records aren't split, so a record over `max_shard_size` gets a
|
|
92
|
-
shard to itself. Values are compressed on a thread pool, so don't modify a value's
|
|
93
|
-
buffer after yielding it to `extend`.
|
|
94
|
-
|
|
95
|
-
Keys and values come back as read-only `memoryview`s, which `np.frombuffer` and
|
|
96
|
-
`torch.frombuffer` can use without copying. Non-empty compressed values are views of
|
|
97
|
-
freshly decompressed `bytes` (`value.obj`). With `compression=None`, values are read
|
|
98
|
-
straight from the mmap with zero copies. Keys are always uncompressed. `at` and `key_at`
|
|
99
|
-
support negative indexes. Key lookup and `key_at` raise `TypeError` in keyless mode.
|
|
100
|
-
|
|
101
|
-
## Format
|
|
102
|
-
|
|
103
|
-
```text
|
|
104
|
-
my-dataset/
|
|
105
|
-
manifest.json
|
|
106
|
-
000000.bin
|
|
107
|
-
000000.idx
|
|
108
|
-
000001.bin
|
|
109
|
-
000001.idx
|
|
110
|
-
```
|
|
111
|
-
|
|
112
|
-
`manifest.json` holds the settings and how many shards are committed:
|
|
113
|
-
|
|
114
|
-
```json
|
|
115
|
-
{"keyed": true, "compression": "lz4", "shards": 2}
|
|
116
|
-
```
|
|
117
|
-
|
|
118
|
-
Each `.idx` lists its shard's records as little-endian `uint32` sizes: `key_size,
|
|
119
|
-
value_size` pairs when keyed, one stored-value size per record when keyless. Keyed
|
|
120
|
-
shards are `key || value || key || value ...`; keyless shards only contain values.
|
|
121
|
-
There are no record headers or padding. Values are independent LZ4 blocks using
|
|
122
|
-
high-compression mode, with the original size in each block. Empty values stay empty.
|
|
123
|
-
|
|
124
|
-
Rewriting the manifest commits an `extend`, so shard files it doesn't list are
|
|
125
|
-
leftovers from an interrupted `extend`; they're ignored and overwritten later. When
|
|
126
|
-
copying a dataset, copy the manifest last.
|
|
127
|
-
|
|
128
|
-
Keyed datasets copy keys into a Python dict in each process that opens them (e.g. every
|
|
129
|
-
DataLoader worker). Each key and stored value is limited to `2**32 - 1` bytes; with LZ4,
|
|
130
|
-
uncompressed values are also limited to about 2 GiB, the LZ4 block limit.
|
|
131
|
-
|
|
132
|
-
## Missing stuff
|
|
133
|
-
|
|
134
|
-
No delete, overwrite, compaction, or concurrent writers. Also not actually an
|
|
135
|
-
implementation of the Bitcask paper, though per-shard indexes are close to its hint
|
|
136
|
-
files.
|
|
137
|
-
|
|
138
|
-
## Dev
|
|
139
|
-
|
|
140
|
-
```bash
|
|
141
|
-
uv sync
|
|
142
|
-
make format
|
|
143
|
-
make lint
|
|
144
|
-
make test
|
|
145
|
-
```
|
bitcask_dataset-0.2.0/README.md
DELETED
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
# bitcask-dataset
|
|
2
|
-
|
|
3
|
-
A disk-backed blob store that's a good fit if:
|
|
4
|
-
|
|
5
|
-
- you mostly do random-access reads, e.g. ML training data,
|
|
6
|
-
- values are biggish blobs (KiB to MiB) and keys are small,
|
|
7
|
-
- you add data in batches and never update or delete.
|
|
8
|
-
|
|
9
|
-
Each `extend` writes new shard files. Opening a dataset mmaps the shards as one logical
|
|
10
|
-
file, so lookups are fast and efficient.
|
|
11
|
-
Values are optionally LZ4-compressed (on by default).
|
|
12
|
-
|
|
13
|
-
Because old shards never change, keeping a copy elsewhere (say, S3) after an `extend`
|
|
14
|
-
means uploading only the new shards and the manifest, and never the whole dataset.
|
|
15
|
-
|
|
16
|
-
Not a good fit if you need deletes, overwrites, concurrent writers, or more keys
|
|
17
|
-
than what your RAM can fit.
|
|
18
|
-
|
|
19
|
-
## Install
|
|
20
|
-
|
|
21
|
-
```bash
|
|
22
|
-
pip install bitcask-dataset
|
|
23
|
-
```
|
|
24
|
-
|
|
25
|
-
## Example
|
|
26
|
-
|
|
27
|
-
```python
|
|
28
|
-
from bitcask_dataset import BitcaskDataset
|
|
29
|
-
|
|
30
|
-
ds = BitcaskDataset.create("my-dataset", keyed=True)
|
|
31
|
-
ds.extend([
|
|
32
|
-
(b"image/1", image_bytes),
|
|
33
|
-
(b"image/2", more_image_bytes),
|
|
34
|
-
])
|
|
35
|
-
|
|
36
|
-
ds = BitcaskDataset("my-dataset") # settings come from the manifest
|
|
37
|
-
value = ds[b"image/1"] # read-only memoryview of the decompressed bytes
|
|
38
|
-
|
|
39
|
-
# insertion order is available too
|
|
40
|
-
value = ds.at(0)
|
|
41
|
-
key = ds.key_at(0)
|
|
42
|
-
|
|
43
|
-
len(ds)
|
|
44
|
-
b"image/1" in ds
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
Keys are optional:
|
|
48
|
-
|
|
49
|
-
```python
|
|
50
|
-
ds = BitcaskDataset.create("my-keyless-dataset")
|
|
51
|
-
ds.extend([image_bytes, more_image_bytes])
|
|
52
|
-
value = ds.at(0)
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
Keys and values can be any C-contiguous buffer (`bytes`, `bytearray`, a contiguous NumPy
|
|
56
|
-
array, ...).
|
|
57
|
-
|
|
58
|
-
## API
|
|
59
|
-
|
|
60
|
-
```python
|
|
61
|
-
BitcaskDataset.create(path, keyed=False, compression="lz4")
|
|
62
|
-
BitcaskDataset(path)
|
|
63
|
-
ds.extend(entries, max_shard_size=1 << 30)
|
|
64
|
-
ds[key]
|
|
65
|
-
ds.at(index)
|
|
66
|
-
ds.key_at(index)
|
|
67
|
-
len(ds)
|
|
68
|
-
key in ds
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
`create` needs a missing or empty directory. `BitcaskDataset(path)` opens an existing
|
|
72
|
-
dataset; `keyed` and `compression` are fixed at creation and read from the manifest.
|
|
73
|
-
|
|
74
|
-
In keyed mode, `extend` takes `(key, value)` pairs and duplicate keys are errors. In
|
|
75
|
-
keyless mode it takes values directly. Each `extend` is all-or-nothing and only
|
|
76
|
-
creates new shards. Records aren't split, so a record over `max_shard_size` gets a
|
|
77
|
-
shard to itself. Values are compressed on a thread pool, so don't modify a value's
|
|
78
|
-
buffer after yielding it to `extend`.
|
|
79
|
-
|
|
80
|
-
Keys and values come back as read-only `memoryview`s, which `np.frombuffer` and
|
|
81
|
-
`torch.frombuffer` can use without copying. Non-empty compressed values are views of
|
|
82
|
-
freshly decompressed `bytes` (`value.obj`). With `compression=None`, values are read
|
|
83
|
-
straight from the mmap with zero copies. Keys are always uncompressed. `at` and `key_at`
|
|
84
|
-
support negative indexes. Key lookup and `key_at` raise `TypeError` in keyless mode.
|
|
85
|
-
|
|
86
|
-
## Format
|
|
87
|
-
|
|
88
|
-
```text
|
|
89
|
-
my-dataset/
|
|
90
|
-
manifest.json
|
|
91
|
-
000000.bin
|
|
92
|
-
000000.idx
|
|
93
|
-
000001.bin
|
|
94
|
-
000001.idx
|
|
95
|
-
```
|
|
96
|
-
|
|
97
|
-
`manifest.json` holds the settings and how many shards are committed:
|
|
98
|
-
|
|
99
|
-
```json
|
|
100
|
-
{"keyed": true, "compression": "lz4", "shards": 2}
|
|
101
|
-
```
|
|
102
|
-
|
|
103
|
-
Each `.idx` lists its shard's records as little-endian `uint32` sizes: `key_size,
|
|
104
|
-
value_size` pairs when keyed, one stored-value size per record when keyless. Keyed
|
|
105
|
-
shards are `key || value || key || value ...`; keyless shards only contain values.
|
|
106
|
-
There are no record headers or padding. Values are independent LZ4 blocks using
|
|
107
|
-
high-compression mode, with the original size in each block. Empty values stay empty.
|
|
108
|
-
|
|
109
|
-
Rewriting the manifest commits an `extend`, so shard files it doesn't list are
|
|
110
|
-
leftovers from an interrupted `extend`; they're ignored and overwritten later. When
|
|
111
|
-
copying a dataset, copy the manifest last.
|
|
112
|
-
|
|
113
|
-
Keyed datasets copy keys into a Python dict in each process that opens them (e.g. every
|
|
114
|
-
DataLoader worker). Each key and stored value is limited to `2**32 - 1` bytes; with LZ4,
|
|
115
|
-
uncompressed values are also limited to about 2 GiB, the LZ4 block limit.
|
|
116
|
-
|
|
117
|
-
## Missing stuff
|
|
118
|
-
|
|
119
|
-
No delete, overwrite, compaction, or concurrent writers. Also not actually an
|
|
120
|
-
implementation of the Bitcask paper, though per-shard indexes are close to its hint
|
|
121
|
-
files.
|
|
122
|
-
|
|
123
|
-
## Dev
|
|
124
|
-
|
|
125
|
-
```bash
|
|
126
|
-
uv sync
|
|
127
|
-
make format
|
|
128
|
-
make lint
|
|
129
|
-
make test
|
|
130
|
-
```
|
|
File without changes
|
|
File without changes
|