"""
Practice: splitting data across shards. Lab 10's logic for 01_partitioning.py (Exercise 1) and
02_shards.py, from partitioner.py and service/app.py: the remainder rule (modulo), a ring with
points per shard (consistent hashing), how many keys move when a shard is added, how uneven the
shards are, a hot counter split into rows, and what a query over every shard waits for.

Fill in the TODOs (h64 is given), then run the tests:
    pytest test_shard_practice.py        (locally)
or press "Run tests" on the chapter page.
"""
import bisect
import hashlib


def h64(key):
    """A stable 64-bit hash of str(key): BLAKE2b, as in the lab's partitioner.py. (Python's own
    hash() changes from process to process, so it can't decide where data lives.) Given."""
    return int.from_bytes(hashlib.blake2b(str(key).encode(), digest_size=8).digest(), "big")


def modulo_shard(key, shards):
    """The remainder rule: the shard at position h64(key) % len(shards)."""
    # TODO
    raise NotImplementedError


def share_that_stays(n_old, n_new):
    """Under the remainder rule, the share of hashes whose shard is the same with n_old and with
    n_new shards. The remainders repeat every n_old * n_new numbers (4 × 5 = 20), so counting
    0 .. n_old * n_new - 1 is exact."""
    # TODO
    raise NotImplementedError


def build_ring(shards, vnodes):
    """The ring: each shard gets `vnodes` points, at h64(f"{shard}#{i}") for i in 0 .. vnodes-1.
    Return a list of (point, shard), sorted by point."""
    # TODO
    raise NotImplementedError


def ring_shard(ring, key):
    """The shard of the first point after h64(key), going clockwise; past the last point, wrap
    round to the first. (A key exactly on a point belongs to the next one, as in the lab.)"""
    # TODO
    raise NotImplementedError


def arc_shares(ring):
    """Each shard's share of the ring: a point owns the stretch from the previous point up to
    itself (the first point's stretch wraps round from the last). Return {shard: share}."""
    # TODO
    raise NotImplementedError


def moves(before, after, keys):
    """before and after are functions key -> shard. Return {(from, to): number of keys} for the
    keys whose shard changed."""
    # TODO
    raise NotImplementedError


def imbalance(counts):
    """Busiest ÷ average (1 = perfectly even). counts: {shard: keys or requests}."""
    # TODO
    raise NotImplementedError


def like_key(post, split, row):
    """The key a like is stored under. One row (split 1): str(post). Split K: f"{post}#{row}",
    where the router picks row at random from 0 .. K-1."""
    # TODO
    raise NotImplementedError


def shards_to_read(ring, post, split):
    """Reading the count adds up all the rows: the sorted list of shards that hold one."""
    # TODO
    raise NotImplementedError


def expected_like_shares(ring, post, split, hot_share):
    """Each shard's expected share of all likes: the hot post's hot_share spread over its rows
    (each row gets 1/split of it), plus the rest (1 - hot_share), spread like keys: by each
    shard's share of the ring. Return {shard: share}."""
    # TODO
    raise NotImplementedError


def scatter_gather_ms(shard_ms):
    """A query that asks every shard and merges the answers finishes when the last shard
    answers: its time, given each shard's time."""
    # TODO
    raise NotImplementedError
