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

Commit ade5922

Browse files
author
Simone Mosciatti
committed
refactor and improve do_copy function
1 parent da3af14 commit ade5922

1 file changed

Lines changed: 39 additions & 17 deletions

File tree

redisql_lib/src/redis.rs

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,12 @@ pub struct Loop {
245245
redis_context: ProtectedRedisContext,
246246
}
247247

248+
impl Drop for Loop {
249+
fn drop(&mut self) {
250+
debug!("### Dropping Loop ###")
251+
}
252+
}
253+
248254
unsafe impl Send for Loop {}
249255

250256
pub trait LoopData {
@@ -498,7 +504,6 @@ pub enum Command {
498504
client: BlockedClient,
499505
},
500506
MakeCopy {
501-
source: DBKey,
502507
destination: DBKey,
503508
client: BlockedClient,
504509
},
@@ -560,26 +565,26 @@ pub fn do_query(
560565

561566
/// implements the copy of the source database into the destination one
562567
/// it also leak the two DBKeys
563-
pub fn do_copy(
564-
source: DBKey,
565-
destination: DBKey,
568+
pub fn do_copy<L: LoopData>(
569+
source_db: &Arc<Mutex<sql::RawConnection>>,
570+
destination_loopdata: &L,
566571
) -> Result<QueryResult, err::RediSQLError> {
567-
let source_connection = source.loop_data.get_db();
568-
let destination_connection = destination.loop_data.get_db();
572+
debug!("DoCopy | Start");
569573

570574
let backup_result = {
571-
let source_db = source_connection.lock().unwrap();
572-
let destination_db = destination_connection.lock().unwrap();
575+
let destination_db = destination_loopdata.get_db();
576+
let destination_db = destination_db.lock().unwrap();
577+
let source_db = source_db.lock().unwrap();
573578
match make_backup(&source_db, &destination_db) {
574579
Err(e) => Err(RediSQLError::from(e)),
575580
Ok(_) => Ok(QueryResult::OK {}),
576581
}
577582
};
578583

579-
restore_previous_statements(&destination.loop_data);
580-
581-
std::mem::forget(source);
582-
std::mem::forget(destination);
584+
if backup_result.is_ok() {
585+
restore_previous_statements(destination_loopdata);
586+
}
587+
debug!("DoCopy | End");
583588

584589
backup_result
585590
}
@@ -973,20 +978,37 @@ pub fn listen_and_execute<'a, L: 'a + LoopData>(
973978
);
974979
}
975980
Ok(Command::MakeCopy {
976-
source,
977981
destination,
978982
client,
979983
}) => {
980-
// The DBKeys MUST be leaked, they are leaked by do_copy
981-
let result = do_copy(source, destination);
982-
return_value(&client, result);
984+
loopdata.with_contex_set(
985+
Context::thread_safe(&client),
986+
|_| {
987+
debug!("MakeCopy | Doing do_copy");
988+
let destination_loopdata =
989+
&destination.loop_data;
990+
let result = do_copy(
991+
&loopdata.get_db(),
992+
destination_loopdata,
993+
);
994+
match result {
995+
Ok(_) => STATISTICS.copy_ok(),
996+
Err(_) => STATISTICS.copy_err(),
997+
};
998+
return_value(&client, result);
999+
},
1000+
);
1001+
std::mem::forget(destination);
9831002
}
9841003
Ok(Command::Stop) => {
9851004
debug!("Stop, exiting from work loop");
9861005
return;
9871006
}
9881007
Err(RecvError) => {
989-
debug!("RecvError, exiting from work loop");
1008+
debug!(
1009+
"RecvError {}, exiting from work loop",
1010+
RecvError
1011+
);
9901012
return;
9911013
}
9921014
}

0 commit comments

Comments
 (0)