Skip to main content

nodedb_lite/storage/pagedb_storage/open/
memory.rs

1// SPDX-License-Identifier: Apache-2.0
2
3//! In-memory constructor (tests and WASM without persistence).
4
5use std::sync::Arc;
6
7use pagedb::vfs::memory::MemVfs;
8use pagedb::{Db, RealmId};
9
10use crate::error::LiteError;
11use crate::storage::pagedb_storage::types::{PagedbStorage, lite_open_options};
12
13impl PagedbStorage<MemVfs> {
14    /// Create an in-memory database (for testing and WASM without persistence).
15    ///
16    /// In-memory storage is volatile (data lives only for the process lifetime),
17    /// so no at-rest encryption is applied; the pagedb KEK is all-zero.
18    pub async fn open_in_memory() -> Result<Self, LiteError> {
19        let kek = [0u8; 32];
20        let realm = RealmId::new([0u8; 16]);
21        let vfs = MemVfs::new();
22        let db = Db::open(vfs, kek, 4096, realm, lite_open_options())
23            .await
24            .map_err(LiteError::from)?;
25        Ok(Self {
26            db: Arc::new(db),
27            #[cfg(not(target_arch = "wasm32"))]
28            snapshot_descriptor: None,
29        })
30    }
31}