tool ·kh57

Uniform samples from a trillion sorted rows.

Pull n uniform samples from any range of a huge sorted key-value store. Deterministic, stable, range-friendly. Total reads stay within 2x of n.

The problem.

You have a sorted-by-key dataset. Billions of rows, maybe trillions. You want a uniform random slice of one range, right now, without loading the whole thing.

Every-Nth sampling is biased. A shuffled index destroys range reads. Loading the range first, then sampling, defeats the point. You want both: uniformity and cheap range access.

Niche. If it is your problem, kh57 is built for exactly that shape.

Four properties, free.

One hash per key, one compound sort key per row, one range walk per sample. Four properties fall out for free.

Deterministic

Same keys, same salt, same sample. Reproduce a report a year later with one line of code.

Stable under growth

Append new keys outside the queried range and the sample inside stays identical. No re-sampling to explain.

Range-friendly

Scan only what the query needs. Ask for 500 samples from a 100k slice, touch on the order of 1000 rows.

Any sorted-KV backend

Implement get, put, delete, range_scan. Done. RocksDB, LMDB, or a dict for tests.

Sample from a range.

Hash keys with kh57, put rows in any sorted-KV backend, then ask for n samples over a range.

Swap MemBackend for a RocksDB or LMDB adapter and the sample call does not change. The compound sort key (level, key) is what makes the range walk cheap.

sample.py
python · 9 locpy
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
rows = sample(backend, 500, begin=100_000, end=200_000)

How it works.

Hash each key with SipHash-2-4. The bit length of the hash is the key's level: half the keys land in level 63, a quarter in 62, and so on. Store each row under a compound sort key of (level, key).

To sample from a range: walk levels sparsest to densest. Take a full level while it fits the quota. Reservoir-sample the boundary level for the remainder. Stop.

Each level is a deterministic uniform subset of the range, so their union is a uniform sample. Read amplification stays within roughly 2x of n.

Algorithm by Karen Hambardzumyan (mahnerak), 2023.

Like what you see?

The project is young. Star it, join the room, watch what we ship next.