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

Changes from feature-search-json branch #144

Merged
merged 10 commits into from
Apr 28, 2021
16 changes: 16 additions & 0 deletions src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ impl Context {
RedisKey::open(self.ctx, key)
}

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

pub fn open_key_writable(&self, key: &str) -> RedisKeyWritable {
RedisKeyWritable::open(self.ctx, key)
}
Expand All @@ -219,6 +223,18 @@ impl Context {
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
36 changes: 19 additions & 17 deletions src/key.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
use libc::size_t;
use std::convert::TryFrom;
use std::os::raw::c_void;
use std::ptr;
use std::str::Utf8Error;
use std::time::Duration;

use libc::size_t;

use raw::KeyType;

use crate::from_byte_string;
use crate::native_types::RedisType;
use crate::raw;
use crate::redismodule::REDIS_OK;
use crate::RedisError;
use crate::redismodule::REDIS_OK;
use crate::RedisResult;
use crate::RedisString;

Expand All @@ -33,17 +34,15 @@ pub enum KeyMode {
pub struct RedisKey {
ctx: *mut raw::RedisModuleCtx,
key_inner: *mut raw::RedisModuleKey,
key_str: RedisString,
}

impl RedisKey {
pub fn open(ctx: *mut raw::RedisModuleCtx, key: &str) -> RedisKey {
let key_str = RedisString::create(ctx, key);
let key_inner = raw::open_key(ctx, key_str.inner, to_raw_mode(KeyMode::Read));
RedisKey {
ctx,
key_inner,
key_str,
ctx: ctx,
key_inner: key_inner,
}
}

Expand Down Expand Up @@ -127,23 +126,15 @@ impl Drop for RedisKey {
pub struct RedisKeyWritable {
ctx: *mut raw::RedisModuleCtx,
key_inner: *mut raw::RedisModuleKey,

// The Redis string
//
// This field is needed on the struct so that its Drop implementation gets
// called when it goes out of scope.
#[allow(dead_code)]
key_str: RedisString,
}

impl RedisKeyWritable {
pub fn open(ctx: *mut raw::RedisModuleCtx, key: &str) -> RedisKeyWritable {
let key_str = RedisString::create(ctx, key);
let key_inner = raw::open_key(ctx, key_str.inner, to_raw_mode(KeyMode::ReadWrite));
RedisKeyWritable {
ctx,
key_inner,
key_str,
ctx: ctx,
key_inner: key_inner,
}
}

Expand Down Expand Up @@ -277,7 +268,18 @@ 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,
key: *mut raw::RedisModuleString,
) -> RedisKeyWritable {
let key_inner = raw::open_key(ctx, key, to_raw_mode(KeyMode::ReadWrite));
RedisKeyWritable {
ctx: ctx,
key_inner: key_inner,
}
}

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 @@ -494,6 +494,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