use std::path::{Path, PathBuf};
use std::{fs, io};
use rayon::prelude::*;
use redb::{Database, Durability, ReadableDatabase as _, ReadableTable as _, TableDefinition};
use crate::SyncError;
use crate::varint::{read_varint, varint};
const WAYS: TableDefinition<u64, &[u8]> = TableDefinition::new("ways");
const META: TableDefinition<&str, i64> = TableDefinition::new("meta");
const BLOCK: u64 = 256;
const CHUNK: usize = 1 << 22;
pub(crate) type Pair = (u64, u64);
pub(crate) struct ReverseIndex {
db: Database,
path: PathBuf,
}
impl ReverseIndex {
pub(crate) fn create(
path: &Path,
id: i64,
rev: i64,
pairs: impl Iterator<Item = Result<Pair, SyncError>>,
) -> Result<(Self, u64), SyncError> {
remove(path)?;
let at = |e: redb::Error| SyncError::index(path, e);
let db = Database::create(path).map_err(|e| at(e.into()))?;
let mut chunk: Vec<Pair> = Vec::with_capacity(CHUNK);
let mut count = 0;
for pair in pairs {
let pair = pair?;
if chunk.len() >= CHUNK && chunk.last().is_some_and(|l| l.0 / BLOCK != pair.0 / BLOCK) {
write_blocks(&db, &chunk, None).map_err(at)?;
chunk.clear();
}
chunk.push(pair);
count += 1;
}
write_blocks(&db, &chunk, Some((id, rev))).map_err(at)?;
let index = Self {
db,
path: path.to_path_buf(),
};
Ok((index, count))
}
pub(crate) fn open(path: &Path) -> Result<Self, SyncError> {
if !path.exists() {
return Err(SyncError::Missing(path.to_path_buf()));
}
let db = Database::open(path).map_err(|e| SyncError::index(path, e.into()))?;
Ok(Self {
db,
path: path.to_path_buf(),
})
}
pub(crate) fn stamp(&self) -> Result<(i64, i64), SyncError> {
(|| -> Result<_, redb::Error> {
let txn = self.db.begin_read()?;
let meta = txn.open_table(META)?;
let get = |k| meta.get(k).map(|v| v.map_or(0, |v| v.value()));
Ok((get("id")?, get("rev")?))
})()
.map_err(|e| SyncError::index(&self.path, e))
}
pub(crate) fn ways_of(
&self,
nodes: impl IntoIterator<Item = u64>,
) -> Result<Vec<Pair>, SyncError> {
let mut nodes: Vec<u64> = nodes.into_iter().collect();
nodes.sort_unstable();
nodes.dedup();
(|| -> Result<_, redb::Error> {
let txn = self.db.begin_read()?;
let ways = txn.open_table(WAYS)?;
let mut out = Vec::new();
for nodes in nodes.chunk_by(|a, b| a / BLOCK == b / BLOCK) {
if let Some(value) = ways.get(nodes[0] / BLOCK)? {
let pairs = decode(nodes[0] / BLOCK, value.value());
out.extend(
pairs
.into_iter()
.filter(|p| nodes.binary_search(&p.0).is_ok()),
);
}
}
Ok(out)
})()
.map_err(|e| SyncError::index(&self.path, e))
}
pub(crate) fn apply(&self, remove: &[Pair], add: &[Pair], rev: i64) -> Result<(), SyncError> {
let mut blocks: Vec<u64> = remove.iter().chain(add).map(|p| p.0 / BLOCK).collect();
blocks.sort_unstable();
blocks.dedup();
let in_block = |pairs: &[Pair], block: u64| {
let start = pairs.partition_point(|p| p.0 < block * BLOCK);
let end = pairs.partition_point(|p| p.0 < (block + 1) * BLOCK);
start..end
};
(|| -> Result<(), redb::Error> {
let txn = self.db.begin_write()?;
{
let mut ways = txn.open_table(WAYS)?;
for block in blocks {
let mut pairs = ways
.get(block)?
.map(|v| decode(block, v.value()))
.unwrap_or_default();
let gone = &remove[in_block(remove, block)];
pairs.retain(|p| gone.binary_search(p).is_err());
pairs.extend_from_slice(&add[in_block(add, block)]);
pairs.sort_unstable();
pairs.dedup();
if pairs.is_empty() {
ways.remove(block)?;
} else {
ways.insert(block, encode(&pairs).as_slice())?;
}
}
txn.open_table(META)?.insert("rev", rev)?;
}
txn.commit()?;
Ok(())
})()
.map_err(|e| SyncError::index(&self.path, e))
}
}
fn write_blocks(
db: &Database,
pairs: &[Pair],
stamp: Option<(i64, i64)>,
) -> Result<(), redb::Error> {
let blocks: Vec<(u64, Vec<u8>)> = pairs
.chunk_by(|a, b| a.0 / BLOCK == b.0 / BLOCK)
.collect::<Vec<_>>()
.into_par_iter()
.map(|pairs| (pairs[0].0 / BLOCK, encode(pairs)))
.collect();
let mut txn = db.begin_write()?;
if stamp.is_none() {
txn.set_durability(Durability::None)?;
}
{
let mut ways = txn.open_table(WAYS)?;
for (block, value) in &blocks {
ways.insert(block, value.as_slice())?;
}
if let Some((id, rev)) = stamp {
let mut meta = txn.open_table(META)?;
meta.insert("id", id)?;
meta.insert("rev", rev)?;
}
}
txn.commit()?;
Ok(())
}
pub(crate) fn remove(path: &Path) -> Result<(), SyncError> {
match fs::remove_file(path) {
Err(e) if e.kind() != io::ErrorKind::NotFound => Err(SyncError::Io(e)),
_ => Ok(()),
}
}
fn encode(pairs: &[Pair]) -> Vec<u8> {
let mut out = Vec::with_capacity(pairs.len() * 4);
let mut node = pairs[0].0 / BLOCK * BLOCK;
for ways in pairs.chunk_by(|a, b| a.0 == b.0) {
varint(&mut out, ways[0].0 - node);
node = ways[0].0;
varint(&mut out, ways.len() as u64);
let mut way = 0;
for &(_, w) in ways {
varint(&mut out, w - way);
way = w;
}
}
out
}
fn decode(block: u64, mut b: &[u8]) -> Vec<Pair> {
let mut out = Vec::new();
let mut node = block * BLOCK;
while !b.is_empty() {
node += read_varint(&mut b);
let mut way = 0;
for _ in 0..read_varint(&mut b) {
way += read_varint(&mut b);
out.push((node, way));
}
}
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn blocks_round_trip() {
let pairs = [(512, 3), (512, 1_000_000_000), (513, 3), (767, 0)];
assert_eq!(decode(2, &encode(&pairs)), pairs);
}
#[test]
fn index_finds_adds_and_removes() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("ways.redb");
let pairs = [(1, 10), (1, 11), (300, 10)];
let (index, count) = ReverseIndex::create(&path, 7, 1, pairs.into_iter().map(Ok)).unwrap();
assert_eq!(count, 3);
assert_eq!(index.stamp().unwrap(), (7, 1));
assert_eq!(index.ways_of([2, 1, 2]).unwrap(), [(1, 10), (1, 11)]);
index.apply(&[(1, 10), (300, 10)], &[(2, 12)], 2).unwrap();
drop(index);
let index = ReverseIndex::open(&path).unwrap();
assert_eq!(index.stamp().unwrap(), (7, 2));
assert_eq!(index.ways_of([1, 2, 300]).unwrap(), [(1, 11), (2, 12)]);
}
}