Fix encoding check correctness in actions

next
Sayan Nandan 3 years ago
parent 6fe3f53aa9
commit f4fbdcae16

@ -29,7 +29,7 @@
use crate::corestore::table::DataModel; use crate::corestore::table::DataModel;
use crate::dbnet::connection::prelude::*; use crate::dbnet::connection::prelude::*;
use crate::kvengine::KVTable; use crate::kvengine::{encoding::ENCODING_LUT_ITER, KVTable};
use crate::util::compiler; use crate::util::compiler;
action!( action!(
@ -42,12 +42,7 @@ action!(
let table = get_tbl!(handle, con); let table = get_tbl!(handle, con);
macro_rules! remove { macro_rules! remove {
($engine:expr) => {{ ($engine:expr) => {{
let encoding_is_okay = if $engine.kve_key_encoded() { let encoding_is_okay = ENCODING_LUT_ITER[$engine.kve_key_encoded()](act.as_ref());
let encoder = $engine.kve_get_key_encoder();
act.as_ref().all(|k| encoder.is_ok(k))
} else {
true
};
if compiler::likely(encoding_is_okay) { if compiler::likely(encoding_is_okay) {
let done_howmany: Option<usize>; let done_howmany: Option<usize>;
{ {

@ -29,7 +29,7 @@
use crate::corestore::table::DataModel; use crate::corestore::table::DataModel;
use crate::dbnet::connection::prelude::*; use crate::dbnet::connection::prelude::*;
use crate::kvengine::KVTable; use crate::kvengine::{encoding::ENCODING_LUT_ITER, KVTable};
use crate::queryengine::ActionIter; use crate::queryengine::ActionIter;
use crate::util::compiler; use crate::util::compiler;
@ -40,12 +40,7 @@ action!(
let mut how_many_of_them_exist = 0usize; let mut how_many_of_them_exist = 0usize;
macro_rules! exists { macro_rules! exists {
($engine:expr) => {{ ($engine:expr) => {{
let encoding_is_okay = if $engine.kve_key_encoded() { let encoding_is_okay = ENCODING_LUT_ITER[$engine.kve_key_encoded()](act.as_ref());
let encoder = $engine.kve_get_key_encoder();
act.as_ref().all(|k| encoder.is_ok(k))
} else {
true
};
if compiler::likely(encoding_is_okay) { if compiler::likely(encoding_is_okay) {
act.for_each(|key| { act.for_each(|key| {
if $engine.kve_exists(key) { if $engine.kve_exists(key) {

@ -24,8 +24,8 @@
* *
*/ */
use crate::corestore::booltable::BoolTable; use crate::corestore::booltable::BytesBoolTable;
use crate::corestore::booltable::NicheLUT; use crate::corestore::booltable::BytesNicheLUT;
use crate::corestore::table::DataModel; use crate::corestore::table::DataModel;
use crate::corestore::Data; use crate::corestore::Data;
use crate::dbnet::connection::prelude::*; use crate::dbnet::connection::prelude::*;
@ -43,9 +43,9 @@ const REMOVE: &[u8] = "REMOVE".as_bytes();
const INSERT: &[u8] = "INSERT".as_bytes(); const INSERT: &[u8] = "INSERT".as_bytes();
const POP: &[u8] = "POP".as_bytes(); const POP: &[u8] = "POP".as_bytes();
const OKAY_OVW_BLUT: BoolTable = BoolTable::new(groups::OKAY, groups::OVERWRITE_ERR); const OKAY_OVW_BLUT: BytesBoolTable = BytesBoolTable::new(groups::OKAY, groups::OVERWRITE_ERR);
const OKAY_BADIDX_NIL_NLUT: NicheLUT = const OKAY_BADIDX_NIL_NLUT: BytesNicheLUT =
NicheLUT::new(groups::NIL, groups::OKAY, groups::LISTMAP_BAD_INDEX); BytesNicheLUT::new(groups::NIL, groups::OKAY, groups::LISTMAP_BAD_INDEX);
macro_rules! listmap { macro_rules! listmap {
($tbl:expr, $con:expr) => { ($tbl:expr, $con:expr) => {

@ -25,6 +25,7 @@
*/ */
use crate::dbnet::connection::prelude::*; use crate::dbnet::connection::prelude::*;
use crate::kvengine::{encoding::ENCODING_LUT_ITER, KVTable};
use crate::queryengine::ActionIter; use crate::queryengine::ActionIter;
use crate::resp::writer::TypedArrayWriter; use crate::resp::writer::TypedArrayWriter;
use crate::util::compiler; use crate::util::compiler;
@ -35,12 +36,7 @@ action!(
fn mget(handle: &crate::corestore::Corestore, con: &mut T, act: ActionIter<'a>) { fn mget(handle: &crate::corestore::Corestore, con: &mut T, act: ActionIter<'a>) {
crate::err_if_len_is!(act, con, eq 0); crate::err_if_len_is!(act, con, eq 0);
let kve = kve!(con, handle); let kve = kve!(con, handle);
let encoding_is_okay = if kve.needs_key_encoding() { let encoding_is_okay = ENCODING_LUT_ITER[kve.kve_key_encoded()](act.as_ref());
true
} else {
let encoder = kve.get_key_encoder();
act.as_ref().all(|k| encoder.is_ok(k))
};
if compiler::likely(encoding_is_okay) { if compiler::likely(encoding_is_okay) {
let mut writer = unsafe { let mut writer = unsafe {
// SAFETY: We are getting the value type ourselves // SAFETY: We are getting the value type ourselves

@ -26,6 +26,7 @@
use crate::corestore; use crate::corestore;
use crate::dbnet::connection::prelude::*; use crate::dbnet::connection::prelude::*;
use crate::kvengine::encoding::ENCODING_LUT_ITER;
use crate::protocol::responses; use crate::protocol::responses;
use crate::queryengine::ActionIter; use crate::queryengine::ActionIter;
use crate::resp::writer::TypedArrayWriter; use crate::resp::writer::TypedArrayWriter;
@ -37,12 +38,7 @@ action!(
err_if_len_is!(act, con, eq 0); err_if_len_is!(act, con, eq 0);
if registry::state_okay() { if registry::state_okay() {
let kve = kve!(con, handle); let kve = kve!(con, handle);
let encoding_is_okay = if kve.needs_key_encoding() { let encoding_is_okay = ENCODING_LUT_ITER[kve.needs_key_encoding()](act.as_ref());
true
} else {
let encoder = kve.get_key_encoder();
act.as_ref().all(|k| encoder.is_ok(k))
};
if compiler::likely(encoding_is_okay) { if compiler::likely(encoding_is_okay) {
let mut writer = unsafe { let mut writer = unsafe {
// SAFETY: We have verified the tsymbol ourselves // SAFETY: We have verified the tsymbol ourselves

@ -28,13 +28,13 @@
//! This module provides functions to work with `SET` queries //! This module provides functions to work with `SET` queries
use crate::corestore; use crate::corestore;
use crate::corestore::booltable::NicheLUT; use crate::corestore::booltable::BytesNicheLUT;
use crate::dbnet::connection::prelude::*; use crate::dbnet::connection::prelude::*;
use crate::queryengine::ActionIter; use crate::queryengine::ActionIter;
use corestore::Data; use corestore::Data;
const SET_NLUT: NicheLUT = const SET_NLUT: BytesNicheLUT =
NicheLUT::new(groups::ENCODING_ERROR, groups::OKAY, groups::OVERWRITE_ERR); BytesNicheLUT::new(groups::ENCODING_ERROR, groups::OKAY, groups::OVERWRITE_ERR);
action!( action!(
/// Run a `SET` query /// Run a `SET` query

@ -28,11 +28,12 @@
//! This module provides functions to work with `UPDATE` queries //! This module provides functions to work with `UPDATE` queries
//! //!
use crate::corestore::booltable::NicheLUT; use crate::corestore::booltable::BytesNicheLUT;
use crate::corestore::Data; use crate::corestore::Data;
use crate::dbnet::connection::prelude::*; use crate::dbnet::connection::prelude::*;
const UPDATE_NLUT: NicheLUT = NicheLUT::new(groups::ENCODING_ERROR, groups::OKAY, groups::NIL); const UPDATE_NLUT: BytesNicheLUT =
BytesNicheLUT::new(groups::ENCODING_ERROR, groups::OKAY, groups::NIL);
action!( action!(
/// Run an `UPDATE` query /// Run an `UPDATE` query

@ -26,22 +26,25 @@
use core::ops::Index; use core::ops::Index;
pub type BytesBoolTable = BoolTable<&'static [u8]>;
pub type BytesNicheLUT = NicheLUT<&'static [u8]>;
/// A two-value boolean LUT /// A two-value boolean LUT
pub struct BoolTable { pub struct BoolTable<T> {
base: [&'static [u8]; 2], base: [T; 2],
} }
impl BoolTable { impl<T> BoolTable<T> {
/// Supply values in the order: `if_true` and `if_false` /// Supply values in the order: `if_true` and `if_false`
pub const fn new(if_true: &'static [u8], if_false: &'static [u8]) -> Self { pub const fn new(if_true: T, if_false: T) -> Self {
Self { Self {
base: [if_false, if_true], base: [if_false, if_true],
} }
} }
} }
impl Index<bool> for BoolTable { impl<T> Index<bool> for BoolTable<T> {
type Output = &'static [u8]; type Output = T;
fn index(&self, index: bool) -> &Self::Output { fn index(&self, index: bool) -> &Self::Output {
unsafe { &*self.base.as_ptr().add(index as usize) } unsafe { &*self.base.as_ptr().add(index as usize) }
} }
@ -51,17 +54,13 @@ impl Index<bool> for BoolTable {
/// structure /// structure
/// ///
/// **Warning:** This is a terrible opt and only works on the Rust ABI /// **Warning:** This is a terrible opt and only works on the Rust ABI
pub struct NicheLUT { pub struct NicheLUT<T> {
base: [&'static [u8]; 3], base: [T; 3],
} }
impl NicheLUT { impl<T> NicheLUT<T> {
/// Supply values in the following order: [`if_none`, `if_true`, `if_false`] /// Supply values in the following order: [`if_none`, `if_true`, `if_false`]
pub const fn new( pub const fn new(if_none: T, if_true: T, if_false: T) -> Self {
if_none: &'static [u8],
if_true: &'static [u8],
if_false: &'static [u8],
) -> Self {
Self { Self {
// 0 == S(F); 1 == S(T); 2 == NULL // 0 == S(F); 1 == S(T); 2 == NULL
base: [if_false, if_true, if_none], base: [if_false, if_true, if_none],
@ -69,8 +68,8 @@ impl NicheLUT {
} }
} }
impl Index<Option<bool>> for NicheLUT { impl<T> Index<Option<bool>> for NicheLUT<T> {
type Output = &'static [u8]; type Output = T;
fn index(&self, idx: Option<bool>) -> &Self::Output { fn index(&self, idx: Option<bool>) -> &Self::Output {
unsafe { unsafe {
&*self &*self

@ -54,6 +54,12 @@
* - Sayan N. <ohsayan@outlook.com> (July, 2021) * - Sayan N. <ohsayan@outlook.com> (July, 2021)
*/ */
use crate::corestore::booltable::BoolTable;
use crate::protocol::iter::BorrowedAnyArrayIter;
pub const ENCODING_LUT_ITER: BoolTable<fn(BorrowedAnyArrayIter) -> bool> =
BoolTable::new(is_okay_encoded_iter, is_okay_no_encoding_iter);
/// This table maps bytes to character classes that helps us reduce the size of the /// This table maps bytes to character classes that helps us reduce the size of the
/// transition table and generate bitmasks /// transition table and generate bitmasks
const UTF8_MAP_BYTE_TO_CHAR_CLASS: [u8; 256] = [ const UTF8_MAP_BYTE_TO_CHAR_CLASS: [u8; 256] = [
@ -78,6 +84,14 @@ const UTF8_TRANSITION_MAP: [u8; 108] = [
12, 36, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 36, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
]; ];
pub fn is_okay_encoded_iter(mut inp: BorrowedAnyArrayIter<'_>) -> bool {
inp.all(|v| self::is_okay_encoded(v))
}
pub const fn is_okay_no_encoding_iter(_inp: BorrowedAnyArrayIter<'_>) -> bool {
true
}
pub fn is_okay_encoded(inp: &[u8]) -> bool { pub fn is_okay_encoded(inp: &[u8]) -> bool {
self::is_utf8(inp) self::is_utf8(inp)
} }

Loading…
Cancel
Save