| 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 super::{snapshot::Snapshot, IterImpl}; |
| 12 |
use crate::backend::traits::BackendRoCursor; |
| 13 |
|
| 14 |
#[derive(Debug)] |
| 15 |
pub struct RoCursorImpl<'c>(pub(crate) &'c Snapshot); |
| 16 |
|
| 17 |
#[cfg(not(feature = "db-dup-sort"))] |
| 18 |
impl<'c> BackendRoCursor<'c> for RoCursorImpl<'c> { |
| 19 |
type Iter = IterImpl<'c>; |
| 20 |
|
| 21 |
fn into_iter(self) -> Self::Iter { |
| 22 |
IterImpl(Box::new(self.0.iter())) |
| 23 |
} |
| 24 |
|
| 25 |
fn into_iter_from<K>(self, key: K) -> Self::Iter |
| 26 |
where |
| 27 |
K: AsRef<[u8]> + 'c, |
| 28 |
{ |
| 29 |
IterImpl(Box::new( |
| 30 |
self.0.iter().skip_while(move |&(k, _)| k < key.as_ref()), |
| 31 |
)) |
| 32 |
} |
| 33 |
|
| 34 |
fn into_iter_dup_of<K>(self, key: K) -> Self::Iter |
| 35 |
where |
| 36 |
K: AsRef<[u8]> + 'c, |
| 37 |
{ |
| 38 |
IterImpl(Box::new( |
| 39 |
self.0.iter().filter(move |&(k, _)| k == key.as_ref()), |
| 40 |
)) |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
#[cfg(feature = "db-dup-sort")] |
| 45 |
impl<'c> BackendRoCursor<'c> for RoCursorImpl<'c> { |
| 46 |
type Iter = IterImpl<'c>; |
| 47 |
|
| 48 |
fn into_iter(self) -> Self::Iter { |
| 49 |
let flattened = self |
| 50 |
.0 |
| 51 |
.iter() |
| 52 |
.flat_map(|(key, values)| values.map(move |value| (key, value))); |
| 53 |
IterImpl(Box::new(flattened)) |
| 54 |
} |
| 55 |
|
| 56 |
fn into_iter_from<K>(self, key: K) -> Self::Iter |
| 57 |
where |
| 58 |
K: AsRef<[u8]> + 'c, |
| 59 |
{ |
| 60 |
let skipped = self.0.iter().skip_while(move |&(k, _)| k < key.as_ref()); |
| 61 |
let flattened = skipped.flat_map(|(key, values)| values.map(move |value| (key, value))); |
| 62 |
IterImpl(Box::new(flattened)) |
| 63 |
} |
| 64 |
|
| 65 |
fn into_iter_dup_of<K>(self, key: K) -> Self::Iter |
| 66 |
where |
| 67 |
K: AsRef<[u8]> + 'c, |
| 68 |
{ |
| 69 |
let filtered = self.0.iter().filter(move |&(k, _)| k == key.as_ref()); |
| 70 |
let flattened = filtered.flat_map(|(key, values)| values.map(move |value| (key, value))); |
| 71 |
IterImpl(Box::new(flattened)) |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
#[derive(Debug)] |
| 76 |
pub struct RwCursorImpl<'c>(&'c mut Snapshot); |
| 77 |
|
| 78 |
impl<'c> BackendRoCursor<'c> for RwCursorImpl<'c> { |
| 79 |
type Iter = IterImpl<'c>; |
| 80 |
|
| 81 |
fn into_iter(self) -> Self::Iter { |
| 82 |
unimplemented!() |
| 83 |
} |
| 84 |
|
| 85 |
fn into_iter_from<K>(self, _key: K) -> Self::Iter |
| 86 |
where |
| 87 |
K: AsRef<[u8]> + 'c, |
| 88 |
{ |
| 89 |
unimplemented!() |
| 90 |
} |
| 91 |
|
| 92 |
fn into_iter_dup_of<K>(self, _key: K) -> Self::Iter |
| 93 |
where |
| 94 |
K: AsRef<[u8]> + 'c, |
| 95 |
{ |
| 96 |
unimplemented!() |
| 97 |
} |
| 98 |
} |