use std::sync::Arc;
use crate::{DbResult, FixedConfig, FixedMap};
pub struct SeqGen {
map: FixedMap<[u8; 8], 8>,
}
impl SeqGen {
pub(crate) fn open(path: impl AsRef<std::path::Path>) -> DbResult<Arc<Self>> {
Ok(Arc::new(Self {
map: FixedMap::<[u8; 8], 8>::open(
path,
FixedConfig {
shard_count: 4,
..Default::default()
},
)?,
}))
}
pub fn next_id(&self, name: &str) -> DbResult<u64> {
let key = xxhash_rust::xxh3::xxh3_64(name.as_bytes()).to_le_bytes();
let val = self.map.get(&key).unwrap_or([0u8; 8]);
let id = u64::from_le_bytes(val);
let next = id + 1;
self.map.put(&key, &next.to_le_bytes())?;
Ok(next)
}
pub fn flush(&self) -> DbResult<()> {
self.map.flush()
}
}