added json meta writing

This commit is contained in:
Paul Masurel
2016-01-21 09:45:31 +09:00
parent 2dd182b7e0
commit d1ff654ded
4 changed files with 38 additions and 7 deletions

View File

@@ -13,3 +13,4 @@ fst = "0.1.26"
rand = "0.3.13"
atomicwrites = "0.0.14"
tempfile = "2.0.0"
rustc-serialize = "0.3.16"

View File

@@ -15,7 +15,8 @@ use std::cell::RefCell;
use core::error::*;
use rand::{thread_rng, Rng};
use fst::raw::MmapReadOnly;
// use sys::fs as fs_imp;
use rustc_serialize::json;
use atomicwrites;
#[derive(Clone, Debug)]
pub struct SegmentId(pub String);
@@ -28,12 +29,25 @@ pub fn generate_segment_name() -> SegmentId {
SegmentId( String::from("_") + &random_name)
}
// #[derive()]
#[derive(Clone,Debug,RustcDecodable, RustcEncodable)]
pub struct DirectoryMeta {
segments: Vec<String>
}
impl DirectoryMeta {
fn new() -> DirectoryMeta {
DirectoryMeta {
segments: Vec::new()
}
}
}
#[derive(Clone)]
pub struct Directory {
index_path: PathBuf,
mmap_cache: Arc<Mutex<HashMap<PathBuf, MmapReadOnly>>>,
segments: Vec<Segment>,
metas: DirectoryMeta,
}
impl fmt::Debug for Directory {
@@ -70,7 +84,7 @@ impl Directory {
// TODO find a rusty way to hide that, while keeping
// it visible for IndexWriters.
pub fn publish_segment(&mut self, segment: Segment) {
self.segments.push(segment.clone());
self.metas.segments.push(segment.segment_id.0.clone());
self.save_metas();
}
@@ -79,7 +93,7 @@ impl Directory {
let mut directory = Directory {
index_path: PathBuf::from(filepath),
mmap_cache: Arc::new(Mutex::new(HashMap::new())),
segments: Vec::new()
metas: DirectoryMeta::new()
};
try!(directory.load_metas()); //< does the directory already exists?
Ok(directory)
@@ -90,9 +104,21 @@ impl Directory {
Ok(())
}
fn meta_filepath(&self,) -> PathBuf {
self.resolve_path(&PathBuf::from("meta.json"))
}
pub fn save_metas(&self,) -> Result<()> {
// TODO
Ok(())
let encoded = json::encode(&self.metas).unwrap();
let meta_filepath = self.meta_filepath();
let meta_file = atomicwrites::AtomicFile::new(meta_filepath, atomicwrites::AllowOverwrite);
let write_result = meta_file.write(|f| {
f.write_all(encoded.as_bytes())
});
match write_result {
Ok(_) => Ok(()),
Err(ioerr) => Err(Error::IOError(ioerr.kind(), format!("Failed to write meta file : {:?}", ioerr))),
}
}
@@ -169,6 +195,8 @@ pub struct Segment {
segment_id: SegmentId,
}
impl Segment {
fn path_suffix(component: &SegmentComponent)-> &'static str {
match *component {

View File

@@ -9,5 +9,4 @@ pub mod serial;
pub mod reader;
pub mod codec;
pub mod error;
pub use core::global::DocId;

View File

@@ -7,5 +7,8 @@ extern crate byteorder;
extern crate memmap;
extern crate rand;
extern crate regex;
extern crate rustc_serialize;
extern crate atomicwrites;
pub mod core;