feat: with sled db and capnp

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
2023-08-27 18:15:25 +02:00
parent 757d1081bd
commit 75d99c2461
13 changed files with 207 additions and 41 deletions

View File

@@ -24,5 +24,6 @@ serde.workspace = true
serde_json.workspace = true
uuid.workspace = true
async-trait.workspace = true
itertools.workspace = true
sled.workspace = true

View File

@@ -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<_>>())
}
}

View File

@@ -5,6 +5,7 @@ use std::sync::Arc;
use axum::async_trait;
use churn_domain::{LogEvent, ServerEnrollReq};
use itertools::Itertools;
use serde::{ser::SerializeStruct, Deserialize, Serialize};
use tokio::sync::{Mutex, RwLock};
@@ -58,7 +59,7 @@ impl EventServiceTrait for DefaultEventService {
async fn append(&self, req: LogEvent) -> anyhow::Result<()> {
self.db
.insert("events_log", &req.id.to_string(), &req.serialize_capnp())
.await;
.await?;
Ok(())
}
@@ -67,8 +68,15 @@ impl EventServiceTrait for DefaultEventService {
let events = events
.iter()
.map(|e| LogEvent::deserialize_capnp(e))
.map(|e| match LogEvent::deserialize_capnp(e) {
Ok(o) => Ok(o),
Err(e) => {
tracing::error!("failed to deserialize capnp: {e}");
Err(e)
}
})
.flatten()
.sorted_by_key(|i| i.timestamp)
.skip_while(|item| item.id != cursor)
.skip(1)
.collect();
@@ -82,6 +90,7 @@ impl EventServiceTrait for DefaultEventService {
.iter()
.map(|e| LogEvent::deserialize_capnp(e))
.flatten()
.sorted_by_key(|i| i.timestamp)
.collect();
Ok(events)

View File

@@ -1,3 +1,5 @@
#![feature(slice_pattern)]
mod agent;
mod db;
mod event;
@@ -204,10 +206,10 @@ async fn logs(
match cursor.cursor {
Some(cursor) => {
tracing::trace!("finding logs from cursor: {}", cursor);
tracing::debug!("finding logs from cursor: {}", cursor);
}
None => {
tracing::trace!("finding logs from beginning");
tracing::debug!("finding logs from beginning");
}
}