Skip to main content

reifydb_core/util/encoding/keycode/
mod.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2// Copyright (c) 2025 ReifyDB
3
4// This file includes and modifies code from the toydb project (https://github.com/erikgrinaker/toydb),
5// originally licensed under the Apache License, Version 2.0.
6// Original copyright:
7//   Copyright (c) 2024 Erik Grinaker
8//
9// The original Apache License can be found at:
10//   http://www.apache.org/licenses/LICENSE-2.0
11
12use serde::{Deserialize, Serialize};
13
14pub mod catalog;
15pub mod deserialize;
16pub mod deserializer;
17pub mod serialize;
18pub mod serializer;
19
20use std::{f32, f64};
21
22use reifydb_type::{
23	Result,
24	error::{Error, TypeError},
25};
26
27use crate::util::encoding::keycode::{deserialize::Deserializer, serialize::Serializer};
28
29/// Encode a bool value for keycode (true=0x00, false=0x01 for descending order)
30pub fn encode_bool(value: bool) -> u8 {
31	if value {
32		0x00
33	} else {
34		0x01
35	}
36}
37
38/// Encode an f32 value for keycode
39pub fn encode_f32(value: f32) -> [u8; 4] {
40	let mut bytes = value.to_be_bytes();
41	match value.is_sign_negative() {
42		false => bytes[0] ^= 1 << 7,                     // positive, flip sign bit
43		true => bytes.iter_mut().for_each(|b| *b = !*b), // negative, flip all bits
44	}
45	for b in bytes.iter_mut() {
46		*b = !*b;
47	}
48	bytes
49}
50
51/// Encode an f64 value for keycode
52pub fn encode_f64(value: f64) -> [u8; 8] {
53	let mut bytes = value.to_be_bytes();
54	match value.is_sign_negative() {
55		false => bytes[0] ^= 1 << 7,                     // positive, flip sign bit
56		true => bytes.iter_mut().for_each(|b| *b = !*b), // negative, flip all bits
57	}
58	for b in bytes.iter_mut() {
59		*b = !*b;
60	}
61	bytes
62}
63
64/// Encode an i8 value for keycode (flip sign bit, then NOT)
65pub fn encode_i8(value: i8) -> [u8; 1] {
66	let mut bytes = value.to_be_bytes();
67	bytes[0] ^= 1 << 7; // flip sign bit
68	for b in bytes.iter_mut() {
69		*b = !*b;
70	}
71	bytes
72}
73
74/// Encode an i16 value for keycode (flip sign bit, then NOT)
75pub fn encode_i16(value: i16) -> [u8; 2] {
76	let mut bytes = value.to_be_bytes();
77	bytes[0] ^= 1 << 7; // flip sign bit
78	for b in bytes.iter_mut() {
79		*b = !*b;
80	}
81	bytes
82}
83
84/// Encode an i32 value for keycode (flip sign bit, then NOT)
85pub fn encode_i32(value: i32) -> [u8; 4] {
86	let mut bytes = value.to_be_bytes();
87	bytes[0] ^= 1 << 7; // flip sign bit
88	for b in bytes.iter_mut() {
89		*b = !*b;
90	}
91	bytes
92}
93
94/// Encode an i64 value for keycode (flip sign bit, then NOT)
95pub fn encode_i64(value: i64) -> [u8; 8] {
96	let mut bytes = value.to_be_bytes();
97	bytes[0] ^= 1 << 7; // flip sign bit
98	for b in bytes.iter_mut() {
99		*b = !*b;
100	}
101	bytes
102}
103
104/// Encode an i128 value for keycode (flip sign bit, then NOT)
105pub fn encode_i128(value: i128) -> [u8; 16] {
106	let mut bytes = value.to_be_bytes();
107	bytes[0] ^= 1 << 7; // flip sign bit
108	for b in bytes.iter_mut() {
109		*b = !*b;
110	}
111	bytes
112}
113
114/// Encode a u8 value for keycode (bitwise NOT)
115pub fn encode_u8(value: u8) -> u8 {
116	!value
117}
118
119/// Encode a u16 value for keycode (bitwise NOT of big-endian)
120pub fn encode_u16(value: u16) -> [u8; 2] {
121	let mut bytes = value.to_be_bytes();
122	for b in bytes.iter_mut() {
123		*b = !*b;
124	}
125	bytes
126}
127
128/// Encode a u32 value for keycode (bitwise NOT of big-endian)
129pub fn encode_u32(value: u32) -> [u8; 4] {
130	let mut bytes = value.to_be_bytes();
131	for b in bytes.iter_mut() {
132		*b = !*b;
133	}
134	bytes
135}
136
137/// Encode a u64 value for keycode (bitwise NOT of big-endian)
138pub fn encode_u64(value: u64) -> [u8; 8] {
139	let mut bytes = value.to_be_bytes();
140	for b in bytes.iter_mut() {
141		*b = !*b;
142	}
143	bytes
144}
145
146/// Encode a u128 value for keycode (bitwise NOT of big-endian)
147pub fn encode_u128(value: u128) -> [u8; 16] {
148	let mut bytes = value.to_be_bytes();
149	for b in bytes.iter_mut() {
150		*b = !*b;
151	}
152	bytes
153}
154
155/// Encode bytes for keycode (escape 0xff, terminate with 0xffff)
156pub fn encode_bytes(bytes: &[u8], output: &mut Vec<u8>) {
157	for &byte in bytes {
158		if byte == 0xff {
159			output.push(0xff);
160			output.push(0x00);
161		} else {
162			output.push(byte);
163		}
164	}
165	output.push(0xff);
166	output.push(0xff);
167}
168
169#[macro_export]
170macro_rules! key_prefix {
171    ($($arg:tt)*) => {
172        &EncodedKey::new((&format!($($arg)*)).as_bytes().to_vec())
173    };
174}
175
176/// Serializes a key to a binary Keycode representation (Descending order)
177pub fn serialize<T: Serialize>(key: &T) -> Vec<u8> {
178	let mut serializer = Serializer {
179		output: Vec::new(),
180	};
181	// Panic on failure, as this is a problem with the data structure.
182	key.serialize(&mut serializer).expect("key must be serializable");
183	serializer.output
184}
185
186/// Deserializes a key from a binary Keycode representation (Descending order)
187pub fn deserialize<'a, T: Deserialize<'a>>(input: &'a [u8]) -> Result<T> {
188	let mut deserializer = Deserializer::from_bytes(input);
189	let t = T::deserialize(&mut deserializer)?;
190	if !deserializer.input.is_empty() {
191		return Err(Error::from(TypeError::SerdeKeycode {
192			message: format!(
193				"unexpected trailing bytes {:x?} at end of key {input:x?}",
194				deserializer.input,
195			),
196		}));
197	}
198	Ok(t)
199}
200
201#[cfg(test)]
202pub mod tests {
203	use std::borrow::Cow;
204
205	const PI_F32: f32 = f32::consts::PI;
206	const PI_F64: f64 = f64::consts::PI;
207
208	use reifydb_type::{
209		util::hex::encode,
210		value::{Value, ordered_f32::OrderedF32, ordered_f64::OrderedF64},
211	};
212	use serde_bytes::ByteBuf;
213
214	use super::*;
215	use crate::util::encoding::keycode::serializer::KeySerializer;
216
217	#[derive(Debug, Deserialize, Serialize, PartialEq)]
218	enum Key<'a> {
219		Unit,
220		NewType(String),
221		Tuple(bool, #[serde(with = "serde_bytes")] Vec<u8>, u64),
222		Cow(
223			#[serde(with = "serde_bytes")]
224			#[serde(borrow)]
225			Cow<'a, [u8]>,
226			bool,
227			#[serde(borrow)] Cow<'a, str>,
228		),
229	}
230
231	macro_rules! test_serde {
232        ( $( $name:ident: $input:expr => $expect:literal, )* ) => {
233        $(
234            #[test]
235            fn $name(){
236                let mut input = $input;
237                let expect = $expect;
238                let output = serialize(&input);
239                assert_eq!(encode(&output), expect, "encode failed");
240
241                let expect = input;
242                input = deserialize(&output).unwrap();
243                assert_eq!(input, expect, "decode failed");
244            }
245        )*
246        };
247    }
248
249	test_serde! {
250	bool_false: false => "01",
251	bool_true: true => "00",
252
253	f32_min: f32::MIN => "ff7fffff",
254	f32_neg_inf: f32::NEG_INFINITY => "ff800000",
255	f32_neg_pi: -PI_F32 => "c0490fdb",
256	f32_neg_zero: -0f32 => "80000000",
257	f32_zero: 0f32 => "7fffffff",
258	f32_pi: PI_F32 => "3fb6f024",
259	f32_max: f32::MAX => "00800000",
260	f32_inf: f32::INFINITY => "007fffff",
261
262	f64_min: f64::MIN => "ffefffffffffffff",
263	f64_neg_inf: f64::NEG_INFINITY => "fff0000000000000",
264	f64_neg_pi: -PI_F64 => "c00921fb54442d18",
265	f64_neg_zero: -0f64 => "8000000000000000",
266	f64_zero: 0f64 => "7fffffffffffffff",
267	f64_pi: PI_F64 => "3ff6de04abbbd2e7",
268	f64_max: f64::MAX => "0010000000000000",
269	f64_inf: f64::INFINITY => "000fffffffffffff",
270
271	i8_min: i8::MIN => "ff",
272	i8_neg_1: -1i8 => "80",
273	i8_0: 0i8 => "7f",
274	i8_1: 1i8 => "7e",
275	i8_max: i8::MAX => "00",
276
277	i16_min: i16::MIN => "ffff",
278	i16_neg_1: -1i16 => "8000",
279	i16_0: 0i16 => "7fff",
280	i16_1: 1i16 => "7ffe",
281	i16_max: i16::MAX => "0000",
282
283	i32_min: i32::MIN => "ffffffff",
284	i32_neg_1: -1i32 => "80000000",
285	i32_0: 0i32 => "7fffffff",
286	i32_1: 1i32 => "7ffffffe",
287	i32_max: i32::MAX => "00000000",
288
289	i64_min: i64::MIN => "ffffffffffffffff",
290	i64_neg_65535: -65535i64 => "800000000000fffe",
291	i64_neg_1: -1i64 => "8000000000000000",
292	i64_0: 0i64 => "7fffffffffffffff",
293	i64_1: 1i64 => "7ffffffffffffffe",
294	i64_65535: 65535i64 => "7fffffffffff0000",
295	i64_max: i64::MAX => "0000000000000000",
296
297	i128_min: i128::MIN => "ffffffffffffffffffffffffffffffff",
298	i128_neg_1: -1i128 => "80000000000000000000000000000000",
299	i128_0: 0i128 => "7fffffffffffffffffffffffffffffff",
300	i128_1: 1i128 => "7ffffffffffffffffffffffffffffffe",
301	i128_max: i128::MAX => "00000000000000000000000000000000",
302
303	u8_min: u8::MIN => "ff",
304	u8_1: 1_u8 => "fe",
305	u8_255: 255_u8 => "00",
306
307	u16_min: u16::MIN => "ffff",
308	u16_1: 1_u16 => "fffe",
309	u16_255: 255_u16 => "ff00",
310	u16_65535: u16::MAX => "0000",
311
312	u32_min: u32::MIN => "ffffffff",
313	u32_1: 1_u32 => "fffffffe",
314	u32_65535: 65535_u32 => "ffff0000",
315	u32_max: u32::MAX => "00000000",
316
317	u64_min: u64::MIN => "ffffffffffffffff",
318	u64_1: 1_u64 => "fffffffffffffffe",
319	u64_65535: 65535_u64 => "ffffffffffff0000",
320	u64_max: u64::MAX => "0000000000000000",
321
322	u128_min: u128::MIN => "ffffffffffffffffffffffffffffffff",
323	u128_1: 1_u128 => "fffffffffffffffffffffffffffffffe",
324	u128_65535: 65535_u128 => "ffffffffffffffffffffffffffff0000",
325	u128_max: u128::MAX => "00000000000000000000000000000000",
326
327	bytes: ByteBuf::from(vec![0x01, 0xff]) => "01ff00ffff",
328	bytes_empty: ByteBuf::new() => "ffff",
329	bytes_escape: ByteBuf::from(vec![0x00, 0x01, 0x02]) => "000102ffff",
330
331	string: "foo".to_string() => "666f6fffff",
332	string_empty: "".to_string() => "ffff",
333	string_escape: "foo\x00bar".to_string() => "666f6f00626172ffff",
334	string_utf8: "👋".to_string() => "f09f918bffff",
335
336	tuple: (true, u64::MAX, ByteBuf::from(vec![0x00, 0x01])) => "0000000000000000000001ffff",
337	array_bool: [false, true, false] => "010001",
338	vec_bool: vec![false, true, false] => "010001",
339	vec_u64: vec![u64::MIN, u64::MAX, 65535_u64] => "ffffffffffffffff0000000000000000ffffffffffff0000",
340
341	enum_unit: Key::Unit => "00",
342	enum_newtype: Key::NewType("foo".to_string()) => "01666f6fffff",
343	enum_tuple: Key::Tuple(false, vec![0x00, 0x01], u64::MAX) => "02010001ffff0000000000000000",
344	enum_cow: Key::Cow(vec![0x00, 0x01].into(), false, String::from("foo").into()) => "030001ffff01666f6fffff",
345	enum_cow_borrow: Key::Cow([0x00, 0x01].as_slice().into(), false, "foo".into()) => "030001ffff01666f6fffff",
346
347	value_none: Value::none() => "00",
348	value_bool: Value::Boolean(true) => "0100",
349	value_float4: Value::Float4(OrderedF32::try_from(PI_F32).unwrap()) => "023fb6f024",
350	value_float8: Value::Float8(OrderedF64::try_from(PI_F64).unwrap()) => "033ff6de04abbbd2e7",
351	value_int1: Value::Int1(-1) => "0480",
352	value_int4: Value::Int4(123456) => "067ffe1dbf",
353	value_int8: Value::Int8(31415926) => "077ffffffffe20a189",
354	value_int16: Value::Int16(-123456789012345678901234567890i128) => "08800000018ee90ff6c373e0ee4e3f0ad1",
355	value_string: Value::Utf8("foo".to_string()) => "09666f6fffff",
356	value_uint1: Value::Uint1(255) => "0a00",
357	value_uint2: Value::Uint2(65535) => "0b0000",
358	value_uint4: Value::Uint4(4294967295) => "0c00000000",
359	value_uint8: Value::Uint8(18446744073709551615) => "0d0000000000000000",
360	value_uint16: Value::Uint16(340282366920938463463374607431768211455u128) => "0e00000000000000000000000000000000",
361
362	// Option<bool>
363	option_none_bool: None::<bool> => "00",
364	option_some_true: Some(true) => "0100",
365	option_some_false: Some(false) => "0101",
366
367	// Option<f32>
368	option_none_f32: None::<f32> => "00",
369	option_some_f32: Some(PI_F32) => "013fb6f024",
370
371	// Option<f64>
372	option_none_f64: None::<f64> => "00",
373	option_some_f64: Some(PI_F64) => "013ff6de04abbbd2e7",
374
375	// Option<i8>
376	option_none_i8: None::<i8> => "00",
377	option_some_i8: Some(0i8) => "017f",
378
379	// Option<i16>
380	option_none_i16: None::<i16> => "00",
381	option_some_i16: Some(0i16) => "017fff",
382
383	// Option<i32>
384	option_none_i32: None::<i32> => "00",
385	option_some_i32: Some(0i32) => "017fffffff",
386
387	// Option<i64>
388	option_none_i64: None::<i64> => "00",
389	option_some_i64: Some(0i64) => "017fffffffffffffff",
390
391	// Option<i128>
392	option_none_i128: None::<i128> => "00",
393	option_some_i128: Some(0i128) => "017fffffffffffffffffffffffffffffff",
394
395	// Option<u8>
396	option_none_u8: None::<u8> => "00",
397	option_some_u8: Some(0u8) => "01ff",
398
399	// Option<u16>
400	option_none_u16: None::<u16> => "00",
401	option_some_u16: Some(0u16) => "01ffff",
402
403	// Option<u32>
404	option_none_u32: None::<u32> => "00",
405	option_some_u32: Some(0u32) => "01ffffffff",
406
407	// Option<u64>
408	option_none_u64: None::<u64> => "00",
409	option_some_u64: Some(0u64) => "01ffffffffffffffff",
410
411	// Option<u128>
412	option_none_u128: None::<u128> => "00",
413	option_some_u128: Some(0u128) => "01ffffffffffffffffffffffffffffffff",
414
415	// Option<String>
416	option_none_string: None::<String> => "00",
417	option_some_string: Some("foo".to_string()) => "01666f6fffff",
418	option_some_empty_string: Some("".to_string()) => "01ffff",
419
420	// Option<ByteBuf>
421	option_none_bytes: None::<ByteBuf> => "00",
422	option_some_bytes: Some(ByteBuf::from(vec![0x01, 0xff])) => "0101ff00ffff",
423
424	// Nested Option<Option<bool>>
425	option_nested_none: None::<Option<bool>> => "00",
426	option_nested_some_none: Some(None::<bool>) => "0100",
427	option_nested_some_some_true: Some(Some(true)) => "010100",
428	option_nested_some_some_false: Some(Some(false)) => "010101",
429
430	// Nested Option<Option<i32>>
431	option_nested_none_i32: None::<Option<i32>> => "00",
432	option_nested_some_none_i32: Some(None::<i32>) => "0100",
433	option_nested_some_some_i32: Some(Some(0i32)) => "01017fffffff",
434
435	// Nested Option<Option<String>>
436	option_nested_some_some_string: Some(Some("foo".to_string())) => "0101666f6fffff",
437
438	// Triple nested Option<Option<Option<bool>>>
439	option_triple_none: None::<Option<Option<bool>>> => "00",
440	option_triple_some_none: Some(None::<Option<bool>>) => "0100",
441	option_triple_some_some_none: Some(Some(None::<bool>)) => "010100",
442	option_triple_some_some_some: Some(Some(Some(true))) => "01010100",}
443
444	#[test]
445	fn test_option_ordering() {
446		// Descending: None > Some(MAX) > Some(0) > Some(MIN)
447		// Byte order: None < Some(MAX) < Some(0) < Some(MIN)
448		let none = serialize(&None::<i32>);
449		let some_max = serialize(&Some(i32::MAX));
450		let some_zero = serialize(&Some(0i32));
451		let some_min = serialize(&Some(i32::MIN));
452		assert!(none < some_max);
453		assert!(some_max < some_zero);
454		assert!(some_zero < some_min);
455	}
456
457	#[test]
458	fn test_nested_option_ordering() {
459		let none = serialize(&None::<Option<bool>>);
460		let some_none = serialize(&Some(None::<bool>));
461		let some_some_true = serialize(&Some(Some(true)));
462		let some_some_false = serialize(&Some(Some(false)));
463		assert!(none < some_none);
464		assert!(some_none < some_some_true);
465		assert!(some_some_true < some_some_false);
466	}
467
468	#[test]
469	fn test_key_serializer() {
470		// Test bool
471		let mut s = KeySerializer::new();
472		s.extend_bool(true);
473		assert_eq!(s.finish(), vec![0x00]);
474
475		let mut s = KeySerializer::new();
476		s.extend_bool(false);
477		assert_eq!(s.finish(), vec![0x01]);
478
479		// Test u64
480		let mut s = KeySerializer::new();
481		s.extend_u64(0u64);
482		assert_eq!(s.finish(), vec![0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]);
483
484		// Test i64
485		let mut s = KeySerializer::new();
486		s.extend_i64(0i64);
487		assert_eq!(s.finish(), vec![0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]);
488
489		// Test f32
490		let mut s = KeySerializer::new();
491		s.extend_f32(0.0f32);
492		assert_eq!(s.finish(), vec![0x7f, 0xff, 0xff, 0xff]);
493
494		// Test f64
495		let mut s = KeySerializer::new();
496		s.extend_f64(0.0f64);
497		assert_eq!(s.finish(), vec![0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]);
498
499		// Test bytes
500		let mut s = KeySerializer::new();
501		s.extend_bytes(b"foo");
502		assert_eq!(s.finish(), vec![0x66, 0x6f, 0x6f, 0xff, 0xff]);
503
504		// Test chaining
505		let mut s = KeySerializer::with_capacity(32);
506		s.extend_bool(true).extend_u32(1u32).extend_i16(-1i16).extend_bytes(b"test");
507		let result = s.finish();
508		assert!(!result.is_empty());
509		assert!(result.len() > 10); // Should have all the encoded values
510	}
511}