indexedlog: add OpenOptions::index

Summary:
Make it a bit easier to define indexes.

Before:

    OpenOptions::new()
      .index_defs(vec![IndexDef::new("first-byte", |_| {
          vec![IndexOutput::Reference(0..1)]
      })])

After:

    OpenOptions::new()
      .index("first-byte", |_| vec![IndexOutput::Reference(0..1)])

Reviewed By: kulshrax

Differential Revision: D14690357

fbshipit-source-id: 6e80a91f4279f960d9f41369c228e79023b5164c
This commit is contained in:
Jun Wu 2019-04-01 17:08:43 -07:00 committed by Facebook Github Bot
parent 88fb64a6ee
commit 1e59d25e17
3 changed files with 31 additions and 17 deletions

View File

@ -818,6 +818,15 @@ impl OpenOptions {
}
}
/// Add an index function.
///
/// This is a convenient way to define indexes without using [`IndexDef`]
/// explictly.
pub fn index(mut self, name: &'static str, func: fn(&[u8]) -> Vec<IndexOutput>) -> Self {
self.index_defs.push(IndexDef::new(name, func));
self
}
/// Sets index definitions.
///
/// See [`IndexDef::new`] for details.
@ -1409,14 +1418,16 @@ mod tests {
let first_index =
|_data: &[u8]| vec![IndexOutput::Reference(0..2), IndexOutput::Reference(3..5)];
let second_index = |data: &[u8]| vec![IndexOutput::Owned(Box::from(&data[5..10]))];
let mut log = Log::open(
dir.path(),
vec![
let third_index = |_: &[u8]| vec![IndexOutput::Owned(Box::from(&b"x"[..]))];
let mut log = OpenOptions::new()
.create(true)
.index_defs(vec![
IndexDef::new("first", first_index).lag_threshold(0),
IndexDef::new("second", second_index).lag_threshold(0),
],
)
.unwrap();
])
.index("third", third_index)
.open(dir.path())
.unwrap();
let mut expected_keys1 = vec![];
let mut expected_keys2 = vec![];
@ -1447,6 +1458,7 @@ mod tests {
}
assert_eq!(found_keys1, expected_keys1);
assert_eq!(found_keys2, expected_keys2);
assert_eq!(log.iter().count(), log.lookup(2, b"x").unwrap().count());
}
quickcheck! {

View File

@ -86,6 +86,12 @@ impl OpenOptions {
self
}
/// Add an index function.
pub fn index(mut self, name: &'static str, func: fn(&[u8]) -> Vec<log::IndexOutput>) -> Self {
self.log_open_options = self.log_open_options.index(name, func);
self
}
/// Set the index definitions.
///
/// See [`IndexDef`] for details.
@ -374,9 +380,7 @@ mod tests {
.create(true)
.max_bytes_per_log(100)
.max_log_count(2)
.index_defs(vec![IndexDef::new("first-byte", |_| {
vec![IndexOutput::Reference(0..1)]
})])
.index("first-byte", |_| vec![IndexOutput::Reference(0..1)])
.open(&dir)
.unwrap();

View File

@ -4,7 +4,7 @@ use std::ops::Range;
use std::path::Path;
use failure::Fallible;
use indexedlog::log::{IndexDef, IndexOutput, Log};
use indexedlog::log::{self, IndexOutput, Log};
use types::errors::KeyError;
use types::node::Node;
@ -32,13 +32,11 @@ impl NodeMap {
let first_index = |_data: &[u8]| vec![IndexOutput::Reference(0..20)];
let second_index = |_data: &[u8]| vec![IndexOutput::Reference(20..40)];
Ok(NodeMap {
log: Log::open(
dir,
vec![
IndexDef::new("first", first_index),
IndexDef::new("second", second_index),
],
)?,
log: log::OpenOptions::new()
.create(true)
.index("first", first_index)
.index("second", second_index)
.open(dir)?,
})
}