Add 'EXISTS' query

next
Sayan Nandan 4 years ago
parent 3487cccde3
commit 1eaed63fe6
No known key found for this signature in database
GPG Key ID: C31EFD7DDA12AEE0

@ -26,6 +26,7 @@ TerrabaseDB (or TDB for short) is an effort to provide the best of key/value sto
* `GET` - Get a key
* `SET` - Set a key
* `UPDATE` - Update the value of a key which has already been created with `SET`
* `EXISTS` - Check if a key exists
* `DEL` - Delete a key
And, a lot more actions are coming soon!

@ -50,7 +50,7 @@ impl SResp {
self.dataframe.extend(&cmd);
self.dataframe.push(b'\n');
}
pub fn prepare_query(mut self) -> (Vec<u8>, Vec<u8>, Vec<u8>) {
pub fn prepare_response(mut self) -> (Vec<u8>, Vec<u8>, Vec<u8>) {
self.metaline
.extend(self.dataframe.len().to_string().as_bytes());
self.metaline.push(b'!');

@ -105,6 +105,12 @@ impl CoreDB {
Err(RespCodes::NotFound)
}
}
/// Check if a `key` exists
pub fn exists(&self, key: &str) -> bool {
self.acquire_read().contains_key(&key.to_owned())
}
#[cfg(Debug)]
/// Flush the coretable entries when in debug mode
pub fn print_debug_table(&self) {

@ -24,7 +24,7 @@
use crate::coredb::CoreDB;
use corelib::builders::response::*;
use corelib::terrapipe::{RespBytes, RespCodes};
pub mod tags {
mod tags {
//! This module is a collection of tags/strings used for evaluating queries
//! and responses
/// `GET` command tag
@ -37,6 +37,8 @@ pub mod tags {
pub const TAG_DEL: &'static str = "DEL";
/// `HEYA` command tag
pub const TAG_HEYA: &'static str = "HEYA";
/// `EXISTS` command tag
pub const TAG_EXISTS: &'static str = "EXISTS";
}
/// Execute a simple(*) query
@ -54,7 +56,7 @@ pub fn execute_simple(db: &CoreDB, buf: Vec<String>) -> Vec<u8> {
};
let mut resp = SResp::new(RespCodes::Okay.into());
resp.add(res.to_vec());
return cat(resp.prepare_query());
return cat(resp.prepare_response());
}
}
}
@ -112,11 +114,23 @@ pub fn execute_simple(db: &CoreDB, buf: Vec<String>) -> Vec<u8> {
}
}
}
tags::TAG_EXISTS => {
// This is an `EXISTS` query
if let Some(key) = buf.next() {
if buf.next().is_none() {
let ex = db.exists(&key) as u8;
let mut resp = SResp::new(RespCodes::Okay.into());
resp.add(ex.to_string());
return cat(resp.prepare_response());
}
}
}
tags::TAG_HEYA => {
// This is a `HEYA` query
if buf.next().is_none() {
let mut resp = SResp::new(RespCodes::Okay.into());
resp.add("HEY!");
return cat(resp.prepare_query());
return cat(resp.prepare_response());
}
}
_ => return RespCodes::OtherError(Some("Unknown command".to_owned())).into_response(),

Loading…
Cancel
Save