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

Commit 57aa113

Browse files
author
Simone Mosciatti
committed
more standard and correct way to invoke REDISQL.COPY in MakeCopy
1 parent ade5922 commit 57aa113

1 file changed

Lines changed: 75 additions & 68 deletions

File tree

src/commands.rs

Lines changed: 75 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -894,81 +894,88 @@ pub extern "C" fn MakeCopy(
894894
STATISTICS.copy();
895895
let (context, argvector) = r::create_argument(ctx, argv, argc);
896896

897-
if argvector.len() != 3 {
898-
let error = CString::new(
899-
"Wrong number of arguments, it accepts exactly 3",
900-
)
901-
.unwrap();
902-
STATISTICS.copy_err();
903-
return unsafe {
904-
r::rm::ffi::RedisModule_ReplyWithError.unwrap()(
905-
context.as_ptr(),
906-
error.as_ptr(),
907-
)
908-
};
909-
}
910-
911-
let source_db =
912-
get_dbkey_from_name(context.as_ptr(), argvector[1]);
913-
if source_db.is_err() {
914-
let error =
915-
CString::new("Error in opening the SOURCE database")
916-
.unwrap();
917-
STATISTICS.copy_err();
918-
return unsafe {
919-
r::rm::ffi::RedisModule_ReplyWithError.unwrap()(
920-
context.as_ptr(),
921-
error.as_ptr(),
922-
)
923-
};
924-
}
925-
let source_db = source_db.unwrap();
897+
match argvector.len() {
898+
3 => with_ch_and_loopdata(
899+
context.as_ptr(),
900+
argvector[1],
901+
|ch_loopdata| match ch_loopdata {
902+
Err(key_type) => {
903+
STATISTICS.copy_err();
926904

927-
let dest_db = get_dbkey_from_name(context.as_ptr(), argvector[2]);
928-
if dest_db.is_err() {
929-
let error =
905+
reply_with_error_from_key_type(
906+
context.as_ptr(),
907+
key_type,
908+
)
909+
}
910+
Ok((ch, _loopdata)) => {
911+
let dest_db = get_dbkey_from_name(
912+
context.as_ptr(),
913+
argvector[2],
914+
);
915+
if dest_db.is_err() {
916+
let error =
930917
CString::new("Error in opening the DESTINATION database")
931918
.unwrap();
932-
STATISTICS.copy_err();
933-
return unsafe {
934-
r::rm::ffi::RedisModule_ReplyWithError.unwrap()(
935-
context.as_ptr(),
936-
error.as_ptr(),
937-
)
938-
};
939-
}
940-
let dest_db = dest_db.unwrap();
941-
942-
let blocked_client = r::rm::BlockedClient {
943-
client: unsafe {
944-
r::rm::ffi::RedisModule_BlockClient.unwrap()(
945-
context.as_ptr(),
946-
Some(reply_create_statement),
947-
Some(timeout),
948-
Some(free_privdata),
949-
10000,
950-
)
951-
},
952-
};
919+
STATISTICS.copy_err();
920+
return unsafe {
921+
r::rm::ffi::RedisModule_ReplyWithError
922+
.unwrap()(
923+
context.as_ptr(),
924+
error.as_ptr(),
925+
)
926+
};
927+
}
928+
let dest_db = dest_db.unwrap();
953929

954-
let ch = &source_db.tx.clone();
930+
let blocked_client = r::rm::BlockedClient {
931+
client: unsafe {
932+
r::rm::ffi::RedisModule_BlockClient
933+
.unwrap()(
934+
context.as_ptr(),
935+
Some(reply_create_statement),
936+
Some(timeout),
937+
Some(free_privdata),
938+
10000,
939+
)
940+
},
941+
};
955942

956-
let cmd = r::Command::MakeCopy {
957-
source: source_db,
958-
destination: dest_db,
959-
client: blocked_client,
960-
};
943+
let cmd = r::Command::MakeCopy {
944+
destination: dest_db,
945+
client: blocked_client,
946+
};
961947

962-
debug!("MakeCopy | End");
963-
match ch.send(cmd) {
964-
Ok(()) => {
965-
debug!("MakeCopy | Successfully send command");
966-
unsafe {
967-
Replicate(&context, "REDISQL.COPY.NOW", argv, argc);
968-
}
969-
r::rm::ffi::REDISMODULE_OK
948+
match ch.send(cmd) {
949+
Ok(()) => {
950+
debug!("MakeCopy | Successfully send command");
951+
unsafe {
952+
Replicate(
953+
&context,
954+
"REDISQL.COPY.NOW",
955+
argv,
956+
argc,
957+
);
958+
}
959+
r::rm::ffi::REDISMODULE_OK
960+
}
961+
Err(_) => r::rm::ffi::REDISMODULE_OK,
962+
}
963+
}
964+
},
965+
),
966+
_ => {
967+
let error = CString::new(
968+
"Wrong number of arguments, it accepts exactly 3",
969+
)
970+
.unwrap();
971+
STATISTICS.copy_err();
972+
return unsafe {
973+
r::rm::ffi::RedisModule_ReplyWithError.unwrap()(
974+
context.as_ptr(),
975+
error.as_ptr(),
976+
)
977+
};
970978
}
971-
Err(_) => r::rm::ffi::REDISMODULE_OK,
972979
}
973980
}
974981

0 commit comments

Comments
 (0)