2018-11-08 12:05:59 +01:00
|
|
|
use std::slice::from_raw_parts;
|
|
|
|
use std::sync::Arc;
|
|
|
|
use std::{io, mem};
|
|
|
|
|
2018-12-30 13:22:02 +01:00
|
|
|
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
|
2018-12-09 14:18:23 +01:00
|
|
|
use sdset::Set;
|
2018-11-08 12:05:59 +01:00
|
|
|
|
|
|
|
use crate::DocumentId;
|
2018-12-30 13:22:02 +01:00
|
|
|
use crate::data::SharedData;
|
2018-11-08 12:05:59 +01:00
|
|
|
|
2018-12-01 18:37:21 +01:00
|
|
|
#[derive(Default, Clone)]
|
2018-11-08 12:05:59 +01:00
|
|
|
pub struct DocIds {
|
2018-12-30 13:22:02 +01:00
|
|
|
data: SharedData,
|
2018-11-08 12:05:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DocIds {
|
2018-12-30 16:17:18 +01:00
|
|
|
pub fn empty() -> Self {
|
|
|
|
DocIds { data: SharedData::empty() }
|
|
|
|
}
|
|
|
|
|
2018-12-30 13:22:02 +01:00
|
|
|
pub fn from_bytes(vec: Vec<u8>) -> io::Result<Self> {
|
|
|
|
let len = vec.len();
|
|
|
|
DocIds::from_shared_bytes(Arc::new(vec), 0, len)
|
2018-11-08 12:05:59 +01:00
|
|
|
}
|
|
|
|
|
2018-12-30 13:22:02 +01:00
|
|
|
pub fn from_shared_bytes(bytes: Arc<Vec<u8>>, offset: usize, len: usize) -> io::Result<Self> {
|
|
|
|
let data = SharedData { bytes, offset, len };
|
|
|
|
DocIds::from_data(data)
|
|
|
|
}
|
|
|
|
|
2018-12-30 16:17:18 +01:00
|
|
|
pub fn as_bytes(&self) -> &[u8] {
|
|
|
|
&self.data
|
|
|
|
}
|
|
|
|
|
2018-12-30 13:22:02 +01:00
|
|
|
fn from_data(data: SharedData) -> io::Result<Self> {
|
|
|
|
let len = data.as_ref().read_u64::<LittleEndian>()?;
|
|
|
|
let data = data.range(mem::size_of::<u64>(), len as usize);
|
2018-11-23 18:00:24 +01:00
|
|
|
Ok(DocIds { data })
|
2018-11-08 12:05:59 +01:00
|
|
|
}
|
|
|
|
|
2018-12-30 13:22:02 +01:00
|
|
|
pub fn from_raw(vec: Vec<DocumentId>) -> Self {
|
2018-11-26 17:30:19 +01:00
|
|
|
DocIds::from_bytes(unsafe { mem::transmute(vec) }).unwrap()
|
|
|
|
}
|
|
|
|
|
2018-12-30 13:22:02 +01:00
|
|
|
pub fn write_to_bytes(&self, bytes: &mut Vec<u8>) {
|
|
|
|
let len = self.data.len() as u64;
|
|
|
|
bytes.write_u64::<LittleEndian>(len).unwrap();
|
|
|
|
bytes.extend_from_slice(&self.data);
|
|
|
|
}
|
|
|
|
|
2018-11-08 12:05:59 +01:00
|
|
|
pub fn contains(&self, doc: DocumentId) -> bool {
|
|
|
|
// FIXME prefer using the sdset::exponential_search function
|
|
|
|
self.doc_ids().binary_search(&doc).is_ok()
|
|
|
|
}
|
|
|
|
|
2018-12-09 14:18:23 +01:00
|
|
|
pub fn doc_ids(&self) -> &Set<DocumentId> {
|
2018-11-23 18:00:24 +01:00
|
|
|
let slice = &self.data;
|
2018-11-08 12:05:59 +01:00
|
|
|
let ptr = slice.as_ptr() as *const DocumentId;
|
|
|
|
let len = slice.len() / mem::size_of::<DocumentId>();
|
2018-12-09 14:18:23 +01:00
|
|
|
let slice = unsafe { from_raw_parts(ptr, len) };
|
|
|
|
Set::new_unchecked(slice)
|
2018-11-08 12:05:59 +01:00
|
|
|
}
|
|
|
|
}
|