This commit is contained in:
:NoBody: committed 2026-03-21 23:32:01 +08:00
1 parent bc32432808
commit 12bedd5de7
11 files changed
+303 -4

No files matched your search

+43
View File
@@ -0,0 +1,43 @@
use std::path::Path;
use anyhow::Result;
use async_trait::async_trait;
//底层存储引擎实现
#[async_trait]
pub trait StorageEngine: Sized + Send + Sync {
async fn open(path: &Path) -> Result<Self>;
async fn get_raw(&self, key: &[u8]) -> Result<Option<Vec<u8>>>;
async fn set_raw(&self, key: &[u8], value: &[u8]) -> Result<()>;
async fn delete_raw(&self, key: &[u8]) -> Result<()>;
}
//上层语义化存储实现
#[async_trait]
pub trait DailyBotStore: Send + Sync {
async fn save(&self, scope: &str, id: &str, value: &[u8]) -> Result<()>;
async fn load(&self, scope: &str, id: &str) -> Result<Option<Vec<u8>>>;
async fn remove(&self, scope: &str, id: &str) -> Result<()>;
}
//给所有底层引擎实现专门对底层引擎的特有方法
#[async_trait]
impl<T> DailyBotStore for T
where
T: StorageEngine + Send + Sync,
{
async fn save(&self, scope: &str, id: &str, value: &[u8]) -> Result<()> {
let key = format!("{scope}:{id}");
self.set_raw(key.as_bytes(), value).await
}
async fn load(&self, scope: &str, id: &str) -> Result<Option<Vec<u8>>> {
let key = format!("{scope}:{id}");
self.get_raw(key.as_bytes()).await
}
async fn remove(&self, scope: &str, id: &str) -> Result<()> {
let key = format!("{scope}:{id}");
self.delete_raw(key.as_bytes()).await
}
}
+1 -1
View File
@@ -1,2 +1,2 @@
pub mod define;
pub mod sled_db;
pub mod define;
+111
View File
@@ -0,0 +1,111 @@
use std::path::Path;
use super::define::StorageEngine;
use anyhow::Result;
use async_trait::async_trait;
pub struct SledDB {
pub sled_db: sled::Db,
}
impl SledDB {
pub fn new(path: &Path) -> Result<Self> {
let db = sled::open(path)?;
Ok(Self { sled_db: db })
}
}
#[async_trait]
impl StorageEngine for SledDB {
async fn open(path: &Path) -> Result<Self> {
Self::new(path)
}
async fn set_raw(&self, key: &[u8], value: &[u8]) -> Result<()> {
self.sled_db.insert(key, value)?;
self.sled_db.flush()?;
Ok(())
}
async fn get_raw(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
let result = self.sled_db.get(key)?;
Ok(result.map(|i| i.to_vec()))
}
async fn delete_raw(&self, key: &[u8]) -> Result<()> {
self.sled_db.remove(key)?;
self.sled_db.flush()?;
Ok(())
}
}
//INFO: AI写的集成测试
#[cfg(test)]
mod tests {
use std::path::PathBuf;
use std::time::{SystemTime, UNIX_EPOCH};
use anyhow::Result;
use super::super::define::{DailyBotStore, StorageEngine};
use super::SledDB;
fn unique_db_path() -> PathBuf {
let nanos = SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("time went backwards")
.as_nanos();
std::env::temp_dir().join(format!(
"daily_bot_sled_test_{}_{}",
std::process::id(),
nanos
))
}
fn cleanup_db_path(path: &PathBuf) {
if path.exists() {
let _ = std::fs::remove_dir_all(path);
}
}
#[tokio::test]
async fn raw_crud_roundtrip_works() -> Result<()> {
let db_path = unique_db_path();
let db = <SledDB as StorageEngine>::open(&db_path).await?;
db.set_raw(b"k1", b"v1").await?;
let got = db.get_raw(b"k1").await?;
assert_eq!(got, Some(b"v1".to_vec()));
db.delete_raw(b"k1").await?;
let deleted = db.get_raw(b"k1").await?;
assert_eq!(deleted, None);
drop(db);
cleanup_db_path(&db_path);
Ok(())
}
#[tokio::test]
async fn daily_bot_store_namespace_works() -> Result<()> {
let db_path = unique_db_path();
let db = SledDB::new(&db_path)?;
db.save("user", "alice", b"hello").await?;
let loaded = db.load("user", "alice").await?;
assert_eq!(loaded, Some(b"hello".to_vec()));
let raw = db.get_raw(b"user:alice").await?;
assert_eq!(raw, Some(b"hello".to_vec()));
db.remove("user", "alice").await?;
let removed = db.load("user", "alice").await?;
assert_eq!(removed, None);
drop(db);
cleanup_db_path(&db_path);
Ok(())
}
}