Skip to content

Commit

Permalink
bug/4 Removed useless use of Cursor.
Browse files Browse the repository at this point in the history
  • Loading branch information
fulmicoton committed Oct 16, 2016
1 parent 20c089b commit 9358eb3
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 36 deletions.
3 changes: 1 addition & 2 deletions src/common/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ impl BinarySerializable for String {
#[cfg(test)]
mod test {

use std::io::Cursor;
use common::VInt;
use super::*;

Expand All @@ -131,7 +130,7 @@ mod test {
else {
v.serialize(&mut buffer).unwrap();
}
let mut cursor = Cursor::new(&buffer[..]);
let mut cursor = &buffer[..];
let deser = T::deserialize(&mut cursor).unwrap();
assert_eq!(deser, v);
}
Expand Down
15 changes: 7 additions & 8 deletions src/datastruct/fstmap.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#![allow(should_implement_trait)]

use std::io;
use std::io::Seek;
use std::io::Write;
use std::io::Cursor;
use fst;
use fst::raw::Fst;
use fst::Streamer;
Expand Down Expand Up @@ -92,12 +90,13 @@ impl<V: BinarySerializable> FstMap<V> {
}

pub fn from_source(source: ReadOnlySource) -> io::Result<FstMap<V>> {
let mut cursor = Cursor::new(source.as_slice());
try!(cursor.seek(io::SeekFrom::End(-4)));
let footer_size = try!(u32::deserialize(&mut cursor)) as usize;
let split_len = source.len() - 4 - footer_size;
let total_len = source.len();
let length_offset = total_len - 4;
let mut split_len_buffer: &[u8] = &source.as_slice()[length_offset..];
let footer_size = try!(u32::deserialize(&mut split_len_buffer)) as usize;
let split_len = length_offset - footer_size;
let fst_source = source.slice(0, split_len);
let values_source = source.slice(split_len, source.len() - 4);
let values_source = source.slice(split_len, length_offset);
let fst_index = try!(open_fst_index(fst_source));
Ok(FstMap {
fst_index: fst_index,
Expand All @@ -108,7 +107,7 @@ impl<V: BinarySerializable> FstMap<V> {

fn read_value(&self, offset: u64) -> V {
let buffer = self.values_mmap.as_slice();
let mut cursor = Cursor::new(&buffer[(offset as usize)..]);
let mut cursor = &buffer[(offset as usize)..];
V::deserialize(&mut cursor).expect("Data in FST is corrupted")
}

Expand Down
7 changes: 0 additions & 7 deletions src/directory/read_only_source.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use fst::raw::MmapReadOnly;
use std::ops::Deref;
use std::io::Cursor;
use super::shared_vec_slice::SharedVecSlice;
use common::HasLen;

Expand Down Expand Up @@ -46,12 +45,6 @@ impl ReadOnlySource {
}
}


/// Creates a cursor over the data.
pub fn cursor(&self) -> Cursor<&[u8]> {
Cursor::new(&*self)
}

/// Creates a ReadOnlySource that is just a
/// view over a slice of the data.
///
Expand Down
16 changes: 10 additions & 6 deletions src/fastfield/reader.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::io;
use std::io::{SeekFrom, Seek};
use std::collections::HashMap;
use std::ops::Deref;

Expand Down Expand Up @@ -33,7 +32,7 @@ impl U32FastFieldReader {
let min_val;
let amplitude;
{
let mut cursor = data.cursor();
let mut cursor = data.as_slice();
min_val = try!(u32::deserialize(&mut cursor));
amplitude = try!(u32::deserialize(&mut cursor));
}
Expand Down Expand Up @@ -73,10 +72,15 @@ impl U32FastFieldsReader {
let header_offset;
let field_offsets: Vec<(Field, u32)>;
{
let mut cursor = source.cursor();
header_offset = try!(u32::deserialize(&mut cursor));
try!(cursor.seek(SeekFrom::Start(header_offset as u64)));
field_offsets = try!(Vec::deserialize(&mut cursor));
let buffer = source.as_slice();
{
let mut cursor = buffer;
header_offset = try!(u32::deserialize(&mut cursor));
}
{
let mut cursor = &buffer[header_offset as usize..];
field_offsets = try!(Vec::deserialize(&mut cursor));
}
}
let mut end_offsets: Vec<u32> = field_offsets
.iter()
Expand Down
28 changes: 15 additions & 13 deletions src/store/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ use schema::FieldValue;
use common::BinarySerializable;

use std::io::Read;
use std::io::Cursor;
use std::io;
use std::io::SeekFrom;
use std::io::Seek;
use std::cmp::Ordering;
use lz4;

Expand All @@ -29,11 +26,16 @@ impl StoreReader {
// TODO err
// the first offset is implicitely (0, 0)
let mut offsets = vec!(OffsetIndex(0, 0));
let mut cursor = Cursor::new(data.as_slice());
cursor.seek(SeekFrom::End(-8)).unwrap();
let offset = u64::deserialize(&mut cursor).unwrap();
cursor.seek(SeekFrom::Start(offset)).unwrap();
offsets.append(&mut Vec::deserialize(&mut cursor).unwrap());
let buffer: &[u8] = data.as_slice();

let offset = {
let mut cursor = &buffer[buffer.len() - 8..];
u64::deserialize(&mut cursor).unwrap() as usize
};
{
let mut cursor = &buffer[offset..];
offsets.append(&mut Vec::deserialize(&mut cursor).unwrap());
}
offsets
}

Expand All @@ -57,21 +59,21 @@ impl StoreReader {
let mut current_block_mut = self.current_block.borrow_mut();
current_block_mut.clear();
let total_buffer = self.data.as_slice();
let mut cursor = Cursor::new(&total_buffer[block_offset..]);
let mut cursor = &total_buffer[block_offset..];
let block_length = u32::deserialize(&mut cursor).unwrap();
let block_array: &[u8] = &total_buffer[(block_offset + 4 as usize)..(block_offset + 4 + block_length as usize)];
let mut lz4_decoder = try!(lz4::Decoder::new(Cursor::new(block_array)));
let mut lz4_decoder = try!(lz4::Decoder::new(block_array));
lz4_decoder.read_to_end(&mut current_block_mut).map(|_| ())
}

pub fn get(&self, doc_id: DocId) -> Result<Document> {
let OffsetIndex(first_doc_id, block_offset) = self.block_offset(doc_id);
try!(self.read_block(block_offset as usize));
let mut current_block_mut = self.current_block.borrow_mut();
let mut cursor = Cursor::new(&mut current_block_mut[..]);
let current_block_mut = self.current_block.borrow_mut();
let mut cursor = &current_block_mut[..];
for _ in first_doc_id..doc_id {
let block_length = try!(u32::deserialize(&mut cursor));
try!(cursor.seek(SeekFrom::Current(block_length as i64)));
cursor = &cursor[block_length as usize..];
}
try!(u32::deserialize(&mut cursor));
let mut field_values = Vec::new();
Expand Down

0 comments on commit 9358eb3

Please sign in to comment.