From e0c10955dbc828a085f07c082bca7f9707f1a5df Mon Sep 17 00:00:00 2001 From: zqq Date: Wed, 26 Oct 2022 15:09:49 +0800 Subject: [PATCH 1/3] fix code review Signed-off-by: zqq --- .../common/include/auto_launch.h | 2 +- .../common/include/db_dfx_adapter.h | 2 +- .../distributeddb/common/src/data_value.cpp | 4 +- .../common/src/notification_chain.cpp | 2 +- .../include/communicator_aggregator.h | 14 +-- .../communicator/include/frame_combiner.h | 8 +- .../communicator/include/frame_retainer.h | 10 +- .../communicator/include/iadapter.h | 2 +- .../include/icommunicator_aggregator.h | 2 +- .../communicator/include/parse_result.h | 2 +- .../include/send_task_scheduler.h | 14 +-- .../communicator/src/communicator.cpp | 2 +- .../communicator/src/communicator.h | 12 +-- .../src/communicator_aggregator.cpp | 22 ++--- .../communicator/src/communicator_linker.cpp | 47 ++++----- .../communicator/src/communicator_linker.h | 12 +-- .../communicator/src/frame_combiner.cpp | 16 +-- .../communicator/src/frame_retainer.cpp | 11 ++- .../communicator/src/network_adapter.cpp | 8 +- .../communicator/src/protocol_proto.cpp | 97 +++++++++---------- .../communicator/src/protocol_proto.h | 30 +++--- .../communicator/src/serial_buffer.cpp | 4 +- .../distributeddb/include/query_expression.h | 4 +- .../include/iprocess_communicator.h | 4 +- .../src/sqlite/sqlite_single_ver_result_set.h | 2 +- .../syncer/src/generic_syncer.cpp | 6 +- .../distributeddb/syncer/src/isync_engine.h | 4 +- .../src/query_sync_water_mark_helper.cpp | 34 +++---- .../distributeddb/syncer/src/sync_engine.cpp | 40 ++++---- .../distributeddb/syncer/src/sync_engine.h | 2 +- .../common/syncer/virtual_communicator.cpp | 2 +- 31 files changed, 207 insertions(+), 214 deletions(-) diff --git a/frameworks/libs/distributeddb/common/include/auto_launch.h b/frameworks/libs/distributeddb/common/include/auto_launch.h index a2794eb9620..f120eaf50be 100644 --- a/frameworks/libs/distributeddb/common/include/auto_launch.h +++ b/frameworks/libs/distributeddb/common/include/auto_launch.h @@ -40,7 +40,7 @@ enum class AutoLaunchItemState { }; enum class DBType { - DB_KV = 0, + DB_KV = 1, DB_RELATION, DB_INVALID, }; diff --git a/frameworks/libs/distributeddb/common/include/db_dfx_adapter.h b/frameworks/libs/distributeddb/common/include/db_dfx_adapter.h index cc4f40cd065..b1b67e048c2 100644 --- a/frameworks/libs/distributeddb/common/include/db_dfx_adapter.h +++ b/frameworks/libs/distributeddb/common/include/db_dfx_adapter.h @@ -33,7 +33,7 @@ struct ReportTask { std::string storeId; int errCode = 0; }; -class DBDfxAdapter { +class DBDfxAdapter final { public: static void Dump(int fd, const std::vector &args); diff --git a/frameworks/libs/distributeddb/common/src/data_value.cpp b/frameworks/libs/distributeddb/common/src/data_value.cpp index d5f7ae772e7..99fc897dcbb 100644 --- a/frameworks/libs/distributeddb/common/src/data_value.cpp +++ b/frameworks/libs/distributeddb/common/src/data_value.cpp @@ -84,9 +84,7 @@ int Blob::WriteBlob(const uint8_t *ptrArray, const uint32_t &size) std::vector Blob::ToVector() const { - std::vector vec; - vec.insert(vec.end(), ptr_, ptr_ + size_); - return vec; + return std::vector(ptr_, ptr_ + size_); } DataValue::DataValue() : type_(StorageType::STORAGE_TYPE_NULL) diff --git a/frameworks/libs/distributeddb/common/src/notification_chain.cpp b/frameworks/libs/distributeddb/common/src/notification_chain.cpp index 237fcf9d38a..02a83c3f6a6 100644 --- a/frameworks/libs/distributeddb/common/src/notification_chain.cpp +++ b/frameworks/libs/distributeddb/common/src/notification_chain.cpp @@ -186,7 +186,7 @@ int NotificationChain::ListenerChain::UnRegisterListener(Listener *listener, boo void NotificationChain::ListenerChain::BackupListenerSet(std::set &backupSet) const { for (auto listener : listenerSet_) { - listener->IncObjRef(listener); + RefObject::IncObjRef(listener); backupSet.insert(listener); } } diff --git a/frameworks/libs/distributeddb/communicator/include/communicator_aggregator.h b/frameworks/libs/distributeddb/communicator/include/communicator_aggregator.h index 30ca55f93cf..dbea7c5488c 100644 --- a/frameworks/libs/distributeddb/communicator/include/communicator_aggregator.h +++ b/frameworks/libs/distributeddb/communicator/include/communicator_aggregator.h @@ -16,20 +16,20 @@ #ifndef COMMUNICATORAGGREGATOR_H #define COMMUNICATORAGGREGATOR_H +#include +#include +#include #include #include #include -#include #include -#include -#include -#include "iadapter.h" -#include "parse_result.h" -#include "icommunicator.h" #include "frame_combiner.h" #include "frame_retainer.h" -#include "send_task_scheduler.h" +#include "iadapter.h" +#include "icommunicator.h" #include "icommunicator_aggregator.h" +#include "parse_result.h" +#include "send_task_scheduler.h" namespace DistributedDB { // Forward Declarations diff --git a/frameworks/libs/distributeddb/communicator/include/frame_combiner.h b/frameworks/libs/distributeddb/communicator/include/frame_combiner.h index 33124c497c4..d1ec81f8267 100644 --- a/frameworks/libs/distributeddb/communicator/include/frame_combiner.h +++ b/frameworks/libs/distributeddb/communicator/include/frame_combiner.h @@ -16,20 +16,20 @@ #ifndef FRAME_COMBINER_H #define FRAME_COMBINER_H +#include #include #include -#include -#include "semaphore_utils.h" +#include "combine_status.h" #include "macro_utils.h" #include "parse_result.h" -#include "combine_status.h" #include "runtime_context.h" +#include "semaphore_utils.h" namespace DistributedDB { class SerialBuffer; // Forward Declarations struct CombineWork { - SerialBuffer *buffer; + SerialBuffer *buffer = nullptr; CombineStatus status; ParseResult frameInfo; }; diff --git a/frameworks/libs/distributeddb/communicator/include/frame_retainer.h b/frameworks/libs/distributeddb/communicator/include/frame_retainer.h index 259007369b3..a0b14408708 100644 --- a/frameworks/libs/distributeddb/communicator/include/frame_retainer.h +++ b/frameworks/libs/distributeddb/communicator/include/frame_retainer.h @@ -27,16 +27,16 @@ namespace DistributedDB { class SerialBuffer; // Forward Declarations struct FrameInfo { - SerialBuffer *buffer; + SerialBuffer *buffer = nullptr; std::string srcTarget; LabelType commLabel; - uint32_t frameId; + uint32_t frameId = 0u; }; struct RetainWork { - SerialBuffer *buffer; - uint32_t frameId; - uint32_t remainTime; // in second + SerialBuffer *buffer = nullptr; + uint32_t frameId = 0u; + uint32_t remainTime = 0u; // in second }; class FrameRetainer { diff --git a/frameworks/libs/distributeddb/communicator/include/iadapter.h b/frameworks/libs/distributeddb/communicator/include/iadapter.h index f238114ca4b..d68916fa6cb 100644 --- a/frameworks/libs/distributeddb/communicator/include/iadapter.h +++ b/frameworks/libs/distributeddb/communicator/include/iadapter.h @@ -16,10 +16,10 @@ #ifndef IADAPTER_H #define IADAPTER_H -#include #include #include #include +#include #include "communicator_type_define.h" #include "iprocess_communicator.h" diff --git a/frameworks/libs/distributeddb/communicator/include/icommunicator_aggregator.h b/frameworks/libs/distributeddb/communicator/include/icommunicator_aggregator.h index ee5c59dfda2..06ae9e7c1e9 100644 --- a/frameworks/libs/distributeddb/communicator/include/icommunicator_aggregator.h +++ b/frameworks/libs/distributeddb/communicator/include/icommunicator_aggregator.h @@ -17,9 +17,9 @@ #define ICOMMUNICATORAGGREGATOR_H #include +#include "communicator_type_define.h" #include "iadapter.h" #include "ref_object.h" -#include "communicator_type_define.h" namespace DistributedDB { class ICommunicator; // Forward Declaration diff --git a/frameworks/libs/distributeddb/communicator/include/parse_result.h b/frameworks/libs/distributeddb/communicator/include/parse_result.h index 0e1278821a1..99d0cebd613 100644 --- a/frameworks/libs/distributeddb/communicator/include/parse_result.h +++ b/frameworks/libs/distributeddb/communicator/include/parse_result.h @@ -16,8 +16,8 @@ #ifndef PARSE_RESULT_H #define PARSE_RESULT_H -#include #include +#include #include "communicator_type_define.h" namespace DistributedDB { diff --git a/frameworks/libs/distributeddb/communicator/include/send_task_scheduler.h b/frameworks/libs/distributeddb/communicator/include/send_task_scheduler.h index 8c1e3e12ec3..0725474062a 100644 --- a/frameworks/libs/distributeddb/communicator/include/send_task_scheduler.h +++ b/frameworks/libs/distributeddb/communicator/include/send_task_scheduler.h @@ -16,14 +16,14 @@ #ifndef SEND_TASK_SCHEDULER_H #define SEND_TASK_SCHEDULER_H -#include +#include #include +#include #include -#include #include -#include -#include "macro_utils.h" +#include #include "communicator_type_define.h" +#include "macro_utils.h" namespace DistributedDB { enum class TargetPolicy { @@ -34,14 +34,14 @@ enum class TargetPolicy { class SerialBuffer; // Forward Declaration struct SendTask { - SerialBuffer *buffer; + SerialBuffer *buffer = nullptr; std::string dstTarget; OnSendEnd onEnd; }; struct SendTaskInfo { - bool delayFlag; - Priority taskPrio; + bool delayFlag = false; + Priority taskPrio = Priority::LOW; }; using TaskListByTarget = std::map>; diff --git a/frameworks/libs/distributeddb/communicator/src/communicator.cpp b/frameworks/libs/distributeddb/communicator/src/communicator.cpp index e5c97f41267..e9ac8cf7bce 100644 --- a/frameworks/libs/distributeddb/communicator/src/communicator.cpp +++ b/frameworks/libs/distributeddb/communicator/src/communicator.cpp @@ -116,7 +116,7 @@ int Communicator::SendMessage(const std::string &dstTarget, const Message *inMsg } int error = E_OK; // if error is not E_OK , null pointer will be returned - SerialBuffer *buffer = ProtocolProto::ToSerialBuffer(inMsg, error, extendHandle, false); + SerialBuffer *buffer = ProtocolProto::ToSerialBuffer(inMsg, extendHandle, false, error); extendHandle = nullptr; if (error != E_OK) { LOGE("[Comm][Send] Serial fail, label=%s, error=%d.", VEC_TO_STR(commLabel_), error); diff --git a/frameworks/libs/distributeddb/communicator/src/communicator.h b/frameworks/libs/distributeddb/communicator/src/communicator.h index c1f2ce39d4a..fd9436ddfa6 100644 --- a/frameworks/libs/distributeddb/communicator/src/communicator.h +++ b/frameworks/libs/distributeddb/communicator/src/communicator.h @@ -16,16 +16,16 @@ #ifndef COMMUNICATOR_H #define COMMUNICATOR_H -#include -#include #include -#include +#include #include #include -#include -#include "serial_buffer.h" -#include "icommunicator.h" +#include +#include +#include #include "communicator_aggregator.h" +#include "icommunicator.h" +#include "serial_buffer.h" namespace DistributedDB { class Communicator : public ICommunicator { diff --git a/frameworks/libs/distributeddb/communicator/src/communicator_aggregator.cpp b/frameworks/libs/distributeddb/communicator/src/communicator_aggregator.cpp index 7c9151c9fc2..3009fd471b8 100644 --- a/frameworks/libs/distributeddb/communicator/src/communicator_aggregator.cpp +++ b/frameworks/libs/distributeddb/communicator/src/communicator_aggregator.cpp @@ -16,12 +16,11 @@ #include "communicator_aggregator.h" #include - -#include "hash.h" #include "communicator.h" #include "communicator_linker.h" #include "db_common.h" #include "endian_convert.h" +#include "hash.h" #include "log_print.h" #include "protocol_proto.h" @@ -318,7 +317,7 @@ int CommunicatorAggregator::CreateSendTask(const std::string &dstTarget, SerialB std::lock_guard wakingLockGuard(wakingMutex_); wakingSignal_ = true; wakingCv_.notify_one(); - LOGI("[CommAggr][Create] Exit ok, thread=%s, frameId=%u", GetThreadId().c_str(), info.frameId); // Delete In Future + LOGI("[CommAggr][Create] Exit ok, thread=%s, frameId=%u", GetThreadId().c_str(), info.frameId); return E_OK; } @@ -343,9 +342,9 @@ void CommunicatorAggregator::SendDataRoutine() while (!shutdown_) { if (scheduler_.GetNoDelayTaskCount() == 0) { std::unique_lock wakingUniqueLock(wakingMutex_); - LOGI("[CommAggr][Routine] Send done and sleep."); // Delete In Future + LOGI("[CommAggr][Routine] Send done and sleep."); wakingCv_.wait(wakingUniqueLock, [this] { return this->wakingSignal_; }); - LOGI("[CommAggr][Routine] Send continue."); // Delete In Future + LOGI("[CommAggr][Routine] Send continue."); wakingSignal_ = false; continue; } @@ -392,8 +391,8 @@ void CommunicatorAggregator::SendPacketsAndDisposeTask(const SendTask &inTask, bool taskNeedFinalize = true; int errCode = E_OK; for (auto &entry : eachPacket) { - LOGI("[CommAggr][SendPackets] DoSendBytes, dstTarget=%s{private}, extendHeadLength=%u, totalLength=%u.", - inTask.dstTarget.c_str(), entry.second.first, entry.second.second); + LOGI("[CommAggr][SendPackets] DoSendBytes, dstTarget=%s{private}, extendHeadLength=%" PRIu32 + ", totalLength=%" PRIu32 ".", inTask.dstTarget.c_str(), entry.second.first, entry.second.second); ProtocolProto::DisplayPacketInformation(entry.first + entry.second.first, entry.second.second); errCode = adapterHandle_->SendBytes(inTask.dstTarget, entry.first, entry.second.second); if (errCode == -E_WAIT_RETRY) { @@ -479,7 +478,7 @@ void CommunicatorAggregator::NotifySendableToAllCommunicator() void CommunicatorAggregator::OnBytesReceive(const std::string &srcTarget, const uint8_t *bytes, uint32_t length, const std::string &userId) { - ProtocolProto::DisplayPacketInformation(bytes, length); // For debug, delete in the future + ProtocolProto::DisplayPacketInformation(bytes, length); ParseResult packetResult; int errCode = ProtocolProto::CheckAndParsePacket(srcTarget, bytes, length, packetResult); if (errCode != E_OK) { @@ -536,10 +535,7 @@ void CommunicatorAggregator::OnTargetChange(const std::string &target, bool isCo LOGE("[CommAggr][OnTarget] TargetOnline fail, target=%s{private}, errCode=%d.", target.c_str(), errCode); } } else { - int errCode = commLinker_->TargetOffline(target, relatedLabels); - if (errCode != E_OK) { - LOGE("[CommAggr][OnTarget] TargetOffline fail, target=%s{private}, errCode=%d.", target.c_str(), errCode); - } + commLinker_->TargetOffline(target, relatedLabels); } // All related communicator online or offline this target, no matter TargetOnline or TargetOffline fail or not std::lock_guard commMapLockGuard(commMapMutex_); @@ -782,7 +778,7 @@ void CommunicatorAggregator::GenerateLocalSourceId() // The localSourceId is std::atomic, so there is no concurrency risk uint64_t identityHash = Hash::HashFunc(identity); if (identityHash != localSourceId_) { - LOGI("[CommAggr][GenSrcId] identity=%s{private}, localSourceId=%llu.", identity.c_str(), ULL(identityHash)); + LOGI("[CommAggr][GenSrcId] identity=%s{private}, localSourceId=%" PRIu64, identity.c_str(), ULL(identityHash)); } localSourceId_ = identityHash; } diff --git a/frameworks/libs/distributeddb/communicator/src/communicator_linker.cpp b/frameworks/libs/distributeddb/communicator/src/communicator_linker.cpp index 59a1b77950d..ff9bb80e4a3 100644 --- a/frameworks/libs/distributeddb/communicator/src/communicator_linker.cpp +++ b/frameworks/libs/distributeddb/communicator/src/communicator_linker.cpp @@ -14,12 +14,12 @@ */ #include "communicator_linker.h" -#include "hash.h" +#include "communicator_aggregator.h" #include "db_errno.h" +#include "hash.h" #include "log_print.h" -#include "protocol_proto.h" #include "platform_specific.h" -#include "communicator_aggregator.h" +#include "protocol_proto.h" namespace DistributedDB { namespace { @@ -51,7 +51,7 @@ void CommunicatorLinker::Initialize() } std::string curTimeStr = std::to_string(curTime); localDistinctValue_ = Hash::HashFunc(curTimeStr); - LOGI("[Linker][Init] curTime=%llu, distinct=%llu.", ULL(curTime), ULL(localDistinctValue_)); + LOGI("[Linker][Init] curTime=%" PRIu64 ", distinct=%" PRIu64 ".", ULL(curTime), ULL(localDistinctValue_)); } // Create async task to send out label_exchange and waiting for label_exchange_ack. @@ -73,7 +73,7 @@ int CommunicatorLinker::TargetOnline(const std::string &inTarget, std::set