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

add RESP3 new APIs #280

Merged
merged 26 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
66393c6
add RESP3 new APIs
gkorland Feb 8, 2023
abf94ae
handle Map, Set, Bool
gkorland Feb 8, 2023
8f3ddd4
add missing file
gkorland Feb 8, 2023
a4d462a
add clippy allow
gkorland Feb 8, 2023
5cc389f
fmt code
gkorland Feb 9, 2023
5d5417b
add example and test
gkorland Feb 9, 2023
3185635
add example
gkorland Feb 9, 2023
3a3c978
fix fail back case for array
gkorland Feb 9, 2023
18d4ac0
uncomment method compare
gkorland Feb 9, 2023
fca914b
Merge branch 'master' into gkorland-resp3-api
gkorland Feb 9, 2023
0bbe4aa
set uniqe port for the test
gkorland Feb 12, 2023
50b3e89
Merge branch 'master' into gkorland-resp3-api
gkorland Feb 12, 2023
70adf43
Merge branch 'master' into gkorland-resp3-api
MeirShpilraien Mar 23, 2023
7f44061
Added resp3 to CallReply and refactor CallReply to be more rusty
MeirShpilraien Mar 23, 2023
7d052ce
Added borrow API to CallReply. Document CallReply API.
MeirShpilraien Mar 24, 2023
b341954
Review fixes
MeirShpilraien Mar 25, 2023
071387c
Merge branch 'master' into gkorland-resp3-api
MeirShpilraien Mar 26, 2023
7feceec
Merge branch 'master' into gkorland-resp3-api
MeirShpilraien Mar 26, 2023
022b1be
Review fixes.
MeirShpilraien Mar 26, 2023
447f8ab
Merge branch 'master' into gkorland-resp3-api
MeirShpilraien Mar 26, 2023
83e020c
Review fixes.
MeirShpilraien Apr 2, 2023
674dbfa
Review fixes
MeirShpilraien Apr 4, 2023
f2f27e7
Use from_utf8_lossy on Display instead of printing NULL
MeirShpilraien Apr 5, 2023
d5e59eb
Format fixes.
MeirShpilraien Apr 5, 2023
84a8232
Review fixes.
MeirShpilraien Apr 5, 2023
c2395e8
Review fixes
MeirShpilraien Apr 5, 2023
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
62 changes: 20 additions & 42 deletions src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ impl Context {
#[allow(clippy::must_use_candidate)]
pub fn reply_simple_string(&self, s: &str) -> raw::Status {
let msg = Self::str_as_legal_resp_string(s);
unsafe { raw::RedisModule_ReplyWithSimpleString.unwrap()(self.ctx, msg.as_ptr()).into() }
raw::reply_with_simple_string(self.ctx, msg.as_ptr())
}

#[allow(clippy::must_use_candidate)]
Expand All @@ -225,52 +225,32 @@ impl Context {
#[allow(clippy::must_use_candidate)]
pub fn reply(&self, r: RedisResult) -> raw::Status {
match r {
Ok(RedisValue::Integer(v)) => unsafe {
raw::RedisModule_ReplyWithLongLong.unwrap()(self.ctx, v).into()
},

Ok(RedisValue::Float(v)) => unsafe {
raw::RedisModule_ReplyWithDouble.unwrap()(self.ctx, v).into()
},

Ok(RedisValue::SimpleStringStatic(s)) => unsafe {
Ok(RedisValue::Integer(v)) => raw::reply_with_long_long(self.ctx, v),
Ok(RedisValue::Float(v)) => raw::reply_with_double(self.ctx, v),
Ok(RedisValue::SimpleStringStatic(s)) => {
let msg = CString::new(s).unwrap();
raw::RedisModule_ReplyWithSimpleString.unwrap()(self.ctx, msg.as_ptr()).into()
},
raw::reply_with_simple_string(self.ctx, msg.as_ptr())
}

Ok(RedisValue::SimpleString(s)) => unsafe {
Ok(RedisValue::SimpleString(s)) => {
let msg = CString::new(s).unwrap();
raw::RedisModule_ReplyWithSimpleString.unwrap()(self.ctx, msg.as_ptr()).into()
},
raw::reply_with_simple_string(self.ctx, msg.as_ptr())
}

Ok(RedisValue::BulkString(s)) => unsafe {
raw::RedisModule_ReplyWithStringBuffer.unwrap()(
self.ctx,
s.as_ptr().cast::<c_char>(),
s.len(),
)
.into()
},
Ok(RedisValue::BulkString(s)) => {
raw::reply_with_string_buffer(self.ctx, s.as_ptr().cast::<c_char>(), s.len())
}

Ok(RedisValue::BulkRedisString(s)) => unsafe {
raw::RedisModule_ReplyWithString.unwrap()(self.ctx, s.inner).into()
},
Ok(RedisValue::BulkRedisString(s)) => raw::reply_with_string(self.ctx, s.inner),

Ok(RedisValue::StringBuffer(s)) => unsafe {
raw::RedisModule_ReplyWithStringBuffer.unwrap()(
self.ctx,
s.as_ptr().cast::<c_char>(),
s.len(),
)
.into()
},
Ok(RedisValue::StringBuffer(s)) => {
raw::reply_with_string_buffer(self.ctx, s.as_ptr().cast::<c_char>(), s.len())
}

Ok(RedisValue::Array(array)) => {
unsafe {
// According to the Redis source code this always succeeds,
// so there is no point in checking its return value.
raw::RedisModule_ReplyWithArray.unwrap()(self.ctx, array.len() as c_long);
}
// According to the Redis source code this always succeeds,
// so there is no point in checking its return value.
raw::reply_with_array(self.ctx, array.len() as c_long);

for elem in array {
self.reply(Ok(elem));
Expand All @@ -279,9 +259,7 @@ impl Context {
raw::Status::Ok
}

Ok(RedisValue::Null) => unsafe {
raw::RedisModule_ReplyWithNull.unwrap()(self.ctx).into()
},
Ok(RedisValue::Null) => raw::reply_with_null(self.ctx),
gkorland marked this conversation as resolved.
Show resolved Hide resolved

Ok(RedisValue::NoReply) => raw::Status::Ok,

Expand Down
61 changes: 61 additions & 0 deletions src/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,13 @@ pub fn call_reply_string(reply: *mut RedisModuleCallReply) -> String {
}

#[allow(clippy::not_unsafe_ptr_arg_deref)]
#[inline]
pub fn close_key(kp: *mut RedisModuleKey) {
unsafe { RedisModule_CloseKey.unwrap()(kp) }
}

#[allow(clippy::not_unsafe_ptr_arg_deref)]
#[inline]
pub fn open_key(
ctx: *mut RedisModuleCtx,
keyname: *mut RedisModuleString,
Expand All @@ -244,10 +246,29 @@ pub fn open_key(
}

#[allow(clippy::not_unsafe_ptr_arg_deref)]
#[inline]
pub fn reply_with_array(ctx: *mut RedisModuleCtx, len: c_long) -> Status {
unsafe { RedisModule_ReplyWithArray.unwrap()(ctx, len).into() }
}

#[allow(clippy::not_unsafe_ptr_arg_deref)]
#[inline]
pub fn reply_with_map(ctx: *mut RedisModuleCtx, len: c_long) -> Status {
unsafe { RedisModule_ReplyWithMap.unwrap()(ctx, len).into() }
gkorland marked this conversation as resolved.
Show resolved Hide resolved
}

#[allow(clippy::not_unsafe_ptr_arg_deref)]
#[inline]
pub fn reply_with_set(ctx: *mut RedisModuleCtx, len: c_long) -> Status {
unsafe { RedisModule_ReplyWithSet.unwrap()(ctx, len).into() }
}

#[allow(clippy::not_unsafe_ptr_arg_deref)]
#[inline]
pub fn reply_with_attribute(ctx: *mut RedisModuleCtx, len: c_long) -> Status {
unsafe { RedisModule_ReplyWithAttribute.unwrap()(ctx, len).into() }
}

#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub fn reply_with_error(ctx: *mut RedisModuleCtx, err: *const c_char) {
unsafe {
Expand All @@ -257,34 +278,64 @@ pub fn reply_with_error(ctx: *mut RedisModuleCtx, err: *const c_char) {
}

#[allow(clippy::not_unsafe_ptr_arg_deref)]
#[inline]
pub fn reply_with_null(ctx: *mut RedisModuleCtx) -> Status {
unsafe { RedisModule_ReplyWithNull.unwrap()(ctx).into() }
}

#[allow(clippy::not_unsafe_ptr_arg_deref)]
#[inline]
pub fn reply_with_bool(ctx: *mut RedisModuleCtx, b: c_int) -> Status {
unsafe { RedisModule_ReplyWithBool.unwrap()(ctx, b).into() }
}

#[allow(clippy::not_unsafe_ptr_arg_deref)]
#[inline]
pub fn reply_with_long_long(ctx: *mut RedisModuleCtx, ll: c_longlong) -> Status {
unsafe { RedisModule_ReplyWithLongLong.unwrap()(ctx, ll).into() }
}

#[allow(clippy::not_unsafe_ptr_arg_deref)]
#[inline]
pub fn reply_with_double(ctx: *mut RedisModuleCtx, f: c_double) -> Status {
unsafe { RedisModule_ReplyWithDouble.unwrap()(ctx, f).into() }
}

#[allow(clippy::not_unsafe_ptr_arg_deref)]
#[inline]
pub fn reply_with_string(ctx: *mut RedisModuleCtx, s: *mut RedisModuleString) -> Status {
unsafe { RedisModule_ReplyWithString.unwrap()(ctx, s).into() }
}

#[allow(clippy::not_unsafe_ptr_arg_deref)]
#[inline]
pub fn reply_with_simple_string(ctx: *mut RedisModuleCtx, s: *const c_char) -> Status {
unsafe { RedisModule_ReplyWithSimpleString.unwrap()(ctx, s).into() }
}

#[allow(clippy::not_unsafe_ptr_arg_deref)]
#[inline]
pub fn reply_with_string_buffer(ctx: *mut RedisModuleCtx, s: *const c_char, len: size_t) -> Status {
unsafe { RedisModule_ReplyWithStringBuffer.unwrap()(ctx, s, len).into() }
}

// Sets the expiry on a key.
//
// Expire is in milliseconds.
#[allow(clippy::not_unsafe_ptr_arg_deref)]
#[inline]
pub fn set_expire(key: *mut RedisModuleKey, expire: c_longlong) -> Status {
unsafe { RedisModule_SetExpire.unwrap()(key, expire).into() }
}

#[allow(clippy::not_unsafe_ptr_arg_deref)]
#[inline]
pub fn string_dma(key: *mut RedisModuleKey, len: *mut size_t, mode: KeyMode) -> *mut c_char {
unsafe { RedisModule_StringDMA.unwrap()(key, len, mode.bits) }
}

#[allow(clippy::not_unsafe_ptr_arg_deref)]
#[inline]
pub fn string_truncate(key: *mut RedisModuleKey, new_len: size_t) -> Status {
unsafe { RedisModule_StringTruncate.unwrap()(key, new_len).into() }
}
Expand Down Expand Up @@ -375,6 +426,7 @@ where
}

#[allow(clippy::not_unsafe_ptr_arg_deref)]
#[inline]
pub fn hash_set(key: *mut RedisModuleKey, field: &str, value: *mut RedisModuleString) -> Status {
let field = CString::new(field).unwrap();

Expand All @@ -391,6 +443,7 @@ pub fn hash_set(key: *mut RedisModuleKey, field: &str, value: *mut RedisModuleSt
}

#[allow(clippy::not_unsafe_ptr_arg_deref)]
#[inline]
pub fn hash_del(key: *mut RedisModuleKey, field: &str) -> Status {
let field = CString::new(field).unwrap();

Expand All @@ -411,6 +464,7 @@ pub fn hash_del(key: *mut RedisModuleKey, field: &str) -> Status {
}

#[allow(clippy::not_unsafe_ptr_arg_deref)]
#[inline]
pub fn list_push(
key: *mut RedisModuleKey,
list_where: Where,
Expand All @@ -420,37 +474,44 @@ pub fn list_push(
}

#[allow(clippy::not_unsafe_ptr_arg_deref)]
#[inline]
pub fn list_pop(key: *mut RedisModuleKey, list_where: Where) -> *mut RedisModuleString {
unsafe { RedisModule_ListPop.unwrap()(key, list_where as i32) }
}

// Returns pointer to the C string, and sets len to its length
#[allow(clippy::not_unsafe_ptr_arg_deref)]
#[inline]
pub fn string_ptr_len(s: *const RedisModuleString, len: *mut size_t) -> *const c_char {
unsafe { RedisModule_StringPtrLen.unwrap()(s, len) }
}

#[allow(clippy::not_unsafe_ptr_arg_deref)]
#[inline]
pub fn string_retain_string(ctx: *mut RedisModuleCtx, s: *mut RedisModuleString) {
unsafe { RedisModule_RetainString.unwrap()(ctx, s) }
}

#[allow(clippy::not_unsafe_ptr_arg_deref)]
#[inline]
pub fn string_to_longlong(s: *const RedisModuleString, len: *mut i64) -> Status {
unsafe { RedisModule_StringToLongLong.unwrap()(s, len).into() }
}

#[allow(clippy::not_unsafe_ptr_arg_deref)]
#[inline]
pub fn string_to_double(s: *const RedisModuleString, len: *mut f64) -> Status {
unsafe { RedisModule_StringToDouble.unwrap()(s, len).into() }
}

#[allow(clippy::not_unsafe_ptr_arg_deref)]
#[inline]
pub fn string_set(key: *mut RedisModuleKey, s: *mut RedisModuleString) -> Status {
unsafe { RedisModule_StringSet.unwrap()(key, s).into() }
}

#[allow(clippy::not_unsafe_ptr_arg_deref)]
#[inline]
pub fn replicate_verbatim(ctx: *mut RedisModuleCtx) -> Status {
unsafe { RedisModule_ReplicateVerbatim.unwrap()(ctx).into() }
}
Expand Down