2018-11-08 19:05:59 +08:00
|
|
|
mod doc_ids;
|
|
|
|
mod doc_indexes;
|
|
|
|
|
2019-01-01 01:33:59 +08:00
|
|
|
use std::slice::from_raw_parts;
|
|
|
|
use std::mem::size_of;
|
2018-11-08 19:05:59 +08:00
|
|
|
use std::ops::Deref;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2018-11-30 21:31:46 +08:00
|
|
|
pub use self::doc_ids::DocIds;
|
2018-12-01 18:35:16 +08:00
|
|
|
pub use self::doc_indexes::{DocIndexes, DocIndexesBuilder};
|
2018-11-08 19:05:59 +08:00
|
|
|
|
2019-01-01 01:33:59 +08:00
|
|
|
#[derive(Default, Clone)]
|
|
|
|
pub struct SharedData {
|
|
|
|
pub bytes: Arc<Vec<u8>>,
|
|
|
|
pub offset: usize,
|
|
|
|
pub len: usize,
|
2018-11-08 19:05:59 +08:00
|
|
|
}
|
|
|
|
|
2018-12-30 20:22:02 +08:00
|
|
|
impl SharedData {
|
2019-01-01 01:33:59 +08:00
|
|
|
pub fn from_bytes(vec: Vec<u8>) -> SharedData {
|
|
|
|
let len = vec.len();
|
|
|
|
let bytes = Arc::new(vec);
|
|
|
|
SharedData::new(bytes, 0, len)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new(bytes: Arc<Vec<u8>>, offset: usize, len: usize) -> SharedData {
|
|
|
|
SharedData { bytes, offset, len }
|
2018-12-30 23:17:18 +08:00
|
|
|
}
|
|
|
|
|
2018-12-30 20:22:02 +08:00
|
|
|
pub fn range(&self, offset: usize, len: usize) -> SharedData {
|
|
|
|
assert!(offset + len <= self.len);
|
|
|
|
SharedData {
|
|
|
|
bytes: self.bytes.clone(),
|
|
|
|
offset: self.offset + offset,
|
|
|
|
len: len,
|
2018-11-29 00:12:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-30 20:22:02 +08:00
|
|
|
impl Deref for SharedData {
|
2018-11-08 19:05:59 +08:00
|
|
|
type Target = [u8];
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
2018-11-24 01:00:24 +08:00
|
|
|
self.as_ref()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-30 20:22:02 +08:00
|
|
|
impl AsRef<[u8]> for SharedData {
|
2018-11-24 01:00:24 +08:00
|
|
|
fn as_ref(&self) -> &[u8] {
|
2018-12-30 20:22:02 +08:00
|
|
|
&self.bytes[self.offset..self.offset + self.len]
|
2018-11-08 19:05:59 +08:00
|
|
|
}
|
|
|
|
}
|
2019-01-01 01:33:59 +08:00
|
|
|
|
|
|
|
unsafe fn into_u8_slice<T: Sized>(slice: &[T]) -> &[u8] {
|
|
|
|
let ptr = slice.as_ptr() as *const u8;
|
|
|
|
let len = slice.len() * size_of::<T>();
|
|
|
|
from_raw_parts(ptr, len)
|
|
|
|
}
|