Debian Codegraph @

third_party/rust/rkv/src/backend/impl_safe/snapshot.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 std::{
12 collections::{BTreeMap, BTreeSet},
13 sync::Arc,
14 };
15
16 use serde_derive::{Deserialize, Serialize};
17
18 use super::DatabaseFlagsImpl;
19
20 type Key = Box<[u8]>;
21 type Value = Box<[u8]>;
22
23 #[derive(Debug, Clone, Serialize, Deserialize)]
24 pub struct Snapshot {
25 flags: DatabaseFlagsImpl,
26 #[cfg(not(feature = "db-dup-sort"))]
27 map: Arc<BTreeMap<Key, Value>>,
28 #[cfg(feature = "db-dup-sort")]
29 map: Arc<BTreeMap<Key, BTreeSet<Value>>>,
30 }
31
32 impl Snapshot {
33 pub(crate) fn new(flags: Option<DatabaseFlagsImpl>) -> Snapshot {
34 Snapshot {
35 flags: flags.unwrap_or_default(),
36 map: Default::default(),
37 }
38 }
39
40 pub(crate) fn flags(&self) -> &DatabaseFlagsImpl {
41 &self.flags
42 }
43
44 pub(crate) fn clear(&mut self) {
45 self.map = Default::default();
46 }
47 }
48
49 #[cfg(not(feature = "db-dup-sort"))]
50 impl Snapshot {
51 pub(crate) fn get(&self, key: &[u8]) -> Option<&[u8]> {
52 self.map.get(key).map(|value| value.as_ref())
53 }
54
55 pub(crate) fn put(&mut self, key: &[u8], value: &[u8]) {
56 let map = Arc::make_mut(&mut self.map);
57 map.insert(Box::from(key), Box::from(value));
58 }
59
60 pub(crate) fn del(&mut self, key: &[u8]) -> Option<()> {
61 let map = Arc::make_mut(&mut self.map);
62 map.remove(key).map(|_| ())
63 }
64
65 pub(crate) fn iter(&self) -> impl Iterator<Item = (&[u8], &[u8])> {
66 self.map
67 .iter()
68 .map(|(key, value)| (key.as_ref(), value.as_ref()))
69 }
70 }
71
72 #[cfg(feature = "db-dup-sort")]
73 impl Snapshot {
74 pub(crate) fn get(&self, key: &[u8]) -> Option<&[u8]> {
75 self.map
76 .get(key)
77 .and_then(|v| v.iter().next())
78 .map(|v| v.as_ref())
79 }
80
81 pub(crate) fn put(&mut self, key: &[u8], value: &[u8]) {
82 let map = Arc::make_mut(&mut self.map);
83 match map.get_mut(key) {
84 None => {
85 let mut values = BTreeSet::new();
86 values.insert(Box::from(value));
87 map.insert(Box::from(key), values);
88 }
89 Some(values) => {
90 values.clear();
91 values.insert(Box::from(value));
92 }
93 }
94 }
95
96 pub(crate) fn del(&mut self, key: &[u8]) -> Option<()> {
97 let map = Arc::make_mut(&mut self.map);
98 match map.get_mut(key) {
99 None => None,
100 Some(values) => {
101 let was_empty = values.is_empty();
102 values.clear();
103 Some(()).filter(|_| !was_empty)
104 }
105 }
106 }
107
108 pub(crate) fn iter(&self) -> impl Iterator<Item = (&[u8], impl Iterator<Item = &[u8]>)> {
109 self.map
110 .iter()
111 .map(|(key, values)| (key.as_ref(), values.iter().map(|value| value.as_ref())))
112 }
113 }
114
115 #[cfg(feature = "db-dup-sort")]
116 impl Snapshot {
117 pub(crate) fn put_dup(&mut self, key: &[u8], value: &[u8]) {
118 let map = Arc::make_mut(&mut self.map);
119 match map.get_mut(key) {
120 None => {
121 let mut values = BTreeSet::new();
122 values.insert(Box::from(value));
123 map.insert(Box::from(key), values);
124 }
125 Some(values) => {
126 values.insert(Box::from(value));
127 }
128 }
129 }
130
131 pub(crate) fn del_exact(&mut self, key: &[u8], value: &[u8]) -> Option<()> {
132 let map = Arc::make_mut(&mut self.map);
133 match map.get_mut(key) {
134 None => None,
135 Some(values) => {
136 let was_removed = values.remove(value);
137 Some(()).filter(|_| was_removed)
138 }
139 }
140 }
141 }