Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
trinity-1686a committed Sep 4, 2019
1 parent 1911b48 commit 32baf73
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/common/counting_writer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::io;
use std::io::Write;
use crate::directory::AntiCallToken;
use crate::directory::TerminatingWrite;
use std::io;
use std::io::Write;

pub struct CountingWriter<W> {
underlying: W,
Expand Down
42 changes: 24 additions & 18 deletions src/directory/managed_directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,30 +244,34 @@ impl ManagedDirectory {

#[derive(Debug, Copy, Clone)]
pub enum Footer {
V0(V0)
V0(V0),
}

impl Footer {
pub fn to_bytes(&self) -> Vec<u8> {
match self {
Footer::V0(f) => f.to_bytes()
Footer::V0(f) => f.to_bytes(),
}
}

pub fn from_bytes(data: &[u8]) -> Self {
let len = data.len();
assert!(len >= 2);
let size = LittleEndian::read_u16(&data[len - 2..]);
assert!(len >= size as usize, "len({}) is smaller than size({})", len, size);
assert!(
len >= size as usize,
"len({}) is smaller than size({})",
len,
size
);
let footer = &data[len - size as usize..];
let index_version = footer[0];
match index_version {
0 => Footer::V0(V0::from_bytes(footer)),
_ => panic!("unsuported index_version")
_ => panic!("unsuported index_version"),
}
}


pub fn index_version(&self) -> u8 {
match self {
Footer::V0(_) => 0,
Expand Down Expand Up @@ -295,7 +299,7 @@ impl Footer {

#[derive(Debug, Copy, Clone)]
pub struct V0 {
pub tantivy_version: (u8,u8,u8),
pub tantivy_version: (u8, u8, u8),
pub crc: u32,
}

Expand All @@ -319,18 +323,18 @@ impl V0 {

V0 {
tantivy_version,
crc
crc,
}
}

pub fn from_crc(crc: u32) -> Self {
Self {
tantivy_version: (
env!("CARGO_PKG_VERSION_MAJOR").parse().unwrap(),
env!("CARGO_PKG_VERSION_MINOR").parse().unwrap(),
env!("CARGO_PKG_VERSION_PATCH").parse().unwrap(),
env!("CARGO_PKG_VERSION_MAJOR").parse().unwrap(),
env!("CARGO_PKG_VERSION_MINOR").parse().unwrap(),
env!("CARGO_PKG_VERSION_PATCH").parse().unwrap(),
),
crc
crc,
}
}
pub fn size() -> u16 {
Expand Down Expand Up @@ -372,7 +376,7 @@ impl<W: TerminatingWrite> TerminatingWrite for FooterProxy<W> {
let footer = Footer::V0(V0::from_crc(crc)).to_bytes();
let mut writer = self.writer.take().unwrap();
writer.write_all(&footer)?;
writer.flush()?;//should we assume calling terminate on inner already flush?
writer.flush()?; //should we assume calling terminate on inner already flush?
writer.terminate()
}
}
Expand All @@ -387,9 +391,13 @@ impl Directory for ManagedDirectory {
fn open_write(&mut self, path: &Path) -> result::Result<WritePtr, OpenWriteError> {
self.register_file_as_managed(path)
.map_err(|e| IOError::with_path(path.to_owned(), e))?;
Ok(io::BufWriter::new(Box::new(
FooterProxy::new(self.directory.open_write(path)?.into_inner().map_err(|_|()).expect("buffer should be empty"))
)))
Ok(io::BufWriter::new(Box::new(FooterProxy::new(
self.directory
.open_write(path)?
.into_inner()
.map_err(|_| ())
.expect("buffer should be empty"),
))))
}

// TODO: to be correct, this require an non enforced contract, atomic writes must only be
Expand Down Expand Up @@ -484,9 +492,7 @@ mod tests_mmap_specific {

let mmap_directory = MmapDirectory::open(&tempdir_path).unwrap();
let mut managed_directory = ManagedDirectory::wrap(mmap_directory).unwrap();
let mut write = managed_directory
.open_write(test_path1)
.unwrap();
let mut write = managed_directory.open_write(test_path1).unwrap();
write.write_all(&[0u8, 1u8]).unwrap();
write.terminate().unwrap();
assert!(managed_directory.exists(test_path1));
Expand Down
5 changes: 4 additions & 1 deletion src/directory/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ pub struct AntiCallToken(());
/// Trait used to indicate when no more write need to be done on a writer
pub trait TerminatingWrite: Write {
/// Indicate that the writer will no longer be used. Internally call terminate_ref.
fn terminate(mut self) -> io::Result<()> where Self: Sized {
fn terminate(mut self) -> io::Result<()>
where
Self: Sized,
{
self.terminate_ref(AntiCallToken(()))
}

Expand Down
2 changes: 1 addition & 1 deletion src/directory/ram_directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::core::META_FILEPATH;
use crate::directory::error::{DeleteError, OpenReadError, OpenWriteError};
use crate::directory::AntiCallToken;
use crate::directory::WatchCallbackList;
use crate::directory::{TerminatingWrite, WritePtr};
use crate::directory::{Directory, ReadOnlySource, WatchCallback, WatchHandle};
use crate::directory::{TerminatingWrite, WritePtr};
use fail::fail_point;
use std::collections::HashMap;
use std::fmt;
Expand Down
2 changes: 1 addition & 1 deletion src/indexer/index_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use crate::core::SegmentComponent;
use crate::core::SegmentId;
use crate::core::SegmentMeta;
use crate::core::SegmentReader;
use crate::directory::TerminatingWrite;
use crate::directory::DirectoryLock;
use crate::directory::TerminatingWrite;
use crate::docset::DocSet;
use crate::error::TantivyError;
use crate::fastfield::write_delete_bitset;
Expand Down

0 comments on commit 32baf73

Please sign in to comment.