ekv 1.0.0

Key-value database for embedded systems, for raw NOR flash, using an LSM-Tree
Documentation
import os

abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath)
os.chdir(dname)

features = []


def feature(name, default, min=None, max=None, pow2=None, vals=None, factors=[]):
    if vals is None:
        assert min is not None
        assert max is not None

        vals = set()
        val = min
        while val <= max:
            vals.add(val)
            for f in factors:
                if val*f <= max:
                    vals.add(val*f)
            if (pow2 == True or (isinstance(pow2, int) and val >= pow2)) and val > 0:
                val *= 2
            else:
                val += 1
        vals.add(default)
        vals = sorted(list(vals))

    features.append(
        {
            "name": name,
            "default": default,
            "vals": vals,
        }
    )


feature("align", default=4, vals=[1, 2, 4])
feature("page_size", default=4096, min=128, max=65536, pow2=True)
feature("max_page_count", default=256, min=1,
        max=65536, pow2=True, factors=[3, 5, 9])
feature("erase_value", default=0xFF, vals=[0x00, 0xFF])

feature("max_key_size", default=64, min=1, max=1024, pow2=True)
feature("max_value_size", default=1024, min=1, max=65536, pow2=True)

feature("scratch_page_count", default=4, min=0, max=65536, pow2=True)
feature("branching_factor", default=2, min=2, max=4)
feature("max_chunk_size", default=4096, vals=[128, 256, 512, 1024, 2048, 4096])

# ========= Update Cargo.toml

things = ""
for f in features:
    name = f["name"].replace("_", "-")
    for val in f["vals"]:
        things += f"{name}-{val} = []"
        if val == f["default"]:
            things += " # Default"
        things += "\n"
    things += "\n"

SEPARATOR_START = "# BEGIN AUTOGENERATED CONFIG FEATURES\n"
SEPARATOR_END = "# END AUTOGENERATED CONFIG FEATURES\n"
HELP = "# Generated by gen_config.py. DO NOT EDIT.\n"
with open("Cargo.toml", "r") as f:
    data = f.read()
before, data = data.split(SEPARATOR_START, maxsplit=1)
_, after = data.split(SEPARATOR_END, maxsplit=1)
data = before + SEPARATOR_START + HELP + things + SEPARATOR_END + after
with open("Cargo.toml", "w") as f:
    f.write(data)


# ========= Update build.rs

things = ""
for f in features:
    name = f["name"].upper()
    things += f'    ("{name}", {f["default"]}),\n'

SEPARATOR_START = "// BEGIN AUTOGENERATED CONFIG FEATURES\n"
SEPARATOR_END = "// END AUTOGENERATED CONFIG FEATURES\n"
HELP = "    // Generated by gen_config.py. DO NOT EDIT.\n"
with open("build.rs", "r") as f:
    data = f.read()
before, data = data.split(SEPARATOR_START, maxsplit=1)
_, after = data.split(SEPARATOR_END, maxsplit=1)
data = before + SEPARATOR_START + HELP + \
    things + "    " + SEPARATOR_END + after
with open("build.rs", "w") as f:
    f.write(data)