Struct SerializedDb

Source
#[repr(transparent)]
pub struct SerializedDb<'a>(pub DbAllocation<'a>);
Expand description

Wrapper for a serialized form of a Database.

Tuple Fields§

§0: DbAllocation<'a>

This serialization data can be sourced from a variety of places.

Implementations§

Source§

impl<'a> SerializedDb<'a>

Methods available to all types of allocations.

Source

pub fn deserialize_db(&self) -> Result<Database, VectorscanRuntimeError>

Deserialize into a new db allocation.

This will make a new allocation through the allocator from crate::alloc::set_db_allocator().

 #[cfg(feature = "compiler")]
 fn main() -> Result<(), vectorscan::error::VectorscanError> {
   use vectorscan::{expression::*, flags::*};

   let expr: Expression = "a+".parse()?;
   let serialized_db = expr.compile(Flags::SOM_LEFTMOST, Mode::BLOCK)?.serialize()?;
   let db = serialized_db.deserialize_db()?;

   // Note that the expected deserialized size is the same
   // as the resulting in-memory database size:
   assert_eq!(db.database_size()?, serialized_db.deserialized_size()?);
   Ok(())
 }
Source

pub fn deserialized_size(&self) -> Result<usize, VectorscanRuntimeError>

Return the size of the allocation necessary for a subsequent call to Self::deserialize_db_at().

Source

pub unsafe fn deserialize_db_at( &self, db: *mut NativeDb, ) -> Result<(), VectorscanRuntimeError>

Like Self::deserialize_db(), but points into an existing allocation instead of making a new allocation.

§Safety

db must point to an allocation at least Self::deserialized_size() bytes in size!

 #[cfg(feature = "compiler")]
 fn main() -> Result<(), vectorscan::error::VectorscanError> {
   use vectorscan::{expression::*, flags::*, database::*};
   use std::mem;

   let expr: Expression = "a+".parse()?;
   let serialized_db = expr.compile(Flags::SOM_LEFTMOST, Mode::BLOCK)?.serialize()?;

   // Allocate a vector with sufficient capacity for the deserialized db:
   let mut db_data: Vec<u8> = Vec::with_capacity(serialized_db.deserialized_size()?);
   let db = unsafe {
     let db_ptr: *mut NativeDb = mem::transmute(db_data.as_mut_ptr());
     serialized_db.deserialize_db_at(db_ptr)?;
     // Wrap in ManuallyDrop to avoid freeing memory owned by the `db_data` vector.
     mem::ManuallyDrop::new(Database::from_native(db_ptr))
   };
   // Note that the expected deserialized size is the same
   // as the resulting in-memory database size:
   assert_eq!(db.database_size()?, serialized_db.deserialized_size()?);
   Ok(())
 }
Source

pub fn extract_db_info(&self) -> Result<DbInfo, VectorscanRuntimeError>

Extract metadata about the serialized database into a new string allocation.

 #[cfg(feature = "compiler")]
 fn main() -> Result<(), vectorscan::error::VectorscanError> {
   use vectorscan::{expression::*, flags::*};

   let expr: Expression = "a+".parse()?;
   let serialized_db = expr.compile(Flags::default(), Mode::BLOCK)?.serialize()?;
   let info = serialized_db.extract_db_info()?;
   assert_eq!(info.as_str(), "Version: 5.4.11 Features: AVX2 Mode: BLOCK");
   // Info is the same as would have been provided from deserializing:
   assert_eq!(info, serialized_db.deserialize_db()?.info()?);
   Ok(())
 }
Source§

impl SerializedDb<'static>

§Owned Allocations

Methods that produce new owned ('static) allocations.

A Clone impl is also available for such owned allocations.

Source

pub fn serialize_db(db: &Database) -> Result<Self, VectorscanRuntimeError>

Write a serialized representation of db into a newly allocated region of memory.

Source

pub fn from_cloned_data(s: &SerializedDb<'_>) -> Self

Allocate a new region of memory and copy over the referenced data from s.

Trait Implementations§

Source§

impl Clone for SerializedDb<'static>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for SerializedDb<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for SerializedDb<'a>

§

impl<'a> RefUnwindSafe for SerializedDb<'a>

§

impl<'a> Send for SerializedDb<'a>

§

impl<'a> !Sync for SerializedDb<'a>

§

impl<'a> Unpin for SerializedDb<'a>

§

impl<'a> UnwindSafe for SerializedDb<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.