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.0.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.0-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.0-cp313-cp313-macosx_11_0_arm64.whl (188.7 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.13macOS 10.14+ x86-64

kh57-0.1.0-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.0-cp312-cp312-macosx_11_0_arm64.whl (190.6 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

kh57-0.1.0-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.0.tar.gz.

File metadata

  • Download URL: kh57-0.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 cc0e4fe1ac70af9d00ffb7ef1e9322a5457fc40615604672740edc6c56c79b43
MD5 6d9c0e0806947cc087fe3fac1e963dc9
BLAKE2b-256 dd2acc8cc5bd822dc062295aed40dcf01b7ed9e6d5c12c48ac57e0da5c88dfa4

See more details on using hashes here.

Provenance

The following attestation bundles were made for kh57-0.1.0.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.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for kh57-0.1.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 be280f2c438addc392c9159339fa395423cd37c0e1fcb4c198af3492db2d459c
MD5 ddcd770d7f6050c1229b919117810992
BLAKE2b-256 af9aebfaa8e4c645ac8c2d586d211d5cf214fe1134557dabdc17bcada841ac93

See more details on using hashes here.

Provenance

The following attestation bundles were made for kh57-0.1.0-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.0-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

  • Download URL: kh57-0.1.0-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.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7bc303ae6d806afec81f6eb6e35abd61cb4cb1f958583008cc0c3f5c51280b2d
MD5 15653bd7d41434d224ed7f76858ed56e
BLAKE2b-256 6fcf8c049289af1ee3cc7ee3633b6cb83c8207760ffcdb4c0e61c10a02bdeea2

See more details on using hashes here.

Provenance

The following attestation bundles were made for kh57-0.1.0-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.0-cp313-cp313-macosx_10_14_x86_64.whl.

File metadata

File hashes

Hashes for kh57-0.1.0-cp313-cp313-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 5ae215a7429ed5aea2b27d96e3c85a7e07d5ae89892dcd48a842006e9bde3ee7
MD5 77847ebbe2422fcd6e983693ebdf90e3
BLAKE2b-256 d043a68cfb27704e87f78f53457b49dda1d906fc7355fbde241ca3845546db17

See more details on using hashes here.

Provenance

The following attestation bundles were made for kh57-0.1.0-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.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for kh57-0.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 70ceefa3b750ab63db593d9425f05d895232d4e5da9097bc447369e3bf473c89
MD5 ddab30ee9207f0079c7ee7657e5af13f
BLAKE2b-256 ee59852f1f1f36ad49c1579509210fc1792554cede1efb71e7b8b8334a6cd462

See more details on using hashes here.

Provenance

The following attestation bundles were made for kh57-0.1.0-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.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

  • Download URL: kh57-0.1.0-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.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3574a89ff1650ed82bb871b3f5ba396c63abfc26d0fc975abfe0b0f85197fc55
MD5 0ede5b312fade14448508ce08e815778
BLAKE2b-256 30f7d49f222fe8c938c8aa129030504c7102d158419d638f9aec6140724777e5

See more details on using hashes here.

Provenance

The following attestation bundles were made for kh57-0.1.0-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.0-cp312-cp312-macosx_10_14_x86_64.whl.

File metadata

File hashes

Hashes for kh57-0.1.0-cp312-cp312-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 be460c6080475fb6e916805bc59e071489c403ae0292b479bdde99ae7b57ba60
MD5 6fe183190d2c54fe735b74365a368be7
BLAKE2b-256 5d8081e15f4f19e6535452048bcdc4410c9b78e0fe6689dbf509bbf45aa935f2

See more details on using hashes here.

Provenance

The following attestation bundles were made for kh57-0.1.0-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