|
| 1 | +//! Public type aliases for transactions, cursors, and iterators. |
| 2 | +
|
| 3 | +use crate::{ |
| 4 | + Ro, RoSync, Rw, RwSync, |
| 5 | + tx::{ |
| 6 | + PtrSync, PtrUnsync, |
| 7 | + cursor::Cursor, |
| 8 | + r#impl::Tx, |
| 9 | + iter::{Iter, IterDupFixed, IterDupFixedOfKey}, |
| 10 | + }, |
| 11 | +}; |
| 12 | +use std::{borrow::Cow, sync::Arc}; |
| 13 | + |
| 14 | +// --- Transaction aliases --- |
| 15 | + |
| 16 | +/// Transaction type for synchronized access. |
| 17 | +pub type TxSync<K> = Tx<K, Arc<PtrSync>>; |
| 18 | + |
| 19 | +/// Transaction type for unsynchronized access. |
| 20 | +pub type TxUnsync<K> = Tx<K, PtrUnsync>; |
| 21 | + |
| 22 | +/// A synchronized read-only transaction. |
| 23 | +pub type RoTxSync = TxSync<RoSync>; |
| 24 | + |
| 25 | +/// A synchronized read-write transaction. |
| 26 | +pub type RwTxSync = TxSync<RwSync>; |
| 27 | + |
| 28 | +/// An unsynchronized read-only transaction. |
| 29 | +pub type RoTxUnsync = TxUnsync<Ro>; |
| 30 | + |
| 31 | +/// An unsynchronized read-write transaction. |
| 32 | +pub type RwTxUnsync = TxUnsync<Rw>; |
| 33 | + |
| 34 | +// SAFETY: |
| 35 | +// - RoTxSync and RwTxSync use Arc<PtrSync> which is Send and Sync. |
| 36 | +// - K::Cache is ALWAYS Send |
| 37 | +// - TxMeta is ALWAYS Send |
| 38 | +// - Moving an RO transaction between threads is safe as long as no concurrent |
| 39 | +// access occurs, which is guaranteed by being !Sync. |
| 40 | +// |
| 41 | +// NB: Send is correctly derived for RoTxSync and RwTxSync UNTIL |
| 42 | +// you unsafe impl Sync for RoTxUnsync below. This is a quirk I did not know |
| 43 | +// about. |
| 44 | +unsafe impl Send for RoTxSync {} |
| 45 | +unsafe impl Send for RwTxSync {} |
| 46 | +unsafe impl Send for RoTxUnsync {} |
| 47 | + |
| 48 | +// --- Cursor aliases --- |
| 49 | + |
| 50 | +/// A read-only cursor for a synchronized transaction. |
| 51 | +pub type RoCursorSync<'tx> = Cursor<'tx, RoSync>; |
| 52 | + |
| 53 | +/// A read-write cursor for a synchronized transaction. |
| 54 | +pub type RwCursorSync<'tx> = Cursor<'tx, RwSync>; |
| 55 | + |
| 56 | +/// A read-only cursor for an unsynchronized transaction. |
| 57 | +pub type RoCursorUnsync<'tx> = Cursor<'tx, Ro>; |
| 58 | + |
| 59 | +/// A read-write cursor for an unsynchronized transaction. |
| 60 | +pub type RwCursorUnsync<'tx> = Cursor<'tx, Rw>; |
| 61 | + |
| 62 | +// --- Iterator aliases --- |
| 63 | + |
| 64 | +/// Iterates over KV pairs in an MDBX database. |
| 65 | +pub type IterKeyVals<'tx, 'cur, K, Key = Cow<'tx, [u8]>, Value = Cow<'tx, [u8]>> = |
| 66 | + Iter<'tx, 'cur, K, Key, Value, { ffi::MDBX_NEXT }>; |
| 67 | + |
| 68 | +/// An iterator over the key/value pairs in an MDBX `DUPSORT` with duplicate |
| 69 | +/// keys, yielding the first value for each key. |
| 70 | +/// |
| 71 | +/// See the [`Iter`] documentation for more details. |
| 72 | +pub type IterDupKeys<'tx, 'cur, K, Key = Cow<'tx, [u8]>, Value = Cow<'tx, [u8]>> = |
| 73 | + Iter<'tx, 'cur, K, Key, Value, { ffi::MDBX_NEXT_NODUP }>; |
| 74 | + |
| 75 | +/// An iterator over the key/value pairs in an MDBX `DUPSORT`, yielding each |
| 76 | +/// duplicate value for a specific key. |
| 77 | +pub type IterDupVals<'tx, 'cur, K, Key = Cow<'tx, [u8]>, Value = Cow<'tx, [u8]>> = |
| 78 | + Iter<'tx, 'cur, K, Key, Value, { ffi::MDBX_NEXT_DUP }>; |
| 79 | + |
| 80 | +/// A key-value iterator for a synchronized read-only transaction. |
| 81 | +pub type RoIterSync<'tx, 'cur, Key = Cow<'tx, [u8]>, Value = Cow<'tx, [u8]>> = |
| 82 | + IterKeyVals<'tx, 'cur, RoSync, Key, Value>; |
| 83 | + |
| 84 | +/// A key-value iterator for a synchronized read-write transaction. |
| 85 | +pub type RwIterSync<'tx, 'cur, Key = Cow<'tx, [u8]>, Value = Cow<'tx, [u8]>> = |
| 86 | + IterKeyVals<'tx, 'cur, RwSync, Key, Value>; |
| 87 | + |
| 88 | +/// A key-value iterator for an unsynchronized read-only transaction. |
| 89 | +pub type RoIterUnsync<'tx, 'cur, Key = Cow<'tx, [u8]>, Value = Cow<'tx, [u8]>> = |
| 90 | + IterKeyVals<'tx, 'cur, Ro, Key, Value>; |
| 91 | + |
| 92 | +/// A key-value iterator for an unsynchronized read-write transaction. |
| 93 | +pub type RwIterUnsync<'tx, 'cur, Key = Cow<'tx, [u8]>, Value = Cow<'tx, [u8]>> = |
| 94 | + IterKeyVals<'tx, 'cur, Rw, Key, Value>; |
| 95 | + |
| 96 | +/// A flattening DUPFIXED iterator for a synchronized read-only transaction. |
| 97 | +pub type RoDupFixedIterSync<'tx, 'cur, Key = Cow<'tx, [u8]>, const VALUE_SIZE: usize = 0> = |
| 98 | + IterDupFixed<'tx, 'cur, RoSync, Key, VALUE_SIZE>; |
| 99 | + |
| 100 | +/// A flattening DUPFIXED iterator for a synchronized read-write transaction. |
| 101 | +pub type RwDupFixedIterSync<'tx, 'cur, Key = Cow<'tx, [u8]>, const VALUE_SIZE: usize = 0> = |
| 102 | + IterDupFixed<'tx, 'cur, RwSync, Key, VALUE_SIZE>; |
| 103 | + |
| 104 | +/// A flattening DUPFIXED iterator for an unsynchronized read-only transaction. |
| 105 | +pub type RoDupFixedIterUnsync<'tx, 'cur, Key = Cow<'tx, [u8]>, const VALUE_SIZE: usize = 0> = |
| 106 | + IterDupFixed<'tx, 'cur, Ro, Key, VALUE_SIZE>; |
| 107 | + |
| 108 | +/// A flattening DUPFIXED iterator for an unsynchronized read-write transaction. |
| 109 | +pub type RwDupFixedIterUnsync<'tx, 'cur, Key = Cow<'tx, [u8]>, const VALUE_SIZE: usize = 0> = |
| 110 | + IterDupFixed<'tx, 'cur, Rw, Key, VALUE_SIZE>; |
| 111 | + |
| 112 | +/// A single-key DUPFIXED iterator for a synchronized read-only transaction. |
| 113 | +pub type RoDupFixedIterOfKeySync<'tx, 'cur, const VALUE_SIZE: usize = 0> = |
| 114 | + IterDupFixedOfKey<'tx, 'cur, RoSync, VALUE_SIZE>; |
| 115 | + |
| 116 | +/// A single-key DUPFIXED iterator for a synchronized read-write transaction. |
| 117 | +pub type RwDupFixedIterOfKeySync<'tx, 'cur, const VALUE_SIZE: usize = 0> = |
| 118 | + IterDupFixedOfKey<'tx, 'cur, RwSync, VALUE_SIZE>; |
| 119 | + |
| 120 | +/// A single-key DUPFIXED iterator for an unsynchronized read-only transaction. |
| 121 | +pub type RoDupFixedIterOfKeyUnsync<'tx, 'cur, const VALUE_SIZE: usize = 0> = |
| 122 | + IterDupFixedOfKey<'tx, 'cur, Ro, VALUE_SIZE>; |
| 123 | + |
| 124 | +/// A single-key DUPFIXED iterator for an unsynchronized read-write transaction. |
| 125 | +pub type RwDupFixedIterOfKeyUnsync<'tx, 'cur, const VALUE_SIZE: usize = 0> = |
| 126 | + IterDupFixedOfKey<'tx, 'cur, Rw, VALUE_SIZE>; |
0 commit comments