Efficient range reservoir sampling from massive sorted key-value datasets.
Project description
kh57
Efficient range reservoir sampling from massive sorted key-value datasets. Deterministic, stable, range-friendly, low read amplification. Cython-accelerated hot inner loop (siphash + encoding) with a small Python surface.
Credits: the algorithm was designed by Karen Hambardzumyan (mahnerak) in 2023.
The problem
You have a huge sorted-by-integer-key dataset, potentially trillions of items on disk. You want n uniformly-sampled (key, value) pairs from a sub-range [begin, end) without pulling the whole thing. Naive every-Nth sampling is biased. Random shuffling on disk kills range access. You want both: uniformity and efficient range reads.
kh57 gives you:
- deterministic - same keys, same salt, same sample.
- stable - appending new keys outside the queried range does not change which keys inside get sampled.
- range-friendly - scan only what the query needs.
- low read amplification - total reads stay within ~2x of
n.
How it works
Each key is hashed with SipHash-2-4 (uniform 64-bit output). The bit_length of that hash becomes the key's "level": level 63 holds roughly half the keys, level 62 a quarter, and so on down. Every item is stored under a compound sort key (level << 57) | key - top 7 bits are the level id, bottom 57 bits are the original key. This preserves original key order within a level, and level order across levels.
To sample from [begin, end): walk levels from sparsest to densest, range_scan each level's slice, take full levels while they fit the quota, reservoir-sample the boundary level for the remainder, stop. Each level is a deterministic uniform subset of the range, so the union is a uniform sample.
Install
pip install kh57
Requires Python 3.12+.
Usage
from kh57 import kh57, sample, MemBackend
backend = MemBackend()
for key in range(1_000_000):
encoded = kh57(key).to_bytes(8, "big")
backend.put(encoded, str(key).encode())
# 500 uniform samples from the [100_000, 200_000) range
result = sample(backend, 500, begin=100_000, end=200_000)
Any sorted-by-bytes key-value store can be a backend - just implement the Backend protocol (get, put, delete, range_scan). MemBackend is the reference in-memory adapter; RocksDB / LMDB adapters can be added out-of-tree.
Public API
kh57(key: int) -> int- encode a 57-bit non-negative key into a 64-bit sort key.recover(h: int) -> tuple[int, int]- inverse, returns(level, key).uniform_hash(key: int) -> int- SipHash-2-4 with the default salt.sample(backend, n, begin=None, end=None, *, rng=None) -> list[tuple[int, bytes]].Backend- Protocol.MemBackend- in-memory reference implementation.
Dev
make install # create venv + install with dev,test extras
make build # build cython extensions in-place
make test # run tests
make lint # ruff check
make format # ruff format + fix
make wheels # build manylinux wheels via docker
License
Apache-2.0
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file kh57-0.1.2.tar.gz.
File metadata
- Download URL: kh57-0.1.2.tar.gz
- Upload date:
- Size: 20.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d693c67565c8da20376ad914bb4110beba3747193fd8e681c37a57a0e03e0317
|
|
| MD5 |
75d419fa10f4486eb85a2a7f11465945
|
|
| BLAKE2b-256 |
d8a6a4bbead7fd07d56af837077418a9e7e584838027b9e3783fe4c41585755d
|
Provenance
The following attestation bundles were made for kh57-0.1.2.tar.gz:
Publisher:
publish.yml on nustackdev/kh57
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
kh57-0.1.2.tar.gz -
Subject digest:
d693c67565c8da20376ad914bb4110beba3747193fd8e681c37a57a0e03e0317 - Sigstore transparency entry: 2148687572
- Sigstore integration time:
-
Permalink:
nustackdev/kh57@b42e16ef7e560e589c81a1644cfdd7739352702d -
Branch / Tag:
refs/heads/main - Owner: https://github.com/nustackdev
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b42e16ef7e560e589c81a1644cfdd7739352702d -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file kh57-0.1.2-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.
File metadata
- Download URL: kh57-0.1.2-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
- Upload date:
- Size: 1.2 MB
- Tags: CPython 3.14, manylinux: glibc 2.17+ x86-64, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9ff8f23ce75a7324e16f01eba6766019413c35c1939b395708bf3b874e7cf904
|
|
| MD5 |
d627089dd3216b917ab53e9f1728541a
|
|
| BLAKE2b-256 |
75fff6387c3cd78c8c6d81f3d967daf3ff0b17eeca5fb28caaa17b911b4b4415
|
Provenance
The following attestation bundles were made for kh57-0.1.2-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl:
Publisher:
publish.yml on nustackdev/kh57
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
kh57-0.1.2-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl -
Subject digest:
9ff8f23ce75a7324e16f01eba6766019413c35c1939b395708bf3b874e7cf904 - Sigstore transparency entry: 2148687914
- Sigstore integration time:
-
Permalink:
nustackdev/kh57@b42e16ef7e560e589c81a1644cfdd7739352702d -
Branch / Tag:
refs/heads/main - Owner: https://github.com/nustackdev
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b42e16ef7e560e589c81a1644cfdd7739352702d -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file kh57-0.1.2-cp314-cp314-macosx_11_0_arm64.whl.
File metadata
- Download URL: kh57-0.1.2-cp314-cp314-macosx_11_0_arm64.whl
- Upload date:
- Size: 190.4 kB
- Tags: CPython 3.14, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f9f1eeaf27963efed085fefc70e79dafb1c3802f00c6c03dbf606b2b856edae4
|
|
| MD5 |
922d00ddc812e68bfd25a2f0b51bc151
|
|
| BLAKE2b-256 |
9534f75a8e08f01780ef33eba3fca34351f8456ca0abf6f7347c13d0213a8f9e
|
Provenance
The following attestation bundles were made for kh57-0.1.2-cp314-cp314-macosx_11_0_arm64.whl:
Publisher:
publish.yml on nustackdev/kh57
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
kh57-0.1.2-cp314-cp314-macosx_11_0_arm64.whl -
Subject digest:
f9f1eeaf27963efed085fefc70e79dafb1c3802f00c6c03dbf606b2b856edae4 - Sigstore transparency entry: 2148689968
- Sigstore integration time:
-
Permalink:
nustackdev/kh57@b42e16ef7e560e589c81a1644cfdd7739352702d -
Branch / Tag:
refs/heads/main - Owner: https://github.com/nustackdev
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b42e16ef7e560e589c81a1644cfdd7739352702d -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file kh57-0.1.2-cp314-cp314-macosx_10_15_x86_64.whl.
File metadata
- Download URL: kh57-0.1.2-cp314-cp314-macosx_10_15_x86_64.whl
- Upload date:
- Size: 192.7 kB
- Tags: CPython 3.14, macOS 10.15+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4dde072c18523970d3bb13b5a87759794e5838360c9749a6cfa4b95a3d58e78b
|
|
| MD5 |
31c8caf588eabba934a73ae48b0d6a25
|
|
| BLAKE2b-256 |
818fd658c1f9ebb012b8a95f71e89c938701c34b60eee6d7d5882e0a3daf439d
|
Provenance
The following attestation bundles were made for kh57-0.1.2-cp314-cp314-macosx_10_15_x86_64.whl:
Publisher:
publish.yml on nustackdev/kh57
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
kh57-0.1.2-cp314-cp314-macosx_10_15_x86_64.whl -
Subject digest:
4dde072c18523970d3bb13b5a87759794e5838360c9749a6cfa4b95a3d58e78b - Sigstore transparency entry: 2148689823
- Sigstore integration time:
-
Permalink:
nustackdev/kh57@b42e16ef7e560e589c81a1644cfdd7739352702d -
Branch / Tag:
refs/heads/main - Owner: https://github.com/nustackdev
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b42e16ef7e560e589c81a1644cfdd7739352702d -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file kh57-0.1.2-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.
File metadata
- Download URL: kh57-0.1.2-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
- Upload date:
- Size: 1.2 MB
- Tags: CPython 3.13, manylinux: glibc 2.17+ x86-64, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
42a9ea2da2e3c2fa4e2748a3636db8689e1326298aff7b5131a29e4f541c59f0
|
|
| MD5 |
847d3b6f526347701e9b242c80f72203
|
|
| BLAKE2b-256 |
0d67050e50232293576e88eff468a40a5cf18154b598c5d2c370f31f139f884f
|
Provenance
The following attestation bundles were made for kh57-0.1.2-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl:
Publisher:
publish.yml on nustackdev/kh57
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
kh57-0.1.2-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl -
Subject digest:
42a9ea2da2e3c2fa4e2748a3636db8689e1326298aff7b5131a29e4f541c59f0 - Sigstore transparency entry: 2148688855
- Sigstore integration time:
-
Permalink:
nustackdev/kh57@b42e16ef7e560e589c81a1644cfdd7739352702d -
Branch / Tag:
refs/heads/main - Owner: https://github.com/nustackdev
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b42e16ef7e560e589c81a1644cfdd7739352702d -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file kh57-0.1.2-cp313-cp313-macosx_11_0_arm64.whl.
File metadata
- Download URL: kh57-0.1.2-cp313-cp313-macosx_11_0_arm64.whl
- Upload date:
- Size: 188.7 kB
- Tags: CPython 3.13, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b1535a5b26658364dbe6a63596a192c9ac75d2977a2168618fe0608031a7fa6b
|
|
| MD5 |
694a1ddc8d93c8afc85829db51e26e98
|
|
| BLAKE2b-256 |
7af6de416907c08c0752c4fee0588bd8e034554a19b35489c7efce653496377e
|
Provenance
The following attestation bundles were made for kh57-0.1.2-cp313-cp313-macosx_11_0_arm64.whl:
Publisher:
publish.yml on nustackdev/kh57
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
kh57-0.1.2-cp313-cp313-macosx_11_0_arm64.whl -
Subject digest:
b1535a5b26658364dbe6a63596a192c9ac75d2977a2168618fe0608031a7fa6b - Sigstore transparency entry: 2148688323
- Sigstore integration time:
-
Permalink:
nustackdev/kh57@b42e16ef7e560e589c81a1644cfdd7739352702d -
Branch / Tag:
refs/heads/main - Owner: https://github.com/nustackdev
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b42e16ef7e560e589c81a1644cfdd7739352702d -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file kh57-0.1.2-cp313-cp313-macosx_10_14_x86_64.whl.
File metadata
- Download URL: kh57-0.1.2-cp313-cp313-macosx_10_14_x86_64.whl
- Upload date:
- Size: 192.1 kB
- Tags: CPython 3.13, macOS 10.14+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ec585af54ee6a351bacd9bca9e34df9e2f9575750e4fca9be7b9f69978da3ecd
|
|
| MD5 |
93b3ceb4a3aa2e199e23ee4e22e0c57a
|
|
| BLAKE2b-256 |
e704db84f3903d62b5af08b0c33971358ad3b17f4eeed3cd89c31d87ced29596
|
Provenance
The following attestation bundles were made for kh57-0.1.2-cp313-cp313-macosx_10_14_x86_64.whl:
Publisher:
publish.yml on nustackdev/kh57
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
kh57-0.1.2-cp313-cp313-macosx_10_14_x86_64.whl -
Subject digest:
ec585af54ee6a351bacd9bca9e34df9e2f9575750e4fca9be7b9f69978da3ecd - Sigstore transparency entry: 2148689502
- Sigstore integration time:
-
Permalink:
nustackdev/kh57@b42e16ef7e560e589c81a1644cfdd7739352702d -
Branch / Tag:
refs/heads/main - Owner: https://github.com/nustackdev
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b42e16ef7e560e589c81a1644cfdd7739352702d -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file kh57-0.1.2-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.
File metadata
- Download URL: kh57-0.1.2-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
- Upload date:
- Size: 1.2 MB
- Tags: CPython 3.12, manylinux: glibc 2.17+ x86-64, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e9d3dab8495b52c7f4abda3da860912bb3b971ceb6fa27c7224dd9fff760bb09
|
|
| MD5 |
8e3499798d4d3631144ebf866fde8658
|
|
| BLAKE2b-256 |
fe4ee36dca35f21ddf9032c06d8fcf2375ea43b4a30f86ed2b59bdb65a72ddaa
|
Provenance
The following attestation bundles were made for kh57-0.1.2-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl:
Publisher:
publish.yml on nustackdev/kh57
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
kh57-0.1.2-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl -
Subject digest:
e9d3dab8495b52c7f4abda3da860912bb3b971ceb6fa27c7224dd9fff760bb09 - Sigstore transparency entry: 2148688611
- Sigstore integration time:
-
Permalink:
nustackdev/kh57@b42e16ef7e560e589c81a1644cfdd7739352702d -
Branch / Tag:
refs/heads/main - Owner: https://github.com/nustackdev
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b42e16ef7e560e589c81a1644cfdd7739352702d -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file kh57-0.1.2-cp312-cp312-macosx_11_0_arm64.whl.
File metadata
- Download URL: kh57-0.1.2-cp312-cp312-macosx_11_0_arm64.whl
- Upload date:
- Size: 190.6 kB
- Tags: CPython 3.12, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c556f1b8e7f3acb50925d236ebd5a705f7f7dfa4e7cb4e3a83ec29a5c6d11a1e
|
|
| MD5 |
75d3428378211d77099e86cfe9ef1b6e
|
|
| BLAKE2b-256 |
cf7902e76103ca0a6239c4927cb21ed8cf76b1f570a75d91f9af62b033a0ab3c
|
Provenance
The following attestation bundles were made for kh57-0.1.2-cp312-cp312-macosx_11_0_arm64.whl:
Publisher:
publish.yml on nustackdev/kh57
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
kh57-0.1.2-cp312-cp312-macosx_11_0_arm64.whl -
Subject digest:
c556f1b8e7f3acb50925d236ebd5a705f7f7dfa4e7cb4e3a83ec29a5c6d11a1e - Sigstore transparency entry: 2148690257
- Sigstore integration time:
-
Permalink:
nustackdev/kh57@b42e16ef7e560e589c81a1644cfdd7739352702d -
Branch / Tag:
refs/heads/main - Owner: https://github.com/nustackdev
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b42e16ef7e560e589c81a1644cfdd7739352702d -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file kh57-0.1.2-cp312-cp312-macosx_10_14_x86_64.whl.
File metadata
- Download URL: kh57-0.1.2-cp312-cp312-macosx_10_14_x86_64.whl
- Upload date:
- Size: 194.0 kB
- Tags: CPython 3.12, macOS 10.14+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
11f423a906d83c08c7a00a65c81bc942bbe263cda073eb56b4adaf7d689ae814
|
|
| MD5 |
11fbeb2481f08aa3141e8f1398e6e189
|
|
| BLAKE2b-256 |
a36f69772a92e490ce4e4ebbb287599acbd278d04c6fac66f7aaf956d5088093
|
Provenance
The following attestation bundles were made for kh57-0.1.2-cp312-cp312-macosx_10_14_x86_64.whl:
Publisher:
publish.yml on nustackdev/kh57
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
kh57-0.1.2-cp312-cp312-macosx_10_14_x86_64.whl -
Subject digest:
11f423a906d83c08c7a00a65c81bc942bbe263cda073eb56b4adaf7d689ae814 - Sigstore transparency entry: 2148688097
- Sigstore integration time:
-
Permalink:
nustackdev/kh57@b42e16ef7e560e589c81a1644cfdd7739352702d -
Branch / Tag:
refs/heads/main - Owner: https://github.com/nustackdev
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b42e16ef7e560e589c81a1644cfdd7739352702d -
Trigger Event:
workflow_dispatch
-
Statement type: