diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b53122f..f6c2cbb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -34,6 +34,8 @@ jobs: profile: minimal toolchain: nightly-2023-04-07 - uses: actions/checkout@v2 + - name: Install Protoc + uses: arduino/setup-protoc@v2 - name: Build uses: actions-rs/cargo@v1 with: @@ -48,6 +50,8 @@ jobs: profile: minimal toolchain: nightly-2023-04-07 - uses: actions/checkout@v2 + - name: Install Protoc + uses: arduino/setup-protoc@v2 - name: Test uses: actions-rs/cargo@v1 with: diff --git a/Cargo.toml b/Cargo.toml index 1976b38..e17aa96 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ repository = "https://gitee.com/Kould/KipDB" readme = "README.md" keywords = ["async", "KV-Store", "Persistence"] categories = ["development-tools", "database"] +default-run = "server" [[bin]] name = "cli" diff --git a/Dockerfile b/Dockerfile index a864b19..05b89d1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,9 +2,10 @@ FROM rust:1.62 as builder ADD ./src ./builder/src ADD ./Cargo.toml ./builder/Cargo.toml -ADD ./.cargo ./builder/.cargo ADD ./build.rs ./builder/build.rs +RUN apt update && apt install -y protobuf-compiler + WORKDIR /builder RUN rustup default nightly diff --git a/build.rs b/build.rs index 1e5220f..86d86a7 100644 --- a/build.rs +++ b/build.rs @@ -1,4 +1,6 @@ fn main() -> Result<(), Box> { - tonic_build::compile_protos("src/proto/kipdb.proto")?; + tonic_build::configure() + .protoc_arg("--experimental_allow_proto3_optional") + .compile(&["src/proto/kipdb.proto"], &["src/proto"])?; Ok(()) } diff --git a/examples/simple_crud.rs b/examples/simple_crud.rs index 8afc37f..a48e9bf 100644 --- a/examples/simple_crud.rs +++ b/examples/simple_crud.rs @@ -28,25 +28,5 @@ async fn main() -> Result<(), KernelError> { kip_storage.flush().await?; - let join_cmd_1 = vec![ - CommandData::set(b"moon".to_vec(), b"star".to_vec()), - // CommandData::remove(b"apple".to_vec()), - ]; - // TODO need refactor join - // println!( - // "Join 1: {:?} -> {:?}", - // join_cmd_1.clone(), - // kip_storage.join(join_cmd_1).await? - // ); - let join_cmd_2 = vec![ - CommandData::get(b"moon".to_vec()), - CommandData::get(b"apple".to_vec()), - ]; - // println!( - // "Join 2: {:?} -> {:?}", - // join_cmd_2.clone(), - // kip_storage.join(join_cmd_2).await? - // ); - Ok(()) } diff --git a/src/bin/cli.rs b/src/bin/cli.rs index d812177..3a2e56c 100644 --- a/src/bin/cli.rs +++ b/src/bin/cli.rs @@ -1,15 +1,13 @@ -use clap::Parser; +use clap::{Parser, Subcommand}; use itertools::Itertools; -use kip_db::cmd::Command; use kip_db::server::client::KipdbClient; use kip_db::server::client::Result; use kip_db::DEFAULT_PORT; +use serde::{Deserialize, Serialize}; use tracing::{error, info}; const DONE: &str = "Done!"; -const UNKNOWN_COMMAND: &str = "Unknown Command!"; - #[derive(Parser, Debug)] #[clap(name = "KipDB-Cli", version, author, about = "Issue KipDB Commands")] struct Cli { @@ -95,7 +93,6 @@ async fn main() -> Result<()> { client.flush().await?; DONE.to_string() } - _ => UNKNOWN_COMMAND.to_string(), }; info!("{line}"); @@ -110,3 +107,32 @@ fn encode(value: &String) -> Vec { fn decode(value: Vec) -> String { bincode::deserialize(value.as_slice()).unwrap() } + +#[derive(Serialize, Deserialize, Debug, Subcommand)] +#[non_exhaustive] +pub enum Command { + Set { + key: String, + value: String, + }, + Remove { + key: String, + }, + Get { + key: String, + }, + Flush, + + #[clap(about = "cli.exe batch-set [keys]... [values]...")] + BatchSet { + batch: Vec, + }, + BatchRemove { + keys: Vec, + }, + BatchGet { + keys: Vec, + }, + SizeOfDisk, + Len, +} diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs deleted file mode 100644 index 2fbec0e..0000000 --- a/src/cmd/mod.rs +++ /dev/null @@ -1,68 +0,0 @@ -use clap::Subcommand; -use serde::{Deserialize, Serialize}; - -#[derive(Serialize, Deserialize, Debug, Subcommand)] -#[non_exhaustive] -pub enum Command { - Set { - key: String, - value: String, - }, - Remove { - key: String, - }, - Get { - key: String, - }, - Flush, - - #[clap(about = "cli.exe batch-set [keys]... [values]...")] - BatchSet { - batch: Vec, - }, - BatchRemove { - keys: Vec, - }, - BatchGet { - keys: Vec, - }, - SizeOfDisk, - Len, -} - -impl Command { - #[inline] - pub fn set(key: String, value: String) -> Command { - Command::Set { key, value } - } - - #[inline] - pub fn remove(key: String) -> Command { - Command::Remove { key } - } - - #[inline] - pub fn get(key: String) -> Command { - Command::Get { key } - } - - #[inline] - pub fn flush() -> Command { - Command::Flush - } - - #[inline] - pub fn batch_set(batch: Vec) -> Command { - Command::BatchSet { batch } - } - - #[inline] - pub fn batch_remove(keys: Vec) -> Command { - Command::BatchRemove { keys } - } - - #[inline] - pub fn batch_get(keys: Vec) -> Command { - Command::BatchGet { keys } - } -} diff --git a/src/kernel/mod.rs b/src/kernel/mod.rs index c4d0ec8..6e98016 100644 --- a/src/kernel/mod.rs +++ b/src/kernel/mod.rs @@ -1,7 +1,6 @@ use async_trait::async_trait; use bytes::Bytes; use fslock::LockFile; -use itertools::Itertools; use serde::{Deserialize, Serialize}; use std::ffi::OsStr; use std::path::Path; @@ -44,11 +43,6 @@ pub trait Storage: Send + Sync + 'static + Sized { /// 通过键删除键值对 async fn remove(&self, key: &[u8]) -> Result<()>; - /// 并行批量执行 - /// TODO need refactor - // #[inline] - // async fn join(&self, vec_cmd: Vec) -> Result>>> {} - async fn size_of_disk(&self) -> Result; async fn len(&self) -> Result; @@ -64,55 +58,6 @@ pub enum CommandData { Get { key: Vec }, } -pub(crate) struct ByteUtils; - -impl ByteUtils { - /// 从u8的slice中前四位获取数据的长度 - pub(crate) fn from_4_bit_with_start(len_u8: &[u8]) -> usize { - usize::from(len_u8[3]) - | usize::from(len_u8[2]) << 8 - | usize::from(len_u8[1]) << 16 - | usize::from(len_u8[0]) << 24 - } - - /// 返回字节数组Vec与对应的字节数组长度Vec - /// - /// bytes必须使用'ByteUtils::tag_with_head'进行标记 - pub(crate) fn sharding_tag_bytes(bytes: &[u8]) -> Vec<&[u8]> { - let mut vec_cmd_u8 = Vec::new(); - let mut last_pos = 0; - - if bytes.len() < 4 { - return vec_cmd_u8; - } - - loop { - let pos = last_pos + 4; - if pos >= bytes.len() { - break; - } - let len_u8 = &bytes[last_pos..pos]; - let len = Self::from_4_bit_with_start(len_u8); - if len < 1 || len > bytes.len() { - break; - } - - last_pos += len + 4; - vec_cmd_u8.push(&bytes[pos..last_pos]); - } - - vec_cmd_u8 - } - - /// 标记bytes以支持'ByteUtils::sharding_tag_bytes'方法 - pub(crate) fn tag_with_head(mut bytes: Vec) -> Vec { - let i = bytes.len(); - let mut vec_head = vec![(i >> 24) as u8, (i >> 16) as u8, (i >> 8) as u8, i as u8]; - vec_head.append(&mut bytes); - vec_head - } -} - impl CommandData { #[inline] pub fn get_key(&self) -> &Vec { diff --git a/src/lib.rs b/src/lib.rs index eae9365..0297206 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,7 +2,7 @@ #![feature(cursor_remaining)] #![feature(slice_pattern)] #![feature(bound_map)] -pub mod cmd; + pub mod config; pub mod error; pub mod kernel; diff --git a/src/server/server.rs b/src/server/server.rs index 9771a79..0fd914f 100644 --- a/src/server/server.rs +++ b/src/server/server.rs @@ -4,11 +4,9 @@ use crate::kernel::Storage; use crate::proto::kipdb_rpc_server::{KipdbRpc, KipdbRpcServer}; use crate::proto::{ BatchGetReq, BatchGetResp, BatchRemoveReq, BatchRemoveResp, BatchSetReq, BatchSetResp, Empty, - FlushResp, GetReq, GetResp, Kv, LenResp, RemoveReq, RemoveResp, SetReq, SetResp, - SizeOfDiskResp, + FlushResp, GetReq, GetResp, LenResp, RemoveReq, RemoveResp, SetReq, SetResp, SizeOfDiskResp, }; use bytes::Bytes; -use itertools::Itertools; use std::sync::Arc; use tonic::transport::Server; use tonic::{Request, Response, Status};