Debian Codegraph @

third_party/rust/rkv/src/store/multi.rs

raw
1 // Copyright 2018 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 std::marker::PhantomData;
12
13 use crate::{
14 backend::{BackendDatabase, BackendFlags, BackendIter, BackendRoCursor, BackendRwTransaction},
15 error::StoreError,
16 helpers::read_transform,
17 readwrite::{Readable, Writer},
18 value::Value,
19 };
20
21 type EmptyResult = Result<(), StoreError>;
22
23 #[derive(Debug, Eq, PartialEq, Copy, Clone)]
24 pub struct MultiStore<D> {
25 db: D,
26 }
27
28 pub struct Iter<'i, I> {
29 iter: I,
30 phantom: PhantomData<&'i ()>,
31 }
32
33 impl<D> MultiStore<D>
34 where
35 D: BackendDatabase,
36 {
37 pub(crate) fn new(db: D) -> MultiStore<D> {
38 MultiStore { db }
39 }
40
41 /// Provides a cursor to all of the values for the duplicate entries that match this
42 /// key
43 pub fn get<'r, R, I, C, K>(&self, reader: &'r R, k: K) -> Result<Iter<'r, I>, StoreError>
44 where
45 R: Readable<'r, Database = D, RoCursor = C>,
46 I: BackendIter<'r>,
47 C: BackendRoCursor<'r, Iter = I>,
48 K: AsRef<[u8]> + 'r,
49 {
50 let cursor = reader.open_ro_cursor(&self.db)?;
51 let iter = cursor.into_iter_dup_of(k);
52
53 Ok(Iter {
54 iter,
55 phantom: PhantomData,
56 })
57 }
58
59 /// Provides the first value that matches this key
60 pub fn get_first<'r, R, K>(&self, reader: &'r R, k: K) -> Result<Option<Value<'r>>, StoreError>
61 where
62 R: Readable<'r, Database = D>,
63 K: AsRef<[u8]>,
64 {
65 reader.get(&self.db, &k)
66 }
67
68 /// Insert a value at the specified key.
69 /// This put will allow duplicate entries. If you wish to have duplicate entries
70 /// rejected, use the `put_with_flags` function and specify NO_DUP_DATA
71 pub fn put<T, K>(&self, writer: &mut Writer<T>, k: K, v: &Value) -> EmptyResult
72 where
73 T: BackendRwTransaction<Database = D>,
74 K: AsRef<[u8]>,
75 {
76 writer.put(&self.db, &k, v, T::Flags::empty())
77 }
78
79 pub fn put_with_flags<T, K>(
80 &self,
81 writer: &mut Writer<T>,
82 k: K,
83 v: &Value,
84 flags: T::Flags,
85 ) -> EmptyResult
86 where
87 T: BackendRwTransaction<Database = D>,
88 K: AsRef<[u8]>,
89 {
90 writer.put(&self.db, &k, v, flags)
91 }
92
93 pub fn delete_all<T, K>(&self, writer: &mut Writer<T>, k: K) -> EmptyResult
94 where
95 T: BackendRwTransaction<Database = D>,
96 K: AsRef<[u8]>,
97 {
98 writer.delete(&self.db, &k, None)
99 }
100
101 pub fn delete<T, K>(&self, writer: &mut Writer<T>, k: K, v: &Value) -> EmptyResult
102 where
103 T: BackendRwTransaction<Database = D>,
104 K: AsRef<[u8]>,
105 {
106 writer.delete(&self.db, &k, Some(&v.to_bytes()?))
107 }
108
109 pub fn clear<T>(&self, writer: &mut Writer<T>) -> EmptyResult
110 where
111 T: BackendRwTransaction<Database = D>,
112 {
113 writer.clear(&self.db)
114 }
115 }
116
117 impl<'i, I> Iterator for Iter<'i, I>
118 where
119 I: BackendIter<'i>,
120 {
121 type Item = Result<(&'i [u8], Value<'i>), StoreError>;
122
123 fn next(&mut self) -> Option<Self::Item> {
124 match self.iter.next() {
125 None => None,
126 Some(Ok((key, bytes))) => match read_transform(Ok(bytes)) {
127 Ok(val) => Some(Ok((key, val))),
128 Err(err) => Some(Err(err)),
129 },
130 Some(Err(err)) => Some(Err(err.into())),
131 }
132 }
133 }