feat: hyperlog with basic engine

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
Kasper Juul Hermansen 2024-04-28 23:10:15 +02:00
commit 7de4872816
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912
15 changed files with 3435 additions and 0 deletions

2
.drone.yml Normal file
View File

@ -0,0 +1,2 @@
kind: template
load: cuddle-rust-service-plan.yaml

1
.env Normal file
View File

@ -0,0 +1 @@
DATABASE_URL="postgres://root@localhost:26257/defaultdb?sslmode=disable"

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
target/
.cuddle/

2333
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

14
Cargo.toml Normal file
View File

@ -0,0 +1,14 @@
[workspace]
members = ["crates/*"]
resolver = "2"
[workspace.dependencies]
hyperlog = { path = "crates/hyperlog" }
anyhow = { version = "1" }
tokio = { version = "1", features = ["full"] }
tracing = { version = "0.1", features = ["log"] }
tracing-subscriber = { version = "0.3.18" }
clap = { version = "4", features = ["derive", "env"] }
dotenv = { version = "0.15" }
axum = { version = "0.7" }

1
README.md Normal file
View File

@ -0,0 +1 @@
# hyperlog

1
crates/hyperlog/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

View File

@ -0,0 +1,22 @@
[package]
name = "hyperlog"
version = "0.1.0"
edition = "2021"
[dependencies]
anyhow.workspace = true
tokio.workspace = true
tracing.workspace = true
tracing-subscriber.workspace = true
clap.workspace = true
dotenv.workspace = true
axum.workspace = true
serde = { version = "1.0.197", features = ["derive"] }
sqlx = { version = "0.7.3", features = ["runtime-tokio", "tls-rustls", "postgres", "uuid", "time"] }
uuid = { version = "1.7.0", features = ["v4"] }
tower-http = { version = "0.5.2", features = ["cors", "trace"] }
serde_json = "1.0.116"
[dev-dependencies]
similar-asserts = "1.5.0"

View File

@ -0,0 +1 @@
-- Add migration script here

View File

@ -0,0 +1,676 @@
use std::{collections::BTreeMap, fmt::Display};
use anyhow::anyhow;
use crate::log::{Graph, GraphItem};
#[derive(Default)]
pub struct Engine {
graph: Graph,
}
impl Engine {
pub fn create_root(&mut self, root: &str) -> anyhow::Result<()> {
self.graph
.try_insert(root.to_string(), GraphItem::User(BTreeMap::default()))
.map_err(|_| anyhow!("entry was already found, aborting"))?;
Ok(())
}
pub fn create(&mut self, root: &str, path: &[&str], item: GraphItem) -> anyhow::Result<()> {
let graph = &mut self.graph;
let (last, items) = path.split_last().ok_or(anyhow!(
"path cannot be empty, must contain at least one item"
))?;
let root = graph
.get_mut(root)
.ok_or(anyhow!("root was missing a user, aborting"))?;
let mut current_item = root;
for section in items {
match current_item {
GraphItem::User(u) => match u.get_mut(section.to_owned()) {
Some(graph_item) => {
current_item = graph_item.as_mut();
}
None => anyhow::bail!("path: {} section was not found", section),
},
GraphItem::Item { .. } => anyhow::bail!("path: {} was already found", section),
GraphItem::Section(s) => match s.get_mut(section.to_owned()) {
Some(graph_item) => {
current_item = graph_item;
}
None => anyhow::bail!("path: {} section was not found", section),
},
}
}
match current_item {
GraphItem::User(u) => {
u.insert(last.to_string(), Box::new(item));
}
GraphItem::Section(s) => {
s.insert(last.to_string(), Box::new(item));
}
GraphItem::Item { .. } => anyhow::bail!("cannot insert an item into an item"),
}
Ok(())
}
pub fn get(&self, root: &str, path: &[&str]) -> Option<&GraphItem> {
let root = self.graph.get(root)?;
root.get(path)
}
pub fn get_mut(&mut self, root: &str, path: &[&str]) -> Option<&mut GraphItem> {
let root = self.graph.get_mut(root)?;
root.get_mut(path)
}
pub fn take(&mut self, root: &str, path: &[&str]) -> Option<GraphItem> {
let root = self.graph.get_mut(root)?;
root.take(path)
}
pub fn section_move(
&mut self,
root: &str,
src_path: &[&str],
dest_path: &[&str],
) -> anyhow::Result<()> {
let src = self
.take(root, src_path)
.ok_or(anyhow!("failed to find source path"))?;
let dest = self
.get_mut(root, dest_path)
.ok_or(anyhow!("failed to find destination"))?;
let src_item = src_path
.last()
.ok_or(anyhow!("src path must have at least one item"))?;
match dest {
GraphItem::User(u) => {
u.try_insert(src_item.to_string(), Box::new(src))
.map_err(|_e| anyhow!("key was already found, aborting: {}", src_item))?;
}
GraphItem::Section(s) => {
s.try_insert(src_item.to_string(), Box::new(src))
.map_err(|_e| anyhow!("key was already found, aborting: {}", src_item))?;
}
GraphItem::Item { .. } => {
anyhow::bail!("failed to insert src at item, item doesn't support arbitrary items")
}
}
Ok(())
}
pub fn delete(&mut self, root: &str, path: &[&str]) -> anyhow::Result<()> {
self.take(root, path)
.map(|_| ())
.ok_or(anyhow!("item was not found"))
}
}
impl Display for Engine {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let output = serde_json::to_string_pretty(&self.graph).unwrap();
f.write_str(&output)
}
}
#[cfg(test)]
mod test {
use std::collections::BTreeMap;
use similar_asserts::assert_eq;
use crate::log::{GraphItem, ItemState};
use super::Engine;
#[test]
fn test_can_create_root() {
let mut engine = Engine::default();
engine.create_root("kjuulh").unwrap();
assert_eq!(
r#"{
"kjuulh": {
"type": "user"
}
}"#,
engine.to_string()
);
}
#[test]
fn test_can_create_section() {
let mut engine = Engine::default();
engine.create_root("kjuulh").unwrap();
engine
.create(
"kjuulh",
&["some-section"],
crate::log::GraphItem::Section(BTreeMap::default()),
)
.unwrap();
assert_eq!(
r#"{
"kjuulh": {
"type": "user",
"some-section": {
"type": "section"
}
}
}"#,
engine.to_string()
);
}
#[test]
fn test_can_create_subsection() {
let mut engine = Engine::default();
engine.create_root("kjuulh").unwrap();
engine
.create(
"kjuulh",
&["some-section"],
crate::log::GraphItem::Section(BTreeMap::default()),
)
.unwrap();
engine
.create(
"kjuulh",
&["some-section", "some-sub-section"],
crate::log::GraphItem::Section(BTreeMap::default()),
)
.unwrap();
assert_eq!(
r#"{
"kjuulh": {
"type": "user",
"some-section": {
"type": "section",
"some-sub-section": {
"type": "section"
}
}
}
}"#,
engine.to_string()
);
}
#[test]
fn test_can_create_item() {
let mut engine = Engine::default();
engine.create_root("kjuulh").unwrap();
engine
.create(
"kjuulh",
&["some-item"],
GraphItem::Item {
title: "some-title".to_string(),
description: "some-description".to_string(),
state: ItemState::NotDone,
},
)
.unwrap();
assert_eq!(
r#"{
"kjuulh": {
"type": "user",
"some-item": {
"type": "item",
"title": "some-title",
"description": "some-description",
"state": "not-done"
}
}
}"#,
engine.to_string()
);
}
#[test]
fn test_can_create_nested_item() {
let mut engine = Engine::default();
engine.create_root("kjuulh").unwrap();
engine
.create(
"kjuulh",
&["some-section"],
GraphItem::Section(BTreeMap::default()),
)
.unwrap();
engine
.create(
"kjuulh",
&["some-section", "some-item"],
GraphItem::Item {
title: "some-title".to_string(),
description: "some-description".to_string(),
state: ItemState::NotDone,
},
)
.unwrap();
assert_eq!(
r#"{
"kjuulh": {
"type": "user",
"some-section": {
"type": "section",
"some-item": {
"type": "item",
"title": "some-title",
"description": "some-description",
"state": "not-done"
}
}
}
}"#,
engine.to_string()
);
}
#[test]
fn test_can_create_deeply_nested_item() {
let mut engine = Engine::default();
engine.create_root("kjuulh").unwrap();
engine
.create(
"kjuulh",
&["some-section"],
GraphItem::Section(BTreeMap::default()),
)
.unwrap();
engine
.create(
"kjuulh",
&["some-section", "some-sub-section"],
GraphItem::Section(BTreeMap::default()),
)
.unwrap();
engine
.create(
"kjuulh",
&["some-section", "some-sub-section", "sub-sub-section"],
GraphItem::Section(BTreeMap::default()),
)
.unwrap();
engine
.create(
"kjuulh",
&[
"some-section",
"some-sub-section",
"sub-sub-section",
"some-item",
],
GraphItem::Item {
title: "some-title".to_string(),
description: "some-description".to_string(),
state: ItemState::NotDone,
},
)
.unwrap();
assert_eq!(
r#"{
"kjuulh": {
"type": "user",
"some-section": {
"type": "section",
"some-sub-section": {
"type": "section",
"sub-sub-section": {
"type": "section",
"some-item": {
"type": "item",
"title": "some-title",
"description": "some-description",
"state": "not-done"
}
}
}
}
}
}"#,
engine.to_string()
);
}
#[test]
fn test_can_get_user() {
let engine = get_complex_graph();
let res = engine.get("kjuulh", &[]).unwrap();
let actual = serde_json::to_string_pretty(res).unwrap();
let expected = r#"{
"type": "user",
"some-section": {
"type": "section",
"some-sub-section": {
"type": "section",
"sub-sub-section": {
"type": "section",
"some-item": {
"type": "item",
"title": "some-title",
"description": "some-description",
"state": "not-done"
}
}
}
}
}"#;
assert_eq!(expected, actual)
}
#[test]
fn test_can_get_first_section() {
let engine = get_complex_graph();
let res = engine.get("kjuulh", &["some-section"]).unwrap();
let actual = serde_json::to_string_pretty(res).unwrap();
let expected = r#"{
"type": "section",
"some-sub-section": {
"type": "section",
"sub-sub-section": {
"type": "section",
"some-item": {
"type": "item",
"title": "some-title",
"description": "some-description",
"state": "not-done"
}
}
}
}"#;
assert_eq!(expected, actual)
}
#[test]
fn test_can_get_middle_section() {
let engine = get_complex_graph();
let res = engine
.get("kjuulh", &["some-section", "some-sub-section"])
.unwrap();
let actual = serde_json::to_string_pretty(res).unwrap();
let expected = r#"{
"type": "section",
"sub-sub-section": {
"type": "section",
"some-item": {
"type": "item",
"title": "some-title",
"description": "some-description",
"state": "not-done"
}
}
}"#;
assert_eq!(expected, actual)
}
#[test]
fn test_can_get_last_section() {
let engine = get_complex_graph();
let res = engine
.get(
"kjuulh",
&["some-section", "some-sub-section", "sub-sub-section"],
)
.unwrap();
let actual = serde_json::to_string_pretty(res).unwrap();
let expected = r#"{
"type": "section",
"some-item": {
"type": "item",
"title": "some-title",
"description": "some-description",
"state": "not-done"
}
}"#;
assert_eq!(expected, actual)
}
#[test]
fn test_can_get_item() {
let engine = get_complex_graph();
let res = engine
.get(
"kjuulh",
&[
"some-section",
"some-sub-section",
"sub-sub-section",
"some-item",
],
)
.unwrap();
let actual = serde_json::to_string_pretty(res).unwrap();
let expected = r#"{
"type": "item",
"title": "some-title",
"description": "some-description",
"state": "not-done"
}"#;
assert_eq!(expected, actual)
}
#[test]
fn test_can_move_item() {
let mut engine = get_complex_graph();
engine
.section_move(
"kjuulh",
&[
"some-section",
"some-sub-section",
"sub-sub-section",
"some-item",
],
&["some-section"],
)
.unwrap();
let expected = r#"{
"kjuulh": {
"type": "user",
"some-section": {
"type": "section",
"some-item": {
"type": "item",
"title": "some-title",
"description": "some-description",
"state": "not-done"
},
"some-sub-section": {
"type": "section",
"sub-sub-section": {
"type": "section"
}
}
}
}
}"#;
assert_eq!(expected, engine.to_string())
}
#[test]
fn test_can_move_section() {
let mut engine = get_complex_graph();
engine
.section_move(
"kjuulh",
&["some-section", "some-sub-section", "sub-sub-section"],
&["some-section"],
)
.unwrap();
let expected = r#"{
"kjuulh": {
"type": "user",
"some-section": {
"type": "section",
"some-sub-section": {
"type": "section"
},
"sub-sub-section": {
"type": "section",
"some-item": {
"type": "item",
"title": "some-title",
"description": "some-description",
"state": "not-done"
}
}
}
}
}"#;
assert_eq!(expected, engine.to_string())
}
#[test]
fn test_can_delete_section() {
let mut engine = get_complex_graph();
let res = engine.delete("kjuulh", &["some-section", "some-sub-section"]);
assert!(res.is_ok());
assert_eq!(
r#"{
"kjuulh": {
"type": "user",
"some-section": {
"type": "section"
}
}
}"#,
&engine.to_string()
);
}
#[test]
fn test_can_delete_item() {
let mut engine = get_complex_graph();
let res = engine.delete(
"kjuulh",
&[
"some-section",
"some-sub-section",
"sub-sub-section",
"some-item",
],
);
assert!(res.is_ok());
assert_eq!(
r#"{
"kjuulh": {
"type": "user",
"some-section": {
"type": "section",
"some-sub-section": {
"type": "section",
"sub-sub-section": {
"type": "section"
}
}
}
}
}"#,
&engine.to_string()
);
}
fn get_complex_graph() -> Engine {
let mut engine = Engine::default();
engine.create_root("kjuulh").unwrap();
engine
.create(
"kjuulh",
&["some-section"],
GraphItem::Section(BTreeMap::default()),
)
.unwrap();
engine
.create(
"kjuulh",
&["some-section", "some-sub-section"],
GraphItem::Section(BTreeMap::default()),
)
.unwrap();
engine
.create(
"kjuulh",
&["some-section", "some-sub-section", "sub-sub-section"],
GraphItem::Section(BTreeMap::default()),
)
.unwrap();
engine
.create(
"kjuulh",
&[
"some-section",
"some-sub-section",
"sub-sub-section",
"some-item",
],
GraphItem::Item {
title: "some-title".to_string(),
description: "some-description".to_string(),
state: ItemState::NotDone,
},
)
.unwrap();
engine
}
}

234
crates/hyperlog/src/log.rs Normal file
View File

@ -0,0 +1,234 @@
use std::{
collections::BTreeMap,
ops::{Deref, DerefMut},
};
use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize, PartialEq, Eq, Clone)]
pub enum ItemState {
#[serde(rename = "not-done")]
NotDone,
#[serde(rename = "done")]
Done,
}
#[derive(Deserialize, Serialize, PartialEq, Eq, Clone)]
#[serde(tag = "type")]
pub enum GraphItem {
#[serde(rename = "user")]
User(BTreeMap<String, Box<GraphItem>>),
#[serde(rename = "section")]
Section(BTreeMap<String, Box<GraphItem>>),
#[serde(rename = "item")]
Item {
title: String,
description: String,
state: ItemState,
},
}
impl GraphItem {
pub fn get(&self, path: &[&str]) -> Option<&GraphItem> {
match path.split_first() {
Some((first, rest)) => match self {
GraphItem::User(u) => u.get(*first)?.get(rest),
GraphItem::Section(s) => s.get(*first)?.get(rest),
GraphItem::Item { .. } => None,
},
None => Some(self),
}
}
pub fn get_mut(&mut self, path: &[&str]) -> Option<&mut GraphItem> {
match path.split_first() {
Some((first, rest)) => match self {
GraphItem::User(u) => u.get_mut(*first)?.get_mut(rest),
GraphItem::Section(s) => s.get_mut(*first)?.get_mut(rest),
GraphItem::Item { .. } => None,
},
None => Some(self),
}
}
pub fn take(&mut self, path: &[&str]) -> Option<GraphItem> {
match path.split_first() {
Some((first, rest)) => match self {
GraphItem::User(u) => {
if rest.is_empty() {
let val = u.remove(*first);
val.map(|v| *v)
} else {
u.get_mut(*first)?.take(rest)
}
}
GraphItem::Section(s) => {
if rest.is_empty() {
let val = s.remove(*first);
val.map(|v| *v)
} else {
s.get_mut(*first)?.take(rest)
}
}
GraphItem::Item { .. } => None,
},
None => None,
}
}
}
#[derive(Deserialize, Serialize, PartialEq, Eq, Default)]
pub struct Graph(BTreeMap<String, GraphItem>);
impl Deref for Graph {
type Target = BTreeMap<String, GraphItem>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for Graph {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
#[cfg(test)]
mod test {
use std::collections::{hash_map, BTreeMap, HashMap};
use crate::log::{GraphItem, ItemState};
use super::Graph;
#[test]
fn some_test() {
let test_graph = r#"{
"kjuulh": {
"type": "user"
}
}"#;
let graph: Graph = serde_json::from_str(test_graph).unwrap();
let mut expected = Graph::default();
let user = BTreeMap::new();
expected.insert("kjuulh".into(), GraphItem::User(user));
similar_asserts::assert_eq!(expected, graph);
}
#[test]
fn some_user_test() {
let test_graph = r#"{
"kjuulh": {
"type": "user",
"some-project": {
"type": "section"
}
}
}"#;
let graph: Graph = serde_json::from_str(test_graph).unwrap();
let mut expected = Graph::default();
let mut user = BTreeMap::new();
user.insert(
"some-project".into(),
Box::new(GraphItem::Section(BTreeMap::default())),
);
expected.insert("kjuulh".into(), GraphItem::User(user));
similar_asserts::assert_eq!(expected, graph);
}
#[test]
fn some_section_test() {
let test_graph = r#"{
"kjuulh": {
"type": "user",
"some-project": {
"type": "section",
"some-nested-project": {
"type": "section"
}
}
}
}"#;
let graph: Graph = serde_json::from_str(test_graph).unwrap();
let mut expected = Graph::default();
let mut some_project = BTreeMap::default();
some_project.insert(
"some-nested-project".into(),
Box::new(GraphItem::Section(BTreeMap::default())),
);
let mut user = BTreeMap::new();
user.insert(
"some-project".into(),
Box::new(GraphItem::Section(some_project)),
);
expected.insert("kjuulh".into(), GraphItem::User(user));
similar_asserts::assert_eq!(expected, graph);
}
#[test]
fn some_item_test() {
let test_graph = r#"{
"kjuulh": {
"type": "user",
"some-project": {
"type": "section",
"some-nested-project": {
"type": "section",
"some-todo": {
"type": "item",
"title": "some title",
"description": "some description",
"state": "not-done"
}
}
}
}
}"#;
let graph: Graph = serde_json::from_str(test_graph).unwrap();
let mut expected = Graph::default();
let mut nested_project = BTreeMap::default();
nested_project.insert(
"some-todo".into(),
Box::new(GraphItem::Item {
title: "some title".into(),
description: "some description".into(),
state: ItemState::NotDone,
}),
);
let mut some_project = BTreeMap::default();
some_project.insert(
"some-nested-project".into(),
Box::new(GraphItem::Section(nested_project)),
);
let mut user = BTreeMap::new();
user.insert(
"some-project".into(),
Box::new(GraphItem::Section(some_project)),
);
expected.insert("kjuulh".into(), GraphItem::User(user));
similar_asserts::assert_eq!(
serde_json::to_string_pretty(&expected).unwrap(),
serde_json::to_string_pretty(&graph).unwrap()
);
}
}

109
crates/hyperlog/src/main.rs Normal file
View File

@ -0,0 +1,109 @@
#![feature(map_try_insert)]
use std::{net::SocketAddr, ops::Deref, sync::Arc};
use anyhow::Context;
use axum::extract::MatchedPath;
use axum::http::Request;
use axum::routing::get;
use axum::Router;
use clap::{Parser, Subcommand};
use sqlx::{Pool, Postgres};
use tower_http::trace::TraceLayer;
pub mod engine;
pub mod log;
#[derive(Parser)]
#[command(author, version, about, long_about = None, subcommand_required = true)]
struct Command {
#[command(subcommand)]
command: Option<Commands>,
}
#[derive(Subcommand)]
enum Commands {
Serve {
#[arg(env = "SERVICE_HOST", long, default_value = "127.0.0.1:3000")]
host: SocketAddr,
},
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
dotenv::dotenv().ok();
tracing_subscriber::fmt::init();
let cli = Command::parse();
if let Some(Commands::Serve { host }) = cli.command {
tracing::info!("Starting service");
let state = SharedState(Arc::new(State::new().await?));
let app = Router::new()
.route("/", get(root))
.with_state(state.clone())
.layer(
TraceLayer::new_for_http().make_span_with(|request: &Request<_>| {
// Log the matched route's path (with placeholders not filled in).
// Use request.uri() or OriginalUri if you want the real path.
let matched_path = request
.extensions()
.get::<MatchedPath>()
.map(MatchedPath::as_str);
tracing::info_span!(
"http_request",
method = ?request.method(),
matched_path,
some_other_field = tracing::field::Empty,
)
}), // ...
);
tracing::info!("listening on {}", host);
let listener = tokio::net::TcpListener::bind(host).await.unwrap();
axum::serve(listener, app.into_make_service())
.await
.unwrap();
}
Ok(())
}
async fn root() -> &'static str {
"Hello, hyperlog!"
}
#[derive(Clone)]
pub struct SharedState(Arc<State>);
impl Deref for SharedState {
type Target = Arc<State>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
pub struct State {
pub db: Pool<Postgres>,
}
impl State {
pub async fn new() -> anyhow::Result<Self> {
let db = sqlx::PgPool::connect(
&std::env::var("DATABASE_URL").context("DATABASE_URL is not set")?,
)
.await?;
sqlx::migrate!("migrations/crdb")
.set_locking(false)
.run(&db)
.await?;
let _ = sqlx::query("SELECT 1;").fetch_one(&db).await?;
Ok(Self { db })
}
}

21
cuddle.yaml Normal file
View File

@ -0,0 +1,21 @@
# yaml-language-server: $schema=https://git.front.kjuulh.io/kjuulh/cuddle/raw/branch/main/schemas/base.json
base: "git@git.front.kjuulh.io:kjuulh/cuddle-rust-service-plan.git"
vars:
service: "hyperlog"
registry: kasperhermansen
clusters:
clank-prod:
replicas: "3"
namespace: prod
deployment:
registry: git@git.front.kjuulh.io:kjuulh/clank-clusters
env:
prod:
clusters:
- clank-prod

3
renovate.json Normal file
View File

@ -0,0 +1,3 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json"
}

View File

@ -0,0 +1,15 @@
version: "3"
services:
crdb:
restart: 'always'
image: 'cockroachdb/cockroach:v23.1.14'
command: 'start-single-node --advertise-addr 0.0.0.0 --insecure'
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8080/health?ready=1"]
interval: '10s'
timeout: '30s'
retries: 5
start_period: '20s'
ports:
- 8080:8080
- '26257:26257'