Struct persy::Persy

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

Main structure to operate persy storage files

Implementations§

source§

impl Persy

source

pub fn create<P: AsRef<Path>>(path: P) -> Result<(), PE<CreateError>>

Create a new database file.

Errors

Fails if the file already exists.

Example
use std::path::Path;
use persy::{Persy, Config};

let path = Path::new("target/created.db");
Persy::create(path)?;
let persy = Persy::open(path, Config::new())?;
source

pub fn create_from_file(file: File) -> Result<(), PE<CreateError>>

Create a new database file.

Errors

Fails if the file already exists.

source

pub fn open<P: AsRef<Path>>(
path: P,
config: Config
) -> Result<Persy, PE<OpenError>>

Open a database file.

The file should have been created with Persy::create

Errors

Fails if the file does not exist.

source

pub fn open_with_recover<P: AsRef<Path>, C>(
path: P,
config: Config,
recover: C
) -> Result<Persy, PE<OpenError>>where
C: Fn(&TransactionId) -> bool,

Open a database file from a path with a recover function.

The file should have been created with Persy::create

Errors

Fails if the file does not exist.

source

pub fn recover<P: AsRef<Path>>(
path: P,
config: Config
) -> Result<Recover, PE<OpenError>>

Open a database file from a path and return a recover structure that allow to select the transactions to commit and recover them.

The file should have been created with Persy::create

Errors

Fails if the file does not exist.

source

pub fn open_from_file(path: File, config: Config) -> Result<Persy, PE<OpenError>>

Open a database file from a direct file handle.

The file should have been created with Persy::create

Errors

Fails if the file does not exist.

source

pub fn open_from_file_with_recover<C>(
file: File,
config: Config,
recover: C
) -> Result<Persy, PE<OpenError>>where
C: Fn(&TransactionId) -> bool,

Open a database file, from a direct file handle and a transaction recover function.

The file should have been created with Persy::create

Errors

Fails if the file does not exist.

source

pub fn open_or_create_with<P, F>(
path: P,
config: Config,
prepare: F
) -> Result<Persy, PE<OpenError>>where
P: AsRef<Path>,
F: FnOnce(&Persy) -> Result<(), Box<dyn Error>> + 'static,

Open an existing database or create it if it does not exist yet, calling the prepare function just after the creation.

Example
use std::path::Path;
use persy::{Persy, Config, PersyId, ValueMode};

let path = Path::new("target/open_or_create.db");
let config = Config::new();

let persy = Persy::open_or_create_with(path, config, |persy| {
    // this closure is only called on database creation
    let mut tx = persy.begin()?;
    tx.create_segment("data")?;
    tx.create_index::<u64, PersyId>("index", ValueMode::Replace)?;
    let prepared = tx.prepare()?;
    prepared.commit()?;
    println!("Segment and Index successfully created");
    Ok(())
})?;
source

pub fn begin(&self) -> Result<Transaction, PE<BeginTransactionError>>

Begin a new transaction.

The transaction isolation level is ‘read_committed’. for commit call prepare and commit

Example
let mut tx = persy.begin()?;
// ...
tx.prepare()?.commit()?;
source

pub fn begin_with(
&self,
config: TransactionConfig
) -> Result<Transaction, PE<BeginTransactionError>>

Begin a new transaction specifying parameters for the transaction.

The transaction isolation level is ‘read_committed’. for commit call prepare and commit

Example
let tx_id = vec![2;2];
let mut tx = persy.begin_with(TransactionConfig::new().set_transaction_id(tx_id))?;
// ...
tx.prepare()?.commit()?;
source

pub fn exists_segment(&self, segment: &str) -> Result<bool, PE<GenericError>>

Check if a segment already exist in the storage

Example
let mut tx = persy.begin()?;
tx.create_segment("my_new_segment")?;
let prepared = tx.prepare()?;
prepared.commit()?;
assert!(persy.exists_segment("my_new_segment")?);
source

pub fn solve_segment_id(
&self,
segment: impl ToSegmentId
) -> Result<SegmentId, PE<SegmentError>>

Resolves the segment to a SegmentId

Example
let mut tx = persy.begin()?;
tx.create_segment("my_new_segment")?;
let prepared = tx.prepare()?;
prepared.commit()?;
let segment_id = persy.solve_segment_id("my_new_segment")?;
source

pub fn solve_index_id(
&self,
index: impl ToIndexId
) -> Result<IndexId, PE<IndexError>>

Resolves the index to a IndexId, this has no public use as today, may be used in future.

Example
let mut tx = persy.begin()?;
tx.create_index::<u8,u8>("my_new_index", ValueMode::Cluster)?;
let prepared = tx.prepare()?;
prepared.commit()?;
let index_id = persy.solve_index_id("my_new_index")?;
source

pub fn read(
&self,
segment: impl ToSegmentId,
id: &PersyId
) -> Result<Option<Vec<u8>>, PE<ReadError>>

Read the record content from persistent data.

Example
let mut tx = persy.begin()?;
let data = vec![1;20];
let id = tx.insert("seg", &data)?;
let prepared = tx.prepare()?;
prepared.commit()?;
let read = persy.read("seg", &id)?.expect("record exits");
assert_eq!(data,read);
source

pub fn scan(
&self,
segment: impl ToSegmentId
) -> Result<SegmentIter, PE<SegmentError>>

Scan a segment for persistent records

Example
let mut tx = persy.begin()?;
let data = vec![1;20];
let id = tx.insert("seg", &data)?;
let prepared = tx.prepare()?;
prepared.commit()?;
let mut count = 0;
for (id,content) in persy.scan("seg")? {
    println!("record size:{}",content.len());
    count+=1;
}
assert_eq!(count,1);
source

pub fn exists_index(&self, index_name: &str) -> Result<bool, PE<GenericError>>

Check if a segment already exist in the storage

Example
let mut tx = persy.begin()?;
tx.create_index::<u8,u8>("my_new_index", ValueMode::Replace)?;
let prepared = tx.prepare()?;
prepared.commit()?;
assert!(persy.exists_index("my_new_index")?);
source

pub fn get<K, V>(
&self,
index_name: &str,
k: &K
) -> Result<ValueIter<V>, PE<IndexOpsError>>where
K: IndexType,
V: IndexType,

Get a value or a group of values from a key.

Example
let values = persy.get::<u8,u8>("my_new_index",&10)?;
for value in values {
    //...
}
source

pub fn one<K, V>(
&self,
index_name: &str,
k: &K
) -> Result<Option<V>, PE<IndexOpsError>>where
K: IndexType,
V: IndexType,

Get one value or none from a key.

Example
if let Some(value) = persy.one::<u8,u8>("my_new_index",&10)? {
    //...
}
source

pub fn range<K, V, R>(
&self,
index_name: &str,
range: R
) -> Result<IndexIter<K, V>, PE<IndexOpsError>>where
K: IndexType,
V: IndexType,
R: RangeBounds<K>,

Browse a range of keys and values from and index.

Example
let iter:IndexIter<u8,u8> = persy.range("my_new_index",10..12)?;
for (k,values) in iter  {
    for value in values {
        //...
    }
}
source

pub fn list_segments(
&self
) -> Result<Vec<(String, SegmentId)>, PE<GenericError>>

List all the existing segments.

Example
let mut tx = persy.begin()?;
tx.create_segment("seg")?;
let prepared = tx.prepare()?;
prepared.commit()?;
let segments = persy.list_segments()?;
let names = segments.into_iter().map(|(name,_id)|name).collect::<Vec<String>>();
assert!(names.contains(&"seg".to_string()));
source

pub fn list_indexes(&self) -> Result<Vec<(String, IndexInfo)>, PE<GenericError>>

List all the existing indexes.

Example
let mut tx = persy.begin()?;
tx.create_index::<u8,u8>("index", ValueMode::Cluster)?;
let prepared = tx.prepare()?;
prepared.commit()?;
let indexes = persy.list_indexes()?;
let names = indexes.into_iter().map(|(name,_info)|name).collect::<Vec<String>>();
assert!(names.contains(&"index".to_string()));
source

pub fn snapshot(&self) -> Result<Snapshot, PE<GenericError>>

Create a read snapshot at the current data status.

// ... More logic
let snapshot = persy.snapshot()?;
// .. Access data from the snapshot

Trait Implementations§

source§

impl Clone for Persy

source§

fn clone(&self) -> Persy

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

Auto Trait Implementations§

§

impl !RefUnwindSafe for Persy

§

impl Send for Persy

§

impl Sync for Persy

§

impl Unpin for Persy

§

impl !UnwindSafe for Persy

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,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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.
const: unstable · 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.
const: unstable · 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