use crate::{
encoded::key::{EncodedKey, EncodedKeyRange},
interface::catalog::{id::ColumnId, shape::ShapeId},
key::{EncodableKey, KeyKind},
util::encoding::keycode::{deserializer::KeyDeserializer, serializer::KeySerializer},
};
#[derive(Debug, Clone, PartialEq)]
pub struct ColumnKey {
pub shape: ShapeId,
pub column: ColumnId,
}
const VERSION: u8 = 1;
impl EncodableKey for ColumnKey {
const KIND: KeyKind = KeyKind::Column;
fn encode(&self) -> EncodedKey {
let mut serializer = KeySerializer::with_capacity(19); serializer
.extend_u8(VERSION)
.extend_u8(Self::KIND as u8)
.extend_shape_id(self.shape)
.extend_u64(self.column);
serializer.to_encoded_key()
}
fn decode(key: &EncodedKey) -> Option<Self> {
let mut de = KeyDeserializer::from_bytes(key.as_slice());
let version = de.read_u8().ok()?;
if version != VERSION {
return None;
}
let kind: KeyKind = de.read_u8().ok()?.try_into().ok()?;
if kind != Self::KIND {
return None;
}
let shape = de.read_shape_id().ok()?;
let column = de.read_u64().ok()?;
Some(Self {
shape,
column: ColumnId(column),
})
}
}
impl ColumnKey {
pub fn encoded(shape: impl Into<ShapeId>, column: impl Into<ColumnId>) -> EncodedKey {
Self {
shape: shape.into(),
column: column.into(),
}
.encode()
}
pub fn full_scan(shape: impl Into<ShapeId>) -> EncodedKeyRange {
let shape = shape.into();
EncodedKeyRange::start_end(Some(Self::start(shape)), Some(Self::end(shape)))
}
fn start(shape: ShapeId) -> EncodedKey {
let mut serializer = KeySerializer::with_capacity(11);
serializer.extend_u8(VERSION).extend_u8(Self::KIND as u8).extend_shape_id(shape);
serializer.to_encoded_key()
}
fn end(shape: ShapeId) -> EncodedKey {
let mut serializer = KeySerializer::with_capacity(11);
serializer.extend_u8(VERSION).extend_u8(Self::KIND as u8).extend_shape_id(shape.prev());
serializer.to_encoded_key()
}
}
#[cfg(test)]
pub mod tests {
use super::EncodableKey;
use crate::{
interface::catalog::{id::ColumnId, shape::ShapeId},
key::ColumnKey,
};
#[test]
fn test_encode_decode() {
let key = ColumnKey {
shape: ShapeId::table(0xABCD),
column: ColumnId(0x123456789ABCDEF0),
};
let encoded = key.encode();
let expected: Vec<u8> = vec![
0xFE, 0xF8, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x54, 0x32, 0xED, 0xCB, 0xA9, 0x87, 0x65, 0x43, 0x21, 0x0F, ];
assert_eq!(encoded.as_slice(), expected);
let key = ColumnKey::decode(&encoded).unwrap();
assert_eq!(key.shape, 0xABCD);
assert_eq!(key.column, 0x123456789ABCDEF0);
}
#[test]
fn test_order_preserving() {
let key1 = ColumnKey {
shape: ShapeId::table(1),
column: ColumnId(100),
};
let key2 = ColumnKey {
shape: ShapeId::table(1),
column: ColumnId(200),
};
let key3 = ColumnKey {
shape: ShapeId::table(2),
column: ColumnId(0),
};
let encoded1 = key1.encode();
let encoded2 = key2.encode();
let encoded3 = key3.encode();
assert!(encoded3 < encoded2, "ordering not preserved");
assert!(encoded2 < encoded1, "ordering not preserved");
}
}