Skip to content
This repository was archived by the owner on Jul 11, 2021. It is now read-only.

Commit b6851ec

Browse files
author
Simone Mosciatti
committed
use reference to contenxt instead of whole context
1 parent 8e5266c commit b6851ec

File tree

6 files changed

+235
-180
lines changed

6 files changed

+235
-180
lines changed

engine_pro

Submodule engine_pro updated from 603f76a to bd08ad9

redisql_lib/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn main() {
1515
cc::Build::new()
1616
.file("src/CDeps/Redis/redismodule.c")
1717
.include("src/CDeps/Redis/include")
18-
.debug(true)
18+
//.debug(true)
1919
.compile("libredismodule.a");
2020

2121
cc::Build::new()

redisql_lib/src/redis.rs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,8 @@ impl<'a> StatementCache<'a> for ReplicationBook {
203203
type ProtectedRedisContext =
204204
Arc<Mutex<Arc<Mutex<RefCell<Option<Context>>>>>>;
205205

206-
pub struct RedisContextSet<'a>(
207-
MutexGuard<'a, Arc<Mutex<RefCell<Option<Context>>>>>,
206+
pub struct RedisContextSet<'external>(
207+
MutexGuard<'external, Arc<Mutex<RefCell<Option<Context>>>>>,
208208
);
209209

210210
impl<'a> RedisContextSet<'a> {
@@ -219,6 +219,12 @@ impl<'a> RedisContextSet<'a> {
219219
}
220220
RedisContextSet(wrap)
221221
}
222+
pub fn release(&self) -> Context {
223+
let locked = self.0.lock().unwrap();
224+
let none_to_ctx = RefCell::new(None);
225+
locked.swap(&none_to_ctx);
226+
none_to_ctx.into_inner().unwrap()
227+
}
222228
}
223229

224230
impl<'a> Drop for RedisContextSet<'a> {
@@ -291,11 +297,11 @@ impl Loop {
291297
}
292298

293299
pub trait RedisReply {
294-
fn reply(&self, ctx: rm::Context) -> i32;
300+
fn reply(&self, ctx: &rm::Context) -> i32;
295301
}
296302

297303
impl RedisReply for sql::Entity {
298-
fn reply(&self, ctx: rm::Context) -> i32 {
304+
fn reply(&self, ctx: &rm::Context) -> i32 {
299305
match *self {
300306
sql::Entity::Integer { int } => {
301307
rm::ReplyWithLongLong(ctx, i64::from(int))
@@ -357,7 +363,7 @@ fn reply_with_done(
357363
rm::ffi::REDISMODULE_OK
358364
}
359365

360-
fn reply_with_array(ctx: rm::Context, array: Vec<sql::Row>) -> i32 {
366+
fn reply_with_array(ctx: &rm::Context, array: Vec<sql::Row>) -> i32 {
361367
let len = array.len() as c_long;
362368
unsafe {
363369
rm::ffi::RedisModule_ReplyWithArray.unwrap()(
@@ -373,21 +379,21 @@ fn reply_with_array(ctx: rm::Context, array: Vec<sql::Row>) -> i32 {
373379
);
374380
}
375381
for entity in row {
376-
entity.reply(ctx);
382+
entity.reply(&ctx);
377383
}
378384
}
379385
rm::ffi::REDISMODULE_OK
380386
}
381387

382388
impl RedisReply for sql::SQLite3Error {
383-
fn reply(&self, ctx: Context) -> i32 {
389+
fn reply(&self, ctx: &Context) -> i32 {
384390
let error = format!("{}", self);
385391
reply_with_error(ctx.as_ptr(), error)
386392
}
387393
}
388394

389395
impl RedisReply for RediSQLError {
390-
fn reply(&self, ctx: Context) -> i32 {
396+
fn reply(&self, ctx: &Context) -> i32 {
391397
let error = format!("{}", self);
392398
reply_with_error(ctx.as_ptr(), error)
393399
}
@@ -502,7 +508,8 @@ pub enum QueryResult {
502508
}
503509

504510
impl QueryResult {
505-
pub fn reply(self, ctx: rm::Context) -> i32 {
511+
pub fn reply(self, ctx: &rm::Context) -> i32 {
512+
debug!("Start replying!");
506513
match self {
507514
QueryResult::OK { .. } => reply_with_ok(ctx.as_ptr()),
508515
QueryResult::DONE { modified_rows, .. } => {
@@ -629,16 +636,12 @@ pub fn listen_and_execute<'a, L: 'a + LoopData>(
629636
match rx.recv() {
630637
Ok(Command::Exec { query, client }) => {
631638
debug!("Exec | Query = {:?}", query);
632-
633639
loopdata.with_contex_set(
634640
Context::thread_safe(&client),
635641
|_| {
636-
debug!("A");
637642
let result =
638643
do_execute(&loopdata.get_db(), query);
639-
debug!("B");
640644
return_value(&client, result);
641-
debug!("C");
642645
},
643646
);
644647
debug!("Exec | DONE, returning result");
@@ -997,8 +1000,9 @@ pub fn get_dbkeyptr_from_name(
9971000
name: &str,
9981001
) -> Result<*mut DBKey, i32> {
9991002
let context = Context::new(ctx);
1000-
let key_name = rm::RMString::new(context, name);
1001-
let key = OpenKey(context, &key_name, rm::ffi::REDISMODULE_WRITE);
1003+
let key_name = rm::RMString::new(&context, name);
1004+
let key =
1005+
OpenKey(&context, &key_name, rm::ffi::REDISMODULE_WRITE);
10021006
let safe_key = RedisKey { key };
10031007
let key_type = unsafe {
10041008
rm::ffi::RedisModule_KeyType.unwrap()(safe_key.key)
@@ -1067,7 +1071,7 @@ pub fn reply_with_error_from_key_type(
10671071
ctx: *mut rm::ffi::RedisModuleCtx,
10681072
key_type: i32,
10691073
) -> i32 {
1070-
let context = Context::new(ctx);
1074+
let context = &Context::new(ctx);
10711075
match key_type {
10721076
rm::ffi::REDISMODULE_KEYTYPE_EMPTY => {
10731077
ReplyWithError(context, "ERR - Error the key is empty\0")
@@ -1111,15 +1115,15 @@ fn remove_statement(
11111115

11121116
#[allow(non_snake_case)]
11131117
pub unsafe fn Replicate(
1114-
_ctx: rm::Context,
1118+
_ctx: &rm::Context,
11151119
_command: &str,
11161120
_argv: *mut *mut rm::ffi::RedisModuleString,
11171121
_argc: std::os::raw::c_int,
11181122
) {
11191123
}
11201124

11211125
pub fn register_function(
1122-
context: rm::Context,
1126+
context: &rm::Context,
11231127
name: String,
11241128
flags: String,
11251129
f: extern "C" fn(
@@ -1139,7 +1143,7 @@ pub fn register_function(
11391143
}
11401144

11411145
pub fn register_write_function(
1142-
ctx: rm::Context,
1146+
ctx: &rm::Context,
11431147
name: String,
11441148
f: extern "C" fn(
11451149
*mut rm::ffi::RedisModuleCtx,

redisql_lib/src/redis_type.rs

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,20 @@ pub mod ffi {
1313
include!(concat!(env!("OUT_DIR"), "/bindings_redis.rs"));
1414
}
1515

16-
#[allow(dead_code)]
17-
#[derive(Copy, Clone, Debug)]
16+
#[derive(Debug)]
1817
pub struct Context {
1918
ctx: *mut ffi::RedisModuleCtx,
19+
thread_safe: bool,
2020
}
2121

2222
impl Context {
2323
pub fn new(ctx: *mut ffi::RedisModuleCtx) -> Context {
24-
Context { ctx }
24+
Context {
25+
ctx,
26+
thread_safe: false,
27+
}
2528
}
26-
pub fn as_ptr(self) -> *mut ffi::RedisModuleCtx {
29+
pub fn as_ptr(&self) -> *mut ffi::RedisModuleCtx {
2730
self.ctx
2831
}
2932
pub fn thread_safe(blocked_client: &BlockedClient) -> Context {
@@ -32,19 +35,43 @@ impl Context {
3235
blocked_client.as_ptr(),
3336
)
3437
};
35-
Context { ctx }
38+
Context {
39+
ctx,
40+
thread_safe: true,
41+
}
42+
}
43+
pub fn lock(&self) {
44+
unsafe {
45+
ffi::RedisModule_ThreadSafeContextLock.unwrap()(
46+
self.as_ptr(),
47+
);
48+
}
49+
}
50+
pub fn release(&self) {
51+
unsafe {
52+
ffi::RedisModule_ThreadSafeContextUnlock.unwrap()(
53+
self.as_ptr(),
54+
);
55+
}
3656
}
3757
}
3858

39-
unsafe impl Send for Context {}
40-
unsafe impl Sync for Context {}
41-
42-
impl From<Context> for *mut ffi::RedisModuleCtx {
43-
fn from(c: Context) -> *mut ffi::RedisModuleCtx {
44-
c.ctx
59+
impl Drop for Context {
60+
fn drop(&mut self) {
61+
if self.thread_safe {
62+
debug!("Free thread safe context");
63+
unsafe {
64+
ffi::RedisModule_FreeThreadSafeContext.unwrap()(
65+
self.as_ptr(),
66+
);
67+
}
68+
}
4569
}
4670
}
4771

72+
unsafe impl Send for Context {}
73+
unsafe impl Sync for Context {}
74+
4875
pub struct BlockedClient {
4976
pub client: *mut ffi::RedisModuleBlockedClient,
5077
}
@@ -58,13 +85,13 @@ impl BlockedClient {
5885
}
5986

6087
#[derive(Debug)]
61-
pub struct RMString {
88+
pub struct RMString<'a> {
6289
ptr: *mut ffi::RedisModuleString,
63-
ctx: Context,
90+
ctx: &'a Context,
6491
}
6592

66-
impl RMString {
67-
pub fn new(ctx: Context, s: &str) -> RMString {
93+
impl<'a> RMString<'a> {
94+
pub fn new(ctx: &'a Context, s: &str) -> RMString<'a> {
6895
let ptr = unsafe {
6996
ffi::RedisModule_CreateString.unwrap()(
7097
ctx.as_ptr(),
@@ -79,7 +106,7 @@ impl RMString {
79106
}
80107
}
81108

82-
impl Drop for RMString {
109+
impl<'a> Drop for RMString<'a> {
83110
fn drop(&mut self) {
84111
unsafe {
85112
ffi::RedisModule_FreeString.unwrap()(
@@ -92,7 +119,7 @@ impl Drop for RMString {
92119

93120
#[allow(non_snake_case)]
94121
pub fn CreateCommand(
95-
ctx: Context,
122+
ctx: &Context,
96123
name: String,
97124
f: ffi::RedisModuleCmdFunc,
98125
flags: String,
@@ -117,15 +144,15 @@ pub fn CreateCommand(
117144
}
118145

119146
#[allow(non_snake_case)]
120-
pub fn ReplicateVerbatim(ctx: Context) -> i32 {
147+
pub fn ReplicateVerbatim(ctx: &Context) -> i32 {
121148
unsafe {
122149
ffi::RedisModule_ReplicateVerbatim.unwrap()(ctx.as_ptr())
123150
}
124151
}
125152

126153
#[allow(non_snake_case)]
127154
pub unsafe fn Replicate(
128-
ctx: Context,
155+
ctx: &Context,
129156
command: &str,
130157
argv: *mut *mut ffi::RedisModuleString,
131158
argc: c_int,
@@ -142,7 +169,7 @@ pub unsafe fn Replicate(
142169
}
143170

144171
#[allow(non_snake_case)]
145-
pub fn ReplyWithError(ctx: Context, error: &str) -> i32 {
172+
pub fn ReplyWithError(ctx: &Context, error: &str) -> i32 {
146173
unsafe {
147174
ffi::RedisModule_ReplyWithError.unwrap()(
148175
ctx.as_ptr(),
@@ -153,7 +180,7 @@ pub fn ReplyWithError(ctx: Context, error: &str) -> i32 {
153180

154181
#[allow(non_snake_case)]
155182
pub fn OpenKey(
156-
ctx: Context,
183+
ctx: &Context,
157184
name: &RMString,
158185
mode: i32,
159186
) -> *mut ffi::RedisModuleKey {
@@ -192,26 +219,26 @@ pub unsafe fn SaveStringBuffer(
192219
}
193220

194221
#[allow(non_snake_case)]
195-
pub fn ReplyWithNull(ctx: Context) -> i32 {
222+
pub fn ReplyWithNull(ctx: &Context) -> i32 {
196223
unsafe { ffi::RedisModule_ReplyWithNull.unwrap()(ctx.as_ptr()) }
197224
}
198225

199226
#[allow(non_snake_case)]
200-
pub fn ReplyWithLongLong(ctx: Context, ll: i64) -> i32 {
227+
pub fn ReplyWithLongLong(ctx: &Context, ll: i64) -> i32 {
201228
unsafe {
202229
ffi::RedisModule_ReplyWithLongLong.unwrap()(ctx.as_ptr(), ll)
203230
}
204231
}
205232

206233
#[allow(non_snake_case)]
207-
pub fn ReplyWithDouble(ctx: Context, dd: f64) -> i32 {
234+
pub fn ReplyWithDouble(ctx: &Context, dd: f64) -> i32 {
208235
unsafe {
209236
ffi::RedisModule_ReplyWithDouble.unwrap()(ctx.as_ptr(), dd)
210237
}
211238
}
212239

213240
#[allow(non_snake_case)]
214-
pub fn ReplyWithStringBuffer(ctx: Context, buffer: &[u8]) -> i32 {
241+
pub fn ReplyWithStringBuffer(ctx: &Context, buffer: &[u8]) -> i32 {
215242
let ptr = buffer.as_ptr() as *const c_char;
216243
let len = buffer.len();
217244
unsafe {

0 commit comments

Comments
 (0)