Struct structsy::Snapshot

source ·
pub struct Snapshot { /* private fields */ }
Expand description

Read data from a snapshot freezed in a specific moment ignoring all the subsequent committed transactions.

Implementations§

source§

impl Snapshot

source

pub fn read<T: Persistent>(&self, sref: &Ref<T>) -> SRes<Option<T>>

Read a persistent instance.

Example
use structsy::{Structsy,StructsyTx};
use structsy_derive::Persistent;
#[derive(Persistent)]
struct Example {
    value:u8,
}
//.. open structsy etc.
let mut tx = structsy.begin()?;
let id = tx.insert(&Example{value:10})?;
tx.commit()?;
let snapshot = structsy.snapshot()?;
let read = snapshot.read(&id)?;
assert_eq!(10,read.unwrap().value);
source

pub fn scan<T: Persistent>(&self) -> SRes<SnapshotRecordIter<T>>

Scan records of a specific struct.

Example
use structsy::Structsy;
use structsy_derive::Persistent;
#[derive(Persistent)]
struct Simple {
    name:String,
}
let stry = Structsy::open("path/to/file.stry")?;
stry.define::<Simple>()?;
let snapshot = stry.snapshot()?;
for (id, inst) in snapshot.scan::<Simple>()? {
    // logic here
}
source

pub fn fetch<R: Fetch<T>, T>(&self, filter: R) -> StructsyIter<'_, T>

Execute a filter query and return an iterator of results for the current snapshot

Example
use structsy::{ Structsy, StructsyTx, StructsyError, Filter};
use structsy_derive::{queries, embedded_queries, Persistent, PersistentEmbedded};

#[derive(Persistent)]
struct WithEmbedded {
    embedded: Embedded,
}

#[derive(PersistentEmbedded)]
struct Embedded {
    name: String,
}
impl WithEmbedded {
    fn new(name: &str) -> WithEmbedded {
        WithEmbedded {
            embedded: Embedded { name: name.to_string() },
        }
    }
}

#[queries(WithEmbedded)]
trait WithEmbeddedQuery {
    fn embedded(self, embedded: Filter<Embedded>) -> Self;
}

#[embedded_queries(Embedded)]
trait EmbeddedQuery {
    fn by_name(self, name: String) -> Self;
}

fn embedded_query() -> Result<(), StructsyError> {
    let structsy = Structsy::open("file.structsy")?;
    structsy.define::<WithEmbedded>()?;
    let mut tx = structsy.begin()?;
    tx.insert(&WithEmbedded::new("aaa"))?;
    tx.commit()?;
    let snapshot = structsy.snapshot()?;
    let embedded_filter = Filter::<Embedded>::new().by_name("aaa".to_string());
    let filter = Filter::<WithEmbedded>::new().embedded(embedded_filter);
    let count = snapshot.fetch(filter).count();
    assert_eq!(count, 1);
    Ok(())
}
source

pub fn query<T: Persistent + 'static>(&self) -> SnapshotQuery<T>

Query for a persistent struct in the snapshot

Example
use structsy::{ Structsy, StructsyTx, StructsyError};
use structsy_derive::{queries, Persistent};
#[derive(Persistent)]
struct Basic {
    name: String,
}
impl Basic {
    fn new(name: &str) -> Basic {
        Basic { name: name.to_string() }
    }
}

#[queries(Basic)]
trait BasicQuery {
     fn by_name(self, name: String) -> Self;
}

fn basic_query() -> Result<(), StructsyError> {
    let structsy = Structsy::open("file.structsy")?;
    structsy.define::<Basic>()?;
    let mut tx = structsy.begin()?;
    tx.insert(&Basic::new("aaa"))?;
    tx.commit()?;
    let snapshot = structsy.snapshot()?;
    let count = snapshot.query::<Basic>().by_name("aaa".to_string()).fetch().count();
    assert_eq!(count, 1);
    Ok(())
}
source

pub fn list_defined(&self) -> SRes<impl Iterator<Item = Description>>

Trait Implementations§

source§

impl Clone for Snapshot

source§

fn clone(&self) -> Snapshot

Returns a copy 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 RawAccess for Snapshot

source§

fn raw_begin(&self) -> SRes<RawTransaction>

Begin a new raw transaction
source§

fn raw_define(&self, _desc: Description) -> SRes<bool>

Declare a new struct or enum from the generic description
source§

fn raw_scan(&self, ty_name: &str) -> SRes<RawIter>

Scan the records of a struct or enum in a raw format
source§

fn raw_read(&self, id: &str) -> SRes<Option<Record>>

read a single record in a raw formant from a string id
source§

impl RawRead for Snapshot

source§

fn raw_scan(&self, strct_name: &str) -> SRes<RawIter>

Scan the records of a struct or enum in a raw format
source§

fn raw_read(&self, id: &str) -> SRes<Option<Record>>

read a single record in a raw formant from a string id

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V