web search done right
Very simple WAL built on top of file-store primitives (#219)
Mikkel Denker365ed02813a5
diff --git a/Cargo.lock b/Cargo.lockindex 9aaf06b1..7626b520 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -5042,6 +5042,15 @@ version = "2.6.0"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "1de1d4f81173b03af4c0cbed3c898f6bff5b870e4a7f5d6f4057d62a7a4b686e"+[[package]]+name = "simple_wal"+version = "0.1.0"+dependencies = [+ "anyhow",+ "bincode",+ "file_store",+]+[[package]]name = "siphasher"version = "0.3.11"diff --git a/Cargo.toml b/Cargo.tomlindex c43146ad..cb4ab77b 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -15,6 +15,7 @@ members = ["crates/tantivy","crates/leechy","crates/common",+ "crates/simple-wal","fuzz",]resolver = "2"diff --git a/assets/licenses.html b/assets/licenses.htmlindex 137eea3e..7792efcc 100644--- a/assets/licenses.html+++ b/assets/licenses.html@@ -46,9 +46,9 @@<ul class="licenses-overview"><li><a href="#Apache-2.0">Apache License 2.0</a> (428)</li><li><a href="#MIT">MIT License</a> (187)</li>+ <li><a href="#AGPL-3.0">GNU Affero General Public License v3.0</a> (9)</li><li><a href="#BSD-3-Clause">BSD 3-Clause "New" or "Revised" License</a> (9)</li><li><a href="#MPL-2.0">Mozilla Public License 2.0</a> (9)</li>- <li><a href="#AGPL-3.0">GNU Affero General Public License v3.0</a> (8)</li><li><a href="#ISC">ISC License</a> (4)</li><li><a href="#Unicode-3.0">Unicode License v3</a> (4)</li><li><a href="#BSD-2-Clause">BSD 2-Clause "Simplified" License</a> (3)</li>@@ -74,6 +74,7 @@<li><a href=" https://crates.io/crates/lending-iter ">lending-iter 0.1.0</a></li><li><a href=" https://crates.io/crates/ownedbytes ">ownedbytes 0.1.0</a></li><li><a href=" https://crates.io/crates/robotstxt ">robotstxt 0.1.0</a></li>+ <li><a href=" https://crates.io/crates/simple_wal ">simple_wal 0.1.0</a></li><li><a href=" https://crates.io/crates/speedy_kv ">speedy_kv 0.1.0</a></li></ul><pre class="license-text">GNU AFFERO GENERAL PUBLIC LICENSEdiff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rsindex 924eb167..730600c5 100644--- a/crates/core/src/lib.rs+++ b/crates/core/src/lib.rs@@ -33,7 +33,8 @@ use distributed::{cluster::Cluster,member::{Member, Service},};-use std::{cmp::Reverse, path::PathBuf, sync::Arc};+pub use file_store::gen_temp_path;+use std::{cmp::Reverse, sync::Arc};use thiserror::Error;pub mod entrypoint;@@ -139,33 +140,6 @@ pub enum Error {pub type Result<T, E = anyhow::Error> = std::result::Result<T, E>;-// taken from https://docs.rs/sled/0.34.7/src/sled/config.rs.html#445-pub fn gen_temp_path() -> PathBuf {- use std::sync::atomic::{AtomicUsize, Ordering};- use std::time::SystemTime;-- static SALT_COUNTER: AtomicUsize = AtomicUsize::new(0);-- let seed = SALT_COUNTER.fetch_add(1, Ordering::SeqCst) as u128;-- let now = SystemTime::now()- .duration_since(SystemTime::UNIX_EPOCH)- .unwrap()- .as_nanos()- << 48;-- let pid = u128::from(std::process::id());-- let salt = (pid << 16) + now + seed;-- if cfg!(target_os = "linux") {- // use shared memory for temporary linux files- format!("/dev/shm/pagecache.tmp.{salt}").into()- } else {- std::env::temp_dir().join(format!("pagecache.tmp.{salt}"))- }-}-/// Starts a gossip cluster in the background and returns a handle to it./// This is useful for blocking contexts where there is no runtime to spawn the cluster on.pub fn start_gossip_cluster_thread(config: GossipConfig, service: Option<Service>) -> Arc<Cluster> {diff --git a/crates/file-store/src/iterable.rs b/crates/file-store/src/iterable.rsindex 5f06828a..212848f7 100644--- a/crates/file-store/src/iterable.rs+++ b/crates/file-store/src/iterable.rs@@ -87,10 +87,8 @@ wherenext_start: u64,_marker: std::marker::PhantomData<T>,}-impl<T, W> IterableStoreWriter<T, W>where- T: bincode::Encode,W: io::Write,{pub fn new(writer: W) -> Self {@@ -100,7 +98,13 @@ wherenext_start: 0,}}+}+impl<T, W> IterableStoreWriter<T, W>+where+ T: bincode::Encode,+ W: io::Write,+{pub fn write(&mut self, item: &T) -> Result<WrittenOffset> {let serialized = bincode::encode_to_vec(item, common::bincode_config())?;let header = IterableHeader {diff --git a/crates/file-store/src/lib.rs b/crates/file-store/src/lib.rsindex aa3b6b67..e2bd4a93 100644--- a/crates/file-store/src/lib.rs+++ b/crates/file-store/src/lib.rs@@ -25,3 +25,30 @@ pub mod random_lookup;pub use const_serializable::ConstSerializable;pub use peekable::Peekable;++// taken from https://docs.rs/sled/0.34.7/src/sled/config.rs.html#445+pub fn gen_temp_path() -> std::path::PathBuf {+ use std::sync::atomic::{AtomicUsize, Ordering};+ use std::time::SystemTime;++ static SALT_COUNTER: AtomicUsize = AtomicUsize::new(0);++ let seed = SALT_COUNTER.fetch_add(1, Ordering::SeqCst) as u128;++ let now = SystemTime::now()+ .duration_since(SystemTime::UNIX_EPOCH)+ .unwrap()+ .as_nanos()+ << 48;++ let pid = u128::from(std::process::id());++ let salt = (pid << 16) + now + seed;++ if cfg!(target_os = "linux") {+ // use shared memory for temporary linux files+ format!("/dev/shm/pagecache.tmp.{salt}").into()+ } else {+ std::env::temp_dir().join(format!("pagecache.tmp.{salt}"))+ }+}diff --git a/crates/simple-wal/Cargo.toml b/crates/simple-wal/Cargo.tomlnew file mode 100644index 00000000..c4b2d2f9--- /dev/null+++ b/crates/simple-wal/Cargo.toml@@ -0,0 +1,12 @@+[package]+edition = "2021"+license = "AGPL-3.0"+name = "simple_wal"+version = "0.1.0"++# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html++[dependencies]+anyhow.workspace = true+bincode.workspace = true+file_store = {path = "../file-store"}diff --git a/crates/simple-wal/src/lib.rs b/crates/simple-wal/src/lib.rsnew file mode 100644index 00000000..5d6aabb7--- /dev/null+++ b/crates/simple-wal/src/lib.rs@@ -0,0 +1,151 @@+// Stract is an open source web search engine.+// Copyright (C) 2024 Stract ApS+//+// This program is free software: you can redistribute it and/or modify+// it under the terms of the GNU Affero General Public License as+// published by the Free Software Foundation, either version 3 of the+// License, or (at your option) any later version.+//+// This program is distributed in the hope that it will be useful,+// but WITHOUT ANY WARRANTY; without even the implied warranty of+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+// GNU Affero General Public License for more details.+//+// You should have received a copy of the GNU Affero General Public License+// along with this program. If not, see <https://www.gnu.org/licenses/>.++use anyhow::Result;+use std::{+ fs::File,+ path::{Path, PathBuf},+};++pub struct Wal<T> {+ writer: file_store::iterable::IterableStoreWriter<T, File>,+ path: PathBuf,+}++impl<T> Wal<T> {+ pub fn open<P: AsRef<Path>>(file: P) -> Result<Self> {+ let path = file.as_ref().to_path_buf();+ let file = if file.as_ref().exists() {+ File::open(file)?+ } else {+ File::create(file)?+ };++ Ok(Wal {+ writer: file_store::iterable::IterableStoreWriter::new(file),+ path,+ })+ }++ pub fn clear(&mut self) -> Result<()> {+ std::fs::remove_file(&self.path)?;+ self.writer = file_store::iterable::IterableStoreWriter::new(File::create(&self.path)?);++ Ok(())+ }+}++impl<T> Wal<T>+where+ T: bincode::Encode,+{+ pub fn write(&mut self, item: &T) -> Result<()> {+ self.writer.write(item)?;+ self.writer.flush()?;++ Ok(())+ }+}++impl<T> Wal<T>+where+ T: bincode::Decode,+{+ pub fn iter(&self) -> Result<WalIterator<T>> {+ WalIterator::open(&self.path)+ }+}++pub struct WalIterator<T> {+ iter: file_store::iterable::IterableStoreReader<T>,+}++impl<T> WalIterator<T> {+ pub fn open<P: AsRef<Path>>(file: P) -> Result<Self> {+ Ok(Self {+ iter: file_store::iterable::IterableStoreReader::open(file)?,+ })+ }+}++impl<T> Iterator for WalIterator<T>+where+ T: bincode::Decode,+{+ type Item = T;++ fn next(&mut self) -> Option<Self::Item> {+ self.iter.next()+ }+}++#[cfg(test)]+mod tests {+ use super::*;+ use anyhow::Result;++ #[test]+ fn test_write_read() -> Result<()> {+ let mut writer = Wal::open(file_store::gen_temp_path())?;++ writer.write(&1u64)?;+ writer.write(&2u64)?;+ writer.write(&3u64)?;++ let res: Vec<_> = writer.iter()?.collect();++ assert_eq!(&res, &[1, 2, 3]);++ writer.write(&4u64)?;++ let res: Vec<_> = writer.iter()?.collect();++ assert_eq!(&res, &[1, 2, 3, 4]);++ Ok(())+ }++ #[test]+ fn test_empty_write() -> Result<()> {+ let writer: Wal<u64> = Wal::open(file_store::gen_temp_path())?;++ let res: Vec<_> = writer.iter()?.collect();++ assert!(res.is_empty());++ Ok(())+ }++ #[test]+ fn test_clear() -> Result<()> {+ let mut writer = Wal::open(file_store::gen_temp_path())?;++ writer.write(&1u64)?;+ writer.write(&2u64)?;+ writer.write(&3u64)?;++ let res: Vec<_> = writer.iter()?.collect();++ assert_eq!(&res, &[1, 2, 3]);++ writer.clear()?;++ let res: Vec<_> = writer.iter()?.collect();+ assert!(res.is_empty());++ Ok(())+ }+}diff --git a/frontend/src/routes/sorry/+page.svelte b/frontend/src/routes/sorry/+page.svelteindex b526ed53..48611770 100644--- a/frontend/src/routes/sorry/+page.svelte+++ b/frontend/src/routes/sorry/+page.svelte@@ -19,7 +19,3 @@<Button title="Verify challenge" type="submit" kind="accent">VERIFY</Button></div></Captcha>--<!-- <audio controls="controls">- <source src={audioBase64} />-</audio> -->