Add listmap compatibility to `keylen` and `lskeys`

next
Sayan Nandan 3 years ago
parent 68b9c9b81f
commit b930c5d31b

@ -24,7 +24,9 @@
*
*/
use crate::corestore::table::DataModel;
use crate::dbnet::connection::prelude::*;
use crate::kvengine::KVTable;
action!(
/// Run a `KEYLEN` query
@ -33,14 +35,11 @@ action!(
fn keylen(handle: &crate::corestore::Corestore, con: &mut T, mut act: ActionIter<'a>) {
err_if_len_is!(act, con, not 1);
let res: Option<usize> = {
let reader = kve!(con, handle);
unsafe {
// UNSAFE(@ohsayan): this is completely safe as we've already checked
// the number of arguments is one
match reader.get(act.next_unchecked()) {
Ok(v) => v.map(|b| b.len()),
Err(_) => None,
}
let tbl = get_tbl!(handle, con);
let key = unsafe { act.next_unchecked() };
match tbl.get_model_ref() {
DataModel::KV(kv) => kv.kve_keylen(key),
DataModel::KVExtListmap(kv) => kv.kve_keylen(key),
}
};
if let Some(value) = res {

@ -24,10 +24,11 @@
*
*/
use crate::corestore::memstore::DdlError;
use crate::corestore::table::DataModel;
use crate::corestore::Data;
use crate::dbnet::connection::prelude::*;
use crate::kvengine::KVTable;
use crate::resp::writer::TypedArrayWriter;
use bytes::Bytes;
const DEFAULT_COUNT: usize = 10;
@ -65,13 +66,14 @@ action!(
};
(get_tbl!(entity, handle, con), count)
};
let kve = match table.get_kvstore() {
Ok(kv) => kv,
Err(DdlError::WrongModel) => return conwrite!(con, responses::groups::WRONG_MODEL),
Err(_) => unsafe { impossible!() },
let tsymbol = match table.get_model_ref() {
DataModel::KV(kv) => kv.kve_payload_tsymbol(),
DataModel::KVExtListmap(kv) => kv.kve_payload_tsymbol(),
};
let items: Vec<Data> = match table.get_model_ref() {
DataModel::KV(kv) => kv.kve_inner_ref().get_keys(count),
DataModel::KVExtListmap(kv) => kv.kve_inner_ref().get_keys(count),
};
let items: Vec<Bytes> = kve.__get_inner_ref().get_keys(count);
let tsymbol = kve.get_kt();
let mut writer = unsafe {
// SAFETY: We have checked kty ourselves
TypedArrayWriter::new(con, tsymbol, items.len())

@ -178,13 +178,13 @@ impl<K: Eq + Hash, V: Clone> Coremap<K, V> {
}
}
impl Coremap<Data, Data> {
impl<K: Eq + Hash + Clone, V> Coremap<K, V> {
/// Returns atleast `count` number of keys from the hashtable
pub fn get_keys(&self, count: usize) -> Vec<Bytes> {
pub fn get_keys(&self, count: usize) -> Vec<K> {
let mut v = Vec::with_capacity(count);
self.iter()
.take(count)
.map(|kv| kv.key().get_blob().clone())
.map(|kv| kv.key().clone())
.for_each(|key| v.push(key));
v
}

@ -73,6 +73,22 @@ pub trait KVTable<'a, T> {
fn kve_keylen<Q: ?Sized + Eq + Hash>(&self, input: &Q) -> Option<usize>
where
Data: Borrow<Q>;
/// Get the tsymbol for the KVE key
fn kve_key_tsymbol(&self) -> u8 {
if self.kve_key_encoded() {
TSYMBOL_UNICODE
} else {
TSYMBOL_BINARY
}
}
/// Get the tsymbol for the KVE payload
fn kve_payload_tsymbol(&self) -> u8 {
if self.kve_payload_encoded() {
TSYMBOL_UNICODE
} else {
TSYMBOL_BINARY
}
}
}
impl<'a> KVTable<'a, Coremap<Data, Data>> for KVEngine {

Loading…
Cancel
Save