1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
use std::{
    borrow::Cow,
    marker::PhantomData,
    ops::{Bound, RangeBounds},
};

use brontes_libmdbx::{Error, TransactionKind, WriteFlags, RW};
use reth_db::{
    common::{PairResult, ValueOnlyResult},
    cursor::{
        DbCursorRO, DbCursorRW, DbDupCursorRO, DbDupCursorRW, DupWalker, RangeWalker,
        ReverseWalker, Walker,
    },
    table::{DupSort, Encode, Table},
    DatabaseError, DatabaseWriteOperation,
};
use reth_interfaces::db::DatabaseWriteError;

use super::utils::{decode_one, decode_value, decoder, uncompressable_ref_util};

#[macro_export]
macro_rules! decode {
    ($v:expr) => {
        $v.map_err(|e| reth_db::DatabaseError::Read(e.into()))?
            .map(decoder::<T>)
            .transpose()
    };
}

/// Cursor wrapper to access KV items.
#[derive(Debug)]
pub struct LibmdbxCursor<T: Table, K: TransactionKind> {
    /// Inner `libmdbx` cursor.
    pub(crate) inner: brontes_libmdbx::Cursor<K>,
    /// Phantom data to enforce encoding/decoding.
    _dbi:             PhantomData<T>,
}

impl<T: Table, K: TransactionKind> LibmdbxCursor<T, K> {
    pub(crate) fn new(inner: brontes_libmdbx::Cursor<K>) -> Self {
        Self { inner, _dbi: PhantomData }
    }

    pub fn seek_raw(&mut self, key: &[u8]) -> PairResult<T> {
        decode!(self.inner.set_key(key))
    }
}

/// Takes `(key, value)` from the database and decodes it appropriately.

impl<T: Table, K: TransactionKind> DbCursorRO<T> for LibmdbxCursor<T, K> {
    fn first(&mut self) -> PairResult<T> {
        decode!(self.inner.first())
    }

    fn seek_exact(&mut self, key: <T as Table>::Key) -> PairResult<T> {
        decode!(self.inner.set_key(key.encode().as_ref()))
    }

    fn seek(&mut self, key: <T as Table>::Key) -> PairResult<T> {
        decode!(self.inner.set_range(key.encode().as_ref()))
    }

    fn next(&mut self) -> PairResult<T> {
        decode!(self.inner.next())
    }

    fn prev(&mut self) -> PairResult<T> {
        decode!(self.inner.prev())
    }

    fn last(&mut self) -> PairResult<T> {
        decode!(self.inner.last())
    }

    fn current(&mut self) -> PairResult<T> {
        decode!(self.inner.get_current())
    }

    fn walk(&mut self, start_key: Option<T::Key>) -> Result<Walker<'_, T, Self>, DatabaseError> {
        let start = if let Some(start_key) = start_key {
            self.inner
                .set_range(start_key.encode().as_ref())
                .map_err(|e| DatabaseError::Read(e.into()))?
                .map(decoder::<T>)
        } else {
            self.first().transpose()
        };

        Ok(Walker::new(self, start))
    }

    fn walk_range(
        &mut self,
        range: impl RangeBounds<T::Key>,
    ) -> Result<RangeWalker<'_, T, Self>, DatabaseError> {
        let start = match range.start_bound().cloned() {
            Bound::Included(key) => self.inner.set_range(key.encode().as_ref()),
            Bound::Excluded(_key) => {
                unreachable!("Rust doesn't allow for Bound::Excluded in starting bounds");
            }
            Bound::Unbounded => self.inner.first(),
        }
        .map_err(|e| DatabaseError::Read(e.into()))?
        .map(decoder::<T>);

        Ok(RangeWalker::new(self, start, range.end_bound().cloned()))
    }

    fn walk_back(
        &mut self,
        start_key: Option<T::Key>,
    ) -> Result<ReverseWalker<'_, T, Self>, DatabaseError> {
        let start = if let Some(start_key) = start_key {
            decode!(self.inner.set_range(start_key.encode().as_ref()))
        } else {
            self.last()
        }
        .transpose();

        Ok(ReverseWalker::new(self, start))
    }
}

impl<T: DupSort, K: TransactionKind> DbDupCursorRO<T> for LibmdbxCursor<T, K> {
    /// Returns the next `(key, value)` pair of a DUPSORT table.
    fn next_dup(&mut self) -> PairResult<T> {
        decode!(self.inner.next_dup())
    }

    /// Returns the next `(key, value)` pair skipping the duplicates.
    fn next_no_dup(&mut self) -> PairResult<T> {
        decode!(self.inner.next_nodup())
    }

    /// Returns the next `value` of a duplicate `key`.
    fn next_dup_val(&mut self) -> ValueOnlyResult<T> {
        self.inner
            .next_dup()
            .map_err(|e| DatabaseError::Read(e.into()))?
            .map(decode_value::<T>)
            .transpose()
    }

    fn seek_by_key_subkey(
        &mut self,
        key: <T as Table>::Key,
        subkey: <T as DupSort>::SubKey,
    ) -> ValueOnlyResult<T> {
        self.inner
            .get_both_range(key.encode().as_ref(), subkey.encode().as_ref())
            .map_err(|e| DatabaseError::Read(e.into()))?
            .map(decode_one::<T>)
            .transpose()
    }

    /// Depending on its arguments, returns an iterator starting at:
    /// - Some(key), Some(subkey): a `key` item whose data is >= than `subkey`
    /// - Some(key), None: first item of a specified `key`
    /// - None, Some(subkey): like first case, but in the first key
    /// - None, None: first item in the table of a DUPSORT table.
    fn walk_dup(
        &mut self,
        key: Option<T::Key>,
        subkey: Option<T::SubKey>,
    ) -> Result<DupWalker<'_, T, Self>, DatabaseError> {
        let start = match (key, subkey) {
            (Some(key), Some(subkey)) => {
                // encode key and decode it after.
                let key: Vec<u8> = key.encode().into();
                self.inner
                    .get_both_range(key.as_ref(), subkey.encode().as_ref())
                    .map_err(|e| DatabaseError::Read(e.into()))?
                    .map(|val| decoder::<T>((Cow::Owned(key), val)))
            }
            (Some(key), None) => {
                let key: Vec<u8> = key.encode().into();
                self.inner
                    .set(key.as_ref())
                    .map_err(|e| DatabaseError::Read(e.into()))?
                    .map(|val| decoder::<T>((Cow::Owned(key), val)))
            }
            (None, Some(subkey)) => {
                if let Some((key, _)) = self.first()? {
                    let key: Vec<u8> = key.encode().into();
                    self.inner
                        .get_both_range(key.as_ref(), subkey.encode().as_ref())
                        .map_err(|e| DatabaseError::Read(e.into()))?
                        .map(|val| decoder::<T>((Cow::Owned(key), val)))
                } else {
                    let err_code = Error::to_err_code(&Error::NotFound);
                    Some(Err(DatabaseError::Read(err_code.into())))
                }
            }
            (None, None) => self.first().transpose(),
        };

        Ok(DupWalker::<'_, T, Self> { cursor: self, start })
    }
}

impl<T: Table> DbCursorRW<T> for LibmdbxCursor<T, RW> {
    /// Database operation that will update an existing row if a specified value
    /// already exists in a table, and insert a new row if the specified
    /// value doesn't already exist
    ///
    /// For a DUPSORT table, `upsert` will not actually update-or-insert. If the
    /// key already exists, it will append the value to the subkey, even if
    /// the subkeys are the same. So if you want to properly upsert, you'll
    /// need to `seek_exact` & `delete_current` if the key+subkey was found,
    /// before calling `upsert`.
    fn upsert(&mut self, key: T::Key, value: T::Value) -> Result<(), DatabaseError> {
        let (key, value) = uncompressable_ref_util::<T>(key, value);
        self.inner
            .put(&key, &value, WriteFlags::UPSERT)
            .map_err(|e| {
                DatabaseWriteError {
                    info: e.into(),
                    operation: DatabaseWriteOperation::CursorUpsert,
                    table_name: T::NAME,
                    key,
                }
                .into()
            })
    }

    fn insert(&mut self, key: T::Key, value: T::Value) -> Result<(), DatabaseError> {
        let (key, value) = uncompressable_ref_util::<T>(key, value);
        self.inner
            .put(&key, &value, WriteFlags::NO_OVERWRITE)
            .map_err(|e| {
                DatabaseWriteError {
                    info: e.into(),
                    operation: DatabaseWriteOperation::CursorInsert,
                    table_name: T::NAME,
                    key,
                }
                .into()
            })
    }

    /// Appends the data to the end of the table. Consequently, the append
    /// operation will fail if the inserted key is less than the last table
    /// key
    fn append(&mut self, key: T::Key, value: T::Value) -> Result<(), DatabaseError> {
        let (key, value) = uncompressable_ref_util::<T>(key, value);
        self.inner
            .put(&key, &value, WriteFlags::APPEND)
            .map_err(|e| {
                DatabaseWriteError {
                    info: e.into(),
                    operation: DatabaseWriteOperation::CursorAppend,
                    table_name: T::NAME,
                    key,
                }
                .into()
            })
    }

    fn delete_current(&mut self) -> Result<(), DatabaseError> {
        self.inner
            .del(WriteFlags::CURRENT)
            .map_err(|e| DatabaseError::Delete(e.into()))
    }
}

impl<T: DupSort> DbDupCursorRW<T> for LibmdbxCursor<T, RW> {
    fn delete_current_duplicates(&mut self) -> Result<(), DatabaseError> {
        self.inner
            .del(WriteFlags::NO_DUP_DATA)
            .map_err(|e| DatabaseError::Delete(e.into()))
    }

    fn append_dup(&mut self, key: T::Key, value: T::Value) -> Result<(), DatabaseError> {
        let (key, value) = uncompressable_ref_util::<T>(key, value);
        self.inner
            .put(&key, &value, WriteFlags::APPEND_DUP)
            .map_err(|e| {
                DatabaseWriteError {
                    info: e.into(),
                    operation: DatabaseWriteOperation::CursorAppendDup,
                    table_name: T::NAME,
                    key,
                }
                .into()
            })
    }
}