diff --git a/Cargo.lock b/Cargo.lock index 260da48c..d5ee992c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -591,6 +591,27 @@ dependencies = [ "uuid", ] +[[package]] +name = "cozo-bin" +version = "0.5.0" +dependencies = [ + "chrono", + "clap 4.0.32", + "cozo", + "ctrlc", + "env_logger", + "log", + "miette", + "minreq", + "prettytable", + "rand 0.8.5", + "rouille", + "rustyline", + "serde", + "serde_derive", + "serde_json", +] + [[package]] name = "cozo-lib-wasm" version = "0.5.0" @@ -664,27 +685,6 @@ dependencies = [ "zstd-sys", ] -[[package]] -name = "cozoserver" -version = "0.5.0" -dependencies = [ - "chrono", - "clap 4.0.32", - "cozo", - "ctrlc", - "env_logger", - "log", - "miette", - "minreq", - "prettytable", - "rand 0.8.5", - "rouille", - "rustyline", - "serde", - "serde_derive", - "serde_json", -] - [[package]] name = "cpufeatures" version = "0.2.5" diff --git a/Cargo.toml b/Cargo.toml index 73f1bd21..6dff8896 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ members = [ "cozo-core", "cozorocks", - "cozoserver", + "cozo-bin", "cozo-lib-c", "cozo-lib-java", "cozo-lib-wasm", diff --git a/cozoserver/Cargo.toml b/cozo-bin/Cargo.toml similarity index 98% rename from cozoserver/Cargo.toml rename to cozo-bin/Cargo.toml index c46aaeb3..0bfa8d8c 100644 --- a/cozoserver/Cargo.toml +++ b/cozo-bin/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "cozoserver" +name = "cozo-bin" version = "0.5.0" edition = "2021" license = "MPL-2.0" diff --git a/cozoserver/README-zh.md b/cozo-bin/README-zh.md similarity index 95% rename from cozoserver/README-zh.md rename to cozo-bin/README-zh.md index 47ed3cef..76e4de43 100644 --- a/cozoserver/README-zh.md +++ b/cozo-bin/README-zh.md @@ -1,8 +1,8 @@ -# CozoServer(独立服务) +# Cozo(独立程序) [![server](https://img.shields.io/github/v/release/cozodb/cozo)](https://github.com/cozodb/cozo/releases) -本文叙述的是如何安装设置服务程序本身。有关如何使用 CozoDB(CozoScript)的信息,见 [文档](https://docs.cozodb.org/zh_CN/latest/index.html) 。 +本文叙述的是如何安装设置 Cozo 的独立程序本身。有关如何使用 CozoDB(CozoScript)的信息,见 [文档](https://docs.cozodb.org/zh_CN/latest/index.html) 。 ## 下载 diff --git a/cozoserver/README.md b/cozo-bin/README.md similarity index 97% rename from cozoserver/README.md rename to cozo-bin/README.md index 37f6e432..75d28eea 100644 --- a/cozoserver/README.md +++ b/cozo-bin/README.md @@ -1,8 +1,8 @@ -# CozoServer +# Cozo (standalone executable) [![server](https://img.shields.io/github/v/release/cozodb/cozo)](https://github.com/cozodb/cozo/releases) -This document describes how to set up cozoserver. +This document describes how to set up cozo (standalone executable). To learn how to use CozoDB (CozoScript), read the [docs](https://docs.cozodb.org/en/latest/index.html). ## Download diff --git a/cozoserver/src/main.rs b/cozo-bin/src/main.rs similarity index 78% rename from cozoserver/src/main.rs rename to cozo-bin/src/main.rs index c0ec1dae..9b610846 100644 --- a/cozoserver/src/main.rs +++ b/cozo-bin/src/main.rs @@ -13,7 +13,7 @@ use std::net::Ipv6Addr; use std::process::exit; use std::str::FromStr; -use clap::Parser; +use clap::{Args, Parser, Subcommand}; use env_logger::Env; use log::{error, info}; use rand::Rng; @@ -26,9 +26,25 @@ use crate::repl::repl_main; mod repl; -#[derive(Parser, Debug)] -#[clap(version, about, long_about = None)] -struct Args { +#[derive(Parser)] +#[command(author, version, about, long_about = None)] +#[command(propagate_version = true)] +struct AppArgs { + #[command(subcommand)] + command: Commands, +} + +#[derive(Subcommand)] +enum Commands { + /// + Server(Server), + Client(Client), + Repl(Repl), + Restore(Restore), +} + +#[derive(Args, Debug)] +struct Repl { /// Database engine, can be `mem`, `sqlite`, `rocksdb` and others. #[clap(short, long, default_value_t = String::from("mem"))] engine: String, @@ -37,18 +53,50 @@ struct Args { #[clap(short, long, default_value_t = String::from("cozo.db"))] path: String, - /// Restore from the specified backup before starting the server - #[clap(long)] - restore: Option, - /// Extra config in JSON format #[clap(short, long, default_value_t = String::from("{}"))] config: String, +} + +#[derive(Args, Debug)] +struct Client { + #[clap(default_value_t = String::from("http://127.0.0.1:9070"))] + address: String, + #[clap(short, long, default_value_t = String::from(""))] + auth: String, +} - /// When on, start REPL instead of starting a webserver +#[derive(Args, Debug)] +struct Restore { + /// Path of the backup file to restore from, must be a SQLite-backed backup file. + from: String, + /// Path of the database to restore into + to: String, + /// Database engine for the database to restore into, can be `mem`, `sqlite`, `rocksdb` and others. #[clap(short, long)] - repl: bool, + engine: String, +} + +#[derive(Args, Debug)] +struct Server { + /// Database engine, can be `mem`, `sqlite`, `rocksdb` and others. + #[clap(short, long, default_value_t = String::from("mem"))] + engine: String, + + /// Path to the directory to store the database + #[clap(short, long, default_value_t = String::from("cozo.db"))] + path: String, + + // Restore from the specified backup before starting the server + // #[clap(long)] + // restore: Option, + /// Extra config in JSON format + #[clap(short, long, default_value_t = String::from("{}"))] + config: String, + // When on, start REPL instead of starting a webserver + // #[clap(short, long)] + // repl: bool, /// Address to bind the service to #[clap(short, long, default_value_t = String::from("127.0.0.1"))] bind: String, @@ -72,7 +120,18 @@ macro_rules! check_auth { } fn main() { - let args = Args::parse(); + let args = match AppArgs::parse().command { + Commands::Server(s) => s, + Commands::Client(_) => { + todo!() + } + Commands::Repl(_) => { + todo!() + } + Commands::Restore(_) => { + todo!() + } + }; if args.bind != "127.0.0.1" { eprintln!("{SECURITY_WARNING}"); } @@ -84,37 +143,37 @@ fn main() { ) .unwrap(); - if let Some(restore_path) = &args.restore { - db.restore_backup(restore_path).unwrap(); - } - - if args.repl { - let db_copy = db.clone(); - ctrlc::set_handler(move || { - let running = db_copy - .run_script("::running", Default::default()) - .expect("Cannot determine running queries"); - for row in running.rows { - let id = row.into_iter().next().unwrap(); - eprintln!("Killing running query {id}"); - db_copy - .run_script("::kill $id", BTreeMap::from([("id".to_string(), id)])) - .expect("Cannot kill process"); - } - }) - .expect("Error setting Ctrl-C handler"); - - if let Err(e) = repl_main(db) { - eprintln!("{e}"); - exit(-1); - } - } else { - env_logger::Builder::from_env(Env::default().default_filter_or("info")).init(); - server_main(args, db) - } + // if let Some(restore_path) = &args.restore { + // db.restore_backup(restore_path).unwrap(); + // } + + // if args.repl { + // let db_copy = db.clone(); + // ctrlc::set_handler(move || { + // let running = db_copy + // .run_script("::running", Default::default()) + // .expect("Cannot determine running queries"); + // for row in running.rows { + // let id = row.into_iter().next().unwrap(); + // eprintln!("Killing running query {id}"); + // db_copy + // .run_script("::kill $id", BTreeMap::from([("id".to_string(), id)])) + // .expect("Cannot kill process"); + // } + // }) + // .expect("Error setting Ctrl-C handler"); + // + // if let Err(e) = repl_main(db) { + // eprintln!("{e}"); + // exit(-1); + // } + // } else { + env_logger::Builder::from_env(Env::default().default_filter_or("info")).init(); + server_main(args, db) + // } } -fn server_main(args: Args, db: DbInstance) { +fn server_main(args: Server, db: DbInstance) { let conf_path = format!("{}.{}.cozo_auth", args.path, args.engine); let auth_guard = match fs::read_to_string(&conf_path) { Ok(s) => s.trim().to_string(), diff --git a/cozoserver/src/repl.rs b/cozo-bin/src/repl.rs similarity index 100% rename from cozoserver/src/repl.rs rename to cozo-bin/src/repl.rs diff --git a/cozo-core/src/lib.rs b/cozo-core/src/lib.rs index 926d8e45..d915e43c 100644 --- a/cozo-core/src/lib.rs +++ b/cozo-core/src/lib.rs @@ -74,7 +74,6 @@ pub use crate::runtime::callback::CallbackOp; pub use crate::runtime::db::Poison; pub use crate::runtime::db::TransactionPayload; -#[cfg(not(target_arch = "wasm32"))] pub(crate) mod data; pub(crate) mod fixed_rule; pub(crate) mod parse; diff --git a/cozo-core/src/runtime/callback.rs b/cozo-core/src/runtime/callback.rs index 9b1886d5..faa54649 100644 --- a/cozo-core/src/runtime/callback.rs +++ b/cozo-core/src/runtime/callback.rs @@ -42,7 +42,7 @@ impl CallbackOp { } } -#[cfg(not(target_arch = "wasm32"))] +#[allow(dead_code)] pub struct CallbackDeclaration { pub(crate) dependent: SmartString, pub(crate) sender: Sender<(CallbackOp, NamedRows, NamedRows)>, @@ -51,7 +51,7 @@ pub struct CallbackDeclaration { pub(crate) type CallbackCollector = BTreeMap, Vec<(CallbackOp, NamedRows, NamedRows)>>; -#[cfg(not(target_arch = "wasm32"))] +#[allow(dead_code)] pub(crate) type EventCallbackRegistry = ( BTreeMap, BTreeMap, BTreeSet>, diff --git a/cozo-core/src/runtime/db.rs b/cozo-core/src/runtime/db.rs index 83467122..81fdfa98 100644 --- a/cozo-core/src/runtime/db.rs +++ b/cozo-core/src/runtime/db.rs @@ -20,6 +20,7 @@ use std::thread; #[allow(unused_imports)] use std::time::{Duration, SystemTime, UNIX_EPOCH}; +#[allow(unused_imports)] use crossbeam::channel::{bounded, unbounded, Receiver, Sender}; use crossbeam::sync::ShardedLock; use either::{Left, Right}; @@ -45,6 +46,7 @@ use crate::query::ra::{ FilteredRA, InnerJoin, NegJoin, RelAlgebra, ReorderRA, StoredRA, StoredWithValidityRA, TempStoreRA, UnificationRA, }; +#[allow(unused_imports)] use crate::runtime::callback::{ CallbackCollector, CallbackDeclaration, CallbackOp, EventCallbackRegistry, }; diff --git a/scripts/build-release-linux.sh b/scripts/build-release-linux.sh index d640c5bf..9d09ca0c 100755 --- a/scripts/build-release-linux.sh +++ b/scripts/build-release-linux.sh @@ -14,8 +14,8 @@ cp target/wheels/*.whl release/ for TARGET in aarch64-unknown-linux-gnu x86_64-unknown-linux-gnu; do # standalone, c, java, nodejs - CARGO_PROFILE_RELEASE_LTO=fat cross build --release -p cozoserver -p cozo_c -p cozo_java -p cozo-node -F compact -F storage-rocksdb --target $TARGET - cp target/$TARGET/release/cozoserver release/cozoserver-$VERSION-$TARGET # standalone + CARGO_PROFILE_RELEASE_LTO=fat cross build --release -p cozo-bin -p cozo_c -p cozo_java -p cozo-node -F compact -F storage-rocksdb --target $TARGET + cp target/$TARGET/release/cozo-bin release/cozo-$VERSION-$TARGET # standalone cp target/$TARGET/release/libcozo_c.a release/libcozo_c-$VERSION-$TARGET.a # c static cp target/$TARGET/release/libcozo_c.so release/libcozo_c-$VERSION-$TARGET.so # c dynamic cp target/$TARGET/release/libcozo_java.so release/libcozo_java-$VERSION-$TARGET.so # java @@ -24,14 +24,14 @@ for TARGET in aarch64-unknown-linux-gnu x86_64-unknown-linux-gnu; do done for TARGET in x86_64-unknown-linux-gnu; do - PROTOC=$PWD/tools/protoc CARGO_PROFILE_RELEASE_LTO=fat cross build --release -p cozoserver \ + PROTOC=$PWD/tools/protoc CARGO_PROFILE_RELEASE_LTO=fat cross build --release -p cozo-bin \ -F compact -F storage-rocksdb -F storage-tikv -F storage-sled --target $TARGET - cp target/$TARGET/release/cozoserver release/cozoserver_all-$VERSION-$TARGET # standalone + cp target/$TARGET/release/cozo-bin release/cozo_all-$VERSION-$TARGET # standalone done for TARGET in aarch64-unknown-linux-musl x86_64-unknown-linux-musl; do - CARGO_PROFILE_RELEASE_LTO=fat cross build --release -p cozoserver -p cozo_c -F compact -F storage-rocksdb --target $TARGET - cp target/$TARGET/release/cozoserver release/cozoserver-$VERSION-$TARGET # standalone + CARGO_PROFILE_RELEASE_LTO=fat cross build --release -p cozo-bin -p cozo_c -F compact -F storage-rocksdb --target $TARGET + cp target/$TARGET/release/cozo-bin release/cozo-$VERSION-$TARGET # standalone cp target/$TARGET/release/libcozo_c.a release/libcozo_c-$VERSION-$TARGET.a # c static done diff --git a/scripts/build-release-mac.sh b/scripts/build-release-mac.sh index 417ae579..caf2fe57 100755 --- a/scripts/build-release-mac.sh +++ b/scripts/build-release-mac.sh @@ -10,8 +10,8 @@ mkdir -p release for TARGET in aarch64-apple-darwin x86_64-apple-darwin; do # standalone, c, java, nodejs - CARGO_PROFILE_RELEASE_LTO=fat cargo build --release -p cozoserver -p cozo_c -p cozo_java -p cozo-node -F compact -F storage-rocksdb --target $TARGET - cp target/$TARGET/release/cozoserver release/cozoserver-$VERSION-$TARGET # standalone + CARGO_PROFILE_RELEASE_LTO=fat cargo build --release -p cozo-bin -p cozo_c -p cozo_java -p cozo-node -F compact -F storage-rocksdb --target $TARGET + cp target/$TARGET/release/cozo-bin release/cozo-$VERSION-$TARGET # standalone cp target/$TARGET/release/libcozo_c.a release/libcozo_c-$VERSION-$TARGET.a # c static cp target/$TARGET/release/libcozo_c.dylib release/libcozo_c-$VERSION-$TARGET.dylib # c dynamic cp target/$TARGET/release/libcozo_java.dylib release/libcozo_java-$VERSION-$TARGET.dylib # java @@ -33,9 +33,9 @@ cd .. # with TiKV for TARGET in aarch64-apple-darwin x86_64-apple-darwin; do - CARGO_PROFILE_RELEASE_LTO=fat cargo build --release -p cozoserver \ + CARGO_PROFILE_RELEASE_LTO=fat cargo build --release -p cozo-bin \ -F compact -F storage-rocksdb -F storage-tikv -F storage-sled --target $TARGET - cp target/$TARGET/release/cozoserver release/cozoserver_all-$VERSION-$TARGET # standalone + cp target/$TARGET/release/cozo-bin release/cozo_all-$VERSION-$TARGET # standalone done # WASM diff --git a/scripts/build-release-windows.ps1 b/scripts/build-release-windows.ps1 index e630a819..ccd0ec21 100644 --- a/scripts/build-release-windows.ps1 +++ b/scripts/build-release-windows.ps1 @@ -11,8 +11,8 @@ cd cozo-lib-python maturin build -F compact -F storage-rocksdb --release --strip --target $TARGET cd .. -cargo build --release -p cozoserver -p cozo_c -p cozo_java -p cozo-node -F compact -F storage-rocksdb --target $TARGET -cp target/$TARGET/release/cozoserver.exe release/cozoserver-$VERSION-$TARGET.exe # standalone +cargo build --release -p cozo-bin -p cozo_c -p cozo_java -p cozo-node -F compact -F storage-rocksdb --target $TARGET +cp target/$TARGET/release/cozo-bin.exe release/cozo-$VERSION-$TARGET.exe # standalone cp target/$TARGET/release/cozo_c.lib release/libcozo_c-$VERSION-$TARGET.lib # c static cp target/$TARGET/release/cozo_c.dll release/libcozo_c-$VERSION-$TARGET.dll # c dynamic cp target/$TARGET/release/cozo_java.dll release/libcozo_java-$VERSION-$TARGET.dll # java @@ -21,8 +21,8 @@ cp target/$TARGET/release/cozo_node.dll release/libcozo_node-$VERSION-$TARGET.dl cp target/wheels/*.whl release/ $TARGET = "x86_64-pc-windows-gnu" -cargo build --release -p cozoserver -p cozo_c -p cozo_java -p cozo-node -F compact -F storage-rocksdb --target $TARGET -cp target/$TARGET/release/cozoserver.exe release/cozoserver-$VERSION-$TARGET.exe # standalone +cargo build --release -p cozo-bin -p cozo_c -p cozo_java -p cozo-node -F compact -F storage-rocksdb --target $TARGET +cp target/$TARGET/release/cozo-bin.exe release/cozo-$VERSION-$TARGET.exe # standalone cp target/$TARGET/release/libcozo_c.a release/libcozo_c-$VERSION-$TARGET.a # c static cp target/$TARGET/release/cozo_c.dll release/libcozo_c-$VERSION-$TARGET.dll # c dynamic cp target/$TARGET/release/cozo_java.dll release/libcozo_java-$VERSION-$TARGET.dll # java