use crate::DbResult;
use crate::Key;
#[derive(Default)]
pub struct SyncNeeds(pub Vec<usize>);
impl SyncNeeds {
pub fn none() -> Self {
SyncNeeds(Vec::new())
}
pub fn push(&mut self, shard_id: usize) {
self.0.push(shard_id);
}
pub fn shards(&self) -> &[usize] {
&self.0
}
}
pub trait MultiTx {
type Key: Key;
type Tx<'a>
where
Self: 'a;
fn collection_id(&self) -> usize {
self as *const Self as *const () as usize
}
fn shard_for_key(&self, key: &Self::Key) -> usize;
fn begin_tx(&self) -> Self::Tx<'_>;
fn lock_shard_into<'a>(&'a self, shard_id: usize, tx: &mut Self::Tx<'a>);
fn release_locks(&self, tx: &mut Self::Tx<'_>) -> SyncNeeds;
fn run_sync(&self, needs: SyncNeeds) -> DbResult<()>;
fn replay_hooks(&self, tx: Self::Tx<'_>);
}
pub(crate) fn unique_sorted_shards<K>(keys: &[K], shard_for: impl Fn(&K) -> usize) -> Vec<usize> {
let mut v: Vec<usize> = keys.iter().map(&shard_for).collect();
v.sort_unstable();
v.dedup();
v
}