@@ -7,6 +7,7 @@ use serde::Serialize;
7
7
8
8
use crate :: bfield_member:: { BFieldLookup , BFieldMember , BFieldVal } ;
9
9
10
+ /// The struct holding the various bfields
10
11
pub struct BField < T > {
11
12
members : Vec < BFieldMember < T > > ,
12
13
read_only : bool ,
@@ -17,6 +18,18 @@ unsafe impl<T> Send for BField<T> {}
17
18
unsafe impl < T > Sync for BField < T > { }
18
19
19
20
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)
20
33
#[ allow( clippy:: too_many_arguments) ]
21
34
pub fn create < P > (
22
35
directory : P ,
@@ -71,6 +84,7 @@ impl<T: Clone + DeserializeOwned + Serialize> BField<T> {
71
84
} )
72
85
}
73
86
87
+ /// Loads the bfield given the path to the "main" db path (eg the one ending with `0.bfd`).
74
88
pub fn load < P : AsRef < Path > > ( main_db_path : P , read_only : bool ) -> Result < Self , io:: Error > {
75
89
let mut members = Vec :: new ( ) ;
76
90
let mut n = 0 ;
@@ -112,6 +126,8 @@ impl<T: Clone + DeserializeOwned + Serialize> BField<T> {
112
126
Ok ( BField { members, read_only } )
113
127
}
114
128
129
+ /// Write the current bfields to disk.
130
+ /// Only useful if you are creating a bfield in memory
115
131
pub fn persist_to_disk ( self ) -> Result < Self , io:: Error > {
116
132
let mut members = Vec :: with_capacity ( self . members . len ( ) ) ;
117
133
for m in self . members {
@@ -123,12 +139,14 @@ impl<T: Clone + DeserializeOwned + Serialize> BField<T> {
123
139
} )
124
140
}
125
141
142
+ /// Returns (n_hashes, marker_width, n_marker_bits, Vec<size of each member>)
126
143
pub fn build_params ( & self ) -> ( u8 , u8 , u8 , Vec < usize > ) {
127
144
let ( _, n_hashes, marker_width, n_marker_bits) = self . members [ 0 ] . info ( ) ;
128
145
let sizes = self . members . iter ( ) . map ( |i| i. info ( ) . 0 ) . collect ( ) ;
129
146
( n_hashes, marker_width, n_marker_bits, sizes)
130
147
}
131
148
149
+ /// Returns the params given at build time to the bfields
132
150
pub fn params ( & self ) -> & Option < T > {
133
151
& self . members [ 0 ] . params . other
134
152
}
@@ -156,6 +174,9 @@ impl<T: Clone + DeserializeOwned + Serialize> BField<T> {
156
174
}
157
175
}
158
176
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.
159
180
pub fn insert ( & self , key : & [ u8 ] , value : BFieldVal , pass : usize ) -> bool {
160
181
debug_assert ! ( !self . read_only, "Can't insert into read_only bfields" ) ;
161
182
debug_assert ! (
@@ -174,6 +195,8 @@ impl<T: Clone + DeserializeOwned + Serialize> BField<T> {
174
195
true
175
196
}
176
197
198
+ /// Returns the value of the given key if found, None otherwise.
199
+ /// If the value is indeterminate, we still return None.
177
200
pub fn get ( & self , key : & [ u8 ] ) -> Option < BFieldVal > {
178
201
for secondary in self . members . iter ( ) {
179
202
match secondary. get ( key) {
@@ -187,6 +210,8 @@ impl<T: Clone + DeserializeOwned + Serialize> BField<T> {
187
210
None
188
211
}
189
212
213
+ /// Get the info of each member
214
+ /// Returns Vec<(size, n_hashes, marker_width, n_marker_bits)>
190
215
pub fn info ( & self ) -> Vec < ( usize , u8 , u8 , u8 ) > {
191
216
self . members . iter ( ) . map ( |m| m. info ( ) ) . collect ( )
192
217
}
0 commit comments