Bedrock.Encoding.Tuple (bedrock v0.7.0)

View Source

FoundationDB's tuple layer: an order-preserving encoding for structured keys.

This is the encoding to reach for when keys are composite — {"balances", account_id}, {user_id, timestamp} — because the byte ordering of packed keys matches the logical ordering of the values inside them. Sorting the encoded keys sorts the tuples, which is what makes a range read over a prefix return the rows you meant, in the order you meant.

iex> Bedrock.Encoding.Tuple.pack({"balances", 1}) <
...>   Bedrock.Encoding.Tuple.pack({"balances", 2})
true

iex> Bedrock.Encoding.Tuple.unpack(Bedrock.Encoding.Tuple.pack({"a", 1, nil}))
{"a", 1, nil}

Supported types

Binaries, integers (64-bit signed range), floats, nil, lists, and tuples, nested arbitrarily. Anything else raises ArgumentError on pack/1.

Integers are encoded by magnitude, smallest representation first, with negatives stored as complements so that the ordering property holds across zero. Null bytes inside binaries are escaped, so a binary containing 0x00 still sorts correctly against one that does not.

The wire format is FoundationDB's, so keys written here are readable by any other FDB tuple-layer implementation.

Summary

Functions

Callback implementation for Bedrock.Encoding.pack/1.

Converts a value to an iolist representation for packing.

Callback implementation for Bedrock.Encoding.unpack/1.

Unpacks a binary containing zero or more concatenated tuple-encoded values.

Functions

pack(unpacked)

@spec pack(nil | tuple() | list() | number() | binary()) :: binary()

Callback implementation for Bedrock.Encoding.pack/1.

to_iolist(unpacked, tail \\ [])

@spec to_iolist(nil | tuple() | list() | number() | binary(), iolist()) :: iolist()

Converts a value to an iolist representation for packing.

unpack(packed)

@spec unpack(packed :: binary()) :: nil | tuple() | list() | number() | binary()

Callback implementation for Bedrock.Encoding.unpack/1.

unpack_all(packed)

@spec unpack_all(packed :: binary()) :: [nil | tuple() | list() | number() | binary()]

Unpacks a binary containing zero or more concatenated tuple-encoded values.

Unlike unpack/1, which decodes exactly one value and raises on trailing data, this decodes the whole binary as a flat sequence of values (FDB tuple semantics, without a top-level nested-list wrapper) and returns them as a list.