Debian Codegraph @

third_party/rust/rkv/src/backend/impl_lmdb/cursor.rs

raw
1 // Copyright 2018-2019 Mozilla
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not use
4 // this file except in compliance with the License. You may obtain a copy of the
5 // License at http://www.apache.org/licenses/LICENSE-2.0
6 // Unless required by applicable law or agreed to in writing, software distributed
7 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
8 // CONDITIONS OF ANY KIND, either express or implied. See the License for the
9 // specific language governing permissions and limitations under the License.
10
11 use lmdb::Cursor;
12
13 use super::IterImpl;
14 use crate::backend::traits::BackendRoCursor;
15
16 #[derive(Debug)]
17 pub struct RoCursorImpl<'c>(pub(crate) lmdb::RoCursor<'c>);
18
19 impl<'c> BackendRoCursor<'c> for RoCursorImpl<'c> {
20 type Iter = IterImpl<'c, lmdb::RoCursor<'c>>;
21
22 fn into_iter(self) -> Self::Iter {
23 // We call RoCursor.iter() instead of RoCursor.iter_start() because
24 // the latter panics when there are no items in the store, whereas the
25 // former returns an iterator that yields no items. And since we create
26 // the Cursor and don't change its position, we can be sure that a call
27 // to Cursor.iter() will start at the beginning.
28 IterImpl::new(self.0, lmdb::RoCursor::iter)
29 }
30
31 fn into_iter_from<K>(self, key: K) -> Self::Iter
32 where
33 K: AsRef<[u8]> + 'c,
34 {
35 IterImpl::new(self.0, |cursor| cursor.iter_from(key))
36 }
37
38 fn into_iter_dup_of<K>(self, key: K) -> Self::Iter
39 where
40 K: AsRef<[u8]> + 'c,
41 {
42 IterImpl::new(self.0, |cursor| cursor.iter_dup_of(key))
43 }
44 }
45
46 #[derive(Debug)]
47 pub struct RwCursorImpl<'c>(pub(crate) lmdb::RwCursor<'c>);
48
49 impl<'c> BackendRoCursor<'c> for RwCursorImpl<'c> {
50 type Iter = IterImpl<'c, lmdb::RwCursor<'c>>;
51
52 fn into_iter(self) -> Self::Iter {
53 IterImpl::new(self.0, lmdb::RwCursor::iter)
54 }
55
56 fn into_iter_from<K>(self, key: K) -> Self::Iter
57 where
58 K: AsRef<[u8]> + 'c,
59 {
60 IterImpl::new(self.0, |cursor| cursor.iter_from(key))
61 }
62
63 fn into_iter_dup_of<K>(self, key: K) -> Self::Iter
64 where
65 K: AsRef<[u8]> + 'c,
66 {
67 IterImpl::new(self.0, |cursor| cursor.iter_dup_of(key))
68 }
69 }