rustlite_core/
lib.rs

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