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;
23pub mod format_version;
24pub mod index;
25/// SQL-like query engine (v0.4+)
26pub mod query;
27pub mod storage;
28pub mod transaction;
29
30#[cfg(test)]
31mod transaction_tests;
32
33pub use error::{Error, Result};
34
35use std::collections::HashMap;
36use std::sync::{Arc, RwLock};
37
38/// The main database handle.
39///
40/// This is the primary interface for interacting with RustLite.
41/// It provides thread-safe access to the underlying storage engine.
42#[derive(Clone)]
43pub struct Database {
44    inner: Arc<DatabaseInner>,
45}
46
47struct DatabaseInner {
48    // Simple in-memory storage for v0.1
49    // This will be replaced with proper storage engine in future versions
50    store: RwLock<HashMap<Vec<u8>, Vec<u8>>>,
51}
52
53impl Database {
54    /// Creates a new in-memory database instance.
55    pub fn new() -> Result<Self> {
56        Ok(Database {
57            inner: Arc::new(DatabaseInner {
58                store: RwLock::new(HashMap::new()),
59            }),
60        })
61    }
62
63    /// Inserts or updates a key-value pair.
64    pub fn put(&self, key: &[u8], value: &[u8]) -> Result<()> {
65        let mut store = self.inner.store.write().map_err(|_| Error::LockPoisoned)?;
66        store.insert(key.to_vec(), value.to_vec());
67        Ok(())
68    }
69
70    /// Retrieves a value by key.
71    pub fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
72        let store = self.inner.store.read().map_err(|_| Error::LockPoisoned)?;
73        Ok(store.get(key).cloned())
74    }
75
76    /// Deletes a key-value pair.
77    pub fn delete(&self, key: &[u8]) -> Result<bool> {
78        let mut store = self.inner.store.write().map_err(|_| Error::LockPoisoned)?;
79        Ok(store.remove(key).is_some())
80    }
81}
82
83impl Default for Database {
84    fn default() -> Self {
85        Self::new().expect("Failed to create default database")
86    }
87}