prepare cozo for crates.io

main
Ziyang Hu 2 years ago
parent 12f6fa50cd
commit 6eae602bd6

@ -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");

@ -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"))]

@ -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<AtomicU64>,
@ -78,7 +79,9 @@ impl Debug for Db {
struct BadDbInit(#[help] String);
impl Db {
pub fn build(builder: DbBuilder<'_>) -> Result<Self> {
/// Creates a database object.
pub fn new(path: impl AsRef<str>) -> Result<Self> {
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,

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

Loading…
Cancel
Save