#[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.
impl<'a> SerializedDb<'a>
Methods available to all types of allocations.
Sourcepub fn deserialize_db(&self) -> Result<Database, VectorscanRuntimeError>
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(())
}
Sourcepub fn deserialized_size(&self) -> Result<usize, VectorscanRuntimeError>
pub fn deserialized_size(&self) -> Result<usize, VectorscanRuntimeError>
Return the size of the allocation necessary for a subsequent call to
Self::deserialize_db_at()
.
Sourcepub unsafe fn deserialize_db_at(
&self,
db: *mut NativeDb,
) -> Result<(), VectorscanRuntimeError>
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(())
}
Sourcepub fn extract_db_info(&self) -> Result<DbInfo, VectorscanRuntimeError>
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.
impl SerializedDb<'static>
§Owned Allocations
Methods that produce new owned ('static
) allocations.
A Clone
impl is also available for such owned allocations.
Sourcepub fn serialize_db(db: &Database) -> Result<Self, VectorscanRuntimeError>
pub fn serialize_db(db: &Database) -> Result<Self, VectorscanRuntimeError>
Write a serialized representation of db
into a newly allocated region of
memory.
Sourcepub fn from_cloned_data(s: &SerializedDb<'_>) -> Self
pub fn from_cloned_data(s: &SerializedDb<'_>) -> Self
Allocate a new region of memory and copy over the referenced data from
s
.