2017-04-18 11:52:43 +00:00
|
|
|
extern crate mmap;
|
|
|
|
#[macro_use] extern crate quick_error;
|
2017-03-10 11:43:32 +00:00
|
|
|
|
|
|
|
use std::path::Path;
|
|
|
|
use std::fs::{File, OpenOptions};
|
|
|
|
use std::mem;
|
|
|
|
use std::io;
|
|
|
|
use std::slice;
|
|
|
|
use std::os::unix::io::AsRawFd;
|
|
|
|
|
|
|
|
use mmap::{MemoryMap, MapOption, MapError};
|
|
|
|
|
2017-03-22 08:19:16 +00:00
|
|
|
|
2017-03-10 11:43:32 +00:00
|
|
|
pub const MAX_USAGE: f64 = 0.9;
|
2017-03-25 12:09:45 +00:00
|
|
|
pub const MIN_USAGE: f64 = 0.35;
|
2017-03-10 11:43:32 +00:00
|
|
|
pub const INITIAL_SIZE: usize = 1024;
|
|
|
|
|
2017-03-22 08:19:16 +00:00
|
|
|
|
|
|
|
quick_error!{
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum IndexError {
|
|
|
|
Io(err: io::Error) {
|
|
|
|
from()
|
|
|
|
cause(err)
|
|
|
|
description("Failed to open index file")
|
|
|
|
display("Index error: failed to open the index file\n\tcaused by: {}", err)
|
|
|
|
}
|
|
|
|
Mmap(err: MapError) {
|
|
|
|
from()
|
|
|
|
cause(err)
|
|
|
|
description("Failed to memory-map the index file")
|
|
|
|
display("Index error: failed to memory-map the index file\n\tcaused by: {}", err)
|
|
|
|
}
|
|
|
|
WrongMagic {
|
|
|
|
description("Wrong header")
|
|
|
|
display("Index error: file has the wrong magic header")
|
|
|
|
}
|
|
|
|
UnsupportedVersion(version: u8) {
|
|
|
|
description("Unsupported version")
|
|
|
|
display("Index error: index file has unsupported version: {}", version)
|
|
|
|
}
|
2017-04-18 11:52:43 +00:00
|
|
|
WrongPosition(should: usize, is: LocateResult) {
|
2017-03-22 08:19:16 +00:00
|
|
|
description("Key at wrong position")
|
2017-04-18 11:52:43 +00:00
|
|
|
display("Index error: key has wrong position, expected at: {}, but is at: {:?}", should, is)
|
2017-03-22 08:19:16 +00:00
|
|
|
}
|
|
|
|
WrongEntryCount(header: usize, actual: usize) {
|
|
|
|
description("Wrong entry count")
|
|
|
|
display("Index error: index has wrong entry count, expected {}, but is {}", header, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-10 11:43:32 +00:00
|
|
|
#[repr(packed)]
|
|
|
|
pub struct Header {
|
|
|
|
magic: [u8; 7],
|
|
|
|
version: u8,
|
|
|
|
entries: u64,
|
|
|
|
capacity: u64,
|
|
|
|
}
|
|
|
|
|
2017-04-18 11:52:43 +00:00
|
|
|
pub trait Key: Clone + Eq + Copy {
|
|
|
|
fn hash(&self) -> u64;
|
|
|
|
fn is_used(&self) -> bool;
|
|
|
|
fn clear(&mut self);
|
2017-03-10 11:43:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(packed)]
|
|
|
|
#[derive(Clone)]
|
2017-04-18 11:52:43 +00:00
|
|
|
pub struct Entry<K, V> {
|
|
|
|
pub key: K,
|
|
|
|
pub data: V
|
2017-03-10 11:43:32 +00:00
|
|
|
}
|
|
|
|
|
2017-04-18 11:52:43 +00:00
|
|
|
impl<K: Key, V> Entry<K, V> {
|
2017-03-10 11:43:32 +00:00
|
|
|
#[inline]
|
|
|
|
fn is_used(&self) -> bool {
|
2017-04-18 11:52:43 +00:00
|
|
|
self.key.is_used()
|
2017-03-10 11:43:32 +00:00
|
|
|
}
|
|
|
|
|
2017-04-18 11:52:43 +00:00
|
|
|
#[inline]
|
2017-03-10 11:43:32 +00:00
|
|
|
fn clear(&mut self) {
|
2017-04-18 11:52:43 +00:00
|
|
|
self.key.clear()
|
2017-03-10 11:43:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-18 11:52:43 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum LocateResult {
|
|
|
|
Found(usize), // Found the key at this position
|
|
|
|
Hole(usize), // Found a hole at this position while searching for a key
|
|
|
|
Steal(usize) // Found a spot to steal at this position while searching for a key
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub struct Iter<'a, K: 'static, V: 'static> {
|
|
|
|
items: &'a [Entry<K, V>],
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, K: Key, V> Iterator for Iter<'a, K, V> {
|
|
|
|
type Item = (&'a K, &'a V);
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
while let Some((first, rest)) = self.items.split_first() {
|
|
|
|
self.items = rest;
|
|
|
|
if first.is_used() {
|
|
|
|
return Some((&first.key, &first.data));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn mmap_as_ref<K, V>(mmap: &MemoryMap, len: usize) -> (&'static mut Header, &'static mut [Entry<K, V>]) {
|
|
|
|
if mmap.len() < mem::size_of::<Header>() + len * mem::size_of::<Entry<K, V>>() {
|
|
|
|
panic!("Memory map too small");
|
|
|
|
}
|
|
|
|
let header = unsafe { &mut *(mmap.data() as *mut Header) };
|
|
|
|
let ptr = unsafe { mmap.data().offset(mem::size_of::<Header>() as isize) as *mut Entry<K, V> };
|
|
|
|
let data = unsafe { slice::from_raw_parts_mut(ptr, len) };
|
|
|
|
(header, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Index<K: 'static, V: 'static> {
|
2017-03-10 11:43:32 +00:00
|
|
|
capacity: usize,
|
2017-04-18 11:52:43 +00:00
|
|
|
mask: usize,
|
2017-03-10 11:43:32 +00:00
|
|
|
entries: usize,
|
|
|
|
max_entries: usize,
|
|
|
|
min_entries: usize,
|
|
|
|
fd: File,
|
|
|
|
mmap: MemoryMap,
|
2017-04-18 11:52:43 +00:00
|
|
|
header: &'static mut Header,
|
|
|
|
data: &'static mut [Entry<K, V>]
|
2017-03-10 11:43:32 +00:00
|
|
|
}
|
|
|
|
|
2017-04-18 11:52:43 +00:00
|
|
|
impl<K: Key, V: Clone + Copy> Index<K, V> {
|
|
|
|
pub fn new(path: &Path, create: bool, magic: &[u8; 7], version: u8) -> Result<Self, IndexError> {
|
2017-03-16 08:42:30 +00:00
|
|
|
let fd = try!(OpenOptions::new().read(true).write(true).create(create).open(path));
|
2017-03-10 11:43:32 +00:00
|
|
|
if create {
|
2017-04-18 11:52:43 +00:00
|
|
|
try!(Self::resize_fd(&fd, INITIAL_SIZE));
|
2017-03-10 11:43:32 +00:00
|
|
|
}
|
2017-04-18 11:52:43 +00:00
|
|
|
let mmap = try!(Self::map_fd(&fd));
|
2017-03-10 11:43:32 +00:00
|
|
|
if mmap.len() < mem::size_of::<Header>() {
|
2017-03-22 08:19:16 +00:00
|
|
|
return Err(IndexError::WrongMagic);
|
2017-03-10 11:43:32 +00:00
|
|
|
}
|
2017-04-18 11:52:43 +00:00
|
|
|
let (header, _) = mmap_as_ref::<K, V>(&mmap, INITIAL_SIZE as usize);
|
|
|
|
if create {
|
|
|
|
header.magic = magic.to_owned();
|
|
|
|
header.version = version;
|
|
|
|
header.entries = 0;
|
|
|
|
header.capacity = INITIAL_SIZE as u64;
|
|
|
|
}
|
|
|
|
if &header.magic != magic {
|
|
|
|
return Err(IndexError::WrongMagic);
|
2017-03-10 11:43:32 +00:00
|
|
|
}
|
2017-04-18 11:52:43 +00:00
|
|
|
if header.version != version {
|
|
|
|
return Err(IndexError::UnsupportedVersion(header.version));
|
|
|
|
}
|
|
|
|
let (header, data) = mmap_as_ref(&mmap, header.capacity as usize);
|
|
|
|
let index = Index{
|
|
|
|
capacity: header.capacity as usize,
|
|
|
|
mask: header.capacity as usize -1,
|
|
|
|
max_entries: (header.capacity as f64 * MAX_USAGE) as usize,
|
|
|
|
min_entries: (header.capacity as f64 * MIN_USAGE) as usize,
|
|
|
|
entries: header.entries as usize,
|
|
|
|
fd: fd,
|
|
|
|
mmap: mmap,
|
|
|
|
data: data,
|
|
|
|
header: header
|
|
|
|
};
|
2017-03-16 08:42:30 +00:00
|
|
|
debug_assert!(index.check().is_ok(), "Inconsistent after creation");
|
2017-03-10 11:43:32 +00:00
|
|
|
Ok(index)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-04-18 11:52:43 +00:00
|
|
|
pub fn open<P: AsRef<Path>>(path: P, magic: &[u8; 7], version: u8) -> Result<Self, IndexError> {
|
|
|
|
Index::new(path.as_ref(), false, magic, version)
|
2017-03-10 11:43:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-04-18 11:52:43 +00:00
|
|
|
pub fn create<P: AsRef<Path>>(path: P, magic: &[u8; 7], version: u8) -> Result<Self, IndexError> {
|
|
|
|
Index::new(path.as_ref(), true, magic, version)
|
2017-03-10 11:43:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-03-16 08:42:30 +00:00
|
|
|
fn map_fd(fd: &File) -> Result<MemoryMap, IndexError> {
|
2017-03-10 11:43:32 +00:00
|
|
|
MemoryMap::new(
|
2017-03-16 08:42:30 +00:00
|
|
|
try!(fd.metadata().map_err(IndexError::Io)).len() as usize,
|
2017-03-10 11:43:32 +00:00
|
|
|
&[MapOption::MapReadable,
|
|
|
|
MapOption::MapWritable,
|
|
|
|
MapOption::MapFd(fd.as_raw_fd()),
|
|
|
|
MapOption::MapNonStandardFlags(0x0001) //libc::consts::os::posix88::MAP_SHARED
|
2017-03-16 08:42:30 +00:00
|
|
|
]).map_err(IndexError::Mmap)
|
2017-03-10 11:43:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-03-16 08:42:30 +00:00
|
|
|
fn resize_fd(fd: &File, capacity: usize) -> Result<(), IndexError> {
|
2017-04-18 11:52:43 +00:00
|
|
|
fd.set_len((mem::size_of::<Header>() + capacity * mem::size_of::<Entry<K, V>>()) as u64).map_err(IndexError::Io)
|
2017-03-10 11:43:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn set_capacity(&mut self, capacity: usize) {
|
|
|
|
self.capacity = capacity;
|
2017-04-18 11:52:43 +00:00
|
|
|
debug_assert_eq!(capacity.count_ones(), 1);
|
|
|
|
self.mask = capacity -1;
|
2017-03-10 11:43:32 +00:00
|
|
|
self.min_entries = (capacity as f64 * MIN_USAGE) as usize;
|
|
|
|
self.max_entries = (capacity as f64 * MAX_USAGE) as usize;
|
|
|
|
}
|
|
|
|
|
2017-03-16 08:42:30 +00:00
|
|
|
fn reinsert(&mut self, start: usize, end: usize) -> Result<(), IndexError> {
|
2017-03-10 11:43:32 +00:00
|
|
|
for pos in start..end {
|
|
|
|
let key;
|
|
|
|
let data;
|
|
|
|
{
|
|
|
|
let entry = &mut self.data[pos];
|
|
|
|
if !entry.is_used() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
key = entry.key;
|
|
|
|
data = entry.data;
|
|
|
|
entry.clear();
|
|
|
|
}
|
|
|
|
self.entries -= 1;
|
|
|
|
try!(self.set(&key, &data));
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-03-16 08:42:30 +00:00
|
|
|
fn shrink(&mut self) -> Result<bool, IndexError> {
|
2017-03-10 11:43:32 +00:00
|
|
|
if self.entries >= self.min_entries || self.capacity <= INITIAL_SIZE {
|
|
|
|
return Ok(false)
|
|
|
|
}
|
|
|
|
let old_capacity = self.capacity;
|
|
|
|
let new_capacity = self.capacity / 2;
|
|
|
|
self.set_capacity(new_capacity);
|
|
|
|
try!(self.reinsert(new_capacity, old_capacity));
|
2017-04-18 11:52:43 +00:00
|
|
|
try!(Self::resize_fd(&self.fd, new_capacity));
|
|
|
|
self.mmap = try!(Self::map_fd(&self.fd));
|
|
|
|
let (header, data) = mmap_as_ref(&self.mmap, new_capacity);
|
|
|
|
self.header = header;
|
|
|
|
self.data = data;
|
2017-03-10 11:43:32 +00:00
|
|
|
assert_eq!(self.data.len(), self.capacity);
|
|
|
|
Ok(true)
|
|
|
|
}
|
|
|
|
|
2017-03-16 08:42:30 +00:00
|
|
|
fn extend(&mut self) -> Result<bool, IndexError> {
|
2017-03-10 11:43:32 +00:00
|
|
|
if self.entries <= self.max_entries {
|
|
|
|
return Ok(false)
|
|
|
|
}
|
|
|
|
let new_capacity = 2 * self.capacity;
|
2017-04-18 11:52:43 +00:00
|
|
|
try!(Self::resize_fd(&self.fd, new_capacity));
|
|
|
|
self.mmap = try!(Self::map_fd(&self.fd));
|
|
|
|
let (header, data) = mmap_as_ref(&self.mmap, new_capacity);
|
|
|
|
self.header = header;
|
|
|
|
self.data = data;
|
2017-03-10 11:43:32 +00:00
|
|
|
self.set_capacity(new_capacity);
|
|
|
|
assert_eq!(self.data.len(), self.capacity);
|
|
|
|
try!(self.reinsert(0, new_capacity));
|
|
|
|
Ok(true)
|
|
|
|
}
|
|
|
|
|
2017-03-16 08:42:30 +00:00
|
|
|
pub fn check(&self) -> Result<(), IndexError> {
|
2017-03-10 11:43:32 +00:00
|
|
|
let mut entries = 0;
|
|
|
|
for pos in 0..self.capacity {
|
|
|
|
let entry = &self.data[pos];
|
|
|
|
if !entry.is_used() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
entries += 1;
|
|
|
|
match self.locate(&entry.key) {
|
|
|
|
LocateResult::Found(p) if p == pos => true,
|
2017-04-18 11:52:43 +00:00
|
|
|
found => return Err(IndexError::WrongPosition(pos, found))
|
2017-03-10 11:43:32 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
if entries != self.entries {
|
2017-03-16 08:42:30 +00:00
|
|
|
return Err(IndexError::WrongEntryCount(self.entries, entries));
|
2017-03-10 11:43:32 +00:00
|
|
|
}
|
2017-03-16 08:42:30 +00:00
|
|
|
Ok(())
|
2017-03-10 11:43:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-03-16 08:42:30 +00:00
|
|
|
fn increase_count(&mut self) -> Result<(), IndexError> {
|
2017-03-10 11:43:32 +00:00
|
|
|
self.entries += 1;
|
|
|
|
try!(self.extend());
|
|
|
|
self.write_header();
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-03-16 08:42:30 +00:00
|
|
|
fn decrease_count(&mut self) -> Result<(), IndexError> {
|
2017-03-10 11:43:32 +00:00
|
|
|
self.entries -= 1;
|
|
|
|
try!(self.shrink());
|
|
|
|
self.write_header();
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn write_header(&mut self) {
|
2017-04-18 11:52:43 +00:00
|
|
|
self.header.entries = self.entries as u64;
|
|
|
|
self.header.capacity = self.capacity as u64;
|
2017-03-10 11:43:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Finds the position for this key
|
|
|
|
/// If the key is in the table, it will be the position of the key,
|
|
|
|
/// otherwise it will be the position where this key should be inserted
|
2017-04-18 11:52:43 +00:00
|
|
|
fn locate(&self, key: &K) -> LocateResult {
|
|
|
|
let mut pos = key.hash() as usize & self.mask;
|
2017-03-10 11:43:32 +00:00
|
|
|
let mut dist = 0;
|
|
|
|
loop {
|
|
|
|
let entry = &self.data[pos];
|
|
|
|
if !entry.is_used() {
|
|
|
|
return LocateResult::Hole(pos);
|
|
|
|
}
|
|
|
|
if entry.key == *key {
|
|
|
|
return LocateResult::Found(pos);
|
|
|
|
}
|
2017-04-18 11:52:43 +00:00
|
|
|
let odist = (pos + self.capacity - entry.key.hash() as usize & self.mask) & self.mask;
|
2017-03-10 11:43:32 +00:00
|
|
|
if dist > odist {
|
|
|
|
return LocateResult::Steal(pos);
|
|
|
|
}
|
2017-04-18 11:52:43 +00:00
|
|
|
pos = (pos + 1) & self.mask;
|
2017-03-10 11:43:32 +00:00
|
|
|
dist += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Shifts all following entries towards the left if they can get closer to their ideal position.
|
|
|
|
/// The entry at the given position will be lost.
|
|
|
|
fn backshift(&mut self, start: usize) {
|
|
|
|
let mut pos = start;
|
|
|
|
let mut last_pos;
|
|
|
|
loop {
|
|
|
|
last_pos = pos;
|
2017-04-18 11:52:43 +00:00
|
|
|
pos = (pos + 1) & self.mask;
|
2017-03-10 11:43:32 +00:00
|
|
|
{
|
|
|
|
let entry = &self.data[pos];
|
|
|
|
if !entry.is_used() {
|
|
|
|
// we found a hole, stop shifting here
|
|
|
|
break;
|
|
|
|
}
|
2017-04-18 11:52:43 +00:00
|
|
|
if entry.key.hash() as usize & self.mask == pos {
|
2017-03-10 11:43:32 +00:00
|
|
|
// we found an entry at the right position, stop shifting here
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.data[last_pos] = self.data[pos].clone();
|
|
|
|
}
|
|
|
|
self.data[last_pos].clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds the key, data pair into the table.
|
2017-04-16 19:06:59 +00:00
|
|
|
/// If the key existed the old data is returned.
|
2017-04-18 11:52:43 +00:00
|
|
|
pub fn set(&mut self, key: &K, data: &V) -> Result<Option<V>, IndexError> {
|
2017-03-10 11:43:32 +00:00
|
|
|
match self.locate(key) {
|
|
|
|
LocateResult::Found(pos) => {
|
2017-04-16 19:06:59 +00:00
|
|
|
let mut old = *data;
|
|
|
|
mem::swap(&mut old, &mut self.data[pos].data);
|
|
|
|
Ok(Some(old))
|
2017-03-10 11:43:32 +00:00
|
|
|
},
|
|
|
|
LocateResult::Hole(pos) => {
|
|
|
|
{
|
|
|
|
let entry = &mut self.data[pos];
|
|
|
|
entry.key = *key;
|
|
|
|
entry.data = *data;
|
|
|
|
}
|
|
|
|
try!(self.increase_count());
|
2017-04-16 19:06:59 +00:00
|
|
|
Ok(None)
|
2017-03-10 11:43:32 +00:00
|
|
|
},
|
|
|
|
LocateResult::Steal(pos) => {
|
|
|
|
let mut stolen_key;
|
|
|
|
let mut stolen_data;
|
|
|
|
let mut cur_pos = pos;
|
|
|
|
{
|
|
|
|
let entry = &mut self.data[pos];
|
|
|
|
stolen_key = entry.key;
|
|
|
|
stolen_data = entry.data;
|
|
|
|
entry.key = *key;
|
|
|
|
entry.data = *data;
|
|
|
|
}
|
|
|
|
loop {
|
2017-04-18 11:52:43 +00:00
|
|
|
cur_pos = (cur_pos + 1) & self.mask;
|
2017-03-10 11:43:32 +00:00
|
|
|
let entry = &mut self.data[cur_pos];
|
|
|
|
if entry.is_used() {
|
|
|
|
mem::swap(&mut stolen_key, &mut entry.key);
|
|
|
|
mem::swap(&mut stolen_data, &mut entry.data);
|
|
|
|
} else {
|
|
|
|
entry.key = stolen_key;
|
|
|
|
entry.data = stolen_data;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try!(self.increase_count());
|
2017-04-16 19:06:59 +00:00
|
|
|
Ok(None)
|
2017-03-10 11:43:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-04-18 11:52:43 +00:00
|
|
|
pub fn contains(&self, key: &K) -> bool {
|
2017-03-16 08:42:30 +00:00
|
|
|
debug_assert!(self.check().is_ok(), "Inconsistent before get");
|
2017-03-10 11:43:32 +00:00
|
|
|
match self.locate(key) {
|
|
|
|
LocateResult::Found(_) => true,
|
|
|
|
_ => false
|
|
|
|
}
|
2017-03-20 13:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-04-18 11:52:43 +00:00
|
|
|
pub fn pos(&self, key: &K) -> Option<usize> {
|
2017-03-20 13:03:29 +00:00
|
|
|
debug_assert!(self.check().is_ok(), "Inconsistent before get");
|
|
|
|
match self.locate(key) {
|
|
|
|
LocateResult::Found(pos) => Some(pos),
|
|
|
|
_ => None
|
|
|
|
}
|
2017-03-10 11:43:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-04-18 11:52:43 +00:00
|
|
|
pub fn get(&self, key: &K) -> Option<V> {
|
2017-03-16 08:42:30 +00:00
|
|
|
debug_assert!(self.check().is_ok(), "Inconsistent before get");
|
2017-03-10 11:43:32 +00:00
|
|
|
match self.locate(key) {
|
|
|
|
LocateResult::Found(pos) => Some(self.data[pos].data),
|
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-04-18 11:52:43 +00:00
|
|
|
pub fn modify<F>(&mut self, key: &K, mut f: F) -> bool where F: FnMut(&mut V) {
|
2017-03-16 08:42:30 +00:00
|
|
|
debug_assert!(self.check().is_ok(), "Inconsistent before get");
|
2017-03-10 11:43:32 +00:00
|
|
|
match self.locate(key) {
|
|
|
|
LocateResult::Found(pos) => {
|
|
|
|
f(&mut self.data[pos].data);
|
|
|
|
true
|
|
|
|
},
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-04-18 11:52:43 +00:00
|
|
|
pub fn delete(&mut self, key: &K) -> Result<bool, IndexError> {
|
2017-03-10 11:43:32 +00:00
|
|
|
match self.locate(key) {
|
|
|
|
LocateResult::Found(pos) => {
|
|
|
|
self.backshift(pos);
|
|
|
|
try!(self.decrease_count());
|
|
|
|
Ok(true)
|
|
|
|
},
|
|
|
|
_ => Ok(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-18 11:52:43 +00:00
|
|
|
pub fn filter<F>(&mut self, mut f: F) -> Result<usize, IndexError> where F: FnMut(&K, &V) -> bool {
|
2017-03-10 11:43:32 +00:00
|
|
|
//TODO: is it faster to walk in reverse direction?
|
|
|
|
let mut deleted = 0;
|
|
|
|
let mut pos = 0;
|
|
|
|
while pos < self.capacity {
|
|
|
|
{
|
|
|
|
let entry = &mut self.data[pos];
|
|
|
|
if !entry.is_used() || f(&entry.key, &entry.data) {
|
|
|
|
pos += 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.backshift(pos);
|
|
|
|
deleted += 1;
|
|
|
|
}
|
|
|
|
self.entries -= deleted;
|
|
|
|
while try!(self.shrink()) {}
|
|
|
|
self.write_header();
|
|
|
|
Ok(deleted)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-04-18 11:52:43 +00:00
|
|
|
pub fn iter<'a>(&'a self) -> Iter<'a, K, V> {
|
|
|
|
Iter{items: &self.data}
|
2017-03-10 11:43:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.entries
|
|
|
|
}
|
|
|
|
|
2017-03-15 20:53:05 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn size(&self) -> usize {
|
2017-03-16 08:42:30 +00:00
|
|
|
self.mmap.len()
|
2017-03-15 20:53:05 +00:00
|
|
|
}
|
|
|
|
|
2017-03-10 11:43:32 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.entries == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn capacity(&self) -> usize {
|
|
|
|
self.capacity
|
|
|
|
}
|
2017-04-07 16:57:49 +00:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn clear(&mut self) {
|
|
|
|
for entry in &mut self.data[..] {
|
|
|
|
entry.clear();
|
|
|
|
}
|
|
|
|
self.entries = 0;
|
|
|
|
}
|
2017-03-10 11:43:32 +00:00
|
|
|
}
|