Skip to main content

hematite/btree/
codec.rs

1//! Typed encoding boundary for the generic B-tree layer.
2//!
3//! The raw tree only understands ordered key bytes and opaque value bytes. `KeyValueCodec` is the
4//! hook that lets higher layers project typed data onto that raw representation without teaching
5//! the B-tree anything about tables, rows, or indexes.
6
7use crate::error::Result;
8
9pub trait KeyValueCodec {
10    type Key;
11    type Value;
12
13    fn encode_key(key: &Self::Key) -> Result<Vec<u8>>;
14    fn decode_key(bytes: &[u8]) -> Result<Self::Key>;
15    fn encode_value(value: &Self::Value) -> Result<Vec<u8>>;
16    fn decode_value(bytes: &[u8]) -> Result<Self::Value>;
17}
18
19#[derive(Debug, Clone, Copy, Default)]
20pub struct RawBytesCodec;
21
22impl KeyValueCodec for RawBytesCodec {
23    type Key = Vec<u8>;
24    type Value = Vec<u8>;
25
26    fn encode_key(key: &Self::Key) -> Result<Vec<u8>> {
27        Ok(key.clone())
28    }
29
30    fn decode_key(bytes: &[u8]) -> Result<Self::Key> {
31        Ok(bytes.to_vec())
32    }
33
34    fn encode_value(value: &Self::Value) -> Result<Vec<u8>> {
35        Ok(value.clone())
36    }
37
38    fn decode_value(bytes: &[u8]) -> Result<Self::Value> {
39        Ok(bytes.to_vec())
40    }
41}