Skip to main content

nimiq_database/traits/
transaction.rs

1use nimiq_database_value::IntoDatabaseValue;
2
3use super::{
4    DupReadCursor, DupTable, DupWriteCursor, ReadCursor, RegularTable, Table, WriteCursor,
5};
6
7/// Read-transactions can only perform read operations on a database.
8pub trait ReadTransaction<'db>: Sized {
9    type Cursor<'txn, T: Table>: ReadCursor<'txn, T>
10    where
11        Self: 'txn;
12    type DupCursor<'txn, T: DupTable>: ReadCursor<'txn, T> + DupReadCursor<'txn, T>
13    where
14        Self: 'txn;
15
16    /// Close the transaction, releasing any resources it holds.
17    fn close(self) {}
18
19    /// Gets the value at a given key.
20    fn get<T: Table>(&self, table: &T, key: &T::Key) -> Option<T::Value>;
21
22    /// Creates a cursor for iterating over the table.
23    fn cursor<'txn, T: RegularTable>(&'txn self, table: &T) -> Self::Cursor<'txn, T>;
24
25    /// Creates a cursor for iterating over the table with duplicate keys.
26    fn dup_cursor<'txn, T: DupTable>(&'txn self, table: &T) -> Self::DupCursor<'txn, T>;
27}
28
29/// Write-transactions can perform read and write operations on a database.
30pub trait WriteTransaction<'db>: ReadTransaction<'db> + Sized {
31    type WriteCursor<'txn, T: Table>: WriteCursor<'txn, T>
32    where
33        Self: 'txn;
34    type DupWriteCursor<'txn, T: DupTable>: DupWriteCursor<'txn, T>
35    where
36        Self: 'txn;
37
38    /// Puts a key/value pair into the database by copying it into a reserved space in the database.
39    /// This works best for values that need to be serialized into the reserved space.
40    /// This method will panic when called on a database with duplicate keys!
41    fn put_reserve<T: RegularTable>(&mut self, table: &T, key: &T::Key, value: &T::Value)
42    where
43        T::Value: IntoDatabaseValue;
44
45    /// Puts a key/value pair into the database by passing a reference to a byte slice.
46    /// This is more efficient than `put_reserve` if no serialization is needed,
47    /// and the existing value can be immediately written into the database.
48    fn put<T: Table>(&mut self, table: &T, key: &T::Key, value: &T::Value);
49
50    /// Appends a key/value pair to the end of the database.
51    /// This method is more efficient than `put`.
52    /// This operation fails if the key is less than the last key.
53    fn append<T: Table>(&mut self, table: &T, key: &T::Key, value: &T::Value);
54
55    /// Removes the entry with this key from the database.
56    /// In dup tables, it removes all entries with this key.
57    fn remove<T: Table>(&mut self, table: &T, key: &T::Key);
58
59    /// Removes the entry with this key and value from the database.
60    /// Only matching entries will be deleted.
61    fn remove_item<T: Table>(&mut self, table: &T, key: &T::Key, value: &T::Value);
62
63    /// Commits the changes to the database.
64    fn commit(self);
65
66    /// Aborts the transaction and discards all changes.
67    fn abort(self) {}
68
69    /// Creates a write cursor for the given table.
70    fn cursor<'txn, T: RegularTable>(&'txn self, table: &T) -> Self::WriteCursor<'txn, T>;
71
72    /// Creates a write cursor for the given duplicate table.
73    fn dup_cursor<'txn, T: DupTable>(&'txn self, table: &T) -> Self::DupWriteCursor<'txn, T>;
74
75    /// Clears the table of all entries.
76    fn clear_table<T: Table>(&mut self, table: &T);
77}