Struct persy::Transaction

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

Transaction container, it include all the changes done in a transaction.

Implementations§

source§

impl Transaction

source

pub fn create_segment( &mut self, segment: &str ) -> Result<SegmentId, PE<CreateSegmentError>>

Create a new segment with the provided name

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

pub fn drop_segment( &mut self, segment: &str ) -> Result<(), PE<DropSegmentError>>

Drop a existing segment

§Example
let mut tx = persy.begin()?;
tx.drop_segment("existing_segment_name")?;
tx.prepare()?.commit()?;
source

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

Check if a segment already exist in the storage considering the transaction

§Example
let mut tx = persy.begin()?;
tx.create_segment("my_new_segment")?;
assert!(tx.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, considering the transaction

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

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

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

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

pub fn insert( &mut self, segment: impl ToSegmentId, rec: &[u8] ) -> Result<PersyId, PE<InsertError>>

Create a new record.

This function return an id that can be used by read, the record content can be read only with the transaction read till the transaction is committed.

§Example
let mut tx = persy.begin()?;
let data = vec![1;20];
tx.insert("seg", &data)?;
tx.prepare()?.commit()?;
source

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

Read the record content considering eventual in transaction changes.

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

pub fn scan( &mut self, segment: impl ToSegmentId ) -> Result<TxSegmentIter<'_>, PE<SegmentError>>

Scan for persistent and in transaction records

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

pub fn update( &mut self, segment: impl ToSegmentId, id: &PersyId, rec: &[u8] ) -> Result<(), PE<UpdateError>>

Update the record content.

This updated content can be read only with the [transaction read] till the transaction is committed.

§Example
let mut tx = persy.begin()?;
let data = vec![1;20];
let id = tx.insert("seg", &data)?;
let new_data = vec![2;20];
tx.update("seg", &id, &new_data)?;
source

pub fn delete( &mut self, segment: impl ToSegmentId, id: &PersyId ) -> Result<(), PE<DeleteError>>

Delete a record.

The record will result deleted only reading it with transaction read till the transaction is committed.

§Example
let mut tx = persy.begin()?;
let data = vec![1;20];
let id = tx.insert("seg", &data)?;
tx.delete("seg", &id)?;
source

pub fn create_index<K, V>( &mut self, index_name: &str, value_mode: ValueMode ) -> Result<(), PE<CreateIndexError>>
where K: IndexType, V: IndexType,

Create a new index with the name and the value management mode.

The create operation require two template arguments that are the types as keys and values of the index this have to match the following operation on the indexes.

§Example
let mut tx = persy.begin()?;
tx.create_index::<u8,u8>("my_new_index", ValueMode::Cluster)?;
source

pub fn drop_index(&mut self, index_name: &str) -> Result<(), PE<DropIndexError>>

Drop an existing index.

§Example
let mut tx = persy.begin()?;
tx.drop_index("my_new_index")?;
source

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

Check if a segment already exist in the storage considering the transaction

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

pub fn put<K, V>( &mut self, index_name: &str, k: K, v: V ) -> Result<(), PE<IndexPutError>>
where K: IndexType, V: IndexType,

Put a key value in an index following the value mode strategy.

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

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

Remove a key and optionally a specific value from an index following the value mode strategy.

§Example
let mut tx = persy.begin()?;
tx.create_index::<u8,u8>("my_new_index", ValueMode::Cluster)?;
tx.put::<u8,u8>("my_new_index",10,10)?;
tx.remove::<u8,u8>("my_new_index",10,Some(10))?;
source

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

Get a value or a group of values from a key considering changes in transaction.

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

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

Get one value or none from a key considering changes in transaction.

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

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

Browse a range of keys and values from an index including the transaction changes

§Example
let mut tx = persy.begin()?;
tx.put::<u8,u8>("my_new_index",10,10)?;
{
    let iter:TxIndexIter<u8,u8> = tx.range("my_new_index",10..12)?;
    for (k,values) in iter  {
        for value in values {
            //...
        }
    }
}
tx.prepare()?.commit()?;
source

pub fn rollback(self) -> Result<(), PE<GenericError>>

Rollback a not yet prepared transaction.

All the resources used for eventual insert or update are released.

§Example
let mut tx = persy.begin()?;
let data = vec![1;20];
tx.insert("seg", &data)?;
tx.rollback()?;
source

pub fn prepare(self) -> Result<TransactionFinalize, PE<PrepareError>>

Prepare to commit a transaction, when this method return all the validation checks are done and is guaranteed that the transaction can be committed successfully

it will lock all the records involved in the transaction till a commit or rollback is called.

§Example
let mut tx = persy.begin()?;
//Do what ever operations on the records
let data = vec![1;20];
tx.insert("seg", &data)?;
tx.prepare()?;
source

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

List all the existing segments, considering all the changes in transaction.

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

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

List all the existing indexes, considering changes in the transaction.

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

pub fn commit(self) -> Result<(), PE<PrepareError>>

Prepare and Commit a transaction

§Example
let mut tx = persy.begin()?;
//Do what ever operations on the records
let data = vec![1;20];
tx.insert("seg", &data)?;
tx.commit()?;

Trait Implementations§

source§

impl Drop for Transaction

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

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> 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, U> TryFrom<U> for T
where 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 T
where 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.
source§

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

source§

fn vzip(self) -> V