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