Add the `whereami` action (#232)
* Move macros into module * Add the `whereami` action to identify the current entity * Show entity group in the skysh prompt * Add tests and actiondoc for `whereami` * Add changelog entry * Upgrade depsnext
parent
c260dac3a4
commit
76f493753b
@ -0,0 +1,179 @@
|
||||
/*
|
||||
* Created on Thu Nov 11 2021
|
||||
*
|
||||
* This file is a part of Skytable
|
||||
* Skytable (formerly known as TerrabaseDB or Skybase) is a free and open-source
|
||||
* NoSQL database written by Sayan Nandan ("the Author") with the
|
||||
* vision to provide flexibility in data modelling without compromising
|
||||
* on performance, queryability or scalability.
|
||||
*
|
||||
* Copyright (c) 2021, Sayan Nandan <ohsayan@outlook.com>
|
||||
*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
Don't modulo because it's an L1 miss and an L2 hit. Use lowbit checks to check for parity
|
||||
*/
|
||||
|
||||
#[macro_export]
|
||||
/// endian independent check to see if the lowbit is set or not. Returns true if the lowbit
|
||||
/// is set. this is undefined to be applied on signed values on one's complement targets
|
||||
macro_rules! is_lowbit_set {
|
||||
($v:expr) => {
|
||||
$v & 1 == 1
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
/// endian independent check to see if the lowbit is unset or not. Returns true if the lowbit
|
||||
/// is unset. this is undefined to be applied on signed values on one's complement targets
|
||||
macro_rules! is_lowbit_unset {
|
||||
($v:expr) => {
|
||||
$v & 1 == 0
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! err_if_len_is {
|
||||
($buf:ident, $con:ident, eq $len:literal) => {
|
||||
if $buf.len() == $len {
|
||||
return $con
|
||||
.write_response(crate::protocol::responses::groups::ACTION_ERR)
|
||||
.await;
|
||||
}
|
||||
};
|
||||
($buf:ident, $con:ident, not $len:literal) => {
|
||||
if $buf.len() != $len {
|
||||
return $con
|
||||
.write_response(crate::protocol::responses::groups::ACTION_ERR)
|
||||
.await;
|
||||
}
|
||||
};
|
||||
($buf:ident, $con:ident, gt $len:literal) => {
|
||||
if $buf.len() > $len {
|
||||
return $con
|
||||
.write_response(crate::protocol::responses::groups::ACTION_ERR)
|
||||
.await;
|
||||
}
|
||||
};
|
||||
($buf:ident, $con:ident, lt $len:literal) => {
|
||||
if $buf.len() < $len {
|
||||
return $con
|
||||
.write_response(crate::protocol::responses::groups::ACTION_ERR)
|
||||
.await;
|
||||
}
|
||||
};
|
||||
($buf:ident, $con:ident, gt_or_eq $len:literal) => {
|
||||
if $buf.len() >= $len {
|
||||
return $con
|
||||
.write_response(crate::protocol::responses::groups::ACTION_ERR)
|
||||
.await;
|
||||
}
|
||||
};
|
||||
($buf:ident, $con:ident, lt_or_eq $len:literal) => {
|
||||
if $buf.len() <= $len {
|
||||
return $con
|
||||
.write_response(crate::protocol::responses::groups::ACTION_ERR)
|
||||
.await;
|
||||
}
|
||||
};
|
||||
($con:ident, $expr:expr) => {
|
||||
if $expr {
|
||||
return $con
|
||||
.write_response(crate::protocol::responses::groups::ACTION_ERR)
|
||||
.await;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! kve {
|
||||
($con:expr, $store:expr) => {
|
||||
match $store.get_kvstore() {
|
||||
Ok(store) => store,
|
||||
_ => {
|
||||
// wrong model
|
||||
return $con
|
||||
.write_response(crate::protocol::responses::groups::WRONG_MODEL)
|
||||
.await;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! default_keyspace {
|
||||
($store:expr, $con:expr) => {
|
||||
match $store.get_keyspace() {
|
||||
Ok(ks) => ks,
|
||||
Err(_) => {
|
||||
return $con
|
||||
.write_response(crate::protocol::responses::groups::DEFAULT_UNSET)
|
||||
.await;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! conwrite {
|
||||
($con:expr, $what:expr) => {
|
||||
$con.write_response($what).await
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! aerr {
|
||||
($con:expr, aerr) => {
|
||||
return conwrite!($con, crate::protocol::responses::groups::ACTION_ERR)
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! get_tbl {
|
||||
($entity:expr, $store:expr, $con:expr) => {{
|
||||
use crate::corestore::memstore::DdlError;
|
||||
match $store.get_table($entity) {
|
||||
Ok(tbl) => tbl,
|
||||
Err(DdlError::DefaultNotFound) => {
|
||||
return conwrite!($con, crate::protocol::responses::groups::DEFAULT_UNSET);
|
||||
}
|
||||
Err(DdlError::ObjectNotFound) => {
|
||||
return conwrite!(
|
||||
$con,
|
||||
crate::protocol::responses::groups::CONTAINER_NOT_FOUND
|
||||
);
|
||||
}
|
||||
Err(_) => unsafe { impossible!() },
|
||||
}
|
||||
}};
|
||||
($store:expr, $con:expr) => {{
|
||||
match $store.get_ctable() {
|
||||
Some(tbl) => tbl,
|
||||
None => return conwrite!($con, crate::protocol::responses::groups::DEFAULT_UNSET),
|
||||
}
|
||||
}};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! handle_entity {
|
||||
($con:expr, $ident:expr) => {{
|
||||
match crate::queryengine::parser::get_query_entity(&$ident) {
|
||||
Ok(e) => e,
|
||||
Err(e) => return conwrite!($con, e),
|
||||
}
|
||||
}};
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Created on Fri Nov 12 2021
|
||||
*
|
||||
* This file is a part of Skytable
|
||||
* Skytable (formerly known as TerrabaseDB or Skybase) is a free and open-source
|
||||
* NoSQL database written by Sayan Nandan ("the Author") with the
|
||||
* vision to provide flexibility in data modelling without compromising
|
||||
* on performance, queryability or scalability.
|
||||
*
|
||||
* Copyright (c) 2021, Sayan Nandan <ohsayan@outlook.com>
|
||||
*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
use crate::dbnet::connection::prelude::*;
|
||||
use crate::resp::writer::FlatArrayWriter;
|
||||
|
||||
action! {
|
||||
fn whereami(store: &Corestore, con: &mut T, iter: ActionIter<'a>) {
|
||||
err_if_len_is!(iter, con, not 0);
|
||||
match store.get_ids() {
|
||||
(Some(ks), Some(tbl)) => {
|
||||
let mut writer = unsafe { FlatArrayWriter::new(con, b'+', 2).await? };
|
||||
writer.write_element(ks).await?;
|
||||
writer.write_element(tbl).await?;
|
||||
},
|
||||
(Some(ks), None) => {
|
||||
let mut writer = unsafe { FlatArrayWriter::new(con, b'+', 1).await? };
|
||||
writer.write_element(ks).await?;
|
||||
},
|
||||
_ => unsafe { impossible!() }
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue