@@ -1,3 +1,4 @@
|
||||
use core::slice::SlicePattern;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
@@ -50,30 +51,29 @@ impl DefaultDb {
|
||||
|
||||
#[async_trait]
|
||||
pub trait DbTrait {
|
||||
async fn insert(&self, namespace: &str, key: &str, value: &str) -> anyhow::Result<()>;
|
||||
async fn get_all(&self, namespace: &str) -> anyhow::Result<Vec<String>>;
|
||||
async fn insert(&self, namespace: &str, key: &str, value: &[u8]) -> anyhow::Result<()>;
|
||||
async fn get_all(&self, namespace: &str) -> anyhow::Result<Vec<Vec<u8>>>;
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl DbTrait for DefaultDb {
|
||||
async fn insert(&self, namespace: &str, key: &str, value: &str) -> anyhow::Result<()> {
|
||||
async fn insert(&self, namespace: &str, key: &str, value: &[u8]) -> anyhow::Result<()> {
|
||||
let tree = self.db.open_tree(namespace)?;
|
||||
|
||||
tree.insert(key, value)?;
|
||||
|
||||
tree.flush_async().await?;
|
||||
//tree.flush_async().await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_all(&self, namespace: &str) -> anyhow::Result<Vec<String>> {
|
||||
async fn get_all(&self, namespace: &str) -> anyhow::Result<Vec<Vec<u8>>> {
|
||||
let tree = self.db.open_tree(namespace)?;
|
||||
|
||||
Ok(tree
|
||||
.iter()
|
||||
.flatten()
|
||||
.map(|(_, val)| val)
|
||||
.flat_map(|v| v.iter().map(|v| v.to_string()).collect::<Vec<String>>())
|
||||
.map(|(_, val)| val.as_slice().to_vec())
|
||||
.collect::<Vec<_>>())
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user