Skip to main content

vsdb/dagmap/
mod.rs

1//!
2//! # Directed Acyclic Graph (DAG) Map
3//!
4//! This module provides data structures for representing and managing directed
5//! acyclic graphs (DAGs) on disk.
6//!
7
8/// A module for raw DAG maps.
9pub mod raw;
10/// A module for DAG maps with raw keys.
11pub mod rawkey;
12
13/// A type alias for a DAG map ID.
14pub type DagMapId = [u8];
15
16/// Generates a new, unique ID for a DAG map.
17///
18/// This function maintains a persistent counter to ensure that each generated
19/// ID is unique.
20pub fn gen_dag_map_id_num() -> u128 {
21    use crate::{Orphan, ValueEnDe};
22    use parking_lot::Mutex;
23    use ruc::*;
24    use std::{fs, io::ErrorKind, sync::LazyLock};
25
26    static ID_NUM: LazyLock<Mutex<Orphan<u128>>> = LazyLock::new(|| {
27        let mut meta_path = vsdb_core::vsdb_get_custom_dir().to_owned();
28        meta_path.push("id_num");
29
30        match fs::read(&meta_path) {
31            Ok(m) => Mutex::new(ValueEnDe::decode(&m).unwrap()),
32            Err(e) => match e.kind() {
33                ErrorKind::NotFound => {
34                    let i = Orphan::new(0);
35                    fs::write(&meta_path, i.encode()).unwrap();
36                    Mutex::new(i)
37                }
38                _ => {
39                    pnk!(Err(eg!("Error!")))
40                }
41            },
42        }
43    });
44
45    let mut hdr = ID_NUM.lock();
46    let mut hdr = hdr.get_mut();
47    *hdr += 1;
48    *hdr
49}