Use refs instead of cloning for pre-compiled resps

For this, `Writable` was implemented for `&'static [u8]`.
Although this won't have a very noticeable impact on performance, we
will stick to using references instead of cloning the data and then
referencing it again.

Along with this, the docs for MKSNAP were updated.

Signed-off-by: Sayan Nandan <nandansayan@outlook.com>
next
Sayan Nandan 4 years ago
parent 9e61921170
commit 106c5bc219
No known key found for this signature in database
GPG Key ID: C31EFD7DDA12AEE0

@ -146,10 +146,10 @@
}, },
{ {
"name": "MKSNAP", "name": "MKSNAP",
"since": "0.4.5", "since": "0.4.6",
"complexity": "O(n)", "complexity": "O(n)",
"args": "MKSNAP", "args": "MKSNAP <SNAPNAME>",
"desc": "This action can be used to create a snapshot. Do note that this action **requires snapshotting to be enabled on the server side**, before it can create snapshots. For more information on enabling snapshots, read [this document](/snapshots)", "desc": "This action can be used to create a snapshot. Do note that this action **requires snapshotting to be enabled on the server side**, before it can create snapshots. <br>If you want to create snapshots **without** snapshots being enabled on the server-side, use a second argument (`[SNAPNAME]`) to specify a snapshot name and a snapshot will be create in a folder called `remote` under your snapshots directory. <br>For more information on snapshots, read [this document](/snapshots)",
"return": "Okay if succeeded, otherwise it returns `err-snapshot-disabled` if snapshotting is disabled or `err-snapshot-busy` if a snapshotting operation is already in progress" "return": "Okay if succeeded, otherwise it returns `err-snapshot-disabled` if snapshotting is disabled or `err-snapshot-busy` if a snapshotting operation is already in progress"
} }
] ]

@ -26,9 +26,7 @@ use libtdb::TResult;
/// Get the number of keys in the database /// Get the number of keys in the database
pub async fn dbsize(handle: &CoreDB, con: &mut Connection, act: ActionGroup) -> TResult<()> { pub async fn dbsize(handle: &CoreDB, con: &mut Connection, act: ActionGroup) -> TResult<()> {
if act.howmany() != 0 { if act.howmany() != 0 {
return con return con.write_response(&**responses::fresp::R_ACTION_ERR).await;
.write_response(responses::fresp::R_ACTION_ERR.to_owned())
.await;
} }
let len; let len;
{ {

@ -34,9 +34,7 @@ use libtdb::TResult;
pub async fn del(handle: &CoreDB, con: &mut Connection, act: ActionGroup) -> TResult<()> { pub async fn del(handle: &CoreDB, con: &mut Connection, act: ActionGroup) -> TResult<()> {
let howmany = act.howmany(); let howmany = act.howmany();
if howmany == 0 { if howmany == 0 {
return con return con.write_response(&**responses::fresp::R_ACTION_ERR).await;
.write_response(responses::fresp::R_ACTION_ERR.to_owned())
.await;
} }
// Write #<m>\n#<n>\n&<howmany>\n to the stream // Write #<m>\n#<n>\n&<howmany>\n to the stream
con.write_response(GroupBegin(1)).await?; con.write_response(GroupBegin(1)).await?;
@ -60,7 +58,6 @@ pub async fn del(handle: &CoreDB, con: &mut Connection, act: ActionGroup) -> TRe
if let Some(done_howmany) = done_howmany { if let Some(done_howmany) = done_howmany {
con.write_response(done_howmany).await con.write_response(done_howmany).await
} else { } else {
con.write_response(responses::fresp::R_SERVER_ERR.to_owned()) con.write_response(&**responses::fresp::R_SERVER_ERR).await
.await
} }
} }

@ -31,9 +31,7 @@ use libtdb::TResult;
pub async fn exists(handle: &CoreDB, con: &mut Connection, act: ActionGroup) -> TResult<()> { pub async fn exists(handle: &CoreDB, con: &mut Connection, act: ActionGroup) -> TResult<()> {
let howmany = act.howmany(); let howmany = act.howmany();
if howmany == 0 { if howmany == 0 {
return con return con.write_response(&**responses::fresp::R_ACTION_ERR).await;
.write_response(responses::fresp::R_ACTION_ERR.to_owned())
.await;
} }
// Write #<m>\n#<n>\n&1\n to the stream // Write #<m>\n#<n>\n&1\n to the stream
con.write_response(GroupBegin(1)).await?; con.write_response(GroupBegin(1)).await?;

@ -26,9 +26,7 @@ use libtdb::TResult;
/// Delete all the keys in the database /// Delete all the keys in the database
pub async fn flushdb(handle: &CoreDB, con: &mut Connection, act: ActionGroup) -> TResult<()> { pub async fn flushdb(handle: &CoreDB, con: &mut Connection, act: ActionGroup) -> TResult<()> {
if act.howmany() != 0 { if act.howmany() != 0 {
return con return con.write_response(&**responses::fresp::R_ACTION_ERR).await;
.write_response(responses::fresp::R_ACTION_ERR.to_owned())
.await;
} }
let failed; let failed;
{ {
@ -40,10 +38,8 @@ pub async fn flushdb(handle: &CoreDB, con: &mut Connection, act: ActionGroup) ->
} }
} }
if failed { if failed {
con.write_response(responses::fresp::R_SERVER_ERR.to_owned()) con.write_response(&**responses::fresp::R_SERVER_ERR).await
.await
} else { } else {
con.write_response(responses::fresp::R_OKAY.to_owned()) con.write_response(&**responses::fresp::R_OKAY).await
.await
} }
} }

@ -32,9 +32,7 @@ use libtdb::TResult;
pub async fn get(handle: &CoreDB, con: &mut Connection, act: ActionGroup) -> TResult<()> { pub async fn get(handle: &CoreDB, con: &mut Connection, act: ActionGroup) -> TResult<()> {
let howmany = act.howmany(); let howmany = act.howmany();
if howmany != 1 { if howmany != 1 {
return con return con.write_response(&**responses::fresp::R_ACTION_ERR).await;
.write_response(responses::fresp::R_ACTION_ERR.to_owned())
.await;
} }
// Write #<m>\n#<n>\n&1\n to the stream // Write #<m>\n#<n>\n&1\n to the stream
con.write_response(GroupBegin(1)).await?; con.write_response(GroupBegin(1)).await?;
@ -52,8 +50,7 @@ pub async fn get(handle: &CoreDB, con: &mut Connection, act: ActionGroup) -> TRe
con.write_response(BytesWrapper(value)).await?; con.write_response(BytesWrapper(value)).await?;
} else { } else {
// Ah, couldn't find that key // Ah, couldn't find that key
con.write_response(responses::groups::NIL.to_owned()) con.write_response(&**responses::groups::NIL).await?;
.await?;
} }
Ok(()) Ok(())
} }

@ -39,9 +39,7 @@ use libtdb::TResult;
pub async fn jget(_handle: &CoreDB, con: &mut Connection, act: ActionGroup) -> TResult<()> { pub async fn jget(_handle: &CoreDB, con: &mut Connection, act: ActionGroup) -> TResult<()> {
let howmany = act.howmany(); let howmany = act.howmany();
if howmany != 1 { if howmany != 1 {
return con return con.write_response(&**responses::fresp::R_ACTION_ERR).await;
.write_response(responses::fresp::R_ACTION_ERR.to_owned())
.await;
} }
todo!() todo!()
} }

@ -29,9 +29,7 @@ use libtdb::TResult;
pub async fn keylen(handle: &CoreDB, con: &mut Connection, act: ActionGroup) -> TResult<()> { pub async fn keylen(handle: &CoreDB, con: &mut Connection, act: ActionGroup) -> TResult<()> {
let howmany = act.howmany(); let howmany = act.howmany();
if howmany != 1 { if howmany != 1 {
return con return con.write_response(&**responses::fresp::R_ACTION_ERR).await;
.write_response(responses::fresp::R_ACTION_ERR.to_owned())
.await;
} }
// Write #<m>\n#<n>\n&1\n to the stream // Write #<m>\n#<n>\n&1\n to the stream
con.write_response(GroupBegin(1)).await?; con.write_response(GroupBegin(1)).await?;
@ -49,8 +47,7 @@ pub async fn keylen(handle: &CoreDB, con: &mut Connection, act: ActionGroup) ->
con.write_response(value).await?; con.write_response(value).await?;
} else { } else {
// Ah, couldn't find that key // Ah, couldn't find that key
con.write_response(responses::groups::NIL.to_owned()) con.write_response(&**responses::groups::NIL).await?;
.await?;
} }
Ok(()) Ok(())
} }

@ -31,9 +31,7 @@ use libtdb::TResult;
pub async fn mget(handle: &CoreDB, con: &mut Connection, act: ActionGroup) -> TResult<()> { pub async fn mget(handle: &CoreDB, con: &mut Connection, act: ActionGroup) -> TResult<()> {
let howmany = act.howmany(); let howmany = act.howmany();
if howmany == 0 { if howmany == 0 {
return con return con.write_response(&**responses::fresp::R_ACTION_ERR).await;
.write_response(responses::fresp::R_ACTION_ERR.to_owned())
.await;
} }
// Write #<m>\n#<n>\n&<howmany>\n to the stream // Write #<m>\n#<n>\n&<howmany>\n to the stream
con.write_response(GroupBegin(howmany)).await?; con.write_response(GroupBegin(howmany)).await?;

@ -46,7 +46,6 @@ pub mod heya {
use protocol::{responses, Connection}; use protocol::{responses, Connection};
/// Returns a `HEY!` `Response` /// Returns a `HEY!` `Response`
pub async fn heya(_db: &CoreDB, con: &mut Connection, _buf: ActionGroup) -> TResult<()> { pub async fn heya(_db: &CoreDB, con: &mut Connection, _buf: ActionGroup) -> TResult<()> {
con.write_response(responses::fresp::R_HEYA.to_owned()) con.write_response(&**responses::fresp::R_HEYA).await
.await
} }
} }

@ -32,9 +32,7 @@ pub async fn mset(handle: &CoreDB, con: &mut Connection, act: ActionGroup) -> TR
// An odd number of arguments means that the number of keys // An odd number of arguments means that the number of keys
// is not the same as the number of values, we won't run this // is not the same as the number of values, we won't run this
// action at all // action at all
return con return con.write_response(&**responses::fresp::R_ACTION_ERR).await;
.write_response(responses::fresp::R_ACTION_ERR.to_owned())
.await;
} }
// Write #<m>\n#<n>\n&<howmany>\n to the stream // Write #<m>\n#<n>\n&<howmany>\n to the stream
// It is howmany/2 since we will be writing howmany/2 number of responses // It is howmany/2 since we will be writing howmany/2 number of responses
@ -61,8 +59,6 @@ pub async fn mset(handle: &CoreDB, con: &mut Connection, act: ActionGroup) -> TR
if let Some(done_howmany) = done_howmany { if let Some(done_howmany) = done_howmany {
return con.write_response(done_howmany as usize).await; return con.write_response(done_howmany as usize).await;
} else { } else {
return con return con.write_response(&**responses::fresp::R_SERVER_ERR).await;
.write_response(responses::fresp::R_SERVER_ERR.to_owned())
.await;
} }
} }

@ -32,9 +32,7 @@ pub async fn mupdate(handle: &CoreDB, con: &mut Connection, act: ActionGroup) ->
// An odd number of arguments means that the number of keys // An odd number of arguments means that the number of keys
// is not the same as the number of values, we won't run this // is not the same as the number of values, we won't run this
// action at all // action at all
return con return con.write_response(&**responses::fresp::R_ACTION_ERR).await;
.write_response(responses::fresp::R_ACTION_ERR.to_owned())
.await;
} }
// Write #<m>\n#<n>\n&<howmany>\n to the stream // Write #<m>\n#<n>\n&<howmany>\n to the stream
// It is howmany/2 since we will be writing howmany/2 number of responses // It is howmany/2 since we will be writing howmany/2 number of responses
@ -61,8 +59,6 @@ pub async fn mupdate(handle: &CoreDB, con: &mut Connection, act: ActionGroup) ->
if let Some(done_howmany) = done_howmany { if let Some(done_howmany) = done_howmany {
return con.write_response(done_howmany as usize).await; return con.write_response(done_howmany as usize).await;
} else { } else {
return con return con.write_response(&**responses::fresp::R_SERVER_ERR).await;
.write_response(responses::fresp::R_SERVER_ERR.to_owned())
.await;
} }
} }

@ -34,9 +34,7 @@ pub async fn set(handle: &CoreDB, con: &mut Connection, act: ActionGroup) -> TRe
let howmany = act.howmany(); let howmany = act.howmany();
if howmany != 2 { if howmany != 2 {
// There should be exactly 2 arguments // There should be exactly 2 arguments
return con return con.write_response(&**responses::fresp::R_ACTION_ERR).await;
.write_response(responses::fresp::R_ACTION_ERR.to_owned())
.await;
} }
let mut it = act.into_iter(); let mut it = act.into_iter();
let did_we = { let did_we = {
@ -60,14 +58,13 @@ pub async fn set(handle: &CoreDB, con: &mut Connection, act: ActionGroup) -> TRe
}; };
if let Some(did_we) = did_we { if let Some(did_we) = did_we {
if did_we { if did_we {
con.write_response(responses::fresp::R_OKAY.to_owned()) con.write_response(&**responses::fresp::R_OKAY).await?;
.await?;
} else { } else {
con.write_response(responses::fresp::R_OVERWRITE_ERR.to_owned()) con.write_response(&**responses::fresp::R_OVERWRITE_ERR)
.await?; .await?;
} }
} else { } else {
con.write_response(responses::fresp::R_SERVER_ERR.to_owned()) con.write_response(&**responses::fresp::R_SERVER_ERR)
.await?; .await?;
} }
Ok(()) Ok(())

@ -42,9 +42,7 @@ use std::hint::unreachable_unchecked;
pub async fn sset(handle: &CoreDB, con: &mut Connection, act: ActionGroup) -> TResult<()> { pub async fn sset(handle: &CoreDB, con: &mut Connection, act: ActionGroup) -> TResult<()> {
let howmany = act.howmany(); let howmany = act.howmany();
if howmany & 1 == 1 || howmany == 0 { if howmany & 1 == 1 || howmany == 0 {
return con return con.write_response(&**responses::fresp::R_ACTION_ERR).await;
.write_response(responses::fresp::R_ACTION_ERR.to_owned())
.await;
} }
let mut failed = Some(false); let mut failed = Some(false);
{ {
@ -89,15 +87,13 @@ pub async fn sset(handle: &CoreDB, con: &mut Connection, act: ActionGroup) -> TR
} }
if let Some(failed) = failed { if let Some(failed) = failed {
if failed { if failed {
con.write_response(responses::fresp::R_OVERWRITE_ERR.to_owned()) con.write_response(&**responses::fresp::R_OVERWRITE_ERR)
.await .await
} else { } else {
con.write_response(responses::fresp::R_OKAY.to_owned()) con.write_response(&**responses::fresp::R_OKAY).await
.await
} }
} else { } else {
con.write_response(responses::fresp::R_SERVER_ERR.to_owned()) con.write_response(&**responses::fresp::R_SERVER_ERR).await
.await
} }
} }
@ -108,9 +104,7 @@ pub async fn sset(handle: &CoreDB, con: &mut Connection, act: ActionGroup) -> TR
pub async fn sdel(handle: &CoreDB, con: &mut Connection, act: ActionGroup) -> TResult<()> { pub async fn sdel(handle: &CoreDB, con: &mut Connection, act: ActionGroup) -> TResult<()> {
let howmany = act.howmany(); let howmany = act.howmany();
if howmany == 0 { if howmany == 0 {
return con return con.write_response(&**responses::fresp::R_ACTION_ERR).await;
.write_response(responses::fresp::R_ACTION_ERR.to_owned())
.await;
} }
let mut failed = Some(false); let mut failed = Some(false);
{ {
@ -150,14 +144,12 @@ pub async fn sdel(handle: &CoreDB, con: &mut Connection, act: ActionGroup) -> TR
} }
if let Some(failed) = failed { if let Some(failed) = failed {
if failed { if failed {
con.write_response(responses::fresp::R_NIL.to_owned()).await con.write_response(&**responses::fresp::R_NIL).await
} else { } else {
con.write_response(responses::fresp::R_OKAY.to_owned()) con.write_response(&**responses::fresp::R_OKAY).await
.await
} }
} else { } else {
con.write_response(responses::fresp::R_SERVER_ERR.to_owned()) con.write_response(&**responses::fresp::R_SERVER_ERR).await
.await
} }
} }
@ -168,9 +160,7 @@ pub async fn sdel(handle: &CoreDB, con: &mut Connection, act: ActionGroup) -> TR
pub async fn supdate(handle: &CoreDB, con: &mut Connection, act: ActionGroup) -> TResult<()> { pub async fn supdate(handle: &CoreDB, con: &mut Connection, act: ActionGroup) -> TResult<()> {
let howmany = act.howmany(); let howmany = act.howmany();
if howmany & 1 == 1 || howmany == 0 { if howmany & 1 == 1 || howmany == 0 {
return con return con.write_response(&**responses::fresp::R_ACTION_ERR).await;
.write_response(responses::fresp::R_ACTION_ERR.to_owned())
.await;
} }
let mut failed = Some(false); let mut failed = Some(false);
{ {
@ -215,13 +205,11 @@ pub async fn supdate(handle: &CoreDB, con: &mut Connection, act: ActionGroup) ->
} }
if let Some(failed) = failed { if let Some(failed) = failed {
if failed { if failed {
con.write_response(responses::fresp::R_NIL.to_owned()).await con.write_response(&**responses::fresp::R_NIL).await
} else { } else {
con.write_response(responses::fresp::R_OKAY.to_owned()) con.write_response(&**responses::fresp::R_OKAY).await
.await
} }
} else { } else {
con.write_response(responses::fresp::R_SERVER_ERR.to_owned()) con.write_response(&**responses::fresp::R_SERVER_ERR).await
.await
} }
} }

@ -34,9 +34,7 @@ pub async fn update(handle: &CoreDB, con: &mut Connection, act: ActionGroup) ->
let howmany = act.howmany(); let howmany = act.howmany();
if howmany != 2 { if howmany != 2 {
// There should be exactly 2 arguments // There should be exactly 2 arguments
return con return con.write_response(&**responses::fresp::R_ACTION_ERR).await;
.write_response(responses::fresp::R_ACTION_ERR.to_owned())
.await;
} }
let mut it = act.into_iter(); let mut it = act.into_iter();
let did_we = { let did_we = {
@ -60,14 +58,12 @@ pub async fn update(handle: &CoreDB, con: &mut Connection, act: ActionGroup) ->
}; };
if let Some(did_we) = did_we { if let Some(did_we) = did_we {
if did_we { if did_we {
con.write_response(responses::fresp::R_OKAY.to_owned()) con.write_response(&**responses::fresp::R_OKAY).await?;
.await?;
} else { } else {
con.write_response(responses::fresp::R_NIL.to_owned()) con.write_response(&**responses::fresp::R_NIL).await?;
.await?;
} }
} else { } else {
con.write_response(responses::fresp::R_SERVER_ERR.to_owned()) con.write_response(&**responses::fresp::R_SERVER_ERR)
.await?; .await?;
} }
Ok(()) Ok(())

@ -33,9 +33,7 @@ pub async fn uset(handle: &CoreDB, con: &mut Connection, act: ActionGroup) -> TR
// An odd number of arguments means that the number of keys // An odd number of arguments means that the number of keys
// is not the same as the number of values, we won't run this // is not the same as the number of values, we won't run this
// action at all // action at all
return con return con.write_response(&**responses::fresp::R_ACTION_ERR).await;
.write_response(responses::fresp::R_ACTION_ERR.to_owned())
.await;
} }
// Write #<m>\n#<n>\n&<howmany>\n to the stream // Write #<m>\n#<n>\n&<howmany>\n to the stream
// It is howmany/2 since we will be writing howmany/2 number of responses // It is howmany/2 since we will be writing howmany/2 number of responses
@ -55,8 +53,7 @@ pub async fn uset(handle: &CoreDB, con: &mut Connection, act: ActionGroup) -> TR
} }
}; };
if failed { if failed {
con.write_response(responses::fresp::R_SERVER_ERR.to_owned()) con.write_response(&**responses::fresp::R_SERVER_ERR).await
.await
} else { } else {
con.write_response(howmany / 2).await con.write_response(howmany / 2).await
} }

@ -87,6 +87,23 @@ impl Writable for Vec<u8> {
} }
} }
impl Writable for &'static [u8] {
fn write<'s>(
self,
con: &'s mut BufWriter<TcpStream>,
) -> Pin<Box<(dyn Future<Output = Result<(), Box<(dyn Error + 'static)>>> + Send + Sync + 's)>>
{
async fn write_bytes(
con: &mut BufWriter<TcpStream>,
resp: &[u8],
) -> Result<(), Box<dyn Error>> {
con.write(&resp).await?;
Ok(())
}
Box::pin(write_bytes(con, &self))
}
}
impl Writable for BytesWrapper { impl Writable for BytesWrapper {
fn write<'s>( fn write<'s>(
self, self,

Loading…
Cancel
Save