rustlite_core/
lib.rs

1//! # RustLite Core
2//!
3//! Core types and implementations for RustLite embedded database.
4//!
5//! ## ⚠️ Internal Implementation Detail
6//!
7//! **This crate is an internal implementation detail of RustLite.**
8//!
9//! Users should depend on the main [`rustlite`](https://crates.io/crates/rustlite) crate
10//! instead, which provides the stable public API. This crate's API may change
11//! without notice between minor versions.
12//!
13//! ```toml
14//! # In your Cargo.toml - use the main crate, not this one:
15//! [dependencies]
16//! rustlite = "0.3"
17//! ```
18
19#![warn(missing_docs)]
20#![warn(clippy::all)]
21
22pub mod error;
23/// File format versioning for SSTable, WAL, and Manifest
24pub mod format_version;
25pub mod index;
26/// SQL-like query engine (v0.4+)
27pub mod query;
28pub mod storage;
29pub mod transaction;
30
31#[cfg(test)]
32mod transaction_tests;
33
34pub use error::{Error, Result};
35
36use std::collections::HashMap;
37use std::sync::{Arc, RwLock};
38
39/// The main database handle.
40///
41/// This is the primary interface for interacting with RustLite.
42/// It provides thread-safe access to the underlying storage engine.
43#[derive(Clone)]
44pub struct Database {
45    inner: Arc<DatabaseInner>,
46}
47
48struct DatabaseInner {
49    // Simple in-memory storage for v0.1
50    // This will be replaced with proper storage engine in future versions
51    store: RwLock<HashMap<Vec<u8>, Vec<u8>>>,
52}
53
54impl Database {
55    /// Creates a new in-memory database instance.
56    pub fn new() -> Result<Self> {
57        Ok(Database {
58            inner: Arc::new(DatabaseInner {
59                store: RwLock::new(HashMap::new()),
60            }),
61        })
62    }
63
64    /// Inserts or updates a key-value pair.
65    pub fn put(&self, key: &[u8], value: &[u8]) -> Result<()> {
66        let mut store = self.inner.store.write().map_err(|_| Error::LockPoisoned)?;
67        store.insert(key.to_vec(), value.to_vec());
68        Ok(())
69    }
70
71    /// Retrieves a value by key.
72    pub fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
73        let store = self.inner.store.read().map_err(|_| Error::LockPoisoned)?;
74        Ok(store.get(key).cloned())
75    }
76
77    /// Deletes a key-value pair.
78    pub fn delete(&self, key: &[u8]) -> Result<bool> {
79        let mut store = self.inner.store.write().map_err(|_| Error::LockPoisoned)?;
80        Ok(store.remove(key).is_some())
81    }
82}
83
84impl Default for Database {
85    fn default() -> Self {
86        Self::new().expect("Failed to create default database")
87    }
88}