Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kh57-0.1.1.tar.gz (20.2 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

kh57-0.1.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

kh57-0.1.1-cp313-cp313-macosx_11_0_arm64.whl (188.7 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

kh57-0.1.1-cp313-cp313-macosx_10_14_x86_64.whl (192.1 kB view details)

Uploaded CPython 3.13macOS 10.14+ x86-64

kh57-0.1.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

kh57-0.1.1-cp312-cp312-macosx_11_0_arm64.whl (190.6 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

kh57-0.1.1-cp312-cp312-macosx_10_14_x86_64.whl (194.0 kB view details)

Uploaded CPython 3.12macOS 10.14+ x86-64

File details

Details for the file kh57-0.1.1.tar.gz.

File metadata

  • Download URL: kh57-0.1.1.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

Hashes for kh57-0.1.1.tar.gz
Algorithm Hash digest
SHA256 33e99a235efcf5f3dd87ce70587f9206012147021387d4e7ab444f31bc2d7d5c
MD5 7e6c2e11e8468f43c6194568aa3afee9
BLAKE2b-256 6eb381a01820629c09af9c249fdb931f20741c74f1ef61fe4308efa7824173ca

See more details on using hashes here.

Provenance

The following attestation bundles were made for kh57-0.1.1.tar.gz:

Publisher: publish.yml on nustackdev/kh57

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file kh57-0.1.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for kh57-0.1.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5ae8bd08608c98b1a40d132fe2b78fa2ec3ae1b09c42be89564c8405f849b8a3
MD5 002170a12e9933aaedc56f03703bfddf
BLAKE2b-256 0974769c66dcb8744983b9e6e1f0c79d8856f11a38c10a3411485e31b5e84706

See more details on using hashes here.

Provenance

The following attestation bundles were made for kh57-0.1.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: publish.yml on nustackdev/kh57

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file kh57-0.1.1-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

  • Download URL: kh57-0.1.1-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

Hashes for kh57-0.1.1-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 009fc349db85a599bfb84ef6fea4cc8cb8f27058e0c14988eaf3730196b1a175
MD5 f08747fd5fe4f79a4ed71e84abe70c2b
BLAKE2b-256 bac762da178fc238fcd3ccd7e3e9f95312c37656b291556bc05b4d50061a667f

See more details on using hashes here.

Provenance

The following attestation bundles were made for kh57-0.1.1-cp313-cp313-macosx_11_0_arm64.whl:

Publisher: publish.yml on nustackdev/kh57

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file kh57-0.1.1-cp313-cp313-macosx_10_14_x86_64.whl.

File metadata

File hashes

Hashes for kh57-0.1.1-cp313-cp313-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 dee302c95c8f128f717f3bcca999a95aa4233c1452209e22435a2326a70dab7d
MD5 3d102aec2bbe38334ea0a7e2ecbb83c5
BLAKE2b-256 ef00038f94fd2114412af2814aabd049ac0ce5b79a57f6e8bcbe5a16198ddc7a

See more details on using hashes here.

Provenance

The following attestation bundles were made for kh57-0.1.1-cp313-cp313-macosx_10_14_x86_64.whl:

Publisher: publish.yml on nustackdev/kh57

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file kh57-0.1.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for kh57-0.1.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 bd209f5cfed57bfe1bf29eed286c057a6a9d40a0cbc93db959540e34dd3d6977
MD5 c6432d10e45b195a254bc1a3bc063bde
BLAKE2b-256 50d2440af3f4b7e1a75b1af7db38bb78326021ec4699c89003eecea1756d1075

See more details on using hashes here.

Provenance

The following attestation bundles were made for kh57-0.1.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: publish.yml on nustackdev/kh57

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file kh57-0.1.1-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

  • Download URL: kh57-0.1.1-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

Hashes for kh57-0.1.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c12b1fa581741e9cbe7d3b8dfaff6e2dc4c33198ac66ca9b149d420cebc67d46
MD5 299b833729151b945621e2eec6960840
BLAKE2b-256 26ff447767366a3bc082ec3cdf0b57f23d4d4771e5020bb18e5c1447e289feb7

See more details on using hashes here.

Provenance

The following attestation bundles were made for kh57-0.1.1-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: publish.yml on nustackdev/kh57

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file kh57-0.1.1-cp312-cp312-macosx_10_14_x86_64.whl.

File metadata

File hashes

Hashes for kh57-0.1.1-cp312-cp312-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 bac5bd8d7929f13c88394205ea613c0b067fbc27b8f99fa1b369748f9c39d278
MD5 fff78e3206cf32919664cec327787b4c
BLAKE2b-256 323d8a46715b9919427c96bd010573d54f86f63787d32ec37cee09687750e2dc

See more details on using hashes here.

Provenance

The following attestation bundles were made for kh57-0.1.1-cp312-cp312-macosx_10_14_x86_64.whl:

Publisher: publish.yml on nustackdev/kh57

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page