summaryrefslogtreecommitdiffstats
path: root/third_party/rust/rkv/src/store
diff options
context:
space:
mode:
Diffstat (limited to 'third_party/rust/rkv/src/store')
-rw-r--r--third_party/rust/rkv/src/store/integer.rs637
-rw-r--r--third_party/rust/rkv/src/store/integermulti.rs709
-rw-r--r--third_party/rust/rkv/src/store/keys.rs46
-rw-r--r--third_party/rust/rkv/src/store/keys/encodables.rs27
-rw-r--r--third_party/rust/rkv/src/store/keys/primitives.rs15
-rw-r--r--third_party/rust/rkv/src/store/multi.rs133
-rw-r--r--third_party/rust/rkv/src/store/single.rs132
7 files changed, 1699 insertions, 0 deletions
diff --git a/third_party/rust/rkv/src/store/integer.rs b/third_party/rust/rkv/src/store/integer.rs
new file mode 100644
index 0000000000..f150f09c5f
--- /dev/null
+++ b/third_party/rust/rkv/src/store/integer.rs
@@ -0,0 +1,637 @@
+// Copyright 2018-2019 Mozilla
+//
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
+// this file except in compliance with the License. You may obtain a copy of the
+// License at http://www.apache.org/licenses/LICENSE-2.0
+// Unless required by applicable law or agreed to in writing, software distributed
+// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
+// CONDITIONS OF ANY KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations under the License.
+
+use std::marker::PhantomData;
+
+use crate::{
+ backend::{BackendDatabase, BackendRwTransaction},
+ error::StoreError,
+ readwrite::{Readable, Writer},
+ store::{
+ keys::{Key, PrimitiveInt},
+ single::SingleStore,
+ },
+ value::Value,
+};
+
+type EmptyResult = Result<(), StoreError>;
+
+#[derive(Debug, Eq, PartialEq, Copy, Clone)]
+pub struct IntegerStore<D, K> {
+ inner: SingleStore<D>,
+ phantom: PhantomData<K>,
+}
+
+impl<D, K> IntegerStore<D, K>
+where
+ D: BackendDatabase,
+ K: PrimitiveInt,
+{
+ pub(crate) fn new(db: D) -> IntegerStore<D, K> {
+ IntegerStore {
+ inner: SingleStore::new(db),
+ phantom: PhantomData,
+ }
+ }
+
+ pub fn get<'r, R>(&self, reader: &'r R, k: K) -> Result<Option<Value<'r>>, StoreError>
+ where
+ R: Readable<'r, Database = D>,
+ {
+ self.inner.get(reader, Key::new(&k)?)
+ }
+
+ pub fn put<T>(&self, writer: &mut Writer<T>, k: K, v: &Value) -> EmptyResult
+ where
+ T: BackendRwTransaction<Database = D>,
+ {
+ self.inner.put(writer, Key::new(&k)?, v)
+ }
+
+ pub fn delete<T>(&self, writer: &mut Writer<T>, k: K) -> EmptyResult
+ where
+ T: BackendRwTransaction<Database = D>,
+ {
+ self.inner.delete(writer, Key::new(&k)?)
+ }
+
+ pub fn clear<T>(&self, writer: &mut Writer<T>) -> EmptyResult
+ where
+ T: BackendRwTransaction<Database = D>,
+ {
+ self.inner.clear(writer)
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::*;
+
+ use std::fs;
+
+ use tempfile::Builder;
+
+ #[test]
+ fn test_integer_keys() {
+ let root = Builder::new()
+ .prefix("test_integer_keys")
+ .tempdir()
+ .expect("tempdir");
+ fs::create_dir_all(root.path()).expect("dir created");
+
+ let k = Rkv::new::<backend::SafeMode>(root.path()).expect("new succeeded");
+ let s = k.open_integer("s", StoreOptions::create()).expect("open");
+
+ macro_rules! test_integer_keys {
+ ($type:ty, $key:expr) => {{
+ let mut writer = k.write().expect("writer");
+
+ s.put(&mut writer, $key, &Value::Str("hello!"))
+ .expect("write");
+ assert_eq!(
+ s.get(&writer, $key).expect("read"),
+ Some(Value::Str("hello!"))
+ );
+ writer.commit().expect("committed");
+
+ let reader = k.read().expect("reader");
+ assert_eq!(
+ s.get(&reader, $key).expect("read"),
+ Some(Value::Str("hello!"))
+ );
+ }};
+ }
+
+ test_integer_keys!(u32, std::u32::MIN);
+ test_integer_keys!(u32, std::u32::MAX);
+ }
+
+ #[test]
+ fn test_clear() {
+ let root = Builder::new()
+ .prefix("test_integer_clear")
+ .tempdir()
+ .expect("tempdir");
+ fs::create_dir_all(root.path()).expect("dir created");
+
+ let k = Rkv::new::<backend::SafeMode>(root.path()).expect("new succeeded");
+ let s = k.open_integer("s", StoreOptions::create()).expect("open");
+
+ {
+ let mut writer = k.write().expect("writer");
+ s.put(&mut writer, 1, &Value::Str("hello!")).expect("write");
+ s.put(&mut writer, 2, &Value::Str("hello!")).expect("write");
+ s.put(&mut writer, 3, &Value::Str("hello!")).expect("write");
+ assert_eq!(s.get(&writer, 1).expect("read"), Some(Value::Str("hello!")));
+ assert_eq!(s.get(&writer, 2).expect("read"), Some(Value::Str("hello!")));
+ assert_eq!(s.get(&writer, 3).expect("read"), Some(Value::Str("hello!")));
+ writer.commit().expect("committed");
+ }
+
+ {
+ let mut writer = k.write().expect("writer");
+ s.clear(&mut writer).expect("cleared");
+ writer.commit().expect("committed");
+
+ let reader = k.read().expect("reader");
+ assert_eq!(s.get(&reader, 1).expect("read"), None);
+ assert_eq!(s.get(&reader, 2).expect("read"), None);
+ assert_eq!(s.get(&reader, 3).expect("read"), None);
+ }
+ }
+
+ #[test]
+ fn test_dup() {
+ let root = Builder::new()
+ .prefix("test_integer_dup")
+ .tempdir()
+ .expect("tempdir");
+ fs::create_dir_all(root.path()).expect("dir created");
+
+ let k = Rkv::new::<backend::SafeMode>(root.path()).expect("new succeeded");
+ let s = k.open_integer("s", StoreOptions::create()).expect("open");
+
+ {
+ let mut writer = k.write().expect("writer");
+ s.put(&mut writer, 1, &Value::Str("hello!")).expect("write");
+ s.put(&mut writer, 1, &Value::Str("foo!")).expect("write");
+ s.put(&mut writer, 1, &Value::Str("bar!")).expect("write");
+ assert_eq!(s.get(&writer, 1).expect("read"), Some(Value::Str("bar!")));
+ assert_eq!(s.get(&writer, 2).expect("read"), None);
+ assert_eq!(s.get(&writer, 3).expect("read"), None);
+ writer.commit().expect("committed");
+ }
+
+ {
+ let mut writer = k.write().expect("writer");
+ s.clear(&mut writer).expect("cleared");
+ writer.commit().expect("committed");
+
+ let reader = k.read().expect("reader");
+ assert_eq!(s.get(&reader, 1).expect("read"), None);
+ assert_eq!(s.get(&reader, 2).expect("read"), None);
+ assert_eq!(s.get(&reader, 3).expect("read"), None);
+ }
+ }
+
+ #[test]
+ fn test_del() {
+ let root = Builder::new()
+ .prefix("test_integer_del")
+ .tempdir()
+ .expect("tempdir");
+ fs::create_dir_all(root.path()).expect("dir created");
+
+ let k = Rkv::new::<backend::SafeMode>(root.path()).expect("new succeeded");
+ let s = k.open_integer("s", StoreOptions::create()).expect("open");
+
+ {
+ let mut writer = k.write().expect("writer");
+ s.put(&mut writer, 1, &Value::Str("hello!")).expect("write");
+ s.put(&mut writer, 1, &Value::Str("foo!")).expect("write");
+ s.put(&mut writer, 1, &Value::Str("bar!")).expect("write");
+ assert_eq!(s.get(&writer, 1).expect("read"), Some(Value::Str("bar!")));
+ assert_eq!(s.get(&writer, 2).expect("read"), None);
+ assert_eq!(s.get(&writer, 3).expect("read"), None);
+ writer.commit().expect("committed");
+ }
+
+ {
+ let mut writer = k.write().expect("writer");
+ s.delete(&mut writer, 1).expect("deleted");
+ writer.commit().expect("committed");
+
+ let reader = k.read().expect("reader");
+ assert_eq!(s.get(&reader, 1).expect("read"), None);
+ }
+
+ {
+ let mut writer = k.write().expect("writer");
+ s.delete(&mut writer, 2).expect_err("not deleted");
+ writer.commit().expect("committed");
+
+ let reader = k.read().expect("reader");
+ assert_eq!(s.get(&reader, 2).expect("read"), None);
+ }
+
+ {
+ let mut writer = k.write().expect("writer");
+ s.delete(&mut writer, 3).expect_err("not deleted");
+ writer.commit().expect("committed");
+
+ let reader = k.read().expect("reader");
+ assert_eq!(s.get(&reader, 3).expect("read"), None);
+ }
+ }
+
+ #[test]
+ fn test_persist() {
+ let root = Builder::new()
+ .prefix("test_integer_persist")
+ .tempdir()
+ .expect("tempdir");
+ fs::create_dir_all(root.path()).expect("dir created");
+
+ {
+ let k = Rkv::new::<backend::SafeMode>(root.path()).expect("new succeeded");
+ let s = k.open_integer("s", StoreOptions::create()).expect("open");
+
+ let mut writer = k.write().expect("writer");
+ s.put(&mut writer, 1, &Value::Str("hello!")).expect("write");
+ s.put(&mut writer, 2, &Value::Str("hello!")).expect("write");
+ s.put(&mut writer, 3, &Value::Str("hello!")).expect("write");
+ assert_eq!(s.get(&writer, 1).expect("read"), Some(Value::Str("hello!")));
+ assert_eq!(s.get(&writer, 2).expect("read"), Some(Value::Str("hello!")));
+ assert_eq!(s.get(&writer, 3).expect("read"), Some(Value::Str("hello!")));
+ writer.commit().expect("committed");
+ }
+
+ {
+ let k = Rkv::new::<backend::SafeMode>(root.path()).expect("new succeeded");
+ let s = k.open_integer("s", StoreOptions::create()).expect("open");
+
+ let reader = k.read().expect("reader");
+ assert_eq!(s.get(&reader, 1).expect("read"), Some(Value::Str("hello!")));
+ assert_eq!(s.get(&reader, 2).expect("read"), Some(Value::Str("hello!")));
+ assert_eq!(s.get(&reader, 3).expect("read"), Some(Value::Str("hello!")));
+ }
+ }
+
+ #[test]
+ fn test_intertwine_read_write() {
+ let root = Builder::new()
+ .prefix("test_integer_intertwine_read_write")
+ .tempdir()
+ .expect("tempdir");
+ fs::create_dir_all(root.path()).expect("dir created");
+
+ let k = Rkv::new::<backend::SafeMode>(root.path()).expect("new succeeded");
+ let s = k.open_integer("s", StoreOptions::create()).expect("open");
+
+ {
+ let mut writer = k.write().expect("writer");
+ s.put(&mut writer, 1, &Value::Str("hello!")).expect("write");
+ assert_eq!(s.get(&writer, 1).expect("read"), Some(Value::Str("hello!")));
+ assert_eq!(s.get(&writer, 2).expect("read"), None);
+ assert_eq!(s.get(&writer, 3).expect("read"), None);
+ writer.commit().expect("committed");
+ }
+
+ let reader = k.read().expect("reader");
+ let mut writer = k.write().expect("writer");
+
+ {
+ assert_eq!(s.get(&reader, 1).expect("read"), Some(Value::Str("hello!")));
+ assert_eq!(s.get(&reader, 2).expect("read"), None);
+ assert_eq!(s.get(&reader, 3).expect("read"), None);
+ }
+
+ {
+ s.put(&mut writer, 1, &Value::Str("goodbye!"))
+ .expect("write");
+ s.put(&mut writer, 2, &Value::Str("goodbye!"))
+ .expect("write");
+ s.put(&mut writer, 3, &Value::Str("goodbye!"))
+ .expect("write");
+ assert_eq!(
+ s.get(&writer, 1).expect("read"),
+ Some(Value::Str("goodbye!"))
+ );
+ assert_eq!(
+ s.get(&writer, 2).expect("read"),
+ Some(Value::Str("goodbye!"))
+ );
+ assert_eq!(
+ s.get(&writer, 3).expect("read"),
+ Some(Value::Str("goodbye!"))
+ );
+ writer.commit().expect("committed");
+ }
+
+ {
+ assert_eq!(s.get(&reader, 1).expect("read"), Some(Value::Str("hello!")));
+ assert_eq!(s.get(&reader, 2).expect("read"), None);
+ assert_eq!(s.get(&reader, 3).expect("read"), None);
+ }
+
+ {
+ let mut writer = k.write().expect("writer");
+ s.put(&mut writer, 1, &Value::Str("hello!")).expect("write");
+ assert_eq!(s.get(&writer, 1).expect("read"), Some(Value::Str("hello!")));
+ assert_eq!(
+ s.get(&writer, 2).expect("read"),
+ Some(Value::Str("goodbye!"))
+ );
+ assert_eq!(
+ s.get(&writer, 3).expect("read"),
+ Some(Value::Str("goodbye!"))
+ );
+ writer.commit().expect("committed");
+ }
+
+ {
+ let reader = k.write().expect("reader");
+ assert_eq!(s.get(&reader, 1).expect("read"), Some(Value::Str("hello!")));
+ assert_eq!(
+ s.get(&reader, 2).expect("read"),
+ Some(Value::Str("goodbye!"))
+ );
+ assert_eq!(
+ s.get(&reader, 3).expect("read"),
+ Some(Value::Str("goodbye!"))
+ );
+ reader.commit().expect("committed");
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests_safe {
+ use super::*;
+ use crate::*;
+
+ use std::fs;
+
+ use tempfile::Builder;
+
+ #[test]
+ fn test_integer_keys() {
+ let root = Builder::new()
+ .prefix("test_integer_keys")
+ .tempdir()
+ .expect("tempdir");
+ fs::create_dir_all(root.path()).expect("dir created");
+
+ let k = Rkv::new::<backend::SafeMode>(root.path()).expect("new succeeded");
+ let s = k.open_integer("s", StoreOptions::create()).expect("open");
+
+ macro_rules! test_integer_keys {
+ ($type:ty, $key:expr) => {{
+ let mut writer = k.write().expect("writer");
+
+ s.put(&mut writer, $key, &Value::Str("hello!"))
+ .expect("write");
+ assert_eq!(
+ s.get(&writer, $key).expect("read"),
+ Some(Value::Str("hello!"))
+ );
+ writer.commit().expect("committed");
+
+ let reader = k.read().expect("reader");
+ assert_eq!(
+ s.get(&reader, $key).expect("read"),
+ Some(Value::Str("hello!"))
+ );
+ }};
+ }
+
+ test_integer_keys!(u32, std::u32::MIN);
+ test_integer_keys!(u32, std::u32::MAX);
+ }
+
+ #[test]
+ fn test_clear() {
+ let root = Builder::new()
+ .prefix("test_integer_clear")
+ .tempdir()
+ .expect("tempdir");
+ fs::create_dir_all(root.path()).expect("dir created");
+
+ let k = Rkv::new::<backend::SafeMode>(root.path()).expect("new succeeded");
+ let s = k.open_integer("s", StoreOptions::create()).expect("open");
+
+ {
+ let mut writer = k.write().expect("writer");
+ s.put(&mut writer, 1, &Value::Str("hello!")).expect("write");
+ s.put(&mut writer, 2, &Value::Str("hello!")).expect("write");
+ s.put(&mut writer, 3, &Value::Str("hello!")).expect("write");
+ assert_eq!(s.get(&writer, 1).expect("read"), Some(Value::Str("hello!")));
+ assert_eq!(s.get(&writer, 2).expect("read"), Some(Value::Str("hello!")));
+ assert_eq!(s.get(&writer, 3).expect("read"), Some(Value::Str("hello!")));
+ writer.commit().expect("committed");
+ }
+
+ {
+ let mut writer = k.write().expect("writer");
+ s.clear(&mut writer).expect("cleared");
+ writer.commit().expect("committed");
+
+ let reader = k.read().expect("reader");
+ assert_eq!(s.get(&reader, 1).expect("read"), None);
+ assert_eq!(s.get(&reader, 2).expect("read"), None);
+ assert_eq!(s.get(&reader, 3).expect("read"), None);
+ }
+ }
+
+ #[test]
+ fn test_dup() {
+ let root = Builder::new()
+ .prefix("test_integer_dup")
+ .tempdir()
+ .expect("tempdir");
+ fs::create_dir_all(root.path()).expect("dir created");
+
+ let k = Rkv::new::<backend::SafeMode>(root.path()).expect("new succeeded");
+ let s = k.open_integer("s", StoreOptions::create()).expect("open");
+
+ {
+ let mut writer = k.write().expect("writer");
+ s.put(&mut writer, 1, &Value::Str("hello!")).expect("write");
+ s.put(&mut writer, 1, &Value::Str("foo!")).expect("write");
+ s.put(&mut writer, 1, &Value::Str("bar!")).expect("write");
+ assert_eq!(s.get(&writer, 1).expect("read"), Some(Value::Str("bar!")));
+ assert_eq!(s.get(&writer, 2).expect("read"), None);
+ assert_eq!(s.get(&writer, 3).expect("read"), None);
+ writer.commit().expect("committed");
+ }
+
+ {
+ let mut writer = k.write().expect("writer");
+ s.clear(&mut writer).expect("cleared");
+ writer.commit().expect("committed");
+
+ let reader = k.read().expect("reader");
+ assert_eq!(s.get(&reader, 1).expect("read"), None);
+ assert_eq!(s.get(&reader, 2).expect("read"), None);
+ assert_eq!(s.get(&reader, 3).expect("read"), None);
+ }
+ }
+
+ #[test]
+ fn test_del() {
+ let root = Builder::new()
+ .prefix("test_integer_del")
+ .tempdir()
+ .expect("tempdir");
+ fs::create_dir_all(root.path()).expect("dir created");
+
+ let k = Rkv::new::<backend::SafeMode>(root.path()).expect("new succeeded");
+ let s = k.open_integer("s", StoreOptions::create()).expect("open");
+
+ {
+ let mut writer = k.write().expect("writer");
+ s.put(&mut writer, 1, &Value::Str("hello!")).expect("write");
+ s.put(&mut writer, 1, &Value::Str("foo!")).expect("write");
+ s.put(&mut writer, 1, &Value::Str("bar!")).expect("write");
+ assert_eq!(s.get(&writer, 1).expect("read"), Some(Value::Str("bar!")));
+ assert_eq!(s.get(&writer, 2).expect("read"), None);
+ assert_eq!(s.get(&writer, 3).expect("read"), None);
+ writer.commit().expect("committed");
+ }
+
+ {
+ let mut writer = k.write().expect("writer");
+ s.delete(&mut writer, 1).expect("deleted");
+ writer.commit().expect("committed");
+
+ let reader = k.read().expect("reader");
+ assert_eq!(s.get(&reader, 1).expect("read"), None);
+ }
+
+ {
+ let mut writer = k.write().expect("writer");
+ s.delete(&mut writer, 2).expect_err("not deleted");
+ writer.commit().expect("committed");
+
+ let reader = k.read().expect("reader");
+ assert_eq!(s.get(&reader, 2).expect("read"), None);
+ }
+
+ {
+ let mut writer = k.write().expect("writer");
+ s.delete(&mut writer, 3).expect_err("not deleted");
+ writer.commit().expect("committed");
+
+ let reader = k.read().expect("reader");
+ assert_eq!(s.get(&reader, 3).expect("read"), None);
+ }
+ }
+
+ #[test]
+ fn test_persist() {
+ let root = Builder::new()
+ .prefix("test_integer_persist")
+ .tempdir()
+ .expect("tempdir");
+ fs::create_dir_all(root.path()).expect("dir created");
+
+ {
+ let k = Rkv::new::<backend::SafeMode>(root.path()).expect("new succeeded");
+ let s = k.open_integer("s", StoreOptions::create()).expect("open");
+
+ let mut writer = k.write().expect("writer");
+ s.put(&mut writer, 1, &Value::Str("hello!")).expect("write");
+ s.put(&mut writer, 2, &Value::Str("hello!")).expect("write");
+ s.put(&mut writer, 3, &Value::Str("hello!")).expect("write");
+ assert_eq!(s.get(&writer, 1).expect("read"), Some(Value::Str("hello!")));
+ assert_eq!(s.get(&writer, 2).expect("read"), Some(Value::Str("hello!")));
+ assert_eq!(s.get(&writer, 3).expect("read"), Some(Value::Str("hello!")));
+ writer.commit().expect("committed");
+ }
+
+ {
+ let k = Rkv::new::<backend::SafeMode>(root.path()).expect("new succeeded");
+ let s = k.open_integer("s", StoreOptions::create()).expect("open");
+
+ let reader = k.read().expect("reader");
+ assert_eq!(s.get(&reader, 1).expect("read"), Some(Value::Str("hello!")));
+ assert_eq!(s.get(&reader, 2).expect("read"), Some(Value::Str("hello!")));
+ assert_eq!(s.get(&reader, 3).expect("read"), Some(Value::Str("hello!")));
+ }
+ }
+
+ #[test]
+ fn test_intertwine_read_write() {
+ let root = Builder::new()
+ .prefix("test_integer_intertwine_read_write")
+ .tempdir()
+ .expect("tempdir");
+ fs::create_dir_all(root.path()).expect("dir created");
+
+ let k = Rkv::new::<backend::SafeMode>(root.path()).expect("new succeeded");
+ let s = k.open_integer("s", StoreOptions::create()).expect("open");
+
+ {
+ let mut writer = k.write().expect("writer");
+ s.put(&mut writer, 1, &Value::Str("hello!")).expect("write");
+ assert_eq!(s.get(&writer, 1).expect("read"), Some(Value::Str("hello!")));
+ assert_eq!(s.get(&writer, 2).expect("read"), None);
+ assert_eq!(s.get(&writer, 3).expect("read"), None);
+ writer.commit().expect("committed");
+ }
+
+ let reader = k.read().expect("reader");
+ let mut writer = k.write().expect("writer");
+
+ {
+ assert_eq!(s.get(&reader, 1).expect("read"), Some(Value::Str("hello!")));
+ assert_eq!(s.get(&reader, 2).expect("read"), None);
+ assert_eq!(s.get(&reader, 3).expect("read"), None);
+ }
+
+ {
+ s.put(&mut writer, 1, &Value::Str("goodbye!"))
+ .expect("write");
+ s.put(&mut writer, 2, &Value::Str("goodbye!"))
+ .expect("write");
+ s.put(&mut writer, 3, &Value::Str("goodbye!"))
+ .expect("write");
+ assert_eq!(
+ s.get(&writer, 1).expect("read"),
+ Some(Value::Str("goodbye!"))
+ );
+ assert_eq!(
+ s.get(&writer, 2).expect("read"),
+ Some(Value::Str("goodbye!"))
+ );
+ assert_eq!(
+ s.get(&writer, 3).expect("read"),
+ Some(Value::Str("goodbye!"))
+ );
+ writer.commit().expect("committed");
+ }
+
+ {
+ assert_eq!(s.get(&reader, 1).expect("read"), Some(Value::Str("hello!")));
+ assert_eq!(s.get(&reader, 2).expect("read"), None);
+ assert_eq!(s.get(&reader, 3).expect("read"), None);
+ }
+
+ {
+ let mut writer = k.write().expect("writer");
+ s.put(&mut writer, 1, &Value::Str("hello!")).expect("write");
+ assert_eq!(s.get(&writer, 1).expect("read"), Some(Value::Str("hello!")));
+ assert_eq!(
+ s.get(&writer, 2).expect("read"),
+ Some(Value::Str("goodbye!"))
+ );
+ assert_eq!(
+ s.get(&writer, 3).expect("read"),
+ Some(Value::Str("goodbye!"))
+ );
+ writer.commit().expect("committed");
+ }
+
+ {
+ let reader = k.write().expect("reader");
+ assert_eq!(s.get(&reader, 1).expect("read"), Some(Value::Str("hello!")));
+ assert_eq!(
+ s.get(&reader, 2).expect("read"),
+ Some(Value::Str("goodbye!"))
+ );
+ assert_eq!(
+ s.get(&reader, 3).expect("read"),
+ Some(Value::Str("goodbye!"))
+ );
+ reader.commit().expect("committed");
+ }
+ }
+}
diff --git a/third_party/rust/rkv/src/store/integermulti.rs b/third_party/rust/rkv/src/store/integermulti.rs
new file mode 100644
index 0000000000..71dd0aae04
--- /dev/null
+++ b/third_party/rust/rkv/src/store/integermulti.rs
@@ -0,0 +1,709 @@
+// Copyright 2018 Mozilla
+//
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
+// this file except in compliance with the License. You may obtain a copy of the
+// License at http://www.apache.org/licenses/LICENSE-2.0
+// Unless required by applicable law or agreed to in writing, software distributed
+// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
+// CONDITIONS OF ANY KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations under the License.
+
+use std::marker::PhantomData;
+
+use crate::{
+ backend::{BackendDatabase, BackendIter, BackendRoCursor, BackendRwTransaction},
+ error::StoreError,
+ readwrite::{Readable, Writer},
+ store::{
+ keys::{Key, PrimitiveInt},
+ multi::{Iter, MultiStore},
+ },
+ value::Value,
+};
+
+type EmptyResult = Result<(), StoreError>;
+
+#[derive(Debug, Eq, PartialEq, Copy, Clone)]
+pub struct MultiIntegerStore<D, K> {
+ inner: MultiStore<D>,
+ phantom: PhantomData<K>,
+}
+
+impl<D, K> MultiIntegerStore<D, K>
+where
+ D: BackendDatabase,
+ K: PrimitiveInt,
+{
+ pub(crate) fn new(db: D) -> MultiIntegerStore<D, K> {
+ MultiIntegerStore {
+ inner: MultiStore::new(db),
+ phantom: PhantomData,
+ }
+ }
+
+ pub fn get<'r, R, I, C>(&self, reader: &'r R, k: K) -> Result<Iter<'r, I>, StoreError>
+ where
+ R: Readable<'r, Database = D, RoCursor = C>,
+ I: BackendIter<'r>,
+ C: BackendRoCursor<'r, Iter = I>,
+ K: 'r,
+ {
+ self.inner.get(reader, Key::new(&k)?)
+ }
+
+ pub fn get_first<'r, R>(&self, reader: &'r R, k: K) -> Result<Option<Value<'r>>, StoreError>
+ where
+ R: Readable<'r, Database = D>,
+ {
+ self.inner.get_first(reader, Key::new(&k)?)
+ }
+
+ pub fn put<T>(&self, writer: &mut Writer<T>, k: K, v: &Value) -> EmptyResult
+ where
+ T: BackendRwTransaction<Database = D>,
+ {
+ self.inner.put(writer, Key::new(&k)?, v)
+ }
+
+ pub fn put_with_flags<T>(
+ &self,
+ writer: &mut Writer<T>,
+ k: K,
+ v: &Value,
+ flags: T::Flags,
+ ) -> EmptyResult
+ where
+ T: BackendRwTransaction<Database = D>,
+ {
+ self.inner.put_with_flags(writer, Key::new(&k)?, v, flags)
+ }
+
+ pub fn delete_all<T>(&self, writer: &mut Writer<T>, k: K) -> EmptyResult
+ where
+ T: BackendRwTransaction<Database = D>,
+ {
+ self.inner.delete_all(writer, Key::new(&k)?)
+ }
+
+ pub fn delete<T>(&self, writer: &mut Writer<T>, k: K, v: &Value) -> EmptyResult
+ where
+ T: BackendRwTransaction<Database = D>,
+ {
+ self.inner.delete(writer, Key::new(&k)?, v)
+ }
+
+ pub fn clear<T>(&self, writer: &mut Writer<T>) -> EmptyResult
+ where
+ T: BackendRwTransaction<Database = D>,
+ {
+ self.inner.clear(writer)
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::*;
+
+ use std::fs;
+
+ use tempfile::Builder;
+
+ #[test]
+ fn test_integer_keys() {
+ let root = Builder::new()
+ .prefix("test_integer_keys")
+ .tempdir()
+ .expect("tempdir");
+ fs::create_dir_all(root.path()).expect("dir created");
+
+ let k = Rkv::new::<backend::SafeMode>(root.path()).expect("new succeeded");
+ let s = k
+ .open_multi_integer("s", StoreOptions::create())
+ .expect("open");
+
+ macro_rules! test_integer_keys {
+ ($type:ty, $key:expr) => {{
+ let mut writer = k.write().expect("writer");
+
+ s.put(&mut writer, $key, &Value::Str("hello!"))
+ .expect("write");
+ assert_eq!(
+ s.get_first(&writer, $key).expect("read"),
+ Some(Value::Str("hello!"))
+ );
+ writer.commit().expect("committed");
+
+ let reader = k.read().expect("reader");
+ assert_eq!(
+ s.get_first(&reader, $key).expect("read"),
+ Some(Value::Str("hello!"))
+ );
+ }};
+ }
+
+ test_integer_keys!(u32, std::u32::MIN);
+ test_integer_keys!(u32, std::u32::MAX);
+ }
+
+ #[test]
+ fn test_clear() {
+ let root = Builder::new()
+ .prefix("test_multi_integer_clear")
+ .tempdir()
+ .expect("tempdir");
+ fs::create_dir_all(root.path()).expect("dir created");
+
+ let k = Rkv::new::<backend::SafeMode>(root.path()).expect("new succeeded");
+ let s = k
+ .open_multi_integer("s", StoreOptions::create())
+ .expect("open");
+
+ {
+ let mut writer = k.write().expect("writer");
+ s.put(&mut writer, 1, &Value::Str("hello!")).expect("write");
+ s.put(&mut writer, 1, &Value::Str("hello1!"))
+ .expect("write");
+ s.put(&mut writer, 2, &Value::Str("hello!")).expect("write");
+ assert_eq!(
+ s.get_first(&writer, 1).expect("read"),
+ Some(Value::Str("hello!"))
+ );
+ assert_eq!(
+ s.get_first(&writer, 2).expect("read"),
+ Some(Value::Str("hello!"))
+ );
+ assert_eq!(s.get_first(&writer, 3).expect("read"), None);
+ writer.commit().expect("committed");
+ }
+
+ {
+ let mut writer = k.write().expect("writer");
+ s.clear(&mut writer).expect("cleared");
+ writer.commit().expect("committed");
+
+ let reader = k.read().expect("reader");
+ assert_eq!(s.get_first(&reader, 1).expect("read"), None);
+ assert_eq!(s.get_first(&reader, 2).expect("read"), None);
+ assert_eq!(s.get_first(&reader, 3).expect("read"), None);
+ }
+ }
+
+ #[test]
+ fn test_dup() {
+ let root = Builder::new()
+ .prefix("test_multi_integer_dup")
+ .tempdir()
+ .expect("tempdir");
+ fs::create_dir_all(root.path()).expect("dir created");
+
+ let k = Rkv::new::<backend::SafeMode>(root.path()).expect("new succeeded");
+ let s = k
+ .open_multi_integer("s", StoreOptions::create())
+ .expect("open");
+
+ {
+ let mut writer = k.write().expect("writer");
+ s.put(&mut writer, 1, &Value::Str("hello!")).expect("write");
+ s.put(&mut writer, 1, &Value::Str("hello!")).expect("write");
+ s.put(&mut writer, 1, &Value::Str("hello1!"))
+ .expect("write");
+ assert_eq!(
+ s.get_first(&writer, 1).expect("read"),
+ Some(Value::Str("hello!"))
+ );
+ assert_eq!(s.get_first(&writer, 2).expect("read"), None);
+ assert_eq!(s.get_first(&writer, 3).expect("read"), None);
+ writer.commit().expect("committed");
+ }
+
+ {
+ let mut writer = k.write().expect("writer");
+ s.clear(&mut writer).expect("cleared");
+ writer.commit().expect("committed");
+
+ let reader = k.read().expect("reader");
+ assert_eq!(s.get_first(&reader, 1).expect("read"), None);
+ assert_eq!(s.get_first(&reader, 2).expect("read"), None);
+ assert_eq!(s.get_first(&reader, 3).expect("read"), None);
+ }
+ }
+
+ #[test]
+ fn test_dup_2() {
+ let root = Builder::new()
+ .prefix("test_multi_integer_dup")
+ .tempdir()
+ .expect("tempdir");
+ fs::create_dir_all(root.path()).expect("dir created");
+
+ let k = Rkv::new::<backend::SafeMode>(root.path()).expect("new succeeded");
+ let s = k
+ .open_multi_integer("s", StoreOptions::create())
+ .expect("open");
+
+ {
+ let mut writer = k.write().expect("writer");
+ s.put(&mut writer, 1, &Value::Str("hello!")).expect("write");
+ s.put(&mut writer, 1, &Value::Str("hello!")).expect("write");
+ s.put(&mut writer, 1, &Value::Str("hello1!"))
+ .expect("write");
+
+ let mut iter = s.get(&writer, 1).expect("read");
+ assert_eq!(
+ iter.next().expect("first").expect("ok").1,
+ Value::Str("hello!")
+ );
+ assert_eq!(
+ iter.next().expect("second").expect("ok").1,
+ Value::Str("hello1!")
+ );
+ assert!(iter.next().is_none());
+ }
+ }
+
+ #[test]
+ fn test_del() {
+ let root = Builder::new()
+ .prefix("test_multi_integer_dup")
+ .tempdir()
+ .expect("tempdir");
+ fs::create_dir_all(root.path()).expect("dir created");
+
+ let k = Rkv::new::<backend::SafeMode>(root.path()).expect("new succeeded");
+ let s = k
+ .open_multi_integer("s", StoreOptions::create())
+ .expect("open");
+
+ {
+ let mut writer = k.write().expect("writer");
+ s.put(&mut writer, 1, &Value::Str("hello!")).expect("write");
+ s.put(&mut writer, 1, &Value::Str("hello!")).expect("write");
+ s.put(&mut writer, 1, &Value::Str("hello1!"))
+ .expect("write");
+ {
+ let mut iter = s.get(&writer, 1).expect("read");
+ assert_eq!(
+ iter.next().expect("first").expect("ok").1,
+ Value::Str("hello!")
+ );
+ assert_eq!(
+ iter.next().expect("second").expect("ok").1,
+ Value::Str("hello1!")
+ );
+ assert!(iter.next().is_none());
+ }
+ writer.commit().expect("committed");
+ }
+
+ {
+ let mut writer = k.write().expect("writer");
+ s.delete(&mut writer, 1, &Value::Str("hello!"))
+ .expect("deleted");
+ writer.commit().expect("committed");
+
+ let reader = k.read().expect("reader");
+ let mut iter = s.get(&reader, 1).expect("read");
+ assert_eq!(
+ iter.next().expect("first").expect("ok").1,
+ Value::Str("hello1!")
+ );
+ assert!(iter.next().is_none());
+ }
+
+ {
+ let mut writer = k.write().expect("writer");
+ s.delete(&mut writer, 1, &Value::Str("hello!"))
+ .expect_err("deleted");
+ writer.commit().expect("committed");
+
+ let reader = k.read().expect("reader");
+ let mut iter = s.get(&reader, 1).expect("read");
+ assert_eq!(
+ iter.next().expect("first").expect("ok").1,
+ Value::Str("hello1!")
+ );
+ assert!(iter.next().is_none());
+ }
+
+ {
+ let mut writer = k.write().expect("writer");
+ s.delete(&mut writer, 1, &Value::Str("hello1!"))
+ .expect("deleted");
+ writer.commit().expect("committed");
+
+ let reader = k.read().expect("reader");
+ let mut iter = s.get(&reader, 1).expect("read");
+ assert!(iter.next().is_none());
+ }
+
+ {
+ let mut writer = k.write().expect("writer");
+ s.delete(&mut writer, 1, &Value::Str("hello1!"))
+ .expect_err("deleted");
+ writer.commit().expect("committed");
+
+ let reader = k.read().expect("reader");
+ let mut iter = s.get(&reader, 1).expect("read");
+ assert!(iter.next().is_none());
+ }
+ }
+
+ #[test]
+ fn test_persist() {
+ let root = Builder::new()
+ .prefix("test_multi_integer_persist")
+ .tempdir()
+ .expect("tempdir");
+ fs::create_dir_all(root.path()).expect("dir created");
+
+ {
+ let k = Rkv::new::<backend::SafeMode>(root.path()).expect("new succeeded");
+ let s = k
+ .open_multi_integer("s", StoreOptions::create())
+ .expect("open");
+
+ let mut writer = k.write().expect("writer");
+ s.put(&mut writer, 1, &Value::Str("hello!")).expect("write");
+ s.put(&mut writer, 1, &Value::Str("hello1!"))
+ .expect("write");
+ s.put(&mut writer, 2, &Value::Str("hello!")).expect("write");
+ {
+ let mut iter = s.get(&writer, 1).expect("read");
+ assert_eq!(
+ iter.next().expect("first").expect("ok").1,
+ Value::Str("hello!")
+ );
+ assert_eq!(
+ iter.next().expect("second").expect("ok").1,
+ Value::Str("hello1!")
+ );
+ assert!(iter.next().is_none());
+ }
+ writer.commit().expect("committed");
+ }
+
+ {
+ let k = Rkv::new::<backend::SafeMode>(root.path()).expect("new succeeded");
+ let s = k
+ .open_multi_integer("s", StoreOptions::create())
+ .expect("open");
+
+ let reader = k.read().expect("reader");
+ let mut iter = s.get(&reader, 1).expect("read");
+ assert_eq!(
+ iter.next().expect("first").expect("ok").1,
+ Value::Str("hello!")
+ );
+ assert_eq!(
+ iter.next().expect("second").expect("ok").1,
+ Value::Str("hello1!")
+ );
+ assert!(iter.next().is_none());
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests_safe {
+ use super::*;
+ use crate::*;
+
+ use std::fs;
+
+ use tempfile::Builder;
+
+ #[test]
+ fn test_integer_keys() {
+ let root = Builder::new()
+ .prefix("test_integer_keys")
+ .tempdir()
+ .expect("tempdir");
+ fs::create_dir_all(root.path()).expect("dir created");
+
+ let k = Rkv::new::<backend::SafeMode>(root.path()).expect("new succeeded");
+ let s = k
+ .open_multi_integer("s", StoreOptions::create())
+ .expect("open");
+
+ macro_rules! test_integer_keys {
+ ($type:ty, $key:expr) => {{
+ let mut writer = k.write().expect("writer");
+
+ s.put(&mut writer, $key, &Value::Str("hello!"))
+ .expect("write");
+ assert_eq!(
+ s.get_first(&writer, $key).expect("read"),
+ Some(Value::Str("hello!"))
+ );
+ writer.commit().expect("committed");
+
+ let reader = k.read().expect("reader");
+ assert_eq!(
+ s.get_first(&reader, $key).expect("read"),
+ Some(Value::Str("hello!"))
+ );
+ }};
+ }
+
+ test_integer_keys!(u32, std::u32::MIN);
+ test_integer_keys!(u32, std::u32::MAX);
+ }
+
+ #[test]
+ fn test_clear() {
+ let root = Builder::new()
+ .prefix("test_multi_integer_clear")
+ .tempdir()
+ .expect("tempdir");
+ fs::create_dir_all(root.path()).expect("dir created");
+
+ let k = Rkv::new::<backend::SafeMode>(root.path()).expect("new succeeded");
+ let s = k
+ .open_multi_integer("s", StoreOptions::create())
+ .expect("open");
+
+ {
+ let mut writer = k.write().expect("writer");
+ s.put(&mut writer, 1, &Value::Str("hello!")).expect("write");
+ s.put(&mut writer, 1, &Value::Str("hello1!"))
+ .expect("write");
+ s.put(&mut writer, 2, &Value::Str("hello!")).expect("write");
+ assert_eq!(
+ s.get_first(&writer, 1).expect("read"),
+ Some(Value::Str("hello!"))
+ );
+ assert_eq!(
+ s.get_first(&writer, 2).expect("read"),
+ Some(Value::Str("hello!"))
+ );
+ assert_eq!(s.get_first(&writer, 3).expect("read"), None);
+ writer.commit().expect("committed");
+ }
+
+ {
+ let mut writer = k.write().expect("writer");
+ s.clear(&mut writer).expect("cleared");
+ writer.commit().expect("committed");
+
+ let reader = k.read().expect("reader");
+ assert_eq!(s.get_first(&reader, 1).expect("read"), None);
+ assert_eq!(s.get_first(&reader, 2).expect("read"), None);
+ assert_eq!(s.get_first(&reader, 3).expect("read"), None);
+ }
+ }
+
+ #[test]
+ fn test_dup() {
+ let root = Builder::new()
+ .prefix("test_multi_integer_dup")
+ .tempdir()
+ .expect("tempdir");
+ fs::create_dir_all(root.path()).expect("dir created");
+
+ let k = Rkv::new::<backend::SafeMode>(root.path()).expect("new succeeded");
+ let s = k
+ .open_multi_integer("s", StoreOptions::create())
+ .expect("open");
+
+ {
+ let mut writer = k.write().expect("writer");
+ s.put(&mut writer, 1, &Value::Str("hello!")).expect("write");
+ s.put(&mut writer, 1, &Value::Str("hello!")).expect("write");
+ s.put(&mut writer, 1, &Value::Str("hello1!"))
+ .expect("write");
+ assert_eq!(
+ s.get_first(&writer, 1).expect("read"),
+ Some(Value::Str("hello!"))
+ );
+ assert_eq!(s.get_first(&writer, 2).expect("read"), None);
+ assert_eq!(s.get_first(&writer, 3).expect("read"), None);
+ writer.commit().expect("committed");
+ }
+
+ {
+ let mut writer = k.write().expect("writer");
+ s.clear(&mut writer).expect("cleared");
+ writer.commit().expect("committed");
+
+ let reader = k.read().expect("reader");
+ assert_eq!(s.get_first(&reader, 1).expect("read"), None);
+ assert_eq!(s.get_first(&reader, 2).expect("read"), None);
+ assert_eq!(s.get_first(&reader, 3).expect("read"), None);
+ }
+ }
+
+ #[test]
+ fn test_dup_2() {
+ let root = Builder::new()
+ .prefix("test_multi_integer_dup")
+ .tempdir()
+ .expect("tempdir");
+ fs::create_dir_all(root.path()).expect("dir created");
+
+ let k = Rkv::new::<backend::SafeMode>(root.path()).expect("new succeeded");
+ let s = k
+ .open_multi_integer("s", StoreOptions::create())
+ .expect("open");
+
+ {
+ let mut writer = k.write().expect("writer");
+ s.put(&mut writer, 1, &Value::Str("hello!")).expect("write");
+ s.put(&mut writer, 1, &Value::Str("hello!")).expect("write");
+ s.put(&mut writer, 1, &Value::Str("hello1!"))
+ .expect("write");
+
+ let mut iter = s.get(&writer, 1).expect("read");
+ assert_eq!(
+ iter.next().expect("first").expect("ok").1,
+ Value::Str("hello!")
+ );
+ assert_eq!(
+ iter.next().expect("second").expect("ok").1,
+ Value::Str("hello1!")
+ );
+ assert!(iter.next().is_none());
+ }
+ }
+
+ #[test]
+ fn test_del() {
+ let root = Builder::new()
+ .prefix("test_multi_integer_dup")
+ .tempdir()
+ .expect("tempdir");
+ fs::create_dir_all(root.path()).expect("dir created");
+
+ let k = Rkv::new::<backend::SafeMode>(root.path()).expect("new succeeded");
+ let s = k
+ .open_multi_integer("s", StoreOptions::create())
+ .expect("open");
+
+ {
+ let mut writer = k.write().expect("writer");
+ s.put(&mut writer, 1, &Value::Str("hello!")).expect("write");
+ s.put(&mut writer, 1, &Value::Str("hello!")).expect("write");
+ s.put(&mut writer, 1, &Value::Str("hello1!"))
+ .expect("write");
+ {
+ let mut iter = s.get(&writer, 1).expect("read");
+ assert_eq!(
+ iter.next().expect("first").expect("ok").1,
+ Value::Str("hello!")
+ );
+ assert_eq!(
+ iter.next().expect("second").expect("ok").1,
+ Value::Str("hello1!")
+ );
+ assert!(iter.next().is_none());
+ }
+ writer.commit().expect("committed");
+ }
+
+ {
+ let mut writer = k.write().expect("writer");
+ s.delete(&mut writer, 1, &Value::Str("hello!"))
+ .expect("deleted");
+ writer.commit().expect("committed");
+
+ let reader = k.read().expect("reader");
+ let mut iter = s.get(&reader, 1).expect("read");
+ assert_eq!(
+ iter.next().expect("first").expect("ok").1,
+ Value::Str("hello1!")
+ );
+ assert!(iter.next().is_none());
+ }
+
+ {
+ let mut writer = k.write().expect("writer");
+ s.delete(&mut writer, 1, &Value::Str("hello!"))
+ .expect_err("deleted");
+ writer.commit().expect("committed");
+
+ let reader = k.read().expect("reader");
+ let mut iter = s.get(&reader, 1).expect("read");
+ assert_eq!(
+ iter.next().expect("first").expect("ok").1,
+ Value::Str("hello1!")
+ );
+ assert!(iter.next().is_none());
+ }
+
+ {
+ let mut writer = k.write().expect("writer");
+ s.delete(&mut writer, 1, &Value::Str("hello1!"))
+ .expect("deleted");
+ writer.commit().expect("committed");
+
+ let reader = k.read().expect("reader");
+ let mut iter = s.get(&reader, 1).expect("read");
+ assert!(iter.next().is_none());
+ }
+
+ {
+ let mut writer = k.write().expect("writer");
+ s.delete(&mut writer, 1, &Value::Str("hello1!"))
+ .expect_err("deleted");
+ writer.commit().expect("committed");
+
+ let reader = k.read().expect("reader");
+ let mut iter = s.get(&reader, 1).expect("read");
+ assert!(iter.next().is_none());
+ }
+ }
+
+ #[test]
+ fn test_persist() {
+ let root = Builder::new()
+ .prefix("test_multi_integer_persist")
+ .tempdir()
+ .expect("tempdir");
+ fs::create_dir_all(root.path()).expect("dir created");
+
+ {
+ let k = Rkv::new::<backend::SafeMode>(root.path()).expect("new succeeded");
+ let s = k
+ .open_multi_integer("s", StoreOptions::create())
+ .expect("open");
+
+ let mut writer = k.write().expect("writer");
+ s.put(&mut writer, 1, &Value::Str("hello!")).expect("write");
+ s.put(&mut writer, 1, &Value::Str("hello1!"))
+ .expect("write");
+ s.put(&mut writer, 2, &Value::Str("hello!")).expect("write");
+ {
+ let mut iter = s.get(&writer, 1).expect("read");
+ assert_eq!(
+ iter.next().expect("first").expect("ok").1,
+ Value::Str("hello!")
+ );
+ assert_eq!(
+ iter.next().expect("second").expect("ok").1,
+ Value::Str("hello1!")
+ );
+ assert!(iter.next().is_none());
+ }
+ writer.commit().expect("committed");
+ }
+
+ {
+ let k = Rkv::new::<backend::SafeMode>(root.path()).expect("new succeeded");
+ let s = k
+ .open_multi_integer("s", StoreOptions::create())
+ .expect("open");
+
+ let reader = k.read().expect("reader");
+ let mut iter = s.get(&reader, 1).expect("read");
+ assert_eq!(
+ iter.next().expect("first").expect("ok").1,
+ Value::Str("hello!")
+ );
+ assert_eq!(
+ iter.next().expect("second").expect("ok").1,
+ Value::Str("hello1!")
+ );
+ assert!(iter.next().is_none());
+ }
+ }
+}
diff --git a/third_party/rust/rkv/src/store/keys.rs b/third_party/rust/rkv/src/store/keys.rs
new file mode 100644
index 0000000000..26a13db47a
--- /dev/null
+++ b/third_party/rust/rkv/src/store/keys.rs
@@ -0,0 +1,46 @@
+// Copyright 2018-2019 Mozilla
+//
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
+// this file except in compliance with the License. You may obtain a copy of the
+// License at http://www.apache.org/licenses/LICENSE-2.0
+// Unless required by applicable law or agreed to in writing, software distributed
+// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
+// CONDITIONS OF ANY KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations under the License.
+
+mod encodables;
+mod primitives;
+
+use std::marker::PhantomData;
+
+use crate::error::DataError;
+
+pub use encodables::*;
+pub use primitives::*;
+
+pub(crate) struct Key<K> {
+ bytes: Vec<u8>,
+ phantom: PhantomData<K>,
+}
+
+impl<K> AsRef<[u8]> for Key<K>
+where
+ K: EncodableKey,
+{
+ fn as_ref(&self) -> &[u8] {
+ self.bytes.as_ref()
+ }
+}
+
+impl<K> Key<K>
+where
+ K: EncodableKey,
+{
+ #[allow(clippy::new_ret_no_self)]
+ pub fn new(k: &K) -> Result<Key<K>, DataError> {
+ Ok(Key {
+ bytes: k.to_bytes()?,
+ phantom: PhantomData,
+ })
+ }
+}
diff --git a/third_party/rust/rkv/src/store/keys/encodables.rs b/third_party/rust/rkv/src/store/keys/encodables.rs
new file mode 100644
index 0000000000..85e3eacdf5
--- /dev/null
+++ b/third_party/rust/rkv/src/store/keys/encodables.rs
@@ -0,0 +1,27 @@
+// Copyright 2018-2019 Mozilla
+//
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
+// this file except in compliance with the License. You may obtain a copy of the
+// License at http://www.apache.org/licenses/LICENSE-2.0
+// Unless required by applicable law or agreed to in writing, software distributed
+// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
+// CONDITIONS OF ANY KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations under the License.
+
+use bincode::serialize;
+use serde::Serialize;
+
+use crate::error::DataError;
+
+pub trait EncodableKey {
+ fn to_bytes(&self) -> Result<Vec<u8>, DataError>;
+}
+
+impl<T> EncodableKey for T
+where
+ T: Serialize,
+{
+ fn to_bytes(&self) -> Result<Vec<u8>, DataError> {
+ serialize(self).map_err(|e| e.into())
+ }
+}
diff --git a/third_party/rust/rkv/src/store/keys/primitives.rs b/third_party/rust/rkv/src/store/keys/primitives.rs
new file mode 100644
index 0000000000..26282b7cbb
--- /dev/null
+++ b/third_party/rust/rkv/src/store/keys/primitives.rs
@@ -0,0 +1,15 @@
+// Copyright 2018-2019 Mozilla
+//
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
+// this file except in compliance with the License. You may obtain a copy of the
+// License at http://www.apache.org/licenses/LICENSE-2.0
+// Unless required by applicable law or agreed to in writing, software distributed
+// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
+// CONDITIONS OF ANY KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations under the License.
+
+use crate::store::keys::EncodableKey;
+
+pub trait PrimitiveInt: EncodableKey {}
+
+impl PrimitiveInt for u32 {}
diff --git a/third_party/rust/rkv/src/store/multi.rs b/third_party/rust/rkv/src/store/multi.rs
new file mode 100644
index 0000000000..c7f816b1ec
--- /dev/null
+++ b/third_party/rust/rkv/src/store/multi.rs
@@ -0,0 +1,133 @@
+// Copyright 2018 Mozilla
+//
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
+// this file except in compliance with the License. You may obtain a copy of the
+// License at http://www.apache.org/licenses/LICENSE-2.0
+// Unless required by applicable law or agreed to in writing, software distributed
+// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
+// CONDITIONS OF ANY KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations under the License.
+
+use std::marker::PhantomData;
+
+use crate::{
+ backend::{BackendDatabase, BackendFlags, BackendIter, BackendRoCursor, BackendRwTransaction},
+ error::StoreError,
+ helpers::read_transform,
+ readwrite::{Readable, Writer},
+ value::Value,
+};
+
+type EmptyResult = Result<(), StoreError>;
+
+#[derive(Debug, Eq, PartialEq, Copy, Clone)]
+pub struct MultiStore<D> {
+ db: D,
+}
+
+pub struct Iter<'i, I> {
+ iter: I,
+ phantom: PhantomData<&'i ()>,
+}
+
+impl<D> MultiStore<D>
+where
+ D: BackendDatabase,
+{
+ pub(crate) fn new(db: D) -> MultiStore<D> {
+ MultiStore { db }
+ }
+
+ /// Provides a cursor to all of the values for the duplicate entries that match this
+ /// key
+ pub fn get<'r, R, I, C, K>(&self, reader: &'r R, k: K) -> Result<Iter<'r, I>, StoreError>
+ where
+ R: Readable<'r, Database = D, RoCursor = C>,
+ I: BackendIter<'r>,
+ C: BackendRoCursor<'r, Iter = I>,
+ K: AsRef<[u8]> + 'r,
+ {
+ let cursor = reader.open_ro_cursor(&self.db)?;
+ let iter = cursor.into_iter_dup_of(k);
+
+ Ok(Iter {
+ iter,
+ phantom: PhantomData,
+ })
+ }
+
+ /// Provides the first value that matches this key
+ pub fn get_first<'r, R, K>(&self, reader: &'r R, k: K) -> Result<Option<Value<'r>>, StoreError>
+ where
+ R: Readable<'r, Database = D>,
+ K: AsRef<[u8]>,
+ {
+ reader.get(&self.db, &k)
+ }
+
+ /// Insert a value at the specified key.
+ /// This put will allow duplicate entries. If you wish to have duplicate entries
+ /// rejected, use the `put_with_flags` function and specify NO_DUP_DATA
+ pub fn put<T, K>(&self, writer: &mut Writer<T>, k: K, v: &Value) -> EmptyResult
+ where
+ T: BackendRwTransaction<Database = D>,
+ K: AsRef<[u8]>,
+ {
+ writer.put(&self.db, &k, v, T::Flags::empty())
+ }
+
+ pub fn put_with_flags<T, K>(
+ &self,
+ writer: &mut Writer<T>,
+ k: K,
+ v: &Value,
+ flags: T::Flags,
+ ) -> EmptyResult
+ where
+ T: BackendRwTransaction<Database = D>,
+ K: AsRef<[u8]>,
+ {
+ writer.put(&self.db, &k, v, flags)
+ }
+
+ pub fn delete_all<T, K>(&self, writer: &mut Writer<T>, k: K) -> EmptyResult
+ where
+ T: BackendRwTransaction<Database = D>,
+ K: AsRef<[u8]>,
+ {
+ writer.delete(&self.db, &k, None)
+ }
+
+ pub fn delete<T, K>(&self, writer: &mut Writer<T>, k: K, v: &Value) -> EmptyResult
+ where
+ T: BackendRwTransaction<Database = D>,
+ K: AsRef<[u8]>,
+ {
+ writer.delete(&self.db, &k, Some(&v.to_bytes()?))
+ }
+
+ pub fn clear<T>(&self, writer: &mut Writer<T>) -> EmptyResult
+ where
+ T: BackendRwTransaction<Database = D>,
+ {
+ writer.clear(&self.db)
+ }
+}
+
+impl<'i, I> Iterator for Iter<'i, I>
+where
+ I: BackendIter<'i>,
+{
+ type Item = Result<(&'i [u8], Value<'i>), StoreError>;
+
+ fn next(&mut self) -> Option<Self::Item> {
+ match self.iter.next() {
+ None => None,
+ Some(Ok((key, bytes))) => match read_transform(Ok(bytes)) {
+ Ok(val) => Some(Ok((key, val))),
+ Err(err) => Some(Err(err)),
+ },
+ Some(Err(err)) => Some(Err(err.into())),
+ }
+ }
+}
diff --git a/third_party/rust/rkv/src/store/single.rs b/third_party/rust/rkv/src/store/single.rs
new file mode 100644
index 0000000000..c456e6aaa6
--- /dev/null
+++ b/third_party/rust/rkv/src/store/single.rs
@@ -0,0 +1,132 @@
+// Copyright 2018 Mozilla
+//
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
+// this file except in compliance with the License. You may obtain a copy of the
+// License at http://www.apache.org/licenses/LICENSE-2.0
+// Unless required by applicable law or agreed to in writing, software distributed
+// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
+// CONDITIONS OF ANY KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations under the License.
+
+use std::marker::PhantomData;
+
+use crate::{
+ backend::{BackendDatabase, BackendFlags, BackendIter, BackendRoCursor, BackendRwTransaction},
+ error::StoreError,
+ helpers::read_transform,
+ readwrite::{Readable, Writer},
+ value::Value,
+};
+
+type EmptyResult = Result<(), StoreError>;
+
+#[derive(Debug, Eq, PartialEq, Copy, Clone)]
+pub struct SingleStore<D> {
+ db: D,
+}
+
+pub struct Iter<'i, I> {
+ iter: I,
+ phantom: PhantomData<&'i ()>,
+}
+
+impl<D> SingleStore<D>
+where
+ D: BackendDatabase,
+{
+ pub(crate) fn new(db: D) -> SingleStore<D> {
+ SingleStore { db }
+ }
+
+ pub fn get<'r, R, K>(&self, reader: &'r R, k: K) -> Result<Option<Value<'r>>, StoreError>
+ where
+ R: Readable<'r, Database = D>,
+ K: AsRef<[u8]>,
+ {
+ reader.get(&self.db, &k)
+ }
+
+ // TODO: flags
+ pub fn put<T, K>(&self, writer: &mut Writer<T>, k: K, v: &Value) -> EmptyResult
+ where
+ T: BackendRwTransaction<Database = D>,
+ K: AsRef<[u8]>,
+ {
+ writer.put(&self.db, &k, v, T::Flags::empty())
+ }
+
+ #[cfg(not(feature = "db-dup-sort"))]
+ pub fn delete<T, K>(&self, writer: &mut Writer<T>, k: K) -> EmptyResult
+ where
+ T: BackendRwTransaction<Database = D>,
+ K: AsRef<[u8]>,
+ {
+ writer.delete(&self.db, &k)
+ }
+
+ #[cfg(feature = "db-dup-sort")]
+ pub fn delete<T, K>(&self, writer: &mut Writer<T>, k: K) -> EmptyResult
+ where
+ T: BackendRwTransaction<Database = D>,
+ K: AsRef<[u8]>,
+ {
+ writer.delete(&self.db, &k, None)
+ }
+
+ pub fn iter_start<'r, R, I, C>(&self, reader: &'r R) -> Result<Iter<'r, I>, StoreError>
+ where
+ R: Readable<'r, Database = D, RoCursor = C>,
+ I: BackendIter<'r>,
+ C: BackendRoCursor<'r, Iter = I>,
+ {
+ let cursor = reader.open_ro_cursor(&self.db)?;
+ let iter = cursor.into_iter();
+
+ Ok(Iter {
+ iter,
+ phantom: PhantomData,
+ })
+ }
+
+ pub fn iter_from<'r, R, I, C, K>(&self, reader: &'r R, k: K) -> Result<Iter<'r, I>, StoreError>
+ where
+ R: Readable<'r, Database = D, RoCursor = C>,
+ I: BackendIter<'r>,
+ C: BackendRoCursor<'r, Iter = I>,
+ K: AsRef<[u8]> + 'r,
+ {
+ let cursor = reader.open_ro_cursor(&self.db)?;
+ let iter = cursor.into_iter_from(k);
+
+ Ok(Iter {
+ iter,
+ phantom: PhantomData,
+ })
+ }
+
+ pub fn clear<T>(&self, writer: &mut Writer<T>) -> EmptyResult
+ where
+ D: BackendDatabase,
+ T: BackendRwTransaction<Database = D>,
+ {
+ writer.clear(&self.db)
+ }
+}
+
+impl<'i, I> Iterator for Iter<'i, I>
+where
+ I: BackendIter<'i>,
+{
+ type Item = Result<(&'i [u8], Value<'i>), StoreError>;
+
+ fn next(&mut self) -> Option<Self::Item> {
+ match self.iter.next() {
+ None => None,
+ Some(Ok((key, bytes))) => match read_transform(Ok(bytes)) {
+ Ok(val) => Some(Ok((key, val))),
+ Err(err) => Some(Err(err)),
+ },
+ Some(Err(err)) => Some(Err(err.into())),
+ }
+ }
+}