diff options
Diffstat (limited to 'database/src/kv/binning.rs')
| -rw-r--r-- | database/src/kv/binning.rs | 36 |
1 files changed, 33 insertions, 3 deletions
diff --git a/database/src/kv/binning.rs b/database/src/kv/binning.rs index 42e58fc..c41d6e9 100644 --- a/database/src/kv/binning.rs +++ b/database/src/kv/binning.rs @@ -4,7 +4,7 @@ Copyright (C) 2026 metamuffin <metamuffin.org> */ -use crate::Filter; +use crate::{Filter, Value}; use jellyobject::{Object, Path}; /// Sorted list of components to bin objects by filtered values. @@ -88,8 +88,7 @@ impl Filter { Filter::Match(path, value) => { vec![vec![(BinningComponent::Match(path.to_owned()), { let mut co = Vec::new(); - co.extend((value.len() as u32).to_be_bytes()); - co.extend(value); + value.write_with_len(&mut co); co })]] } @@ -100,6 +99,37 @@ impl Filter { } } +impl Value { + pub fn write_with_len(&self, out: &mut Vec<u8>) { + match self { + Value::Tag(tag) => { + out.extend(4u32.to_be_bytes()); + out.extend(tag.0.to_be_bytes()); + } + Value::U32(x) => { + out.extend(4u32.to_be_bytes()); + out.extend(x.to_be_bytes()); + } + Value::U64(x) => { + out.extend(8u32.to_be_bytes()); + out.extend(x.to_be_bytes()); + } + Value::I64(x) => { + out.extend(8u32.to_be_bytes()); + out.extend(x.to_be_bytes()); + } + Value::String(s) => { + out.extend((s.len() as u32).to_be_bytes()); + out.extend(s.as_bytes()); + } + Value::Binary(s) => { + out.extend((s.len() as u32).to_be_bytes()); + out.extend(s); + } + } + } +} + #[cfg(test)] mod test { use jellyobject::{Path, Tag}; |