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.2.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.2-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

kh57-0.1.2-cp314-cp314-macosx_11_0_arm64.whl (190.4 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

kh57-0.1.2-cp314-cp314-macosx_10_15_x86_64.whl (192.7 kB view details)

Uploaded CPython 3.14macOS 10.15+ x86-64

kh57-0.1.2-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

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

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.13macOS 10.14+ x86-64

kh57-0.1.2-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

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

Uploaded CPython 3.12macOS 11.0+ ARM64

kh57-0.1.2-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.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

Hashes for kh57-0.1.2.tar.gz
Algorithm Hash digest
SHA256 d693c67565c8da20376ad914bb4110beba3747193fd8e681c37a57a0e03e0317
MD5 75d419fa10f4486eb85a2a7f11465945
BLAKE2b-256 d8a6a4bbead7fd07d56af837077418a9e7e584838027b9e3783fe4c41585755d

See more details on using hashes here.

Provenance

The following attestation bundles were made for kh57-0.1.2.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.2-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for kh57-0.1.2-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 9ff8f23ce75a7324e16f01eba6766019413c35c1939b395708bf3b874e7cf904
MD5 d627089dd3216b917ab53e9f1728541a
BLAKE2b-256 75fff6387c3cd78c8c6d81f3d967daf3ff0b17eeca5fb28caaa17b911b4b4415

See more details on using hashes here.

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

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.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

Hashes for kh57-0.1.2-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f9f1eeaf27963efed085fefc70e79dafb1c3802f00c6c03dbf606b2b856edae4
MD5 922d00ddc812e68bfd25a2f0b51bc151
BLAKE2b-256 9534f75a8e08f01780ef33eba3fca34351f8456ca0abf6f7347c13d0213a8f9e

See more details on using hashes here.

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

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.2-cp314-cp314-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for kh57-0.1.2-cp314-cp314-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 4dde072c18523970d3bb13b5a87759794e5838360c9749a6cfa4b95a3d58e78b
MD5 31c8caf588eabba934a73ae48b0d6a25
BLAKE2b-256 818fd658c1f9ebb012b8a95f71e89c938701c34b60eee6d7d5882e0a3daf439d

See more details on using hashes here.

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

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.2-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for kh57-0.1.2-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 42a9ea2da2e3c2fa4e2748a3636db8689e1326298aff7b5131a29e4f541c59f0
MD5 847d3b6f526347701e9b242c80f72203
BLAKE2b-256 0d67050e50232293576e88eff468a40a5cf18154b598c5d2c370f31f139f884f

See more details on using hashes here.

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

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.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

Hashes for kh57-0.1.2-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b1535a5b26658364dbe6a63596a192c9ac75d2977a2168618fe0608031a7fa6b
MD5 694a1ddc8d93c8afc85829db51e26e98
BLAKE2b-256 7af6de416907c08c0752c4fee0588bd8e034554a19b35489c7efce653496377e

See more details on using hashes here.

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

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.2-cp313-cp313-macosx_10_14_x86_64.whl.

File metadata

File hashes

Hashes for kh57-0.1.2-cp313-cp313-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 ec585af54ee6a351bacd9bca9e34df9e2f9575750e4fca9be7b9f69978da3ecd
MD5 93b3ceb4a3aa2e199e23ee4e22e0c57a
BLAKE2b-256 e704db84f3903d62b5af08b0c33971358ad3b17f4eeed3cd89c31d87ced29596

See more details on using hashes here.

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

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.2-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for kh57-0.1.2-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e9d3dab8495b52c7f4abda3da860912bb3b971ceb6fa27c7224dd9fff760bb09
MD5 8e3499798d4d3631144ebf866fde8658
BLAKE2b-256 fe4ee36dca35f21ddf9032c06d8fcf2375ea43b4a30f86ed2b59bdb65a72ddaa

See more details on using hashes here.

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

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.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

Hashes for kh57-0.1.2-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c556f1b8e7f3acb50925d236ebd5a705f7f7dfa4e7cb4e3a83ec29a5c6d11a1e
MD5 75d3428378211d77099e86cfe9ef1b6e
BLAKE2b-256 cf7902e76103ca0a6239c4927cb21ed8cf76b1f570a75d91f9af62b033a0ab3c

See more details on using hashes here.

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

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.2-cp312-cp312-macosx_10_14_x86_64.whl.

File metadata

File hashes

Hashes for kh57-0.1.2-cp312-cp312-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 11f423a906d83c08c7a00a65c81bc942bbe263cda073eb56b4adaf7d689ae814
MD5 11fbeb2481f08aa3141e8f1398e6e189
BLAKE2b-256 a36f69772a92e490ce4e4ebbb287599acbd278d04c6fac66f7aaf956d5088093

See more details on using hashes here.

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

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