From 6eae602bd67698436b70b619708ab0c9c16b8cac Mon Sep 17 00:00:00 2001 From: Ziyang Hu Date: Mon, 31 Oct 2022 12:31:43 +0800 Subject: [PATCH] prepare cozo for crates.io --- src/bin/cozoserver.rs | 7 ++----- src/lib.rs | 4 +++- src/runtime/db.rs | 6 +++++- tests/air_routes.rs | 36 +++++++++++++++++------------------- 4 files changed, 27 insertions(+), 26 deletions(-) diff --git a/src/bin/cozoserver.rs b/src/bin/cozoserver.rs index bf651ce0..2ec28011 100644 --- a/src/bin/cozoserver.rs +++ b/src/bin/cozoserver.rs @@ -17,7 +17,7 @@ use rand::Rng; use rouille::{router, try_or_400, Request, Response}; use serde_json::json; -use cozo::{Db, DbBuilder}; +use cozo::Db; #[derive(Parser, Debug)] #[clap(version, about, long_about = None)] @@ -42,10 +42,7 @@ fn main() { eprintln!("{}", SECURITY_WARNING); } - let builder = DbBuilder::default() - .path(&args.path) - .create_if_missing(true); - let db = Db::build(builder).unwrap(); + let db = Db::new(args.path.as_str()).unwrap(); let mut path_buf = PathBuf::from(&args.path); path_buf.push("auth.txt"); diff --git a/src/lib.rs b/src/lib.rs index 8930c068..23e7b0f7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,9 @@ * Copyright 2022, The Cozo Project Authors. Licensed under AGPL-3 or later. */ +//! This crate provides the core functionalities of [CozoDB](https://github.com/cozodb/cozo). +//! It may be used directly for embedding CozoDB in other applications. + #![warn(rust_2018_idioms, future_incompatible)] #![warn(missing_docs)] #![allow(clippy::type_complexity)] @@ -9,7 +12,6 @@ pub use miette::Error; -pub use cozorocks::DbBuilder; pub use runtime::db::Db; // #[cfg(not(target_env = "msvc"))] diff --git a/src/runtime/db.rs b/src/runtime/db.rs index 07258367..3d4c9698 100644 --- a/src/runtime/db.rs +++ b/src/runtime/db.rs @@ -59,6 +59,7 @@ pub(crate) struct DbManifest { const CURRENT_STORAGE_VERSION: u64 = 1; +/// The database object of Cozo. pub struct Db { db: RocksDb, relation_store_id: Arc, @@ -78,7 +79,9 @@ impl Debug for Db { struct BadDbInit(#[help] String); impl Db { - pub fn build(builder: DbBuilder<'_>) -> Result { + /// Creates a database object. + pub fn new(path: impl AsRef) -> Result { + let builder = DbBuilder::default().path(path.as_ref()); let path = builder.opts.db_path; fs::create_dir_all(path) .map_err(|err| BadDbInit(format!("cannot create directory {}: {}", path, err)))?; @@ -161,6 +164,7 @@ impl Db { }; Ok(ret) } + /// Run the CozoScript passed in. The `params` argument is a map of parameters. pub fn run_script( &self, payload: &str, diff --git a/tests/air_routes.rs b/tests/air_routes.rs index 231bdc19..7520e515 100644 --- a/tests/air_routes.rs +++ b/tests/air_routes.rs @@ -10,15 +10,13 @@ use lazy_static::lazy_static; use serde_json::json; use cozo::Db; -use cozorocks::DbBuilder; lazy_static! { static ref TEST_DB: Db = { let creation = Instant::now(); let path = "_test_air_routes"; _ = std::fs::remove_dir_all(path); - let builder = DbBuilder::default().path(path).create_if_missing(true); - let db = Db::build(builder).unwrap(); + let db = Db::new(path).unwrap(); dbg!(creation.elapsed()); let init = Instant::now(); @@ -131,7 +129,9 @@ lazy_static! { } fn check_db() { - TEST_DB.get_session_id(); + let _ = TEST_DB + .run_script("?[a] <- [[1]]", &Default::default()) + .unwrap(); } #[test] @@ -1739,11 +1739,7 @@ fn great_circle_distance() { ) .unwrap(); let rows = res.get("rows").unwrap(); - assert_eq!( - *rows, - serde_json::Value::from_str(r#"[[1.0]]"#) - .unwrap() - ); + assert_eq!(*rows, serde_json::Value::from_str(r#"[[1.0]]"#).unwrap()); dbg!(great_circle_distance.elapsed()); } @@ -1770,8 +1766,7 @@ fn aus_to_edi() { let rows = res.get("rows").unwrap(); assert_eq!( *rows, - serde_json::Value::from_str(r#"[[["AUS", "BOS", "EDI"]]]"#) - .unwrap() + serde_json::Value::from_str(r#"[[["AUS", "BOS", "EDI"]]]"#).unwrap() ); dbg!(aus_to_edi.elapsed()); } @@ -1799,7 +1794,8 @@ fn reachable_from_lhr() { let rows = res.get("rows").unwrap(); assert_eq!( *rows, - serde_json::Value::from_str(r#"[ + serde_json::Value::from_str( + r#"[ [8,["LHR","YYZ","YTS","YMO","YFA","ZKE","YAT","YPO"]], [7,["LHR","AUH","BNE","ISA","BQL","BEU","BVI"]], [7,["LHR","AUH","BNE","WTB","SGO","CMA","XTG"]], @@ -1810,13 +1806,13 @@ fn reachable_from_lhr() { [7,["LHR","DEN","ANC","BET","NME","TNK","WWT"]], [7,["LHR","KEF","GOH","JAV","JUV","NAQ","THU"]], [7,["LHR","YUL","YGL","YPX","AKV","YIK","YZG"]]] - "#) - .unwrap() + "# + ) + .unwrap() ); dbg!(reachable_from_lhr.elapsed()); } - #[test] fn furthest_from_lhr() { check_db(); @@ -1842,14 +1838,16 @@ fn furthest_from_lhr() { let rows = res.get("rows").unwrap(); assert_eq!( *rows, - serde_json::Value::from_str(r#"[ + serde_json::Value::from_str( + r#"[ [12922.0,["LHR","JNB","HLE","ASI","BZZ"]],[12093.0,["LHR","PVG","CHC","IVC"]], [12015.0,["LHR","NRT","AKL","WLG","TIU"]],[12009.0,["LHR","PVG","CHC","DUD"]], [11910.0,["LHR","NRT","AKL","WLG","WSZ"]],[11900.0,["LHR","PVG","CHC","HKK"]], [11805.0,["LHR","PVG","CHC"]],[11766.0,["LHR","PVG","BNE","ZQN"]], [11758.0,["LHR","NRT","AKL","BHE"]],[11751.0,["LHR","NRT","AKL","NSN"]]] - "#) - .unwrap() + "# + ) + .unwrap() ); dbg!(furthest_from_lhr.elapsed()); -} \ No newline at end of file +}