Skip to content

Commit

Permalink
TableInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
yito88 committed Nov 16, 2023
1 parent eaf8dd3 commit 5c96acd
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 192 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2018"

[dependencies]
bincode = "1.3.1"
bloomfilter = "1.0.3"
bloomfilter = { version = "1.0.12", features = ["serde"] }
config = "0.11"
cfg-if = "0.1.10"
crc = "1.8.1"
Expand Down
8 changes: 2 additions & 6 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,8 @@ impl Config {
self.bloom_filter.fp_rate
}

pub fn get_filter_file_path(&self, name: &str) -> String {
format!("{}/filter.amph", self.get_table_dir_path(name))
}

pub fn get_index_file_path(&self, name: &str) -> String {
format!("{}/index.amph", self.get_table_dir_path(name))
pub fn get_metadata_path(&self, name: &str) -> String {
format!("{}/metadata.amph", self.get_table_dir_path(name))
}
}

Expand Down
29 changes: 16 additions & 13 deletions src/flush_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::fptree::leaf_manager::NUM_SLOT;
use crate::fptree::Leaf;
use crate::fptree_manager::FPTreeManager;
use crate::sparse_index::SparseIndex;
use crate::sstable_manager::SstableManager;
use crate::sstable_manager::{SstableManager, TableId, TableInfo};
use crate::util::data_util;

#[double]
Expand All @@ -38,8 +38,8 @@ pub fn spawn_flush_writer(
FlushSignal::TryFlush => {
// TODO: error handling
if let Some(first_leaf) = fptree_manager.prepare_flush().unwrap() {
let (table_id, filter, index) = flush_writer.flush(first_leaf).unwrap();
sstable_manager.register(table_id, filter, index).unwrap();
let table_info = flush_writer.flush(first_leaf).unwrap();
sstable_manager.register(table_info).unwrap();
fptree_manager.switch_fptree().unwrap();
}
}
Expand All @@ -52,7 +52,7 @@ pub fn spawn_flush_writer(
pub struct FlushWriter {
name: String,
config: Config,
table_id: usize,
table_id: TableId,
}

impl FlushWriter {
Expand All @@ -65,10 +65,7 @@ impl FlushWriter {
}

/// flush the current tree
pub fn flush(
&mut self,
first_leaf: Arc<RwLock<Leaf>>,
) -> Result<(usize, Bloom<Vec<u8>>, SparseIndex), std::io::Error> {
pub fn flush(&mut self, first_leaf: Arc<RwLock<Leaf>>) -> Result<TableInfo, std::io::Error> {
debug!(
"Starting flush FPTree of {} to SSTable ID {}",
self.name, self.table_id
Expand All @@ -85,15 +82,15 @@ impl FlushWriter {
&mut self,
name: &str,
fptree_id: usize,
) -> Result<(usize, Bloom<Vec<u8>>, SparseIndex), std::io::Error> {
) -> Result<TableInfo, std::io::Error> {
let leaf_manager = LeafManager::new(name, fptree_id, &self.config)?;
let id_list = leaf_manager.get_leaf_id_chain();
debug!("leaf ID list: {:?}", id_list);

self.flush_kv(Arc::new(RwLock::new(leaf_manager)), id_list)
}

fn create_new_table(&mut self) -> Result<(usize, File), std::io::Error> {
fn create_new_table(&mut self) -> Result<(TableId, File), std::io::Error> {
let id = self.table_id;
let table_file_path = self.config.get_table_file_path(&self.name, id);
let table_file = File::create(table_file_path)?;
Expand All @@ -108,10 +105,10 @@ impl FlushWriter {
&mut self,
leaf_manager: Arc<RwLock<LeafManager>>,
id_list: Vec<usize>,
) -> Result<(usize, Bloom<Vec<u8>>, SparseIndex), std::io::Error> {
) -> Result<TableInfo, std::io::Error> {
let mut offset = 0;
let (table_id, table_file) = self.create_new_table()?;
let mut index = SparseIndex::new(table_id);
let mut index = SparseIndex::new();
let mut filter = Bloom::new_for_fp_rate(
self.config.get_filter_items_count(),
self.config.get_filter_fp_rate(),
Expand Down Expand Up @@ -147,6 +144,12 @@ impl FlushWriter {
}
table_file.sync_all()?;

Ok((table_id, filter, index))
Ok(TableInfo {
id: table_id,
size: table_file.metadata()?.len() as _,
level: 0,
filter,
index,
})
}
}
5 changes: 2 additions & 3 deletions src/kvs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ impl KVS {
for entry in std::fs::read_dir(path)? {
if let Some(fptree_id) = file_util::get_tree_id(&entry?.path()) {
debug!("found FPTree ID: {}", fptree_id);
let (table_id, filter, index) =
flush_writer.flush_with_file(name, fptree_id)?;
sstable_manager.register(table_id, filter, index)?;
let table_info = flush_writer.flush_with_file(name, fptree_id)?;
sstable_manager.register(table_info)?;
let leaf_file = config.get_leaf_file_path(name, fptree_id);
std::fs::remove_file(leaf_file)?;
}
Expand Down
8 changes: 1 addition & 7 deletions src/sparse_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,18 @@ const LEAST_OFFSET: usize = 1 << 18;

#[derive(Serialize, Deserialize)]
pub struct SparseIndex {
table_id: usize,
prev_offset: usize,
index: BTreeMap<Vec<u8>, usize>,
}

impl SparseIndex {
pub fn new(table_id: usize) -> Self {
pub fn new() -> Self {
SparseIndex {
table_id,
prev_offset: usize::MAX,
index: BTreeMap::new(),
}
}

pub fn get_table_id(&self) -> usize {
self.table_id
}

pub fn insert(&mut self, key: &[u8], offset: usize) {
if self.prev_offset == usize::MAX || offset - self.prev_offset >= LEAST_OFFSET {
self.prev_offset = offset;
Expand Down
Loading

0 comments on commit 5c96acd

Please sign in to comment.