[][src]Struct flatdata::MultiVector

pub struct MultiVector<'a, Ts> where
    Ts: VariadicRefFactory
{ /* fields omitted */ }

A container for writing an indexed sequence of heterogeneous data items.

The concept of a multivector is used for storing and reading heterogeneous flatdata structs in/from the same container. The data is indexed by integers. Each index refers to a bucket which may contain a variable number of items of different types unified in the same variant enum Ts. Such bucket may also be empty, which allows to represent sparse data in a multivector. For those who are familiar with C++'s std::multimap data structure, a multivector can be considered as a std::multimap mapping integers to sequences of variable length.

A MultiVector corresponds rather to ExternalVector than to Vector, in the sense that the items are flushed to storage whenever the internal buffer is full. In particular, it is only possible to modify the last bucket. There is no access to the buckets previously stored.

For accessing and reading the data stored by in multivector, cf. MultiArrayView.

A multivector must be closed, after the last element was written to it. After closing, it can not be used anymore. Not closing the multivector will result in panic on drop (in debug mode).

Internally data is stored like this:

  • Index: Vector<Idx> - encodes start/end byte in Data array for each element i. * Data: Vec<u8> - sequence of serialized (Tag, ItemData) tuples, where Tag encodes the the variant type, and ItemData contains the underlying variant data. Tag has size of 1 byte, ItemData is of size Ts::Type::SIZE_IN_BYTES.

Examples

struct A {
    x : u32 : 16;
    y : u32 : 16;
}

struct B {
    id : u32 : 16;
}

archive X {
   ab : multivector< 16, A, B >;
}
// create multivector and serialize some data
let mut storage = MemoryResourceStorage::new("/root/multivec");
let mut builder = XBuilder::new(storage.clone()).expect("Fail to create builder");
let mut mv = builder.start_ab().expect("failed to create MultiVector");
let mut item = mv.grow().expect("grow failed");
let mut a = item.add_a();
a.set_x(1);
a.set_y(2);

let mut b = item.add_b();
b.set_id(42);
mv.close().expect("close failed");

// open multivector and read the data
let archive = X::open(storage).expect("open failed");
let mv = archive.ab();

assert_eq!(mv.len(), 1);
let mut item = mv.at(0);
match item.next().unwrap() {
    RefAb::A(a) => assert_eq!((a.x(), a.y()), (1, 2)),
    _ => assert!(false),
}
match item.next().unwrap() {
    RefAb::B(b) => assert_eq!(b.id(), 42),
    _ => assert!(false),
}

Methods

impl<'a, Ts> MultiVector<'a, Ts> where
    Ts: VariadicRefFactory
[src]

pub fn new(
    index: ExternalVector<'a, <Ts as VariadicStruct<'a>>::Index>,
    data_handle: ResourceHandle<'a>
) -> Self
[src]

Creates an empty multivector.

pub fn grow(&mut self) -> Result<<Ts as VariadicStruct>::ItemMut>[src]

Appends a new item to the end of this multivector and returns a builder for it.

The builder is used for storing different variants of Ts in the newly created item.

Calling this method may flush data to storage (cf. flush), which may fail due to different IO reasons.

pub fn close(self) -> Result<MultiArrayView<'a, Ts>, ResourceStorageError>[src]

Flushes the remaining not yet flushed elements in this multivector and finalizes the data inside the storage.

A multivector must be closed, otherwise it will panic on drop

Trait Implementations

impl<'a, Ts> Debug for MultiVector<'a, Ts> where
    Ts: VariadicRefFactory
[src]

Auto Trait Implementations

impl<'a, Ts> !Sync for MultiVector<'a, Ts>

impl<'a, Ts> Unpin for MultiVector<'a, Ts> where
    Ts: Unpin,
    <Ts as VariadicStruct<'a>>::Index: Unpin

impl<'a, Ts> !Send for MultiVector<'a, Ts>

impl<'a, Ts> !UnwindSafe for MultiVector<'a, Ts>

impl<'a, Ts> !RefUnwindSafe for MultiVector<'a, Ts>

Blanket Implementations

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]