This commit is contained in:
1 parent
bc32432808
commit
12bedd5de7
11 files changed
+303
-4
No files matched your search
@@ -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(())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user