Enable `lskeys` to accept entities

next
Sayan Nandan 3 years ago
parent 32fcbc2075
commit bc9abd7ac3

@ -80,6 +80,22 @@ All changes in this project will be noted in this file.
```sql ```sql
USE keyspace:table USE keyspace:table
``` ```
- **Entity respecting actions**:
- **`FLUSHDB`**: To flush all the data in a specific table, run:
```sql
FLUSHDB <entity>
```
- **`DBSIZE`**: To see the number of entries in a specific table, run:
```sql
DBSIZE <entity>
```
- **`LSKEYS`**:
- `LSKEYS` will return keys from the current table
- `LSKEYS <count>` will return _count_ keys from the current table
- `LSKEYS <entity>` will return keys from the given table
- `LSKEYS <entity> <count>` will return _count_ keys from the given table
- TLS port can now be set to a custom port via CLI arguments - TLS port can now be set to a custom port via CLI arguments
### Fixes ### Fixes

@ -24,28 +24,53 @@
* *
*/ */
use crate::corestore::memstore::DdlError;
use crate::dbnet::connection::prelude::*; use crate::dbnet::connection::prelude::*;
use crate::resp::BytesWrapper; use crate::resp::BytesWrapper;
use bytes::Bytes; use bytes::Bytes;
const DEFAULT_COUNT: usize = 10;
action!( action!(
/// Run an `LSKEYS` query /// Run an `LSKEYS` query
fn lskeys(handle: &crate::corestore::Corestore, con: &mut T, mut act: ActionIter) { fn lskeys(handle: &crate::corestore::Corestore, con: &mut T, mut act: ActionIter) {
err_if_len_is!(act, con, gt 1); err_if_len_is!(act, con, gt 2);
let item_count = if let Some(cnt) = act.next() { let (table, count) = if act.len() == 0 {
if let Ok(cnt) = String::from_utf8_lossy(&cnt).parse::<usize>() { (get_tbl!(handle, con), DEFAULT_COUNT)
cnt } else if act.len() == 1 {
// two args, could either be count or an entity
let nextret = unsafe { act.next().unsafe_unwrap() };
if unsafe { nextret.get_unchecked(0) }.is_ascii_digit() {
// noice, this is a number; let's try to parse it
let count = if let Ok(cnt) = String::from_utf8_lossy(&nextret).parse::<usize>() {
cnt
} else {
return con.write_response(responses::groups::WRONGTYPE_ERR).await;
};
(get_tbl!(handle, con), count)
} else { } else {
return con.write_response(responses::groups::WRONGTYPE_ERR).await; // sigh, an entity
let entity = handle_entity!(con, nextret);
(get_tbl!(entity, handle, con), DEFAULT_COUNT)
} }
} else { } else {
10 // an entity and a count, gosh this fella is really trying us
let entity_ret = unsafe { act.next().unsafe_unwrap() };
let count_ret = unsafe { act.next().unsafe_unwrap() };
let entity = handle_entity!(con, entity_ret);
let count = if let Ok(cnt) = String::from_utf8_lossy(&count_ret).parse::<usize>() {
cnt
} else {
return con.write_response(responses::groups::WRONGTYPE_ERR).await;
};
(get_tbl!(entity, handle, con), count)
}; };
let items: Vec<Bytes>; let kve = match table.get_kvstore() {
{ Ok(kv) => kv,
let reader = kve!(con, handle); Err(DdlError::WrongModel) => return conwrite!(con, responses::groups::WRONG_MODEL),
items = reader.__get_inner_ref().get_keys(item_count); Err(_) => unsafe { impossible!() },
} };
let items: Vec<Bytes> = kve.__get_inner_ref().get_keys(count);
con.write_flat_array_length(items.len()).await?; con.write_flat_array_length(items.len()).await?;
for item in items { for item in items {
con.write_response(BytesWrapper(item)).await?; con.write_response(BytesWrapper(item)).await?;

@ -187,8 +187,8 @@ impl Corestore {
_ => unsafe { impossible!() }, _ => unsafe { impossible!() },
} }
} }
pub fn get_ctable(&self) -> Option<&Arc<Table>> { pub fn get_ctable(&self) -> Option<Arc<Table>> {
self.ctable.as_ref() self.ctable.clone()
} }
/// Get the key/value store /// Get the key/value store

@ -1061,18 +1061,11 @@ mod __private {
panic!("Expected flat string array"); panic!("Expected flat string array");
} }
} }
async fn test_lskeys_wrongtype() {
query.push("lskeys");
query.push("abcdefg");
assert_eq!(
con.run_simple_query(&query).await.unwrap(),
Response::Item(Element::RespCode(RespCode::Wrongtype))
);
}
async fn test_lskeys_syntax_error() { async fn test_lskeys_syntax_error() {
query.push("lskeys"); query.push("lskeys");
query.push("abcdefg"); query.push("abcdefg");
query.push("hijklmn"); query.push("hijklmn");
query.push("riufrif");
assert_eq!( assert_eq!(
con.run_simple_query(&query).await.unwrap(), con.run_simple_query(&query).await.unwrap(),
Response::Item(Element::RespCode(RespCode::ActionError)) Response::Item(Element::RespCode(RespCode::ActionError))

Loading…
Cancel
Save