File size: 3,015 Bytes
1269259
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// SPF Smart Gateway - LMDB Storage
// Copyright 2026 Joseph Stone - All Rights Reserved
//
// Persists session state to LMDB at LIVE/SESSION/SESSION.DB.
// Used for: session checkpoints, complexity history, manifest, failures.

use crate::session::Session;
use anyhow::Result;
use heed::types::*;
use heed::{Database, Env, EnvOpenOptions};
use std::path::Path;

/// LMDB storage for SPF gateway state
pub struct SpfStorage {
    env: Env,
    /// Main key-value store: string keys → JSON values
    db: Database<Str, Str>,
}

const SESSION_KEY: &str = "current_session";
const MAX_DB_SIZE: usize = 512 * 1024 * 1024; // 512MB — virtual address space only, no physical cost

impl SpfStorage {
    /// Open or create LMDB at the given path
    pub fn open(path: &Path) -> Result<Self> {
        std::fs::create_dir_all(path)?;

        let env = unsafe {
            EnvOpenOptions::new()
                .map_size(MAX_DB_SIZE)
                .max_dbs(4)
                .open(path)?
        };

        let mut wtxn = env.write_txn()?;
        let db = env.create_database(&mut wtxn, Some("spf_state"))?;
        wtxn.commit()?;

        log::info!("SPF LMDB opened at {:?}", path);
        Ok(Self { env, db })
    }

    /// Save session state to LMDB
    pub fn save_session(&self, session: &Session) -> Result<()> {
        let json = serde_json::to_string(session)?;
        let mut wtxn = self.env.write_txn()?;
        self.db.put(&mut wtxn, SESSION_KEY, &json)?;
        wtxn.commit()?;
        Ok(())
    }

    /// Load session state from LMDB
    pub fn load_session(&self) -> Result<Option<Session>> {
        let rtxn = self.env.read_txn()?;
        match self.db.get(&rtxn, SESSION_KEY)? {
            Some(json) => {
                let session: Session = serde_json::from_str(json)?;
                Ok(Some(session))
            }
            None => Ok(None),
        }
    }

    /// Store arbitrary key-value pair
    pub fn put(&self, key: &str, value: &str) -> Result<()> {
        let mut wtxn = self.env.write_txn()?;
        self.db.put(&mut wtxn, key, value)?;
        wtxn.commit()?;
        Ok(())
    }

    /// Retrieve a value by key
    pub fn get(&self, key: &str) -> Result<Option<String>> {
        let rtxn = self.env.read_txn()?;
        Ok(self.db.get(&rtxn, key)?.map(|s| s.to_string()))
    }

    /// Delete a key
    pub fn delete(&self, key: &str) -> Result<bool> {
        let mut wtxn = self.env.write_txn()?;
        let deleted = self.db.delete(&mut wtxn, key)?;
        wtxn.commit()?;
        Ok(deleted)
    }

    /// Get storage size in bytes
    pub fn size_bytes(&self) -> Result<u64> {
        let rtxn = self.env.read_txn()?;
        let stat = self.db.stat(&rtxn)?;
        // Approximate: entries * average size
        Ok((stat.entries as u64) * 256)
    }

    /// Get entry count
    pub fn entry_count(&self) -> Result<u64> {
        let rtxn = self.env.read_txn()?;
        let stat = self.db.stat(&rtxn)?;
        Ok(stat.entries as u64)
    }
}