11extern crate uuid;
22extern crate fnv;
33
4- use std:: mem;
54use std:: ffi:: { CString , CStr } ;
65use std:: string;
76use std:: fs:: File ;
87use std:: io:: BufReader ;
98
10- use std:: os:: raw:: c_char;
119use std:: os:: raw:: c_long;
1210
1311use std:: io:: { Read , Write } ;
@@ -249,39 +247,37 @@ pub fn create_rm_string(ctx: *mut rm::ffi::RedisModuleCtx,
249247*/
250248
251249pub trait RedisReply {
252- fn reply ( & self , ctx : * mut rm:: ffi :: RedisModuleCtx ) -> i32 ;
250+ fn reply ( & self , ctx : rm:: Context ) -> i32 ;
253251}
254252
255253impl RedisReply for sql:: Entity {
256- fn reply ( & self , ctx : * mut rm:: ffi:: RedisModuleCtx ) -> i32 {
257- unsafe {
258- match * self {
259- sql:: Entity :: Integer { int } => {
260- rm:: ReplyWithLongLong ( ctx, int as i64 )
261- }
262- sql:: Entity :: Float { float } => {
263- rm:: ReplyWithDouble ( ctx, float)
264- }
265- sql:: Entity :: Text { ref text } => {
266- rm:: ReplyWithStringBuffer ( ctx, text. as_bytes ( ) )
267- }
268- sql:: Entity :: Blob { ref blob } => {
269- rm:: ReplyWithStringBuffer ( ctx, blob. as_bytes ( ) )
270- }
271- sql:: Entity :: Null => rm:: ReplyWithNull ( ctx) ,
272- sql:: Entity :: OK { to_replicate } => {
273- QueryResult :: OK { to_replicate } . reply ( ctx)
274- }
275- sql:: Entity :: DONE {
276- modified_rows,
277- to_replicate,
278- } => {
279- QueryResult :: DONE {
280- modified_rows,
281- to_replicate,
282- }
283- . reply ( ctx)
284- }
254+ fn reply ( & self , ctx : rm:: Context ) -> i32 {
255+ match * self {
256+ sql:: Entity :: Integer { int } => {
257+ rm:: ReplyWithLongLong ( ctx, i64:: from ( int) )
258+ }
259+ sql:: Entity :: Float { float } => {
260+ rm:: ReplyWithDouble ( ctx, float)
261+ }
262+ sql:: Entity :: Text { ref text } => {
263+ rm:: ReplyWithStringBuffer ( ctx, text. as_bytes ( ) )
264+ }
265+ sql:: Entity :: Blob { ref blob } => {
266+ rm:: ReplyWithStringBuffer ( ctx, blob. as_bytes ( ) )
267+ }
268+ sql:: Entity :: Null => rm:: ReplyWithNull ( ctx) ,
269+ sql:: Entity :: OK { to_replicate } => {
270+ QueryResult :: OK { to_replicate } . reply ( ctx)
271+ }
272+ sql:: Entity :: DONE {
273+ modified_rows,
274+ to_replicate,
275+ } => {
276+ QueryResult :: DONE {
277+ modified_rows,
278+ to_replicate,
279+ }
280+ . reply ( ctx)
285281 }
286282 }
287283 }
@@ -318,23 +314,21 @@ fn reply_with_done(ctx: *mut rm::ffi::RedisModuleCtx,
318314 reply_with_simple_string ( ctx, "DONE\0 " ) ;
319315 unsafe {
320316 rm:: ffi:: RedisModule_ReplyWithLongLong
321- . unwrap ( ) ( ctx, modified_rows as i64 ) ;
317+ . unwrap ( ) ( ctx, i64:: from ( modified_rows ) ) ;
322318 }
323319 rm:: ffi:: REDISMODULE_OK
324320}
325321
326- fn reply_with_array ( ctx : * mut rm:: ffi:: RedisModuleCtx ,
327- array : Vec < sql:: Row > )
328- -> i32 {
322+ fn reply_with_array ( ctx : rm:: Context , array : Vec < sql:: Row > ) -> i32 {
329323 let len = array. len ( ) as c_long ;
330324 unsafe {
331- rm:: ffi:: RedisModule_ReplyWithArray . unwrap ( ) ( ctx, len) ;
325+ rm:: ffi:: RedisModule_ReplyWithArray . unwrap ( ) ( ctx. as_ptr ( ) ,
326+ len) ;
332327 }
333328 for row in array {
334329 unsafe {
335- rm:: ffi:: RedisModule_ReplyWithArray . unwrap ( ) ( ctx,
336- row. len ( ) as
337- c_long ) ;
330+ rm:: ffi:: RedisModule_ReplyWithArray
331+ . unwrap ( ) ( ctx. as_ptr ( ) , row. len ( ) as c_long ) ;
338332 }
339333 for entity in row {
340334 entity. reply ( ctx) ;
@@ -345,16 +339,16 @@ fn reply_with_array(ctx: *mut rm::ffi::RedisModuleCtx,
345339
346340
347341impl RedisReply for sql:: SQLite3Error {
348- fn reply ( & self , ctx : * mut rm :: ffi :: RedisModuleCtx ) -> i32 {
342+ fn reply ( & self , ctx : Context ) -> i32 {
349343 let error = format ! ( "{}" , self ) ;
350- reply_with_error ( ctx, error)
344+ reply_with_error ( ctx. as_ptr ( ) , error)
351345 }
352346}
353347
354348impl RedisReply for RediSQLError {
355- fn reply ( & self , ctx : * mut rm :: ffi :: RedisModuleCtx ) -> i32 {
349+ fn reply ( & self , ctx : Context ) -> i32 {
356350 let error = format ! ( "{}" , self ) ;
357- reply_with_error ( ctx, error)
351+ reply_with_error ( ctx. as_ptr ( ) , error)
358352 }
359353}
360354
@@ -466,11 +460,11 @@ pub enum QueryResult {
466460}
467461
468462impl QueryResult {
469- pub fn reply ( self , ctx : * mut rm:: ffi :: RedisModuleCtx ) -> i32 {
463+ pub fn reply ( self , ctx : rm:: Context ) -> i32 {
470464 match self {
471- QueryResult :: OK { .. } => reply_with_ok ( ctx) ,
465+ QueryResult :: OK { .. } => reply_with_ok ( ctx. as_ptr ( ) ) ,
472466 QueryResult :: DONE { modified_rows, .. } => {
473- reply_with_done ( ctx, modified_rows)
467+ reply_with_done ( ctx. as_ptr ( ) , modified_rows)
474468 }
475469 QueryResult :: Array { array, .. } => {
476470 reply_with_array ( ctx, array)
@@ -837,11 +831,11 @@ pub fn make_backup(conn1: &sql::RawConnection,
837831 match sql:: create_backup ( conn1, conn2) {
838832 Err ( e) => Err ( e) ,
839833 Ok ( bk) => {
840- let mut result = sql:: backup_step ( bk, 1 ) ;
834+ let mut result = unsafe { sql:: BackupStep ( & bk, 1 ) } ;
841835 while sql:: backup_should_step_again ( result) {
842- result = sql:: backup_step ( bk, 1 ) ;
836+ result = unsafe { sql:: BackupStep ( & bk, 1 ) } ;
843837 }
844- sql:: backup_finish ( bk) ;
838+ unsafe { sql:: BackupFinish ( & bk) } ;
845839 Ok ( result)
846840 }
847841 }
@@ -850,15 +844,15 @@ pub fn make_backup(conn1: &sql::RawConnection,
850844pub fn create_backup ( conn : & sql:: RawConnection ,
851845 path : & str )
852846 -> Result < i32 , sql:: SQLite3Error > {
853- match sql:: open_connection ( path) {
847+ match sql:: RawConnection :: open_connection ( path) {
854848 Err ( e) => Err ( e) ,
855849 Ok ( new_db) => make_backup ( conn, & new_db) ,
856850 }
857851}
858852
859- pub fn write_file_to_rdb ( f : File ,
860- rdb : * mut rm:: ffi:: RedisModuleIO )
861- -> Result < ( ) , std:: io:: Error > {
853+ pub unsafe fn write_file_to_rdb ( f : File ,
854+ rdb : * mut rm:: ffi:: RedisModuleIO )
855+ -> Result < ( ) , std:: io:: Error > {
862856
863857 let block_size = 1024 * 4 as i64 ;
864858 let lenght = f. metadata ( ) . unwrap ( ) . len ( ) ;
@@ -871,12 +865,8 @@ pub fn write_file_to_rdb(f: File,
871865 loop {
872866 let mut tw = to_write. clone ( ) ;
873867 match buffer. read ( tw. as_mut_slice ( ) ) {
874- Ok ( 0 ) => {
875- return Ok ( ( ) ) ;
876- }
877- Ok ( _n) => {
878- rm:: SaveStringBuffer ( rdb, tw. as_slice ( ) ) ;
879- }
868+ Ok ( 0 ) => return Ok ( ( ) ) ,
869+ Ok ( _n) => rm:: SaveStringBuffer ( rdb, tw. as_slice ( ) ) ,
880870 Err ( e) => return Err ( e) ,
881871 }
882872 }
@@ -897,25 +887,22 @@ impl Drop for SafeRedisModuleString {
897887 }
898888}
899889
900- pub fn write_rdb_to_file ( f : & mut File ,
901- rdb : * mut rm:: ffi:: RedisModuleIO )
902- -> Result < ( ) , std:: io:: Error > {
890+ pub unsafe fn write_rdb_to_file ( f : & mut File ,
891+ rdb : * mut rm:: ffi:: RedisModuleIO )
892+ -> Result < ( ) , std:: io:: Error > {
903893
904894 let blocks = rm:: LoadSigned ( rdb) ;
905895 for _ in 0 ..blocks {
906896 let mut dimension: usize = 0 ;
907897 let c_str_ptr = SafeRedisModuleString {
908- ptr : unsafe {
909- rm:: ffi:: RedisModule_LoadStringBuffer
910- . unwrap ( ) ( rdb, & mut dimension)
911- } ,
898+ ptr : rm:: ffi:: RedisModule_LoadStringBuffer
899+ . unwrap ( ) ( rdb, & mut dimension) ,
912900 } ;
913901 if dimension == 0 {
914902 break ;
915903 }
916- let slice = unsafe {
917- slice:: from_raw_parts ( c_str_ptr. ptr as * mut u8 , dimension)
918- } ;
904+ let slice = slice:: from_raw_parts ( c_str_ptr. ptr as * mut u8 ,
905+ dimension) ;
919906 let y = f. write_all ( slice) ;
920907 // mem::forget(slice);
921908 if let Err ( e) = y {
@@ -930,7 +917,7 @@ pub fn get_dbkey_from_name(ctx: *mut rm::ffi::RedisModuleCtx,
930917 -> Result < Box < DBKey > , i32 > {
931918 let context = Context :: new ( ctx) ;
932919 let key_name = rm:: RMString :: new ( context, name) ;
933- let key = OpenKey ( context, key_name, rm:: ffi:: REDISMODULE_WRITE ) ;
920+ let key = OpenKey ( context, & key_name, rm:: ffi:: REDISMODULE_WRITE ) ;
934921 let safe_key = RedisKey { key } ;
935922 let key_type = unsafe {
936923 rm:: ffi:: RedisModule_KeyType . unwrap ( ) ( safe_key. key )
0 commit comments