nodemap: implement indexedlog::DefaultOpenOptions

Summary: This implements `Node{Map,Set}::repair` for free.

Reviewed By: xavierd

Differential Revision: D18737905

fbshipit-source-id: f0a4a64fddeb895d979bee3c51478c138c8235fe
This commit is contained in:
Jun Wu 2019-12-06 19:32:54 -08:00 committed by Facebook Github Bot
parent b0f061f66b
commit 237a9708f6
3 changed files with 31 additions and 16 deletions

View File

@ -12,3 +12,4 @@ pub mod nodeset;
pub use crate::nodemap::NodeMap;
pub use crate::nodeset::NodeSet;
pub use indexedlog::Repair;

View File

@ -9,7 +9,10 @@ use std::ops::Range;
use std::path::Path;
use anyhow::Result;
use indexedlog::log::{self, IndexOutput, Log};
use indexedlog::{
log::{self, IndexOutput, Log},
DefaultOpenOptions,
};
use thiserror::Error;
use types::errors::KeyError;
use types::node::Node;
@ -32,17 +35,21 @@ pub struct NodeMap {
log: Log,
}
impl NodeMap {
pub fn open(dir: impl AsRef<Path>) -> Result<Self> {
// Update the index every 100KB, i.e. every 256 entries
impl DefaultOpenOptions<log::OpenOptions> for NodeMap {
fn default_open_options() -> log::OpenOptions {
let first_index = |_data: &[u8]| vec![IndexOutput::Reference(0..20)];
let second_index = |_data: &[u8]| vec![IndexOutput::Reference(20..40)];
log::OpenOptions::new()
.create(true)
.index("first", first_index)
.index("second", second_index)
}
}
impl NodeMap {
pub fn open(dir: impl AsRef<Path>) -> Result<Self> {
Ok(NodeMap {
log: log::OpenOptions::new()
.create(true)
.index("first", first_index)
.index("second", second_index)
.open(dir)?,
log: Self::default_open_options().open(dir)?,
})
}

View File

@ -6,7 +6,10 @@
*/
use anyhow::Result;
use indexedlog::log::{self, IndexOutput, Log};
use indexedlog::{
log::{self, IndexOutput, Log},
DefaultOpenOptions,
};
use std::path::Path;
use thiserror::Error;
use types::errors::KeyError;
@ -30,17 +33,21 @@ pub struct NodeSet {
log: Log,
}
impl DefaultOpenOptions<log::OpenOptions> for NodeSet {
fn default_open_options() -> log::OpenOptions {
let node_index = |_data: &[u8]| vec![IndexOutput::Reference(0..Node::len() as u64)];
log::OpenOptions::new()
.create(true)
.index("node", node_index)
}
}
impl NodeSet {
const INDEX_NODE: usize = 0;
pub fn open(dir: impl AsRef<Path>) -> Result<Self> {
// Update the index every 100KB, i.e. every 256 entries
let node_index = |_data: &[u8]| vec![IndexOutput::Reference(0..Node::len() as u64)];
Ok(NodeSet {
log: log::OpenOptions::new()
.create(true)
.index("node", node_index)
.open(dir)?,
log: Self::default_open_options().open(dir)?,
})
}