|
| 1 | +//! Present a columnar container as a timely container. |
| 2 | +
|
| 3 | +use serde::{Serialize, Deserialize}; |
| 4 | + |
| 5 | +pub use columnar::*; |
| 6 | +use columnar::common::IterOwn; |
| 7 | + |
| 8 | +use crate::{Container, SizableContainer, PushInto}; |
| 9 | + |
| 10 | +/// A container based on a `columnar` store. |
| 11 | +#[derive(Clone, Default, Serialize, Deserialize)] |
| 12 | +pub struct Columnar<C> { |
| 13 | + store: C, |
| 14 | +} |
| 15 | + |
| 16 | +impl<C: Len + Clear + Clone + Default + 'static> Container for Columnar<C> |
| 17 | +where |
| 18 | + for<'a> &'a C: columnar::Index, |
| 19 | +{ |
| 20 | + fn len(&self) -> usize { self.store.len() } |
| 21 | + fn clear(&mut self) { self.store.clear() } |
| 22 | + |
| 23 | + type ItemRef<'a> = <&'a C as Index>::Ref where Self: 'a; |
| 24 | + type Iter<'a> = IterOwn<&'a C>; |
| 25 | + fn iter<'a>(&'a self) -> Self::Iter<'a> { (&self.store).into_iter() } |
| 26 | + |
| 27 | + type Item<'a> = <&'a C as Index>::Ref where Self: 'a; |
| 28 | + type DrainIter<'a> = IterOwn<&'a C>; |
| 29 | + fn drain<'a>(&'a mut self) -> Self::DrainIter<'a> { (&self.store).into_iter() } |
| 30 | +} |
| 31 | + |
| 32 | +impl<C: Len + Clear + Clone + Default + 'static> SizableContainer for Columnar<C> |
| 33 | +where |
| 34 | + for<'a> &'a C: columnar::Index, |
| 35 | +{ |
| 36 | + fn capacity(&self) -> usize { 1024 } |
| 37 | + fn preferred_capacity() -> usize { 1024 } |
| 38 | + fn reserve(&mut self, _additional: usize) { } |
| 39 | +} |
| 40 | + |
| 41 | +impl<C: columnar::Push<T>, T> PushInto<T> for Columnar<C> { |
| 42 | + #[inline] |
| 43 | + fn push_into(&mut self, item: T) { |
| 44 | + self.store.push(item); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | + |
| 49 | +use columnar::bytes::{AsBytes, FromBytes, serialization::decode}; |
| 50 | + |
| 51 | +/// A container based on a columnar store, encoded in aligned bytes. |
| 52 | +#[derive(Clone, Default)] |
| 53 | +pub struct ColumnarBytes<B, C> { |
| 54 | + bytes: B, |
| 55 | + phantom: std::marker::PhantomData<C>, |
| 56 | +} |
| 57 | + |
| 58 | +impl<B: std::ops::Deref<Target = [u64]> + Clone + Default + 'static, C: AsBytes + Clone + Default + 'static> Container for ColumnarBytes<B, C> |
| 59 | +where |
| 60 | + for<'a> C::Borrowed<'a> : Len + Clear + Index, |
| 61 | +{ |
| 62 | + fn len(&self) -> usize { |
| 63 | + <C::Borrowed<'_> as FromBytes>::from_bytes(&mut decode(&self.bytes)).len() |
| 64 | + } |
| 65 | + // Perhpas this should be an enum that allows the bytes to be un-set, but .. not sure what this should do. |
| 66 | + fn clear(&mut self) { unimplemented!() } |
| 67 | + |
| 68 | + type ItemRef<'a> = <C::Borrowed<'a> as Index>::Ref where Self: 'a; |
| 69 | + type Iter<'a> = IterOwn<C::Borrowed<'a>>; |
| 70 | + fn iter<'a>(&'a self) -> Self::Iter<'a> { |
| 71 | + <C::Borrowed<'a> as FromBytes>::from_bytes(&mut decode(&self.bytes)).into_iter() |
| 72 | + } |
| 73 | + |
| 74 | + type Item<'a> = <C::Borrowed<'a> as Index>::Ref where Self: 'a; |
| 75 | + type DrainIter<'a> = IterOwn<C::Borrowed<'a>>; |
| 76 | + fn drain<'a>(&'a mut self) -> Self::DrainIter<'a> { |
| 77 | + <C::Borrowed<'a> as FromBytes>::from_bytes(&mut decode(&self.bytes)).into_iter() |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +impl<B: std::ops::Deref<Target = [u64]> + Clone + Default + 'static, C: AsBytes + Clone + Default + 'static> SizableContainer for ColumnarBytes<B, C> |
| 82 | +where |
| 83 | + for<'a> C::Borrowed<'a> : Len + Clear + Index, |
| 84 | +{ |
| 85 | + fn capacity(&self) -> usize { 1024 } |
| 86 | + fn preferred_capacity() -> usize { 1024 } |
| 87 | + fn reserve(&mut self, _additional: usize) { } |
| 88 | +} |
0 commit comments