Bedrock.Encoding behaviour (bedrock v0.7.0)

View Source

How a Bedrock.Keyspace turns your keys and values into bytes, and back.

Bedrock itself stores binaries and nothing else — the repo API is binary in, binary out. An encoding is what lets you work in terms of tuples, numbers, or arbitrary Elixir terms above that: a keyspace packs through its :key_encoding before writing a key and through its :value_encoding before writing a value, and unpacks on the way back.

alias Bedrock.{Encoding, Keyspace}

balances =
  "app"
  |> Keyspace.new(key_encoding: Encoding.Tuple)
  |> Keyspace.partition("balances", value_encoding: Encoding.BERT)

Choosing one

EncodingAcceptsOrder-preservingUse for
Bedrock.Encoding.Tupletuples, lists, binaries, integers, floats, nilyeskeys
Bedrock.Encoding.Nonebinaries onlyyes (it is the identity)keys or values
Bedrock.Encoding.BERTany Elixir termnovalues

Order preservation is the property that matters for keys. A key encoding is order-preserving when the byte ordering of packed keys matches the logical ordering of the values they came from — which is exactly what makes a range read over a keyspace return what you expect. BERT does not have this property, so it belongs on :value_encoding.

A keyspace with no encoding set passes binaries through unchanged, the same as Bedrock.Encoding.None.

Implementing your own

Implement both callbacks such that unpack(pack(value)) == value. If the encoding is to be used for keys, it must also be order-preserving.

Summary

Callbacks

Encodes a value as a binary.

Decodes a binary produced by pack/1.

Callbacks

pack(value)

@callback pack(value :: any()) :: binary()

Encodes a value as a binary.

unpack(packed)

@callback unpack(packed :: binary()) :: any()

Decodes a binary produced by pack/1.