Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add badges to readme #4

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
[package]
authors = ["Kristoffer Ström <kristoffer@rymdkoloni.se>"]
authors = ["Kristoffer Ström <kristoffer@rymdkoloni.se>",
"David Stainton <dawuud@riseup.net>"]
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"]
Expand Down
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
42 changes: 41 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -395,6 +395,19 @@ impl<K: Hash + Copy + PartialEq, V: Hash + Copy> Index<K, V> {
}
}

/// 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
Expand Down Expand Up @@ -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
Expand Down