2021-04-10 22:13:12 +05:30
|
|
|
use fixedbitset::FixedBitSet;
|
|
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
|
|
|
|
use crate::io_engine::IoEngine;
|
2021-04-16 15:22:54 +05:30
|
|
|
use crate::pdata::array::{self, ArrayBlock};
|
2021-04-10 22:13:12 +05:30
|
|
|
use crate::pdata::array_walker::{ArrayVisitor, ArrayWalker};
|
2021-04-21 21:06:10 +05:30
|
|
|
use crate::pdata::space_map::*;
|
2021-04-10 22:13:12 +05:30
|
|
|
|
2021-04-20 13:37:25 +05:30
|
|
|
pub struct CheckedBitSet {
|
|
|
|
bits: FixedBitSet,
|
2021-04-10 22:13:12 +05:30
|
|
|
}
|
|
|
|
|
2021-04-20 13:37:25 +05:30
|
|
|
impl CheckedBitSet {
|
|
|
|
pub fn with_capacity(bits: usize) -> CheckedBitSet {
|
|
|
|
CheckedBitSet {
|
|
|
|
bits: FixedBitSet::with_capacity(bits << 1),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set(&mut self, bit: usize, enabled: bool) {
|
|
|
|
self.bits.set(bit << 1, true);
|
|
|
|
self.bits.set((bit << 1) + 1, enabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn contains(&self, bit: usize) -> Option<bool> {
|
|
|
|
if !self.bits.contains(bit << 1) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
Some(self.bits.contains((bit << 1) + 1))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct BitsetVisitor {
|
2021-05-12 13:20:14 +05:30
|
|
|
nr_bits: usize,
|
2021-04-20 13:37:25 +05:30
|
|
|
bits: Mutex<CheckedBitSet>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BitsetVisitor {
|
2021-05-12 13:20:14 +05:30
|
|
|
pub fn new(nr_bits: usize) -> Self {
|
2021-04-10 22:13:12 +05:30
|
|
|
BitsetVisitor {
|
2021-05-12 13:20:14 +05:30
|
|
|
nr_bits,
|
|
|
|
bits: Mutex::new(CheckedBitSet::with_capacity(nr_bits)),
|
2021-04-10 22:13:12 +05:30
|
|
|
}
|
|
|
|
}
|
2021-04-20 13:37:25 +05:30
|
|
|
|
|
|
|
pub fn get_bitset(self) -> CheckedBitSet {
|
|
|
|
self.bits.into_inner().unwrap()
|
|
|
|
}
|
2021-04-10 22:13:12 +05:30
|
|
|
}
|
|
|
|
|
2021-04-20 13:37:25 +05:30
|
|
|
impl ArrayVisitor<u64> for BitsetVisitor {
|
2021-04-16 15:22:54 +05:30
|
|
|
fn visit(&self, index: u64, b: ArrayBlock<u64>) -> array::Result<()> {
|
2021-05-11 18:11:30 +05:30
|
|
|
let mut begin = (index as usize * (b.header.max_entries as usize)) << 6;
|
2021-05-12 13:20:14 +05:30
|
|
|
if begin >= self.nr_bits as usize {
|
|
|
|
return Err(array::value_err(format!(
|
|
|
|
"bitset size exceeds limit: {} bits",
|
|
|
|
self.nr_bits
|
|
|
|
)));
|
|
|
|
}
|
2021-04-16 15:22:54 +05:30
|
|
|
|
2021-05-12 16:09:59 +05:30
|
|
|
for bits in b.values.iter() {
|
2021-05-12 13:20:14 +05:30
|
|
|
let end: usize = std::cmp::min(begin + 64, self.nr_bits as usize);
|
2021-04-16 15:22:54 +05:30
|
|
|
let mut mask = 1;
|
2021-04-10 22:13:12 +05:30
|
|
|
|
2021-04-16 15:22:54 +05:30
|
|
|
for bi in begin..end {
|
|
|
|
self.bits.lock().unwrap().set(bi, bits & mask != 0);
|
|
|
|
mask <<= 1;
|
|
|
|
}
|
|
|
|
begin += 64;
|
2021-04-10 22:13:12 +05:30
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: multi-threaded is possible
|
|
|
|
pub fn read_bitset(
|
|
|
|
engine: Arc<dyn IoEngine + Send + Sync>,
|
|
|
|
root: u64,
|
2021-05-12 13:20:14 +05:30
|
|
|
nr_bits: usize,
|
2021-04-10 22:13:12 +05:30
|
|
|
ignore_none_fatal: bool,
|
2021-05-11 18:11:30 +05:30
|
|
|
) -> (CheckedBitSet, Option<array::ArrayError>) {
|
2021-04-21 21:06:10 +05:30
|
|
|
let w = ArrayWalker::new(engine, ignore_none_fatal);
|
2021-05-12 13:20:14 +05:30
|
|
|
let mut v = BitsetVisitor::new(nr_bits);
|
2021-04-20 13:37:25 +05:30
|
|
|
let err = w.walk(&mut v, root);
|
|
|
|
let e = match err {
|
|
|
|
Ok(()) => None,
|
|
|
|
Err(e) => Some(e),
|
|
|
|
};
|
2021-05-11 18:11:30 +05:30
|
|
|
(v.get_bitset(), e)
|
2021-04-10 22:13:12 +05:30
|
|
|
}
|
2021-04-21 21:06:10 +05:30
|
|
|
|
|
|
|
// TODO: multi-threaded is possible
|
|
|
|
pub fn read_bitset_with_sm(
|
|
|
|
engine: Arc<dyn IoEngine + Send + Sync>,
|
|
|
|
root: u64,
|
2021-05-12 13:20:14 +05:30
|
|
|
nr_bits: usize,
|
2021-04-21 21:06:10 +05:30
|
|
|
sm: Arc<Mutex<dyn SpaceMap + Send + Sync>>,
|
|
|
|
ignore_none_fatal: bool,
|
2021-05-11 18:11:30 +05:30
|
|
|
) -> array::Result<(CheckedBitSet, Option<array::ArrayError>)> {
|
2021-04-21 21:06:10 +05:30
|
|
|
let w = ArrayWalker::new_with_sm(engine, sm, ignore_none_fatal)?;
|
2021-05-12 13:20:14 +05:30
|
|
|
let mut v = BitsetVisitor::new(nr_bits);
|
2021-04-21 21:06:10 +05:30
|
|
|
let err = w.walk(&mut v, root);
|
|
|
|
let e = match err {
|
|
|
|
Ok(()) => None,
|
|
|
|
Err(e) => Some(e),
|
|
|
|
};
|
2021-05-11 18:11:30 +05:30
|
|
|
Ok((v.get_bitset(), e))
|
2021-04-21 21:06:10 +05:30
|
|
|
}
|