Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: convert call and write functions to use RedisString #248

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/threads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn threads(_: &Context, _args: Vec<RedisString>) -> RedisResult {

loop {
let ctx = thread_ctx.lock();
ctx.call("INCR", &["threads"]).unwrap();
ctx.call("INCR", &[&ctx.create_string("threads")]).unwrap();
// release the lock as soon as we're done accessing redis memory
drop(ctx);
thread::sleep(Duration::from_millis(1000));
Expand Down
14 changes: 4 additions & 10 deletions src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,8 @@ impl Context {
}
}

pub fn call(&self, command: &str, args: &[&str]) -> RedisResult {
let terminated_args: Vec<RedisString> = args
.iter()
.map(|s| RedisString::create(self.ctx, s))
.collect();

let inner_args: Vec<*mut raw::RedisModuleString> =
terminated_args.iter().map(|s| s.inner).collect();
pub fn call(&self, command: &str, args: &[&RedisString]) -> RedisResult {
let inner_args: Vec<*mut raw::RedisModuleString> = args.iter().map(|s| s.inner).collect();

let cmd = CString::new(command).unwrap();
let reply: *mut raw::RedisModuleCallReply = unsafe {
Expand All @@ -112,7 +106,7 @@ impl Context {
cmd.as_ptr(),
raw::FMT,
inner_args.as_ptr() as *mut c_char,
terminated_args.len(),
args.len(),
)
};
let result = Self::parse_call_reply(reply);
Expand Down Expand Up @@ -346,7 +340,7 @@ impl Context {
}
_ => {
// Call "info server"
if let Ok(info) = self.call("info", &["server"]) {
if let Ok(info) = self.call("info", &[&self.create_string("server")]) {
Context::version_from_info(info)
} else {
Err(RedisError::Str("Error calling \"info server\""))
Expand Down
24 changes: 14 additions & 10 deletions src/key.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::convert::TryFrom;
use std::ffi::{CString, NulError};
use std::os::raw::c_void;
use std::ptr;
use std::str::Utf8Error;
use std::time::Duration;

use libc::size_t;
Expand Down Expand Up @@ -75,11 +75,12 @@ impl RedisKey {
self.key_inner == null_key
}

pub fn read(&self) -> Result<Option<String>, RedisError> {
pub fn read(&self) -> Result<Option<RedisString>, RedisError> {
let val = if self.is_null() {
None
} else {
Some(read_key(self.key_inner)?)
let (cstr, length) = read_key(self.key_inner)?;
Some(RedisString::create_from_cstring(self.ctx, &cstr, length))
};
Ok(val)
}
Expand Down Expand Up @@ -156,8 +157,11 @@ impl RedisKeyWritable {
}
*/

pub fn read(&self) -> Result<Option<String>, RedisError> {
Ok(Some(read_key(self.key_inner)?))
pub fn read(&self) -> Result<Option<RedisString>, RedisError> {
let (cstr, length) = read_key(self.key_inner)?;
Ok(Some(RedisString::create_from_cstring(
self.ctx, &cstr, length,
)))
}

#[allow(clippy::must_use_candidate)]
Expand Down Expand Up @@ -253,8 +257,7 @@ impl RedisKeyWritable {
}
}

pub fn write(&self, val: &str) -> RedisResult {
let val_str = RedisString::create(self.ctx, val);
pub fn write(&self, val_str: &RedisString) -> RedisResult {
match raw::string_set(self.key_inner, val_str.inner) {
raw::Status::Ok => REDIS_OK,
raw::Status::Err => Err(RedisError::Str("Error while setting key")),
Expand Down Expand Up @@ -453,12 +456,13 @@ impl Drop for RedisKeyWritable {
}
}

fn read_key(key: *mut raw::RedisModuleKey) -> Result<String, Utf8Error> {
fn read_key(key: *mut raw::RedisModuleKey) -> Result<(CString, size_t), NulError> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not return a slice here?

let mut length: size_t = 0;
from_byte_string(
let cstr = from_byte_string(
raw::string_dma(key, &mut length, raw::KeyMode::READ),
length,
)
)?;
Ok((cstr, length))
}

/// Get an arbitrary number of hash fields from a key by batching calls
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//#![allow(dead_code)]

pub use crate::context::InfoContext;
use std::ffi::{CString, NulError};
use std::os::raw::c_char;
use std::str::Utf8Error;
use strum_macros::AsRefStr;
extern crate num_traits;

Expand Down Expand Up @@ -52,14 +52,14 @@ pub enum LogLevel {
Warning,
}

fn from_byte_string(byte_str: *const c_char, length: size_t) -> Result<String, Utf8Error> {
fn from_byte_string(byte_str: *const c_char, length: size_t) -> Result<CString, NulError> {
let mut vec_str: Vec<u8> = Vec::with_capacity(length as usize);
for j in 0..length {
let byte = unsafe { *byte_str.add(j) } as u8;
vec_str.insert(j, byte);
}

String::from_utf8(vec_str).map_err(|e| e.utf8_error())
CString::new(vec_str)
}

pub fn base_info_func(
Expand Down
58 changes: 56 additions & 2 deletions src/redismodule.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::borrow::Borrow;
use std::convert::TryFrom;
use std::ffi::CString;
use std::ffi::{CStr, CString};
use std::fmt;
use std::fmt::Display;
use std::os::raw::{c_char, c_int, c_void};
Expand Down Expand Up @@ -103,7 +103,19 @@ impl RedisString {
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub fn create(ctx: *mut raw::RedisModuleCtx, s: &str) -> Self {
let str = CString::new(s).unwrap();
let inner = unsafe { raw::RedisModule_CreateString.unwrap()(ctx, str.as_ptr(), s.len()) };
Self::create_from_cstring(ctx, &str, s.len())
}

pub fn create_from_cstring(ctx: *mut raw::RedisModuleCtx, s: &CStr, len: usize) -> Self {
let inner = unsafe { raw::RedisModule_CreateString.unwrap()(ctx, s.as_ptr(), len) };

Self { ctx, inner }
}

pub fn create_from_bytes(ctx: *mut raw::RedisModuleCtx, s: &[u8]) -> Self {
let inner = unsafe {
raw::RedisModule_CreateString.unwrap()(ctx, s.as_ptr() as *const i8, s.len())
};

Self { ctx, inner }
}
Expand Down Expand Up @@ -257,6 +269,48 @@ impl From<RedisString> for String {
}
}

trait IntoRedisString {
fn into_redis_string(self, ctx: *mut raw::RedisModuleCtx) -> RedisString;
}

macro_rules! generate_into_redis_string {
($($t: ty) *) => {
$(
impl IntoRedisString for $t {
fn into_redis_string(self, ctx: *mut raw::RedisModuleCtx) -> RedisString {
RedisString::create(ctx, &self.to_string())
}
}
)*
};
}

impl IntoRedisString for &str {
fn into_redis_string(self, ctx: *mut raw::RedisModuleCtx) -> RedisString {
RedisString::create(ctx, &self)
}
}

impl IntoRedisString for String {
fn into_redis_string(self, ctx: *mut raw::RedisModuleCtx) -> RedisString {
RedisString::create(ctx, &self)
}
}

impl IntoRedisString for Vec<u8> {
fn into_redis_string(self, ctx: *mut raw::RedisModuleCtx) -> RedisString {
RedisString::create_from_bytes(ctx, &self)
}
}

impl IntoRedisString for &[u8] {
fn into_redis_string(self, ctx: *mut raw::RedisModuleCtx) -> RedisString {
RedisString::create_from_bytes(ctx, self)
}
}

generate_into_redis_string!(bool u8 i8 u16 i16 u32 i32 u64 i64 usize isize);

///////////////////////////////////////////////////

#[derive(Debug)]
Expand Down