diff --git a/Cargo.toml b/Cargo.toml index 83be29c..3abe191 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,8 +1,9 @@ [package] -authors = ["Kristoffer Ström "] +authors = ["Kristoffer Ström ", + "David Stainton "] edition = "2018" name = "appendix" -version = "0.1.1" +version = "0.1.2" description = "Append-only, on-disk key-value index" documentation = "https://docs.rs/appendix/" keywords = ["index", "datastructure"] diff --git a/README.md b/README.md index 1b1b997..7b8da89 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,4 @@ -# Appendix - -[Documentation](https://docs.rs/appendix/) +# appendix [![](https://travis-ci.org/krl/appendix.png?branch=master)](https://www.travis-ci.org/krl/appendix) [![](https://img.shields.io/crates/v/appendix.svg)](https://crates.io/crates/appendix) [![](https://docs.rs/appendix/badge.svg)](https://docs.rs/appendix/) An immutable, append-only, thread safe, on-disk index mapping `Copy` keys to `Copy` values. diff --git a/src/lib.rs b/src/lib.rs index e306095..de04c72 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,7 +2,7 @@ //! An append-only, on-disk key-value index with lockless reads use std::cell::UnsafeCell; -use std::fs::OpenOptions; +use std::fs::{remove_file, OpenOptions}; use std::hash::{Hash, Hasher}; use std::io; use std::marker::PhantomData; @@ -395,6 +395,19 @@ impl Index { } } + /// Removes all data from disk + pub fn purge(&mut self) -> std::io::Result<()> { + for n in 0..NUM_LANES { + let mut pathbuf = PathBuf::from(&self.path); + pathbuf.push(&format!("{:02x}", n)); + if pathbuf.exists() { + remove_file(&pathbuf)? + } + } + *self = Self::new(&self.path)?; + Ok(()) + } + /// Get the approximate size on disk for the index pub fn on_disk_size(&self) -> usize { *self.pages.lock() as usize * PAGE_SIZE @@ -475,6 +488,33 @@ mod tests { } } + #[test] + fn purge() { + let dir = tempdir().unwrap(); + let mut index = Index::new(&dir).unwrap(); + + for i in 0..N { + index.insert(i, i).unwrap(); + } + for i in 0..N { + assert_eq!(index.get(&i).unwrap(), Some(&i)); + } + + index.purge().unwrap(); + + for i in 0..N { + assert_eq!(index.get(&i).unwrap(), None); + } + + // repopulate + for i in 0..N { + index.insert(i, i).unwrap(); + } + for i in 0..N { + assert_eq!(index.get(&i).unwrap(), Some(&i)); + } + } + const N_THREADS: usize = 8; // The stress test creates an index, and simultaneously writes