diff --git a/Cargo.lock b/Cargo.lock index 08d9af99..c05815bb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -127,6 +127,12 @@ dependencies = [ "libc", ] +[[package]] +name = "itoa" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6" + [[package]] name = "kernel32-sys" version = "0.2.2" @@ -388,6 +394,12 @@ version = "0.6.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "26412eb97c6b088a6997e05f69403a802a92d520de2f8e63c2b65f9e0f47c4e8" +[[package]] +name = "ryu" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" + [[package]] name = "scopeguard" version = "1.1.0" @@ -396,9 +408,34 @@ checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" [[package]] name = "serde" -version = "1.0.114" +version = "1.0.115" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5317f7588f0a5078ee60ef675ef96735a1442132dc645eb1d12c018620ed8cd3" +checksum = "e54c9a88f2da7238af84b5101443f0c0d0a3bbdc455e34a5c9497b1903ed55d5" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.115" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "609feed1d0a73cc36a0182a840a9b37b4a82f0b1150369f0536a9e3f2a31dc48" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.57" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "164eacbdb13512ec2745fb09d51fd5b22b0d65ed294a1dcf7285a360c80a675c" +dependencies = [ + "itoa", + "ryu", + "serde", +] [[package]] name = "signal-hook-registry" @@ -454,6 +491,8 @@ dependencies = [ "lazy_static", "libtdb", "parking_lot", + "serde", + "serde_json", "tokio", ] diff --git a/server/Cargo.toml b/server/Cargo.toml index 02bf5396..6f72a57b 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -13,5 +13,7 @@ libtdb = {path ="../libtdb"} bincode = "1.3.1" parking_lot = "0.11.0" lazy_static = "1.4.0" +serde = {version= "1.0.115", features=["derive"]} +serde_json = "1.0.57" [dev-dependencies] tokio = { version = "0.2", features = ["test-util"] } diff --git a/server/src/kvengine/jget.rs b/server/src/kvengine/jget.rs new file mode 100644 index 00000000..5f8e0699 --- /dev/null +++ b/server/src/kvengine/jget.rs @@ -0,0 +1,67 @@ +/* + * Created on Mon Aug 31 2020 + * + * This file is a part of TerrabaseDB + * Copyright (c) 2020, Sayan Nandan + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * +*/ + +//! #`JGET` queries +//! Functions for handling `JGET` queries + +use crate::coredb::CoreDB; +use crate::protocol::{responses, ActionGroup, Connection}; +use crate::resp::{BytesWrapper, GroupBegin}; +use bytes::Bytes; +use libtdb::TResult; +use serde::{Deserialize, Serialize}; +use std::collections::HashMap; + +/// A key/value pair +/// When we write JSON to the stream in `JGET`, it looks something like: +/// ```json +/// { +/// "keythatexists" : "value", +/// "nilkey": null, +/// } +/// ``` +#[derive(Serialize, Deserialize)] +pub struct KVPair(HashMap>); + +impl KVPair { + pub fn with_capacity(size: usize) -> Self { + KVPair(HashMap::with_capacity(size)) + } +} + +/// Run a `JGET` query +/// This returns a JSON key/value pair of keys and values +/// We need to write something like +/// ```json +/// &1\n +/// $15\n +/// {"key":"value"}\n +/// ``` +/// +pub async fn jget(handle: &CoreDB, con: &mut Connection, act: ActionGroup) -> TResult<()> { + let howmany = act.howmany(); + if howmany != 1 { + return con + .write_response(responses::fresp::R_ACTION_ERR.to_owned()) + .await; + } + todo!() +} diff --git a/server/src/kvengine/mget.rs b/server/src/kvengine/mget.rs index 0205f038..790ec5d0 100644 --- a/server/src/kvengine/mget.rs +++ b/server/src/kvengine/mget.rs @@ -28,7 +28,6 @@ use libtdb::TResult; /// Run an `MGET` query /// -/// **This is currently an experimental query** pub async fn mget(handle: &CoreDB, con: &mut Connection, act: ActionGroup) -> TResult<()> { let howmany = act.howmany(); if howmany == 0 { diff --git a/server/src/kvengine/mod.rs b/server/src/kvengine/mod.rs index 301aa2e0..7a0c55cb 100644 --- a/server/src/kvengine/mod.rs +++ b/server/src/kvengine/mod.rs @@ -26,6 +26,7 @@ pub mod del; pub mod exists; pub mod get; +pub mod jget; pub mod mget; pub mod mset; pub mod mupdate; diff --git a/server/src/kvengine/mset.rs b/server/src/kvengine/mset.rs index 97627a4d..ab3c65f3 100644 --- a/server/src/kvengine/mset.rs +++ b/server/src/kvengine/mset.rs @@ -26,8 +26,6 @@ use libtdb::TResult; use std::collections::hash_map::Entry; /// Run an `MSET` query -/// -/// **This is currently an experimental query** pub async fn mset(handle: &CoreDB, con: &mut Connection, act: ActionGroup) -> TResult<()> { let howmany = act.howmany(); if howmany & 1 == 1 { diff --git a/server/src/kvengine/mupdate.rs b/server/src/kvengine/mupdate.rs index 2be2197c..88a6dd8e 100644 --- a/server/src/kvengine/mupdate.rs +++ b/server/src/kvengine/mupdate.rs @@ -25,7 +25,7 @@ use crate::resp::GroupBegin; use libtdb::TResult; use std::collections::hash_map::Entry; -// Run an `MUPDATE` query +/// Run an `MUPDATE` query pub async fn mupdate(handle: &CoreDB, con: &mut Connection, act: ActionGroup) -> TResult<()> { let howmany = act.howmany(); if howmany & 1 == 1 {