From df8f41e88f36dbcc61485b3d6e7afb7d54daa6d8 Mon Sep 17 00:00:00 2001 From: zhao liwei Date: Mon, 14 Dec 2020 10:45:16 +0800 Subject: [PATCH] refactor: use rocksdb_wrapper::get to reimplement check_and_mutate (#655) --- src/server/pegasus_write_service_impl.h | 36 +++++++++---------------- 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/src/server/pegasus_write_service_impl.h b/src/server/pegasus_write_service_impl.h index 3655a6c2f6..6187ebd0a3 100644 --- a/src/server/pegasus_write_service_impl.h +++ b/src/server/pegasus_write_service_impl.h @@ -412,41 +412,31 @@ class pegasus_write_service::impl : public dsn::replication::replica_base ::dsn::blob check_key; pegasus_generate_key(check_key, update.hash_key, update.check_sort_key); - rocksdb::Slice check_raw_key(check_key.data(), check_key.length()); - std::string check_raw_value; - rocksdb::Status check_status = _db->Get(_rd_opts, check_raw_key, &check_raw_value); - if (check_status.ok()) { - // read check value succeed - if (check_if_record_expired( - _pegasus_data_version, utils::epoch_now(), check_raw_value)) { - // check value ttl timeout - _pfc_recent_expire_count->increment(); - check_status = rocksdb::Status::NotFound(); - } - } else if (!check_status.IsNotFound()) { + + db_get_context get_context; + dsn::string_view check_raw_key(check_key.data(), check_key.length()); + int err = _rocksdb_wrapper->get(check_raw_key, &get_context); + if (err != 0) { // read check value failed - derror_rocksdb("GetCheckValue for CheckAndMutate", - check_status.ToString(), - "decree: {}, hash_key: {}, check_sort_key: {}", + derror_rocksdb("Error to GetCheckValue for CheckAndMutate decree: {}, hash_key: {}, " + "check_sort_key: {}", decree, utils::c_escape_string(update.hash_key), utils::c_escape_string(update.check_sort_key)); - resp.error = check_status.code(); + resp.error = err; return resp.error; } - dassert_f(check_status.ok() || check_status.IsNotFound(), - "status = %s", - check_status.ToString().c_str()); ::dsn::blob check_value; - if (check_status.ok()) { + bool value_exist = !get_context.expired && get_context.found; + if (value_exist) { pegasus_extract_user_data( - _pegasus_data_version, std::move(check_raw_value), check_value); + _pegasus_data_version, std::move(get_context.raw_value), check_value); } if (update.return_check_value) { resp.check_value_returned = true; - if (check_status.ok()) { + if (value_exist) { resp.check_value_exist = true; resp.check_value = check_value; } @@ -456,7 +446,7 @@ class pegasus_write_service::impl : public dsn::replication::replica_base bool passed = validate_check(decree, update.check_type, update.check_operand, - check_status.ok(), + value_exist, check_value, invalid_argument);