Skip to content

Commit

Permalink
Added export_shared_api, open_with_redis_string, create_from_redis_st…
Browse files Browse the repository at this point in the history
…ring (#136)

* WIP

* WIP (export rejson api and expose key_inner)

* WIP (commit before rolling-back to bisect/detect a crash)

* WIP open key using RedisModuleString

* WIP fix borrow of RedisKeyWritable::get_value

* WIP cleanup remove unnecessary code

* WIP cleanup

Co-authored-by: oshadmi <omer.shadmi@redislabs.com>
  • Loading branch information
rafie and oshadmi authored Mar 21, 2021
1 parent baeb1eb commit 06a9724
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ impl Context {
RedisKey::open(self.ctx, key)
}

pub fn open_with_redis_string(&self, string: *mut raw::RedisModuleString) -> RedisKeyWritable {
RedisKeyWritable::open_with_redis_string(self.ctx, string)
}

pub fn open_key_writable(&self, key: &str) -> RedisKeyWritable {
RedisKeyWritable::open(self.ctx, key)
}
Expand All @@ -214,6 +218,18 @@ impl Context {
return self.ctx;
}

pub fn export_shared_api(
&self,
func: *const ::std::os::raw::c_void,
name: *const ::std::os::raw::c_char,
) {
raw::export_shared_api(
self.ctx,
func,
name,
);
}

#[cfg(feature = "experimental-api")]
pub fn notify_keyspace_event(
&self,
Expand Down
15 changes: 14 additions & 1 deletion src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,20 @@ impl RedisKeyWritable {
self.key_type() == KeyType::Empty
}

pub fn get_value<T>(&self, redis_type: &RedisType) -> Result<Option<&mut T>, RedisError> {
pub fn open_with_redis_string(
ctx: *mut raw::RedisModuleCtx,
string: *mut raw::RedisModuleString,
) -> RedisKeyWritable {
let key_str = RedisString::create_from_redis_string(ctx, string);
let key_inner = raw::open_key(ctx, key_str.inner, to_raw_mode(KeyMode::ReadWrite));
RedisKeyWritable {
ctx,
key_inner,
key_str,
}
}

pub fn get_value<'a, 'b, T>(&'a self, redis_type: &RedisType) -> Result<Option<&'b mut T>, RedisError> {
verify_type(self.key_inner, redis_type)?;
let value =
unsafe { raw::RedisModule_ModuleTypeGetValue.unwrap()(self.key_inner) as *mut T };
Expand Down
8 changes: 8 additions & 0 deletions src/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,14 @@ pub fn subscribe_to_server_event(
unsafe { RedisModule_SubscribeToServerEvent.unwrap()(ctx, event, callback).into() }
}

pub fn export_shared_api(
ctx: *mut RedisModuleCtx,
func: *const ::std::os::raw::c_void,
name: *const ::std::os::raw::c_char,
) {
unsafe { RedisModule_ExportSharedAPI.unwrap()(ctx, name, func as *mut ::std::os::raw::c_void) };
}

#[cfg(feature = "experimental-api")]
pub fn notify_keyspace_event(
ctx: *mut RedisModuleCtx,
Expand Down
10 changes: 10 additions & 0 deletions src/redismodule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@ impl RedisString {
RedisString { ctx, inner }
}

pub fn create_from_redis_string(
ctx: *mut raw::RedisModuleCtx,
redis_string: *mut raw::RedisModuleString,
) -> RedisString {
RedisString {
ctx,
inner: redis_string,
}
}

pub fn from_ptr<'a>(ptr: *const raw::RedisModuleString) -> Result<&'a str, Utf8Error> {
let mut len: libc::size_t = 0;
let bytes = unsafe { raw::RedisModule_StringPtrLen.unwrap()(ptr, &mut len) };
Expand Down

0 comments on commit 06a9724

Please sign in to comment.