Skip to content

Commit 68f6deb

Browse files
committed
Update docs and move paper in repo
1 parent f2560fe commit 68f6deb

File tree

5 files changed

+32
-1
lines changed

5 files changed

+32
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# rust-bfield
22
B-field implementation in Rust
33

4-
Full formal description of the data structure is available on Google Drive [here](https://drive.google.com/file/d/0By8-wMpcWky8SGt5YngyUTRfVzg/view?usp=sharing).
4+
Full formal description of the data structure is available [here](bfield.pdf).

bfield.pdf

909 KB
Binary file not shown.

src/bfield.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use serde::Serialize;
77

88
use crate::bfield_member::{BFieldLookup, BFieldMember, BFieldVal};
99

10+
/// The struct holding the various bfields
1011
pub struct BField<T> {
1112
members: Vec<BFieldMember<T>>,
1213
read_only: bool,
@@ -17,6 +18,18 @@ unsafe impl<T> Send for BField<T> {}
1718
unsafe impl<T> Sync for BField<T> {}
1819

1920
impl<T: Clone + DeserializeOwned + Serialize> BField<T> {
21+
/// The (complicated) method to create a bfield.
22+
/// The bfield files will be created in `directory` with the given `filename` and the
23+
/// suffixes `(0..n_secondaries).bfd`
24+
/// `size` is the primary bfield size, subsequent bfield sizes will be determined by
25+
/// `secondary_scaledown` and `max_scaledown`.
26+
/// If you set `in_memory` to true, remember to call `persist_to_disk` when it's built to
27+
/// save it.
28+
/// The params are the following in the paper:
29+
/// `n_hashes` -> k
30+
/// `marker_width` -> v (nu)
31+
/// `n_marker_bits` -> κ (kappa)
32+
/// `secondary_scaledown` -> β (beta)
2033
#[allow(clippy::too_many_arguments)]
2134
pub fn create<P>(
2235
directory: P,
@@ -71,6 +84,7 @@ impl<T: Clone + DeserializeOwned + Serialize> BField<T> {
7184
})
7285
}
7386

87+
/// Loads the bfield given the path to the "main" db path (eg the one ending with `0.bfd`).
7488
pub fn load<P: AsRef<Path>>(main_db_path: P, read_only: bool) -> Result<Self, io::Error> {
7589
let mut members = Vec::new();
7690
let mut n = 0;
@@ -112,6 +126,8 @@ impl<T: Clone + DeserializeOwned + Serialize> BField<T> {
112126
Ok(BField { members, read_only })
113127
}
114128

129+
/// Write the current bfields to disk.
130+
/// Only useful if you are creating a bfield in memory
115131
pub fn persist_to_disk(self) -> Result<Self, io::Error> {
116132
let mut members = Vec::with_capacity(self.members.len());
117133
for m in self.members {
@@ -123,12 +139,14 @@ impl<T: Clone + DeserializeOwned + Serialize> BField<T> {
123139
})
124140
}
125141

142+
/// Returns (n_hashes, marker_width, n_marker_bits, Vec<size of each member>)
126143
pub fn build_params(&self) -> (u8, u8, u8, Vec<usize>) {
127144
let (_, n_hashes, marker_width, n_marker_bits) = self.members[0].info();
128145
let sizes = self.members.iter().map(|i| i.info().0).collect();
129146
(n_hashes, marker_width, n_marker_bits, sizes)
130147
}
131148

149+
/// Returns the params given at build time to the bfields
132150
pub fn params(&self) -> &Option<T> {
133151
&self.members[0].params.other
134152
}
@@ -156,6 +174,9 @@ impl<T: Clone + DeserializeOwned + Serialize> BField<T> {
156174
}
157175
}
158176

177+
/// Insert the given key/value at the given pass
178+
/// Returns whether the value was inserted during this call, eg will return `false` if
179+
/// the value was already present.
159180
pub fn insert(&self, key: &[u8], value: BFieldVal, pass: usize) -> bool {
160181
debug_assert!(!self.read_only, "Can't insert into read_only bfields");
161182
debug_assert!(
@@ -174,6 +195,8 @@ impl<T: Clone + DeserializeOwned + Serialize> BField<T> {
174195
true
175196
}
176197

198+
/// Returns the value of the given key if found, None otherwise.
199+
/// If the value is indeterminate, we still return None.
177200
pub fn get(&self, key: &[u8]) -> Option<BFieldVal> {
178201
for secondary in self.members.iter() {
179202
match secondary.get(key) {
@@ -187,6 +210,8 @@ impl<T: Clone + DeserializeOwned + Serialize> BField<T> {
187210
None
188211
}
189212

213+
/// Get the info of each member
214+
/// Returns Vec<(size, n_hashes, marker_width, n_marker_bits)>
190215
pub fn info(&self) -> Vec<(usize, u8, u8, u8)> {
191216
self.members.iter().map(|m| m.info()).collect()
192217
}

src/bfield_member.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ pub(crate) struct BFieldMember<T> {
6363
pub(crate) params: BFieldParams<T>,
6464
}
6565

66+
/// A simple type alias to make the code more readable
6667
pub type BFieldVal = u32;
6768
const BF_MAGIC: [u8; 2] = [0xBF, 0x1D];
6869

src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
#![deny(missing_docs)]
2+
3+
//! The bfield datastructure, implemented in Rust.
4+
//! A space-efficient, probabilistic data structure and storage and retrieval method for key-value information.
5+
16
mod bfield;
27
mod bfield_member;
38

0 commit comments

Comments
 (0)