Trait mz_persist_types::Codec
source · pub trait Codec:
Default
+ Sized
+ PartialEq
+ 'static {
type Schema: Schema2<Self> + PartialEq;
type Storage: Default;
// Required methods
fn codec_name() -> String;
fn encode<B>(&self, buf: &mut B)
where B: BufMut;
fn decode<'a>(buf: &'a [u8], schema: &Self::Schema) -> Result<Self, String>;
fn encode_schema(schema: &Self::Schema) -> Bytes;
fn decode_schema(buf: &Bytes) -> Self::Schema;
// Provided methods
fn encode_to_vec(&self) -> Vec<u8> ⓘ { ... }
fn decode_from<'a>(
&mut self,
buf: &'a [u8],
_storage: &mut Option<Self::Storage>,
schema: &Self::Schema,
) -> Result<(), String> { ... }
fn validate(_val: &Self, _schema: &Self::Schema) -> Result<(), String> { ... }
}
Expand description
Encoding and decoding operations for a type usable as a persisted key or value.
Required Associated Types§
sourcetype Schema: Schema2<Self> + PartialEq
type Schema: Schema2<Self> + PartialEq
The type of the associated schema for Self.
This is a separate type because Row is not self-describing. For Row, you need a RelationDesc to determine the types of any columns that are Datum::Null.
sourcetype Storage: Default
type Storage: Default
A type used with Self::decode_from for allocation reuse. Set to ()
if unnecessary.
Required Methods§
sourcefn codec_name() -> String
fn codec_name() -> String
Name of the codec.
This name is stored for the key and value when a stream is first created and the same key and value codec must be used for that stream afterward.
sourcefn encode<B>(&self, buf: &mut B)where
B: BufMut,
fn encode<B>(&self, buf: &mut B)where
B: BufMut,
Encode a key or value for permanent storage.
This must perfectly round-trip Self through Codec::decode. If the encode function for this codec ever changes, decode must be able to handle bytes output by all previous versions of encode.
sourcefn decode<'a>(buf: &'a [u8], schema: &Self::Schema) -> Result<Self, String>
fn decode<'a>(buf: &'a [u8], schema: &Self::Schema) -> Result<Self, String>
Decode a key or value previous encoded with this codec’s Codec::encode.
This must perfectly round-trip Self through Codec::encode. If the encode function for this codec ever changes, decode must be able to handle bytes output by all previous versions of encode.
It should also gracefully handle data encoded by future versions of encode (likely with an error).
sourcefn encode_schema(schema: &Self::Schema) -> Bytes
fn encode_schema(schema: &Self::Schema) -> Bytes
Encode a schema for permanent storage.
This must perfectly round-trip the schema through Self::decode_schema. If the encode_schema function ever changes, decode_schema must be able to handle bytes output by all previous versions of encode_schema.
TODO: Move this to instead be a new trait that is required by Self::Schema?
sourcefn decode_schema(buf: &Bytes) -> Self::Schema
fn decode_schema(buf: &Bytes) -> Self::Schema
Decode a schema previous encoded with this codec’s Self::encode_schema.
This must perfectly round-trip the schema through Self::encode_schema. If the encode_schema function ever changes, decode_schema must be able to handle bytes output by all previous versions of encode_schema.
Provided Methods§
sourcefn encode_to_vec(&self) -> Vec<u8> ⓘ
fn encode_to_vec(&self) -> Vec<u8> ⓘ
Encode a key or value to a Vec.
This is a convenience function for calling Self::encode with a fresh
Vec
each time. Reuse an allocation when performance matters!
sourcefn decode_from<'a>(
&mut self,
buf: &'a [u8],
_storage: &mut Option<Self::Storage>,
schema: &Self::Schema,
) -> Result<(), String>
fn decode_from<'a>( &mut self, buf: &'a [u8], _storage: &mut Option<Self::Storage>, schema: &Self::Schema, ) -> Result<(), String>
An alternate form of Self::decode which enables amortizing allocs.
First, instead of returning Self
, it takes &mut Self
as a parameter,
allowing for reuse of the internal Row
/SourceData
allocations.
Second, it adds a type Storage
to Codec
and also passes it in as
&mut Option<Self::Storage>
, allowing for reuse of
ProtoRow
/ProtoSourceData
allocations. If Some
, this impl may
attempt to reuse allocations with it. It may also leave allocations in
it for use in future calls to decode_from
.
A default implementation is provided for Codec
impls that can’t take
advantage of this.