From 75161247731f8afa05777ed780628e6dbee6b28e Mon Sep 17 00:00:00 2001
From: Paul Maruhn <paulmaruhn@posteo.de>
Date: Mon, 27 Dec 2021 14:16:52 +0100
Subject: [PATCH] change db to sled

---
 Cargo.lock | 198 ++++++++++++++++++++---------------------------------
 Cargo.toml |   2 +-
 src/db.rs  |  67 ++++++++++--------
 3 files changed, 113 insertions(+), 154 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index 962f998..ae1d9f3 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -90,25 +90,6 @@ version = "0.1.4"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "383d29d513d8764dcdc42ea295d979eb99c3c9f00607b3692cf68a431f7dca72"
 
-[[package]]
-name = "bindgen"
-version = "0.59.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "2bd2a9a458e8f4304c52c43ebb0cfbd520289f8379a52e329a38afda99bf8eb8"
-dependencies = [
- "bitflags",
- "cexpr",
- "clang-sys",
- "lazy_static",
- "lazycell",
- "peeking_take_while",
- "proc-macro2",
- "quote",
- "regex",
- "rustc-hash",
- "shlex",
-]
-
 [[package]]
 name = "bitflags"
 version = "1.3.2"
@@ -174,18 +155,6 @@ name = "cc"
 version = "1.0.72"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "22a9137b95ea06864e018375b72adfb7db6e6f68cfc8df5a04d00288050485ee"
-dependencies = [
- "jobserver",
-]
-
-[[package]]
-name = "cexpr"
-version = "0.6.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766"
-dependencies = [
- "nom",
-]
 
 [[package]]
 name = "cfg-if"
@@ -235,17 +204,6 @@ dependencies = [
  "phf_codegen",
 ]
 
-[[package]]
-name = "clang-sys"
-version = "1.3.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "fa66045b9cb23c2e9c1520732030608b02ee07e5cfaa5a521ec15ded7fa24c90"
-dependencies = [
- "glob",
- "libc",
- "libloading",
-]
-
 [[package]]
 name = "const_fn"
 version = "0.4.8"
@@ -263,6 +221,28 @@ dependencies = [
  "version_check",
 ]
 
+[[package]]
+name = "crc32fast"
+version = "1.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "738c290dfaea84fc1ca15ad9c168d083b05a714e1efddd8edaab678dc28d2836"
+dependencies = [
+ "cfg-if 1.0.0",
+]
+
+[[package]]
+name = "crossbeam-epoch"
+version = "0.9.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4ec02e091aa634e2c3ada4a392989e7c3116673ef0ac5b72232439094d73b7fd"
+dependencies = [
+ "cfg-if 1.0.0",
+ "crossbeam-utils",
+ "lazy_static",
+ "memoffset",
+ "scopeguard",
+]
+
 [[package]]
 name = "crossbeam-utils"
 version = "0.8.5"
@@ -382,10 +362,10 @@ dependencies = [
  "rand 0.8.4",
  "rocket",
  "rocket_dyn_templates",
- "rocksdb",
  "rust-crypto",
  "serde",
  "serde_json",
+ "sled",
  "tera",
  "toml",
 ]
@@ -422,6 +402,16 @@ version = "1.0.7"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
 
+[[package]]
+name = "fs2"
+version = "0.4.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9564fc758e15025b46aa6643b1b77d047d1a56a1aea6e01002ac0c7026876213"
+dependencies = [
+ "libc",
+ "winapi 0.3.9",
+]
+
 [[package]]
 name = "fsevent"
 version = "0.4.0"
@@ -552,6 +542,15 @@ dependencies = [
  "slab",
 ]
 
+[[package]]
+name = "fxhash"
+version = "0.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c31b6d751ae2c7f11320402d34e41349dd1016f8d5d45e48c4312bc8625af50c"
+dependencies = [
+ "byteorder",
+]
+
 [[package]]
 name = "gcc"
 version = "0.3.55"
@@ -813,15 +812,6 @@ version = "1.0.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35"
 
-[[package]]
-name = "jobserver"
-version = "0.1.24"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "af25a77299a7f711a01975c35a6a424eb6862092cc2d6c72c4ed6cbc56dfc1fa"
-dependencies = [
- "libc",
-]
-
 [[package]]
 name = "kernel32-sys"
 version = "0.2.2"
@@ -850,28 +840,6 @@ version = "0.2.112"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "1b03d17f364a3a042d5e5d46b053bbbf82c92c9430c592dd4c064dc6ee997125"
 
-[[package]]
-name = "libloading"
-version = "0.7.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "afe203d669ec979b7128619bae5a63b7b42e9203c1b29146079ee05e2f604b52"
-dependencies = [
- "cfg-if 1.0.0",
- "winapi 0.3.9",
-]
-
-[[package]]
-name = "librocksdb-sys"
-version = "6.20.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "c309a9d2470844aceb9a4a098cf5286154d20596868b75a6b36357d2bb9ca25d"
-dependencies = [
- "bindgen",
- "cc",
- "glob",
- "libc",
-]
-
 [[package]]
 name = "lock_api"
 version = "0.4.5"
@@ -927,16 +895,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a"
 
 [[package]]
-name = "mime"
-version = "0.3.16"
+name = "memoffset"
+version = "0.6.5"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d"
+checksum = "5aa361d4faea93603064a027415f07bd8e1d5c88c9fbf68bf56a285428fd79ce"
+dependencies = [
+ "autocfg",
+]
 
 [[package]]
-name = "minimal-lexical"
-version = "0.2.1"
+name = "mime"
+version = "0.3.16"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
+checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d"
 
 [[package]]
 name = "mio"
@@ -1034,17 +1005,6 @@ dependencies = [
  "winapi 0.3.9",
 ]
 
-[[package]]
-name = "nom"
-version = "7.1.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "1b1d11e1ef389c76fe5b81bcaf2ea32cf88b62bc494e19f493d0b30e7a930109"
-dependencies = [
- "memchr",
- "minimal-lexical",
- "version_check",
-]
-
 [[package]]
 name = "normpath"
 version = "0.3.1"
@@ -1179,12 +1139,6 @@ dependencies = [
  "syn",
 ]
 
-[[package]]
-name = "peeking_take_while"
-version = "0.1.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099"
-
 [[package]]
 name = "percent-encoding"
 version = "2.1.0"
@@ -1309,9 +1263,9 @@ checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5"
 
 [[package]]
 name = "proc-macro2"
-version = "1.0.34"
+version = "1.0.35"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "2f84e92c0f7c9d58328b85a78557813e4bd845130db68d7184635344399423b1"
+checksum = "392a54546fda6b7cc663379d0e6ce8b324cf88aecc5a499838e1be9781bdce2e"
 dependencies = [
  "unicode-xid",
 ]
@@ -1593,16 +1547,6 @@ dependencies = [
  "uncased",
 ]
 
-[[package]]
-name = "rocksdb"
-version = "0.17.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "7a62eca5cacf2c8261128631bed9f045598d40bfbe4b29f5163f0f802f8f44a7"
-dependencies = [
- "libc",
- "librocksdb-sys",
-]
-
 [[package]]
 name = "rust-crypto"
 version = "0.2.36"
@@ -1616,12 +1560,6 @@ dependencies = [
  "time 0.1.44",
 ]
 
-[[package]]
-name = "rustc-hash"
-version = "1.1.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
-
 [[package]]
 name = "rustc-serialize"
 version = "0.3.24"
@@ -1743,12 +1681,6 @@ dependencies = [
  "lazy_static",
 ]
 
-[[package]]
-name = "shlex"
-version = "1.1.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "43b2853a4d09f215c24cc5489c992ce46052d359b5109343cbafbf26bc62f8a3"
-
 [[package]]
 name = "signal-hook-registry"
 version = "1.4.0"
@@ -1770,6 +1702,22 @@ version = "0.4.5"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "9def91fd1e018fe007022791f865d0ccc9b3a0d5001e01aabb8b40e46000afb5"
 
+[[package]]
+name = "sled"
+version = "0.34.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7f96b4737c2ce5987354855aed3797279def4ebf734436c6aa4552cf8e169935"
+dependencies = [
+ "crc32fast",
+ "crossbeam-epoch",
+ "crossbeam-utils",
+ "fs2",
+ "fxhash",
+ "libc",
+ "log",
+ "parking_lot",
+]
+
 [[package]]
 name = "slug"
 version = "0.1.4"
@@ -1879,9 +1827,9 @@ checksum = "213701ba3370744dcd1a12960caa4843b3d68b4d1c0a5d575e0d65b2ee9d16c0"
 
 [[package]]
 name = "syn"
-version = "1.0.83"
+version = "1.0.84"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "23a1dfb999630e338648c83e91c59a4e9fb7620f520c3194b6b89e276f2f1959"
+checksum = "ecb2e6da8ee5eb9a61068762a32fa9619cc591ceb055b3687f4cd4051ec2e06b"
 dependencies = [
  "proc-macro2",
  "quote",
@@ -2129,9 +2077,9 @@ checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642"
 
 [[package]]
 name = "typenum"
-version = "1.14.0"
+version = "1.15.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "b63708a265f51345575b27fe43f9500ad611579e764c79edbc2037b1121959ec"
+checksum = "dcf81ac59edc17cc8697ff311e8f5ef2d99fcbd9817b34cec66f90b6c3dfd987"
 
 [[package]]
 name = "ubyte"
diff --git a/Cargo.toml b/Cargo.toml
index 61aea5d..6fd108b 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -23,7 +23,7 @@ serde = {version = "1.0.125", features = ["derive"]}
 toml = "0.5.8"
 lazy_static = "1.4.0"
 domain = "0.6.1"
-rocksdb = "0.17.0"
+sled = "0.34.7"
 rust-crypto = "0.2.36"
 pretty_env_logger = "0.4.0"
 
diff --git a/src/db.rs b/src/db.rs
index d05215b..079ef2c 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -2,41 +2,56 @@ use crate::domain::Dname;
 use std::path::PathBuf;
 use chrono::{Utc, DateTime, Duration};
 use std::net::{Ipv4Addr, Ipv6Addr};
+#[allow(unused_imports)]
 use log::{info, warn, error};
 use std::sync::{Mutex, Arc};
-use rocksdb;
 use serde::{Serialize, Deserialize};
 use serde_json as json;
 use crate::ffdyndns::Token;
 use crate::sha256;
-use rocksdb::IteratorMode;
 
 
 #[derive(Clone)]
 pub struct Database {
-	conn: Arc<Mutex<rocksdb::DB>>,
+	conn: Arc<Mutex<sled::Db>>,
 }
 
 
 
 impl Database {
 	pub fn new(path: PathBuf) -> Self {
-		let conn = rocksdb::DB::open_default(path).unwrap();
-		Database { conn: Arc::new(Mutex::new(conn)) }
+		let conn = sled::open(path).unwrap();
+		Self { conn: Arc::new(Mutex::new(conn)) }
 	}
 
-	// pub fn get_all_domains(&self) -> Vec<Domain> {
-	// 	let db = self.conn.lock().unwrap();
-	// 	let mut stmt: sqlite::Statement = db.prepare("SELECT * FROM domains").unwrap();
+	// basic CRUD methods
+	fn set(&self, key: String, val: Vec<u8>) -> Result<(),()> {
+        self.conn.lock().unwrap().insert(
+            key.as_bytes(),
+            val
+        ).map_err(|_| ()).map(|_| ())
+    }
+
+    fn get(&self, key: String) -> Option<Vec<u8>> {
+        self.conn.lock().unwrap().get(key.as_bytes()).unwrap().map(|x| x.as_ref().to_vec())
+    }
+
+	fn delete(&self, key: String) {
+        self.conn.lock().unwrap().remove(key);
+    }
+
+    fn list(&self) -> Vec<Vec<u8>> {
+        self.conn.lock().unwrap()
+			.iter()
+            .map(|r| {
+                r.unwrap().1.as_ref().to_vec()
+            }).collect()
+    }
+
 
-	// 	stmt.query_map(
-	// 		params![],
-	// 		|row| Ok(Domain::from_row(row))
-	// 	).unwrap().map(|x| x.unwrap()).collect()
-	// }
 
 	pub fn insert_new_domain(&self, d: &Domain) {
-		self.conn.lock().unwrap().put(
+		self.set(
 			sha256!(&d.domainname),
 			json::to_vec(&d).unwrap()
 		).unwrap();
@@ -44,12 +59,12 @@ impl Database {
 
 
 	pub fn get_domain(&self, domain: &String) -> Option<Domain> {
-		let r = self.conn.lock().unwrap().get(sha256!(domain)).unwrap();
+		let r = self.get(sha256!(domain));
 		r.map(|x| json::from_slice(&x).unwrap())
 	}
 
 	pub fn remove_domain(&self, domain: &String) {
-		self.conn.lock().unwrap().delete(domain).unwrap();
+		self.delete(sha256!(domain));
 	}
 
 
@@ -57,7 +72,7 @@ impl Database {
 		let mut d = self.get_domain(domain).unwrap();
 		d.lastupdate = lastupdate;
 
-		self.conn.lock().unwrap().put(
+		self.set(
 			sha256!(domain),
 			json::to_vec(&d).unwrap()
 		).unwrap();
@@ -67,7 +82,7 @@ impl Database {
 		let mut d = self.get_domain(domain).unwrap();
 		d.valid_until = valid_until;
 
-		self.conn.lock().unwrap().put(
+		self.set(
 			sha256!(domain),
 			json::to_vec(&d).unwrap()
 		).unwrap();
@@ -82,7 +97,7 @@ impl Database {
 		let mut d = self.get_domain(domain).unwrap();
 		d.ipv4 = Some(addr);
 
-		self.conn.lock().unwrap().put(
+		self.set(
 			sha256!(domain),
 			json::to_vec(&d).unwrap()
 		).unwrap();
@@ -92,7 +107,7 @@ impl Database {
 		let mut d = self.get_domain(domain).unwrap();
 		d.ipv6 = Some(addr);
 
-		self.conn.lock().unwrap().put(
+		self.set(
 			sha256!(domain),
 			json::to_vec(&d).unwrap()
 		).unwrap();
@@ -103,14 +118,10 @@ impl Database {
 	}
 
 	pub fn get_all(&self) -> Vec<Domain> {
-		self.conn
-			.lock()
-			.unwrap()
-			.iterator(IteratorMode::Start)
-			.map(|(_, v)| {
-				json::from_slice(&*v).unwrap()
-			})
-			.collect()
+		self.list().iter().map(|v| {
+			json::from_slice(&*v).unwrap()
+		})
+		.collect()
 	}
 }
 
-- 
GitLab