Crate storekey

source ·
Expand description

Binary encoding for Rust values which preserves lexicographic sort order. Order-preserving encoding is useful for creating keys for sorted key-value stores with byte string typed keys, such as leveldb and sled.

storekey is not a self-describing format. In other words, Type information is not serialized alongside values, and thus the type of serialized data must be known in order to perform deserialization.

Supported Data Types

storekey currently supports all Rust primitives, strings, options, structs, enums, vecs, and tuples. See Serializer for details on the serialization format.

Type Evolution

In general, the exact type of a serialized value must be known in order to correctly deserialize it. For structs and enums, the type is effectively frozen once any values of the type have been serialized: changes to the struct or enum will cause deserialization of already serialized values to fail or return incorrect values. The only exception is adding new variants to the end of an existing enum. Enum variants may not change type, be removed, or be reordered. All changes to structs, including adding, removing, reordering, or changing the type of a field are forbidden.

These restrictions lead to a few best-practices when using storekey serialization:

  • Don’t use storekey unless you need lexicographic ordering of serialized values! A more general encoding library such as Cap’n Proto or bincode will serve you better if this feature is not necessary.
  • If you persist serialized values for longer than the life of a process (i.e. you write the serialized values to a file or a database), consider using an enum as a top-level wrapper type. This will allow you to seamlessly add a new variant when you need to change the key format in a backwards-compatible manner (the different key types will sort seperately). If your enum has less than 16 variants, then the overhead is just a single byte in serialized output.

Re-exports

Modules