"""Reproduce the illustrative cluster-allocation table; no disk or network I/O.

This simplified model excludes resident, sparse and compressed files, metadata,
deduplication and filesystem-specific behavior. It is not a storage benchmark.
"""
import csv
import sys

SIZES = (1, 1000, 4096, 4097)
CLUSTERS = (4096, 65536)


def allocated(size, cluster):
    if not isinstance(size, int) or not isinstance(cluster, int) or size < 0 or cluster < 1:
        raise ValueError("Use nonnegative file bytes and a positive cluster size.")
    return ((size + cluster - 1) // cluster) * cluster


def rows():
    return [
        {"file_bytes": size, "cluster_bytes": cluster,
         "allocated_bytes": allocated(size, cluster),
         "slack_bytes": allocated(size, cluster) - size}
        for cluster in CLUSTERS for size in SIZES
    ]


if __name__ == "__main__":
    writer = csv.DictWriter(sys.stdout, fieldnames=(
        "file_bytes", "cluster_bytes", "allocated_bytes", "slack_bytes"),
        lineterminator="\n")
    writer.writeheader()
    writer.writerows(rows())
