armdb 0.15.3

sharded bitcask key-value storage optimized for NVMe
Documentation
#![cfg(all(
    feature = "armour",
    feature = "typed-tree",
    feature = "rapira-codec",
    feature = "var-collections"
))]

use armdb::{QueueConfig, armour::Db, queue};
use armour_core::{
    Fuid, IdBrand, Plid,
    enc::{Cipher, IdHasher},
};

#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub struct Message;
impl IdHasher for Message {
    const HASHER: Cipher =
        Cipher::new("_mKbKGF2IrkGvIJvl97HuCgWjgt6QRZ7Ye8DHBQ2anvyi18BdMz8uN6Ej3YJApooY6qDu0obqq4");
}
impl IdBrand for Message {
    const BRAND: &'static str = "msg";
}
type OldId = Fuid<Message>;
type NewId = Plid<Message>;

queue!(OldFixed, payload = OldId, name = "plid_fixed_queue");
struct NewFixed;
impl armdb::QueueSpec for NewFixed {
    type Payload = NewId;
    type Mode = armdb::FixedQueueMode;
    const NAME: &'static str = "plid_fixed_queue";
    const PAYLOAD_NAME: &'static str = "plid_fixed_queue.payload";
    const PAYLOAD_VERSION: u16 = 1;
    const PAYLOAD_KEEP_MIGRATIONS: &'static [u16] = &[0];
}
queue!(OldVar, payload = OldId, name = "plid_var_queue", var);
struct NewVar;
impl armdb::QueueSpec for NewVar {
    type Payload = NewId;
    type Mode = armdb::VarQueueMode;
    const NAME: &'static str = "plid_var_queue";
    const PAYLOAD_NAME: &'static str = "plid_var_queue.payload";
    const PAYLOAD_VERSION: u16 = 1;
    const PAYLOAD_KEEP_MIGRATIONS: &'static [u16] = &[0];
}

#[test]
fn queued_old_fixed_and_variable_payloads_survive_plid_cutover() {
    let dir = tempfile::tempdir().unwrap();
    let bytes = 0x8123_4567_89ab_cdefu64.to_be_bytes();
    {
        let db = Db::open_test(dir.path()).unwrap();
        let fixed = db
            .open_queue::<OldFixed>(QueueConfig::balanced().build().unwrap())
            .unwrap();
        let variable = db
            .open_queue::<OldVar>(QueueConfig::balanced().build().unwrap())
            .unwrap();
        fixed.push(OldId::from_be_bytes(bytes), None).unwrap();
        variable.push(OldId::from_be_bytes(bytes), None).unwrap();
        drop(fixed);
        drop(variable);
        db.clean_shutdown().unwrap();
    }
    for pass in 0..2 {
        let db = Db::open_test(dir.path()).unwrap();
        let fixed = db
            .open_queue::<NewFixed>(QueueConfig::balanced().build().unwrap())
            .unwrap();
        let variable = db
            .open_queue::<NewVar>(QueueConfig::balanced().build().unwrap())
            .unwrap();
        assert_eq!(fixed.len(), 1 + pass);
        assert_eq!(variable.len(), 1 + pass);
        assert_eq!(db.db_info().collections["plid_fixed_queue"].version, 1);
        assert_eq!(
            db.db_info().collections["plid_var_queue.payload"].version,
            1
        );
        if pass == 0 {
            fixed.push(NewId::from_native(17), None).unwrap();
            variable.push(NewId::from_native(17), None).unwrap();
        }
        if pass == 1 {
            for actual in [
                fixed.lease(2, None).unwrap(),
                variable.lease(2, None).unwrap(),
            ] {
                let mut ids: Vec<_> = actual
                    .iter()
                    .map(|row| row.payload().to_be_bytes())
                    .collect();
                ids.sort();
                assert_eq!(ids, vec![17u64.to_be_bytes(), bytes]);
            }
        }
        drop(fixed);
        drop(variable);
        db.clean_shutdown().unwrap();
    }
}

struct MissingStep;
impl armdb::QueueSpec for MissingStep {
    type Payload = NewId;
    type Mode = armdb::FixedQueueMode;
    const NAME: &'static str = "plid_fixed_queue";
    const PAYLOAD_NAME: &'static str = "plid_fixed_queue.payload";
    const PAYLOAD_VERSION: u16 = 2;
    const PAYLOAD_KEEP_MIGRATIONS: &'static [u16] = &[1];
}

#[test]
fn missing_queue_migration_refuses_old_rows() {
    let dir = tempfile::tempdir().unwrap();
    {
        let db = Db::open_test(dir.path()).unwrap();
        let queue = db
            .open_queue::<OldFixed>(QueueConfig::balanced().build().unwrap())
            .unwrap();
        queue
            .push(OldId::from_be_bytes(17u64.to_be_bytes()), None)
            .unwrap();
        drop(queue);
        db.clean_shutdown().unwrap();
    }
    let db = Db::open_test(dir.path()).unwrap();
    assert!(matches!(
        db.open_queue::<MissingStep>(QueueConfig::balanced().build().unwrap()),
        Err(armdb::DbError::SchemaMismatch {
            kind: armdb::SchemaMismatchKind::MissingStep { from: 0 },
            ..
        })
    ));
    drop(db);
    let db = Db::open_test(dir.path()).unwrap();
    let queue = db
        .open_queue::<OldFixed>(QueueConfig::balanced().build().unwrap())
        .unwrap();
    assert_eq!(
        queue.lease(1, None).unwrap()[0].payload().to_be_bytes(),
        17u64.to_be_bytes()
    );
}