1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
use crate::{db_state::ScndIdxNum, lsm::unit::UnitDir};
use anyhow::{anyhow, Result};
use pancake_engine_common::{fs_utils, SSTable};
use pancake_types::{
    serde::OptDatum,
    types::{PKShared, PVShared, SVPKShared},
};
use std::any;
use std::collections::HashMap;

pub struct CompactedUnit {
    pub prim: Option<SSTable<PKShared, OptDatum<PVShared>>>,
    pub scnds: HashMap<ScndIdxNum, SSTable<SVPKShared, OptDatum<PVShared>>>,
    pub dir: UnitDir,
}

/// Note, this type does not offer any API to remove its dir.
/// Any logic that writes to a [`CompactedUnit`] should instantiate it lazily,
/// iff there are data to write.
impl CompactedUnit {
    pub fn new_empty(dir: UnitDir) -> Result<Self> {
        let dir_path = dir.path();
        if dir_path.exists() {
            return Err(anyhow!(
                "New {} cannot be created at an existing dir {:?}",
                any::type_name::<Self>(),
                dir_path
            ));
        }
        fs_utils::create_dir_all(dir_path)?;

        Ok(Self {
            prim: None,
            scnds: HashMap::new(),
            dir,
        })
    }
}