Skip to content
Snippets Groups Projects
Verified Commit 00a10c91 authored by David Mehren's avatar David Mehren
Browse files

[BPTree] Add serialization/deserialization

parent 95ee30ba
No related branches found
No related tags found
No related merge requests found
...@@ -8,3 +8,6 @@ bv = "0.7" ...@@ -8,3 +8,6 @@ bv = "0.7"
bio = "0.19.0" bio = "0.19.0"
id_tree = "1.3.0" id_tree = "1.3.0"
failure = "0.1.1" failure = "0.1.1"
serde = "1.0.64"
serde_derive = "1.0.64"
bincode = "1.0.0"
\ No newline at end of file
...@@ -3,6 +3,9 @@ extern crate bv; ...@@ -3,6 +3,9 @@ extern crate bv;
extern crate id_tree; extern crate id_tree;
#[macro_use] #[macro_use]
extern crate failure; extern crate failure;
#[macro_use]
extern crate serde_derive;
extern crate bincode;
pub mod common; pub mod common;
pub mod trees; pub mod trees;
use bincode::{deserialize, serialize};
use bio::data_structures::rank_select::RankSelect; use bio::data_structures::rank_select::RankSelect;
use bv::{BitVec, BitsMut}; use bv::{BitVec, BitsMut};
use common::succinct_tree::SuccinctTree; use common::succinct_tree::SuccinctTree;
use failure::Error; use failure::{Error, ResultExt};
use id_tree::Tree; use id_tree::Tree;
use std::fmt; use std::fmt;
use std::fmt::Debug; use std::fmt::Debug;
use std::fmt::Formatter; use std::fmt::Formatter;
use std::fs;
use std::fs::File;
use std::io::Write;
#[derive(Serialize, Deserialize)]
pub struct BPTree { pub struct BPTree {
bits: BitVec<u8>, bits: BitVec<u8>,
rankselect: RankSelect, rankselect: RankSelect,
rminmax: String, rminmax: String,
} }
impl PartialEq for BPTree {
fn eq(&self, other: &BPTree) -> bool {
self.bits == other.bits
}
}
impl SuccinctTree<BPTree> for BPTree { impl SuccinctTree<BPTree> for BPTree {
fn is_leaf(&self, index: u64) -> bool { fn is_leaf(&self, index: u64) -> bool {
unimplemented!() unimplemented!()
...@@ -64,6 +75,19 @@ impl BPTree { ...@@ -64,6 +75,19 @@ impl BPTree {
rminmax: "foo".to_string(), rminmax: "foo".to_string(),
}) })
} }
pub fn from_file(path: String) -> Result<BPTree, Error> {
let file = fs::read(path).context("Could not read saved tree.")?;
let tree: BPTree = deserialize(&file).context("Error while deserializing tree.")?;
Ok(tree)
}
pub fn save_to(&self, path: String) -> Result<(), Error> {
let encoded = serialize(&self).context("Error while serializing tree.")?;
let mut file = File::create(path).context("Could not save tree.")?;
file.write_all(&encoded)?;
Ok(())
}
} }
#[cfg(test)] #[cfg(test)]
...@@ -84,7 +108,26 @@ mod tests { ...@@ -84,7 +108,26 @@ mod tests {
#[test] #[test]
#[should_panic(expected = "ErrorMessage { msg: \"Bit vector not valid.\" }")] #[should_panic(expected = "ErrorMessage { msg: \"Bit vector not valid.\" }")]
fn new_from_bitvec_invalid() { fn new_from_bitvec_invalid() {
let mut bitvec = BitVec::new_fill(false, 2); let bitvec = BitVec::new_fill(false, 2);
BPTree::from_bitvec(bitvec.clone()).unwrap(); BPTree::from_bitvec(bitvec.clone()).unwrap();
} }
#[test]
fn save_load() {
let tree = BPTree::stub_create();
tree.save_to("testdata/bptree.testdata".to_string())
.unwrap();
let result = BPTree::from_file("testdata/bptree.testdata".to_string()).unwrap();
assert_eq!(
tree, result,
"The loaded tree is not equal to the original one."
);
}
#[test]
#[should_panic(expected = "Error while deserializing tree.")]
fn load_invaild() {
BPTree::from_file("testdata/bptree_invalid.testdata".to_string()).unwrap();
}
} }
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment