use crate::hook::{NoHook, TypedWriteHook};
use std::hash::Hash;
use std::marker::PhantomData;
use std::ops::ControlFlow;
use std::path::Path;
use std::sync::{Arc, OnceLock, Weak};
use std::time::Duration;
use super::config::SegConfig;
use super::core::{OpenOptions, SegCore, SegMeta};
use super::verify::{SegmentInfo, VerifyReport, VerifyScope};
use super::worker::SealStatus;
use crate::codec::Codec;
use crate::error::{DbError, DbResult};
use crate::key::Key;
pub struct SegMap<K: Key + Hash + Eq + Send + Sync + 'static, T, C, H = NoHook> {
core: Arc<SegCore<K>>,
pub(crate) codec: C,
pub(crate) hook: H,
pub(crate) tx_self: OnceLock<Weak<Self>>,
_value: PhantomData<fn() -> T>,
}
impl<K, T, C> SegMap<K, T, C>
where
K: Key + Hash + Eq + Send + Sync + 'static,
C: Codec<T>,
{
pub fn open(
path: impl AsRef<Path>,
meta: SegMeta,
config: SegConfig,
codec: C,
) -> DbResult<Self> {
Self::open_with(
path.as_ref(),
meta,
config,
codec,
OpenOptions::production(),
)
}
pub(crate) fn open_with(
path: &Path,
meta: SegMeta,
config: SegConfig,
codec: C,
options: OpenOptions,
) -> DbResult<Self> {
Ok(Self {
core: SegCore::open(path, meta, config, options)?,
codec,
hook: NoHook,
tx_self: OnceLock::new(),
_value: PhantomData,
})
}
}
impl<K, T, C, H> SegMap<K, T, C, H>
where
K: Key + Hash + Eq + Send + Sync + 'static,
C: Codec<T>,
H: TypedWriteHook<K, T>,
{
pub(crate) fn open_deferred(
path: &Path,
meta: SegMeta,
config: SegConfig,
codec: C,
hook: H,
) -> DbResult<Self> {
Ok(Self {
core: SegCore::open_deferred(path, meta, config, OpenOptions::production())?,
codec,
hook,
tx_self: OnceLock::new(),
_value: PhantomData,
})
}
pub(crate) fn core(&self) -> &Arc<SegCore<K>> {
&self.core
}
pub fn name(&self) -> &'static str {
self.core.meta.name
}
pub(crate) fn is_index_target_participant(&self) -> bool {
false
}
pub fn stats(&self) -> super::verify::SegStats {
self.core.stats()
}
pub fn config(&self) -> &SegConfig {
&self.core.config
}
pub fn put(&self, key: &K, value: &T) -> DbResult<()> {
self.facade_guard()?;
self.core.put(key, &self.codec.encode(value)?)
}
pub fn insert(&self, key: &K, value: &T) -> DbResult<()> {
self.facade_guard()?;
self.core.insert(key, &self.codec.encode(value)?)
}
pub fn insert_hinted(&self, key: &K, value: &T, written_since_ms: u64) -> DbResult<()> {
self.facade_guard()?;
self.core
.insert_since(key, &self.codec.encode(value)?, Some(written_since_ms))
}
pub fn delete(&self, key: &K) -> DbResult<()> {
self.facade_guard()?;
self.core.delete(key)
}
fn decode(&self, bytes: Option<Vec<u8>>) -> DbResult<Option<T>> {
bytes.map(|b| self.codec.decode_from(&b)).transpose()
}
pub fn try_get(&self, key: &K) -> DbResult<Option<T>> {
self.decode(self.core.get(key)?)
}
pub fn get(&self, key: &K) -> Option<T> {
metrics::counter!("armdb.ops", "op" => "get", "tree" => "seg_map").increment(1);
match self.try_get(key) {
Ok(value) => value,
Err(e) => {
tracing::error!(collection = self.name(), error = %e, "SegMap get error");
None
}
}
}
pub fn get_or_err(&self, key: &K) -> DbResult<T> {
self.try_get(key)?.ok_or(DbError::KeyNotFound)
}
pub fn try_contains(&self, key: &K) -> DbResult<bool> {
self.core.contains(key)
}
pub fn contains(&self, key: &K) -> bool {
match self.try_contains(key) {
Ok(found) => found,
Err(e) => {
tracing::error!(collection = self.name(), error = %e, "SegMap contains error");
false
}
}
}
pub fn try_get_hinted(&self, key: &K, written_since_ms: u64) -> DbResult<Option<T>> {
self.decode(self.core.get_hinted(key, written_since_ms)?)
}
pub fn len(&self) -> usize {
self.core.len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn seal(&self) -> DbResult<()> {
self.core.seal()
}
pub fn wait_sealed(&self, timeout: Duration) -> DbResult<bool> {
self.core.wait_sealed(timeout)
}
pub fn seal_status(&self) -> SealStatus {
self.core.seal_status()
}
pub fn segments(&self) -> Vec<SegmentInfo> {
self.core.segments()
}
pub fn drop_segments_before(&self, ts_ms: u64) -> DbResult<usize> {
if self.hook.projection_targets().is_some() || self.hook.counter_targets().is_some() {
return Err(DbError::Config(
"segmented projection sources cannot drop segments",
));
}
self.core.drop_segments_before(ts_ms)
}
pub fn verify(&self, scope: VerifyScope) -> DbResult<VerifyReport> {
self.core.verify(scope)
}
pub fn flush(&self) -> DbResult<()> {
self.core.flush()
}
pub fn durable_flush(&self) -> DbResult<()> {
self.core.durable_flush()
}
pub fn dirty_shards(&self) -> usize {
self.core.dirty_shards()
}
pub fn clean_shutdown(&self) -> DbResult<()> {
self.core.clean_shutdown()
}
pub(super) fn check_own_migration_action(&self) -> DbResult<()> {
Ok(())
}
pub(crate) fn walk_rows(
&self,
decode: bool,
visit: &mut dyn FnMut(DbResult<(K, T)>) -> ControlFlow<()>,
) {
self.core.scan(&mut |row| match row {
Err(e) => {
let _ = visit(Err(e));
ControlFlow::Break(())
}
Ok((key, bytes)) => match self.codec.decode_from(&bytes) {
Ok(value) if decode => visit(Ok((key, value))),
Ok(_) => ControlFlow::Continue(()),
Err(e) => {
tracing::warn!(collection = self.name(), error = %e, "a segmented row does not decode");
visit(Err(e))
}
},
});
}
}
impl<K: Key + Hash + Eq + Send + Sync + 'static, T, C, H> Drop for SegMap<K, T, C, H> {
fn drop(&mut self) {
self.core.stop_worker();
}
}
impl<K, T, C, H> crate::armour::collection::Collection for SegMap<K, T, C, H>
where
K: Key + Hash + Eq + Send + Sync + 'static,
T: Send + Sync,
C: Codec<T>,
H: TypedWriteHook<K, T>,
{
fn reconcile_counters(&self) -> DbResult<u64> {
SegMap::reconcile_counters(self)
}
fn counter_target_names(&self) -> Vec<&'static str> {
SegMap::counter_target_names(self)
}
fn name(&self) -> &str {
self.core.meta.name
}
fn len(&self) -> usize {
self.core.len()
}
fn compact(&self) -> DbResult<usize> {
Ok(0)
}
fn flush(&self) -> DbResult<()> {
self.core.flush()
}
fn periodic_flush(&self) -> DbResult<()> {
self.core.durable_flush()
}
fn durable_flush(&self) -> DbResult<()> {
self.core.durable_flush()
}
fn clean_shutdown(&self) -> DbResult<()> {
self.core.clean_shutdown()
}
fn dirty_shards(&self) -> Option<usize> {
Some(self.core.dirty_shards())
}
}
#[cfg(test)]
#[cfg(feature = "segmented")]
mod tests {
use super::*;
use crate::error::DbError;
use crate::seg::config::SegConfig;
use crate::seg::testkit::{TestKey, TextCodec, key_of, meta};
use crate::MigrateAction;
use crate::armour::collection::Collection;
#[test]
fn the_barriers_reach_every_generation() {
let dir = tempfile::tempdir().unwrap();
let map = SegMap::<TestKey, String, TextCodec>::open_with(
dir.path(),
meta(),
config(),
TextCodec,
crate::seg::core::OpenOptions {
worker: false,
..crate::seg::core::OpenOptions::production()
},
)
.unwrap();
for i in 0..50u64 {
map.put(&key_of(i), &format!("v{i}")).unwrap();
}
map.seal().unwrap();
for i in 50..100u64 {
map.put(&key_of(i), &format!("v{i}")).unwrap();
}
assert!(map.dirty_shards() > 0);
assert_eq!(Collection::dirty_shards(&map), Some(map.dirty_shards()));
Collection::flush(&map).unwrap();
Collection::durable_flush(&map).unwrap();
assert_eq!(map.dirty_shards(), 0);
map.put(&key_of(200), &"late".to_owned()).unwrap();
Collection::periodic_flush(&map).unwrap();
assert_eq!(map.dirty_shards(), 0);
assert_eq!(Collection::name(&map), "test_seg");
assert_eq!(Collection::len(&map), 101);
assert_eq!(Collection::compact(&map).unwrap(), 0);
Collection::clean_shutdown(&map).unwrap();
}
#[test]
fn a_migration_rewrites_only_what_it_changes() {
let dir = tempfile::tempdir().unwrap();
let map = open(dir.path());
for i in 0..30u64 {
map.put(&key_of(i), &format!("v{i}")).unwrap();
}
map.seal().unwrap();
assert!(map.wait_sealed(Duration::from_secs(20)).unwrap());
for i in 30..40u64 {
map.put(&key_of(i), &format!("v{i}")).unwrap();
}
let sealed_before = map.segments()[0].clone();
let changed = map
.migrate_rows(|_key, value| {
let n: u64 = value[1..].parse().unwrap();
match n % 3 {
0 => MigrateAction::Update(format!("migrated {n}")),
1 => MigrateAction::Delete,
_ => MigrateAction::Keep,
}
})
.unwrap();
assert_eq!(changed, 14 + 13);
for n in 0..40u64 {
let expected = match n % 3 {
0 => Some(format!("migrated {n}")),
1 => None,
_ => Some(format!("v{n}")),
};
assert_eq!(map.get(&key_of(n)), expected, "key {n}");
}
assert_eq!(map.segments()[0], sealed_before);
}
#[test]
fn a_row_that_does_not_decode_stops_the_migration() {
let dir = tempfile::tempdir().unwrap();
let map = open(dir.path());
map.put(&key_of(1), &"fine".to_owned()).unwrap();
map.core().put(&key_of(2), &[0xff]).unwrap();
assert!(matches!(
map.migrate_rows(|_, _| MigrateAction::Keep),
Err(DbError::CorruptedEntry { .. })
));
}
fn tight_config() -> SegConfig {
SegConfig {
seal_max_keys: 50,
max_frozen_generations: 1,
seal_wait_timeout: Duration::from_millis(1),
..SegConfig::test()
}
}
fn populated_tight(dir: &Path, rows: u64, options: OpenOptions) -> Map {
{
let map = open(dir);
for n in 0..rows {
map.put(&key_of(n), &format!("v{n}")).unwrap();
}
map.clean_shutdown().unwrap();
}
Map::open_with(dir, meta(), tight_config(), TextCodec, options).unwrap()
}
fn rewrite_all(_key: &TestKey, value: &String) -> MigrateAction<String> {
assert!(value.starts_with('v'), "a migrated row came back: {value}");
MigrateAction::Update(format!("m{}", &value[1..]))
}
#[test]
fn a_migration_waits_for_the_seals_it_needs_beyond_the_wait_timeout() {
let dir = tempfile::tempdir().unwrap();
let rows = 1_000u64;
let map = populated_tight(dir.path(), rows, OpenOptions::production());
let config = map.config();
assert!(rows > config.seal_max_keys * (config.max_frozen_generations as u64 + 1) * 4);
assert_eq!(map.migrate_rows(rewrite_all).unwrap(), rows as usize);
for n in 0..rows {
assert_eq!(map.get(&key_of(n)), Some(format!("m{n}")), "key {n}");
}
}
#[test]
fn without_a_worker_a_migration_keeps_the_wait_timeout() {
let dir = tempfile::tempdir().unwrap();
let map = populated_tight(
dir.path(),
200,
OpenOptions {
worker: false,
..OpenOptions::production()
},
);
assert!(matches!(
map.migrate_rows(rewrite_all),
Err(DbError::SealBackpressure { .. })
));
}
struct NoSegmentsFs(Arc<dyn crate::seg::fs::SegFs>);
impl NoSegmentsFs {
fn refuse(path: &Path) -> std::io::Result<()> {
if path.to_string_lossy().contains(".sdat") {
return Err(std::io::Error::other("injected: no segment can be written"));
}
Ok(())
}
}
impl crate::seg::fs::SegFs for NoSegmentsFs {
fn create(&self, path: &Path) -> std::io::Result<std::fs::File> {
Self::refuse(path)?;
self.0.create(path)
}
fn create_new(&self, path: &Path) -> std::io::Result<std::fs::File> {
Self::refuse(path)?;
self.0.create_new(path)
}
fn write_all(
&self,
out: &mut dyn std::io::Write,
path: &Path,
bytes: &[u8],
) -> std::io::Result<()> {
self.0.write_all(out, path, bytes)
}
fn sync_file(&self, file: &std::fs::File, path: &Path) -> std::io::Result<()> {
self.0.sync_file(file, path)
}
fn rename(&self, from: &Path, to: &Path) -> std::io::Result<()> {
self.0.rename(from, to)
}
fn sync_dir(&self, dir: &Path) -> std::io::Result<()> {
self.0.sync_dir(dir)
}
fn create_dir_all(&self, dir: &Path) -> std::io::Result<()> {
self.0.create_dir_all(dir)
}
fn remove_file(&self, path: &Path) -> std::io::Result<()> {
self.0.remove_file(path)
}
fn remove_dir_all(&self, dir: &Path) -> std::io::Result<()> {
self.0.remove_dir_all(dir)
}
}
#[test]
fn a_migration_ends_when_a_seal_fails_while_it_waits() {
let dir = tempfile::tempdir().unwrap();
let map = Arc::new(populated_tight(
dir.path(),
200,
OpenOptions {
fs: Arc::new(NoSegmentsFs(crate::seg::fs::real())),
..OpenOptions::production()
},
));
let (done, result) = std::sync::mpsc::channel();
let migrating = map.clone();
std::thread::spawn(move || {
let _ = done.send(migrating.migrate_rows(rewrite_all));
});
let result = result
.recv_timeout(Duration::from_secs(30))
.expect("a migration must not wait for a seal pipeline that fails");
assert!(
matches!(result, Err(DbError::SealBackpressure { .. })),
"{result:?}"
);
assert!(map.seal_status().last_error.is_some());
}
type Map = SegMap<TestKey, String, TextCodec>;
fn config() -> SegConfig {
SegConfig {
seal_max_keys: 1_000_000,
max_frozen_generations: 8,
..SegConfig::test()
}
}
fn open(dir: &Path) -> Map {
SegMap::open(dir, meta(), config(), TextCodec).unwrap()
}
#[test]
fn typed_values_round_trip() {
let dir = tempfile::tempdir().unwrap();
let map = open(dir.path());
assert!(map.is_empty());
map.put(&key_of(1), &"one".to_owned()).unwrap();
map.insert(&key_of(2), &"two".to_owned()).unwrap();
assert!(matches!(
map.insert(&key_of(2), &"again".to_owned()),
Err(DbError::KeyExists)
));
map.delete(&key_of(1)).unwrap();
assert_eq!(map.get(&key_of(1)), None);
assert_eq!(map.get(&key_of(2)), Some("two".to_owned()));
assert_eq!(map.try_get(&key_of(2)).unwrap(), Some("two".to_owned()));
assert_eq!(map.get_or_err(&key_of(2)).unwrap(), "two");
assert!(matches!(
map.get_or_err(&key_of(1)),
Err(DbError::KeyNotFound)
));
assert!(map.contains(&key_of(2)));
assert!(!map.try_contains(&key_of(1)).unwrap());
assert_eq!(map.len(), 1);
assert_eq!(map.name(), "test_seg");
}
#[test]
fn values_survive_a_seal_and_a_reopen() {
let dir = tempfile::tempdir().unwrap();
{
let map = open(dir.path());
for i in 0..300u64 {
map.put(&key_of(i), &format!("value {i}")).unwrap();
}
map.seal().unwrap();
assert!(map.wait_sealed(Duration::from_secs(20)).unwrap());
assert_eq!(map.segments().len(), 1);
assert!(map.verify(VerifyScope::All).unwrap().is_clean());
assert_eq!(map.seal_status().frozen, 0);
}
let map = open(dir.path());
for i in 0..300u64 {
assert_eq!(map.get(&key_of(i)), Some(format!("value {i}")));
}
assert_eq!(
map.try_get_hinted(&key_of(7), 0).unwrap(),
Some("value 7".to_owned())
);
assert_eq!(map.drop_segments_before(u64::MAX).unwrap(), 1);
assert_eq!(map.get(&key_of(7)), None);
}
#[test]
fn a_strict_read_reports_what_a_lenient_one_hides() {
let dir = tempfile::tempdir().unwrap();
let map = open(dir.path());
map.core().put(&key_of(1), &[0xff, 0xfe]).unwrap();
assert!(matches!(
map.try_get(&key_of(1)),
Err(DbError::CorruptedEntry { .. })
));
assert_eq!(map.get(&key_of(1)), None);
assert!(map.try_contains(&key_of(1)).unwrap());
}
#[test]
fn walking_rows_decodes_each_one() {
let dir = tempfile::tempdir().unwrap();
let map = open(dir.path());
for i in 0..50u64 {
map.put(&key_of(i), &format!("v{i}")).unwrap();
}
map.core().put(&key_of(99), &[0xff]).unwrap();
let mut good = 0;
let mut bad = 0;
map.walk_rows(true, &mut |row| {
match row {
Ok((key, value)) => {
assert_eq!(map.get(&key), Some(value));
good += 1;
}
Err(_) => bad += 1,
}
ControlFlow::Continue(())
});
assert_eq!(
(good, bad),
(50, 1),
"a row that does not decode does not end the walk"
);
let mut seen = 0;
map.walk_rows(false, &mut |row| {
assert!(row.is_err());
seen += 1;
ControlFlow::Continue(())
});
assert_eq!(seen, 1);
}
#[test]
fn encode_errors_leave_put_and_insert_unapplied() {
struct RejectingCodec;
impl Codec<String> for RejectingCodec {
fn encode_to(&self, value: &String, buf: &mut Vec<u8>) -> DbResult<()> {
buf.extend_from_slice(b"partially encoded");
if value == "reject" {
return Err(DbError::Client("rejected value"));
}
TextCodec.encode_to(value, buf)
}
fn decode_from(&self, bytes: &[u8]) -> DbResult<String> {
TextCodec.decode_from(bytes)
}
}
let dir = tempfile::tempdir().unwrap();
let map = SegMap::<TestKey, String, _>::open(dir.path(), meta(), config(), RejectingCodec)
.unwrap();
map.put(&key_of(1), &"old".to_owned()).unwrap();
let active = map.core().view().active.clone();
let before = (active.keys(), active.bytes(), active.next_gsn());
assert!(matches!(
map.put(&key_of(1), &"reject".to_owned()),
Err(DbError::Client(_))
));
assert!(matches!(
map.insert(&key_of(2), &"reject".to_owned()),
Err(DbError::Client(_))
));
assert_eq!((active.keys(), active.bytes(), active.next_gsn()), before);
assert_eq!(map.try_get(&key_of(1)).unwrap(), Some("old".to_owned()));
assert_eq!(map.try_get(&key_of(2)).unwrap(), None);
assert_eq!(map.core().count_exact().unwrap(), 1);
}
#[test]
fn row_visitor_break_and_storage_error_end_the_walk() {
let dir = tempfile::tempdir().unwrap();
crate::seg::testkit::build_collection(dir.path());
let clock = crate::seg::testkit::TestClock::new(1_000_000);
let map = Map::open_with(
dir.path(),
meta(),
config(),
TextCodec,
OpenOptions {
fs: crate::seg::fs::real(),
clock: clock.clock(),
worker: false,
},
)
.unwrap();
let mut seen = 0;
map.walk_rows(true, &mut |row| {
row.unwrap();
seen += 1;
ControlFlow::Break(())
});
assert_eq!(seen, 1);
let _damaged = crate::seg::testkit::UnreadableJournals::new(dir.path());
let (mut errors, mut after_error) = (0, 0);
map.walk_rows(true, &mut |row| {
match row {
Err(_) => errors += 1,
Ok(_) if errors > 0 => after_error += 1,
Ok(_) => {}
}
ControlFlow::Continue(())
});
assert_eq!((errors, after_error), (1, 0));
}
}