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

Commit 09f511a

Browse files
author
Simone Mosciatti
committed
implement Copy comand
1 parent 10a3389 commit 09f511a

3 files changed

Lines changed: 52 additions & 13 deletions

File tree

engine_pro

Submodule engine_pro updated from bd08ad9 to 0e5ef74

redisql_lib/src/redis.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,11 @@ pub enum Command {
489489
arguments: Vec<&'static str>,
490490
client: BlockedClient,
491491
},
492+
Copy {
493+
source: Arc<Mutex<sql::RawConnection>>,
494+
destination: Arc<Mutex<sql::RawConnection>>,
495+
client: BlockedClient,
496+
},
492497
}
493498

494499
pub enum QueryResult {
@@ -721,6 +726,20 @@ pub fn listen_and_execute<'a, L: 'a + LoopData>(
721726
},
722727
);
723728
}
729+
Ok(Command::Copy {
730+
source,
731+
destination,
732+
client,
733+
}) => {
734+
let source = source.lock().unwrap();
735+
let destination = destination.lock().unwrap();
736+
let result = match make_backup(&source, &destination)
737+
{
738+
Err(e) => Err(RediSQLError::from(e)),
739+
Ok(_) => Ok(QueryResult::OK {}),
740+
};
741+
return_value(&client, result);
742+
}
724743
Ok(Command::Stop) => {
725744
debug!("Stop, exiting from work loop");
726745
return;
@@ -1020,7 +1039,8 @@ pub fn get_dbkey_from_name(
10201039
name: &str,
10211040
) -> Result<DBKey, i32> {
10221041
let dbptr = get_dbkeyptr_from_name(ctx, name)?;
1023-
Ok(unsafe { dbptr.read() })
1042+
let dbkey = unsafe { dbptr.read() };
1043+
Ok(dbkey)
10241044
}
10251045

10261046
pub fn with_ch_and_loopdata<F>(

src/lib.rs

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -806,14 +806,13 @@ unsafe extern "C" fn rdb_load(
806806
}
807807
}
808808

809-
// TODO attention with the metadata table
810-
// What should happen?
811809
#[allow(non_snake_case)]
812810
extern "C" fn Backup(
813811
ctx: *mut r::rm::ffi::RedisModuleCtx,
814812
argv: *mut *mut r::rm::ffi::RedisModuleString,
815813
argc: ::std::os::raw::c_int,
816814
) -> i32 {
815+
debug!("Backup | Start");
817816
let (context, argvector) = r::create_argument(ctx, argv, argc);
818817

819818
if argvector.len() != 3 {
@@ -857,18 +856,38 @@ extern "C" fn Backup(
857856
}
858857
let dest_db = dest_db.unwrap();
859858

860-
let source_connection = &source_db.loop_data.get_db();
861-
let dest_connection = &dest_db.loop_data.get_db();
859+
let blocked_client = r::rm::BlockedClient {
860+
client: unsafe {
861+
r::rm::ffi::RedisModule_BlockClient.unwrap()(
862+
context.as_ptr(),
863+
Some(reply_create_statement),
864+
Some(timeout),
865+
Some(free_privdata),
866+
10000,
867+
)
868+
},
869+
};
862870

863-
let source_connection = source_connection.lock().unwrap();
864-
let dest_connection = dest_connection.lock().unwrap();
871+
let source_connection = source_db.loop_data.get_db();
872+
let dest_connection = dest_db.loop_data.get_db();
865873

866-
match r::make_backup(&source_connection, &dest_connection) {
867-
Err(e) => e.reply(&context),
868-
_ => {
869-
let ok = r::QueryResult::OK {};
870-
ok.reply(&context)
874+
let cmd = r::Command::Copy {
875+
source: source_connection,
876+
destination: dest_connection,
877+
client: blocked_client,
878+
};
879+
880+
let ch = &source_db.tx.clone();
881+
std::mem::forget(source_db);
882+
std::mem::forget(dest_db);
883+
884+
debug!("Backup | End");
885+
match ch.send(cmd) {
886+
Ok(()) => {
887+
debug!("Backup | Successfully send command");
888+
r::rm::ffi::REDISMODULE_OK
871889
}
890+
Err(_) => r::rm::ffi::REDISMODULE_OK,
872891
}
873892
}
874893

0 commit comments

Comments
 (0)