diff --git a/frameworks/libs/distributeddb/gaussdb_rd/src/interface/include/projection_tree.h b/frameworks/libs/distributeddb/gaussdb_rd/src/interface/include/projection_tree.h index 7d3b080b40c355b2554fe51319e3526376c45175..f7b197f444f280daa6a52fd7f61a0b13d1fff49d 100644 --- a/frameworks/libs/distributeddb/gaussdb_rd/src/interface/include/projection_tree.h +++ b/frameworks/libs/distributeddb/gaussdb_rd/src/interface/include/projection_tree.h @@ -25,20 +25,23 @@ namespace DocumentDB { struct ProjectionNode { - std::unordered_map sonNode; - bool isDeepest; - int Deep; - ProjectionNode() - { - Deep = 0; - isDeepest = true; - } +public: + ProjectionNode(); + ProjectionNode(int deep); + ~ProjectionNode(); + + const bool &IsDeepest() const; + int CreateChildNode(const std::string &nodeName, ProjectionNode* &childNodeHolder); + ProjectionNode *GetChildNodeByName(const std::string &nodeName); + +private: + std::unordered_map sonNode_; + bool isDeepest_; + int deep_; + int DeleteProjectionNode(); - ~ProjectionNode() - { - DeleteProjectionNode(); - } }; + class ProjectionTree { public: int ParseTree(std::vector> &path); diff --git a/frameworks/libs/distributeddb/gaussdb_rd/src/interface/src/projection_tree.cpp b/frameworks/libs/distributeddb/gaussdb_rd/src/interface/src/projection_tree.cpp index f33d694f32c04c21f6bc3a1a9fa0196815d98ec8..e9fc8c920667b75463689d21a2f4a60cce16518b 100644 --- a/frameworks/libs/distributeddb/gaussdb_rd/src/interface/src/projection_tree.cpp +++ b/frameworks/libs/distributeddb/gaussdb_rd/src/interface/src/projection_tree.cpp @@ -20,33 +20,80 @@ constexpr int JSON_DEEP_MAX = 4; static int ParseSinglePathToTree(ProjectionNode *node, std::vector &singlePath) { for (size_t j = 0; j < singlePath.size(); j++) { - if (node->sonNode[singlePath[j]] != nullptr) { - node = node->sonNode[singlePath[j]]; - if (j < singlePath.size() - 1 && node->isDeepest) { + if (node->GetChildNodeByName(singlePath[j]) != nullptr) { + node = node->GetChildNodeByName(singlePath[j]); + if (j < singlePath.size() - 1 && node->IsDeepest()) { return -E_INVALID_ARGS; } - if (j == singlePath.size() - 1 && !node->isDeepest) { + if (j == singlePath.size() - 1 && !node->IsDeepest()) { return -E_INVALID_ARGS; } } else { - auto tempNode = new (std::nothrow) ProjectionNode; - if (tempNode == nullptr) { - GLOGE("Memory allocation failed!"); - return -E_FAILED_MEMORY_ALLOCATE; + int errCode = node->CreateChildNode(singlePath[j], node); + if (errCode != E_OK) { + return errCode; } - tempNode->Deep = node->Deep + 1; - if (tempNode->Deep > JSON_DEEP_MAX) { - delete tempNode; - return -E_INVALID_ARGS; - } - node->isDeepest = false; - node->sonNode[singlePath[j]] = tempNode; - node = node->sonNode[singlePath[j]]; } } return E_OK; } +ProjectionNode::ProjectionNode() +{ + deep_ = 0; + isDeepest_ = true; +} + +ProjectionNode::ProjectionNode(int deep) +{ + deep_ = deep; + isDeepest_ = true; +} + +ProjectionNode::~ProjectionNode() +{ + DeleteProjectionNode(); +} + +const bool &ProjectionNode::IsDeepest() const +{ + return isDeepest_; +} + +int ProjectionNode::CreateChildNode(const std::string &nodeName, ProjectionNode* &childNodeHolder) +{ + int childDeep = deep_ + 1; + if (childDeep > JSON_DEEP_MAX) { + return -E_INVALID_ARGS; + } + + auto tempNode = new (std::nothrow) ProjectionNode(childDeep); + if (tempNode == nullptr) { + GLOGE("Memory allocation failed!"); + return -E_FAILED_MEMORY_ALLOCATE; + } + + isDeepest_ = false; + sonNode_[nodeName] = tempNode; + childNodeHolder = sonNode_[nodeName]; + return E_OK; +} + +ProjectionNode *ProjectionNode::GetChildNodeByName(const std::string &nodeName) +{ + return sonNode_[nodeName]; +} + +int ProjectionNode::DeleteProjectionNode() +{ + for (auto item : sonNode_) { + if (item.second != nullptr) { + delete item.second; + item.second = nullptr; + } + } + return E_OK; +} int ProjectionTree::ParseTree(std::vector> &path) { ProjectionNode *node = &node_; @@ -67,9 +114,9 @@ bool ProjectionTree::SearchTree(std::vector &singlePath, size_t &in { ProjectionNode *node = &node_; for (size_t i = 0; i < singlePath.size(); i++) { - if (node->sonNode[singlePath[i]] != nullptr) { - node = node->sonNode[singlePath[i]]; - if (node->isDeepest) { + if (node->GetChildNodeByName(singlePath[i]) != nullptr) { + node = node->GetChildNodeByName(singlePath[i]); + if (node->IsDeepest()) { index = i + 1; } } else { @@ -79,14 +126,5 @@ bool ProjectionTree::SearchTree(std::vector &singlePath, size_t &in return true; } -int ProjectionNode::DeleteProjectionNode() -{ - for (auto item : sonNode) { - if (item.second != nullptr) { - delete item.second; - item.second = nullptr; - } - } - return E_OK; -} + } // namespace DocumentDB \ No newline at end of file diff --git a/frameworks/libs/distributeddb/storage/include/kvdb_pragma.h b/frameworks/libs/distributeddb/storage/include/kvdb_pragma.h index 5114992397d5e33f143afbf54773a9e77bcef1e6..fc807b38126f5b76c3a1987cc6ab7079c20d5c76 100644 --- a/frameworks/libs/distributeddb/storage/include/kvdb_pragma.h +++ b/frameworks/libs/distributeddb/storage/include/kvdb_pragma.h @@ -20,6 +20,7 @@ #include #include +#include "isyncer.h" #include "store_types.h" #include "query_sync_object.h" @@ -55,6 +56,7 @@ enum : int { }; struct PragmaSync { +public: PragmaSync(const std::vector &devices, int mode, const QuerySyncObject &query, const std::function &devicesMap)> &onComplete, bool wait = false) @@ -102,6 +104,24 @@ struct PragmaSync { { } + // convert PragmaSync to SyncParma, callbacks are copied to paramters + ISyncer::SyncParma ConvertToSyncPragma(DeviceSyncProcessCallback &onSyncProcess, + std::function &devicesMap)> &onComplete) const + { + ISyncer::SyncParma syncParam; + syncParam.devices = devices_; + syncParam.mode = mode_; + syncParam.wait = wait_; + syncParam.isQuerySync = isQuerySync_; + syncParam.syncQuery = query_; + + onSyncProcess = onSyncProcess_; + onComplete = onComplete_; + + return syncParam; + } + +private: std::vector devices_; int mode_; std::function &devicesMap)> onComplete_; diff --git a/frameworks/libs/distributeddb/storage/src/kv/sync_able_kvdb_connection.cpp b/frameworks/libs/distributeddb/storage/src/kv/sync_able_kvdb_connection.cpp index 37d7c2d4be4e09fa18ebee46d37b3bd800d5d1af..9ee890c6ef1a14568eaa47f0426ab8eff4090af3 100644 --- a/frameworks/libs/distributeddb/storage/src/kv/sync_able_kvdb_connection.cpp +++ b/frameworks/libs/distributeddb/storage/src/kv/sync_able_kvdb_connection.cpp @@ -145,21 +145,18 @@ int SyncAbleKvDBConnection::PragmaSyncAction(const PragmaSync *syncParameter) IncObjRef(this); } - ISyncer::SyncParma syncParam; - syncParam.devices = syncParameter->devices_; - syncParam.mode = syncParameter->mode_; - syncParam.wait = syncParameter->wait_; - syncParam.isQuerySync = syncParameter->isQuerySync_; - syncParam.syncQuery = syncParameter->query_; + DeviceSyncProcessCallback onSyncProcess; + std::function &devicesMap)> onComplete; + ISyncer::SyncParma syncParam = syncParameter->ConvertToSyncPragma(onSyncProcess, onComplete); syncParam.onFinalize = [this]() { DecObjRef(this); }; - if (syncParameter->onComplete_) { - syncParam.onComplete = [this, onComplete = syncParameter->onComplete_, wait = syncParameter->wait_]( + if (onComplete) { + syncParam.onComplete = [this, onComplete, wait = syncParam.wait]( const std::map &statuses) { OnSyncComplete(statuses, onComplete, wait); }; } - if (syncParameter->onSyncProcess_) { - syncParam.onSyncProcess = [this, onSyncProcess = syncParameter->onSyncProcess_]( + if (onSyncProcess) { + syncParam.onSyncProcess = [this, onSyncProcess]( const std::map &syncRecordMap) { OnDeviceSyncProcess(syncRecordMap, onSyncProcess); }; diff --git a/frameworks/libs/distributeddb/storage/src/relational/relational_sync_data_inserter.cpp b/frameworks/libs/distributeddb/storage/src/relational/relational_sync_data_inserter.cpp index 0edb9c65c65bca32a42949f420c2cd2718d2b71f..97bc44abd70ee775a4098a4ca54a86913841ff4a 100644 --- a/frameworks/libs/distributeddb/storage/src/relational/relational_sync_data_inserter.cpp +++ b/frameworks/libs/distributeddb/storage/src/relational/relational_sync_data_inserter.cpp @@ -22,20 +22,20 @@ namespace DistributedDB { int SaveSyncDataStmt::ResetStatements(bool isNeedFinalize) { int errCode = E_OK; - if (saveDataStmt != nullptr) { - SQLiteUtils::ResetStatement(saveDataStmt, isNeedFinalize, errCode); + if (saveDataStmt_ != nullptr) { + SQLiteUtils::ResetStatement(saveDataStmt_, isNeedFinalize, errCode); } - if (saveLogStmt != nullptr) { - SQLiteUtils::ResetStatement(saveLogStmt, isNeedFinalize, errCode); + if (saveLogStmt_ != nullptr) { + SQLiteUtils::ResetStatement(saveLogStmt_, isNeedFinalize, errCode); } - if (queryStmt != nullptr) { - SQLiteUtils::ResetStatement(queryStmt, isNeedFinalize, errCode); + if (queryStmt_ != nullptr) { + SQLiteUtils::ResetStatement(queryStmt_, isNeedFinalize, errCode); } - if (rmDataStmt != nullptr) { - SQLiteUtils::ResetStatement(rmDataStmt, isNeedFinalize, errCode); + if (rmDataStmt_ != nullptr) { + SQLiteUtils::ResetStatement(rmDataStmt_, isNeedFinalize, errCode); } - if (rmLogStmt != nullptr) { - SQLiteUtils::ResetStatement(rmLogStmt, isNeedFinalize, errCode); + if (rmLogStmt_ != nullptr) { + SQLiteUtils::ResetStatement(rmLogStmt_, isNeedFinalize, errCode); } return errCode; } @@ -234,12 +234,12 @@ int RelationalSyncDataInserter::GetSaveLogStatement(sqlite3 *db, sqlite3_stmt *& int RelationalSyncDataInserter::PrepareStatement(sqlite3 *db, SaveSyncDataStmt &stmt) { - int errCode = GetSaveLogStatement(db, stmt.saveLogStmt, stmt.queryStmt); + int errCode = GetSaveLogStatement(db, stmt.GetSaveLogStmt(), stmt.GetQueryStmt()); if (errCode != E_OK) { LOGE("Get save log statement failed. err=%d", errCode); return errCode; } - errCode = GetInsertStatement(db, stmt.saveDataStmt); + errCode = GetInsertStatement(db, stmt.GetSaveDataStmt()); if (errCode != E_OK) { LOGE("Get insert statement failed. err=%d", errCode); } diff --git a/frameworks/libs/distributeddb/storage/src/relational/relational_sync_data_inserter.h b/frameworks/libs/distributeddb/storage/src/relational/relational_sync_data_inserter.h index 7120c28a3a734bae7d433b50e79d3761d7f9fce7..0df33e9702178a47f628b05ba5cb942bd98f4ff0 100644 --- a/frameworks/libs/distributeddb/storage/src/relational/relational_sync_data_inserter.h +++ b/frameworks/libs/distributeddb/storage/src/relational/relational_sync_data_inserter.h @@ -24,11 +24,7 @@ namespace DistributedDB { struct SaveSyncDataStmt { - sqlite3_stmt *saveDataStmt = nullptr; - sqlite3_stmt *saveLogStmt = nullptr; - sqlite3_stmt *queryStmt = nullptr; - sqlite3_stmt *rmDataStmt = nullptr; - sqlite3_stmt *rmLogStmt = nullptr; +public: SaveSyncDataStmt() {} ~SaveSyncDataStmt() { @@ -36,6 +32,35 @@ struct SaveSyncDataStmt { } int ResetStatements(bool isNeedFinalize); + + // getters + sqlite3_stmt* &GetSaveDataStmt() + { + return saveDataStmt_; + } + sqlite3_stmt* &GetSaveLogStmt() + { + return saveLogStmt_; + } + sqlite3_stmt* &GetQueryStmt() + { + return queryStmt_; + } + sqlite3_stmt* &GetRmDataStmt() + { + return rmDataStmt_; + } + sqlite3_stmt* &GetRmLogStmt() + { + return rmLogStmt_; + } + +private: + sqlite3_stmt *saveDataStmt_ = nullptr; + sqlite3_stmt *saveLogStmt_ = nullptr; + sqlite3_stmt *queryStmt_ = nullptr; + sqlite3_stmt *rmDataStmt_ = nullptr; + sqlite3_stmt *rmLogStmt_ = nullptr; }; class RelationalSyncDataInserter { diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_single_ver_relational_storage_executor.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_single_ver_relational_storage_executor.cpp index f1884789924e6e52efb047299dc002d71a15b127..b43fbd31f1bbb742d6746b78a69663863340b7d3 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_single_ver_relational_storage_executor.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_single_ver_relational_storage_executor.cpp @@ -867,26 +867,26 @@ int SQLiteSingleVerRelationalStorageExecutor::SaveSyncDataItem(const DataItem &d RelationalSyncDataInserter &inserter, int64_t &rowid) { if ((dataItem.flag & DataItem::DELETE_FLAG) != 0) { - return DeleteSyncDataItem(dataItem, inserter, saveStmt.rmDataStmt); + return DeleteSyncDataItem(dataItem, inserter, saveStmt.GetRmDataStmt()); } if ((mode_ == DistributedTableMode::COLLABORATION && inserter.GetLocalTable().GetIdentifyKey().size() == 1u && inserter.GetLocalTable().GetIdentifyKey().at(0) == "rowid") || (mode_ == DistributedTableMode::SPLIT_BY_DEVICE && inserter.GetLocalTable().GetPrimaryKey().size() == 1u && inserter.GetLocalTable().GetPrimaryKey().at(0) == "rowid") || inserter.GetLocalTable().GetAutoIncrement()) { // No primary key of auto increment - int errCode = DeleteSyncDataItem(dataItem, inserter, saveStmt.rmDataStmt); + int errCode = DeleteSyncDataItem(dataItem, inserter, saveStmt.GetRmDataStmt()); if (errCode != E_OK) { LOGE("Delete no pk data before insert failed, errCode=%d.", errCode); return errCode; } } - int errCode = inserter.BindInsertStatement(saveStmt.saveDataStmt, dataItem); + int errCode = inserter.BindInsertStatement(saveStmt.GetSaveDataStmt(), dataItem); if (errCode != E_OK) { LOGE("Bind data failed, errCode=%d.", errCode); return errCode; } - errCode = SQLiteUtils::StepWithRetry(saveStmt.saveDataStmt, isMemDb_); + errCode = SQLiteUtils::StepWithRetry(saveStmt.GetSaveDataStmt(), isMemDb_); if (errCode == SQLiteUtils::MapSQLiteErrno(SQLITE_DONE)) { rowid = SQLiteUtils::GetLastRowId(dbHandle_); errCode = E_OK; @@ -968,7 +968,7 @@ int SQLiteSingleVerRelationalStorageExecutor::SaveSyncDataItem(RelationalSyncDat SaveSyncDataStmt &saveStmt, DataItem &item) { bool isDefeated = false; - int errCode = CheckDataConflictDefeated(item, saveStmt.queryStmt, isDefeated); + int errCode = CheckDataConflictDefeated(item, saveStmt.GetQueryStmt(), isDefeated); if (errCode != E_OK) { LOGE("check data conflict failed. %d", errCode); return errCode; @@ -979,12 +979,12 @@ int SQLiteSingleVerRelationalStorageExecutor::SaveSyncDataItem(RelationalSyncDat return E_OK; } if ((item.flag & DataItem::REMOTE_DEVICE_DATA_MISS_QUERY) != 0) { - return ProcessMissQueryData(item, inserter, saveStmt.rmDataStmt, saveStmt.rmLogStmt); + return ProcessMissQueryData(item, inserter, saveStmt.GetRmDataStmt(), saveStmt.GetRmLogStmt()); } int64_t rowid = -1; errCode = SaveSyncDataItem(item, saveStmt, inserter, rowid); if (errCode == E_OK || errCode == -E_NOT_FOUND) { - errCode = SaveSyncLog(saveStmt.saveLogStmt, saveStmt.queryStmt, item, rowid); + errCode = SaveSyncLog(saveStmt.GetSaveLogStmt(), saveStmt.GetQueryStmt(), item, rowid); } return errCode; } diff --git a/frameworks/libs/distributeddb/syncer/src/device/ability_sync.cpp b/frameworks/libs/distributeddb/syncer/src/device/ability_sync.cpp index 3e75eefdc6cd32aa7d09f2fcb2edc65bad6cb53f..fbfca713a9f65b3c75f99a7913e400279446c474 100644 --- a/frameworks/libs/distributeddb/syncer/src/device/ability_sync.cpp +++ b/frameworks/libs/distributeddb/syncer/src/device/ability_sync.cpp @@ -30,100 +30,131 @@ #endif namespace DistributedDB { -AbilitySyncRequestPacket::AbilitySyncRequestPacket() + +AbilitySyncPacket::AbilitySyncPacket() : protocolVersion_(ABILITY_SYNC_VERSION_V1), - sendCode_(E_OK), softwareVersion_(SOFTWARE_VERSION_CURRENT), + schemaType_(0), + schemaVersion_(0), secLabel_(0), secFlag_(0), - schemaType_(0), - dbCreateTime_(0), - schemaVersion_(0) + dbCreateTime_(0) { } -AbilitySyncRequestPacket::~AbilitySyncRequestPacket() +AbilitySyncPacket::~AbilitySyncPacket() { } -void AbilitySyncRequestPacket::SetProtocolVersion(uint32_t protocolVersion) +const uint32_t &AbilitySyncPacket::GetProtocolVersion() const { - protocolVersion_ = protocolVersion; + return protocolVersion_; } -uint32_t AbilitySyncRequestPacket::GetProtocolVersion() const +const uint32_t &AbilitySyncPacket::GetSoftwareVersion() const { - return protocolVersion_; + return softwareVersion_; } -void AbilitySyncRequestPacket::SetSendCode(int32_t sendCode) +const std::string &AbilitySyncPacket::GetSchema() const { - sendCode_ = sendCode; + return schema_; } -int32_t AbilitySyncRequestPacket::GetSendCode() const +const uint32_t &AbilitySyncPacket::GetSchemaType() const { - return sendCode_; + return schemaType_; } -void AbilitySyncRequestPacket::SetSoftwareVersion(uint32_t swVersion) +const uint64_t &AbilitySyncPacket::GetSchemaVersion() const { - softwareVersion_ = swVersion; + return schemaVersion_; } -uint32_t AbilitySyncRequestPacket::GetSoftwareVersion() const +const int32_t &AbilitySyncPacket::GetSecLabel() const { - return softwareVersion_; + return secLabel_; } -void AbilitySyncRequestPacket::SetSchema(const std::string &schema) +const int32_t &AbilitySyncPacket::GetSecFlag() const { - schema_ = schema; + return secFlag_; } -std::string AbilitySyncRequestPacket::GetSchema() const +const uint64_t &AbilitySyncPacket::GetDbCreateTime() const { - return schema_; + return dbCreateTime_; +} + +const DbAbility &AbilitySyncPacket::GetDbAbility() const +{ + return dbAbility_; } -void AbilitySyncRequestPacket::SetSchemaType(uint32_t schemaType) +void AbilitySyncPacket::SetSchemaInfo(const std::string &schema, uint32_t schemaType) { + schema_ = schema; schemaType_ = schemaType; } -uint32_t AbilitySyncRequestPacket::GetSchemaType() const +AbilitySyncRequestPacket::AbilitySyncRequestPacket() + : sendCode_(E_OK) { - return schemaType_; } -void AbilitySyncRequestPacket::SetSecLabel(int32_t secLabel) +AbilitySyncRequestPacket::AbilitySyncRequestPacket(uint32_t protocolVersion, + uint32_t swVersion, const std::string &schema) + : sendCode_(E_OK) { - secLabel_ = secLabel; + protocolVersion_ = protocolVersion; + softwareVersion_ = swVersion; + schema_ = schema; } -int32_t AbilitySyncRequestPacket::GetSecLabel() const +AbilitySyncRequestPacket::AbilitySyncRequestPacket(uint32_t swVersion, + const std::string &schema, const DbAbility &dbAbility) + : sendCode_(E_OK) { - return secLabel_; + softwareVersion_ = swVersion; + schema_ = schema; + dbAbility_ = dbAbility; } -void AbilitySyncRequestPacket::SetSecFlag(int32_t secFlag) +AbilitySyncRequestPacket::AbilitySyncRequestPacket(uint32_t swVersion, + const std::string &schema, int32_t secLabel, int32_t secFlag) + : sendCode_(E_OK) { + softwareVersion_ = swVersion; + schema_ = schema; + secLabel_ = secLabel; secFlag_ = secFlag; } -int32_t AbilitySyncRequestPacket::GetSecFlag() const +AbilitySyncRequestPacket::~AbilitySyncRequestPacket() { - return secFlag_; } -void AbilitySyncRequestPacket::SetDbCreateTime(uint64_t dbCreateTime) +const int32_t &AbilitySyncRequestPacket::GetSendCode() const { - dbCreateTime_ = dbCreateTime; + return sendCode_; } -uint64_t AbilitySyncRequestPacket::GetDbCreateTime() const +void AbilitySyncRequestPacket::SetCodeAndSchema(int32_t code, const std::string &schema) { - return dbCreateTime_; + sendCode_ = code; + schema_ = schema; +} + +void AbilitySyncRequestPacket::SetBodyInfo(const SecurityOption &option, uint64_t dbCreateTime, + const DbAbility &dbAbility, uint64_t schemaVer) +{ + protocolVersion_ = ABILITY_SYNC_VERSION_V1; + softwareVersion_ = SOFTWARE_VERSION_CURRENT; + secLabel_ = option.securityLabel; + secFlag_ = option.securityFlag; + dbCreateTime_ = dbCreateTime; + dbAbility_ = dbAbility; + schemaVersion_ = schemaVer; } uint32_t AbilitySyncRequestPacket::CalculateLen() const @@ -153,152 +184,178 @@ uint32_t AbilitySyncRequestPacket::CalculateLen() const return len; } -DbAbility AbilitySyncRequestPacket::GetDbAbility() const +int AbilitySyncRequestPacket::DeSerialization(Parcel &parcel, Message *inMsg, AbilitySyncRequestPacket *packet) { - return dbAbility_; -} + uint32_t version = 0; + uint32_t softwareVersion = 0; + std::string schema; + int32_t sendCode = 0; + int errCode; -void AbilitySyncRequestPacket::SetDbAbility(const DbAbility &dbAbility) -{ - dbAbility_ = dbAbility; -} + parcel.ReadUInt32(version); + if (parcel.IsError()) { + LOGE("[AbilitySyncRequestPacket][DeSerialization] read version failed!"); + errCode = -E_PARSE_FAIL; + return errCode; + } + protocolVersion_ = version; + if (version > ABILITY_SYNC_VERSION_V1) { + sendCode_ = -E_VERSION_NOT_SUPPORT; + errCode = inMsg->SetExternalObject<>(packet); + return errCode; + } + parcel.ReadInt(sendCode); + parcel.ReadUInt32(softwareVersion); + parcel.ReadString(schema); + errCode = DeSerializationTailPart(parcel, softwareVersion); + if (parcel.IsError() || errCode != E_OK) { + errCode = -E_PARSE_FAIL; + return errCode; + } + softwareVersion_ = softwareVersion; + SetCodeAndSchema(sendCode, schema); -void AbilitySyncRequestPacket::SetSchemaVersion(uint64_t schemaVersion) -{ - schemaVersion_ = schemaVersion; + errCode = inMsg->SetExternalObject<>(packet); + return errCode; } -uint64_t AbilitySyncRequestPacket::GetSchemaVersion() const +int AbilitySyncRequestPacket::DeSerializationTailPart(Parcel &parcel, uint32_t version) { - return schemaVersion_; + if (!parcel.IsError() && version > SOFTWARE_VERSION_RELEASE_2_0) { + int32_t secLabel = 0; + int32_t secFlag = 0; + uint32_t schemaType = 0; + parcel.ReadInt(secLabel); + parcel.ReadInt(secFlag); + parcel.ReadUInt32(schemaType); + secLabel_ = secLabel; + secFlag_ = secFlag; + schemaType_ = schemaType; + } + if (!parcel.IsError() && version > SOFTWARE_VERSION_RELEASE_3_0) { + uint64_t dbCreateTime = 0; + parcel.ReadUInt64(dbCreateTime); + dbCreateTime_ = dbCreateTime; + } + DbAbility remoteDbAbility; + int errCode = DbAbility::DeSerialize(parcel, remoteDbAbility); + if (errCode != E_OK) { + LOGE("[AbilitySync] request packet DeSerializ failed."); + return errCode; + } + dbAbility_ = remoteDbAbility; + if (version >= SOFTWARE_VERSION_RELEASE_9_0) { + uint64_t schemaVersion = 0u; + parcel.ReadUInt64(schemaVersion); + if (parcel.IsError()) { + LOGW("[AbilitySync] request packet read schema version failed"); + return -E_PARSE_FAIL; + } + schemaVersion_ = schemaVersion; + } + return E_OK; } AbilitySyncAckPacket::AbilitySyncAckPacket() - : protocolVersion_(ABILITY_SYNC_VERSION_V1), - softwareVersion_(SOFTWARE_VERSION_CURRENT), - ackCode_(E_OK), - secLabel_(0), - secFlag_(0), - schemaType_(0), + : ackCode_(E_OK), permitSync_(0), - requirePeerConvert_(0), - dbCreateTime_(0), - schemaVersion_(0) -{ -} - -AbilitySyncAckPacket::~AbilitySyncAckPacket() + requirePeerConvert_(0) { } -void AbilitySyncAckPacket::SetProtocolVersion(uint32_t protocolVersion) +AbilitySyncAckPacket::AbilitySyncAckPacket(int32_t ackCode) + : ackCode_(ackCode), + permitSync_(0), + requirePeerConvert_(0) { - protocolVersion_ = protocolVersion; } -void AbilitySyncAckPacket::SetSoftwareVersion(uint32_t swVersion) +AbilitySyncAckPacket::AbilitySyncAckPacket(uint32_t swVersion, int32_t ackCode) + : ackCode_(ackCode), + permitSync_(0), + requirePeerConvert_(0) { softwareVersion_ = swVersion; } -uint32_t AbilitySyncAckPacket::GetSoftwareVersion() const -{ - return softwareVersion_; -} - -uint32_t AbilitySyncAckPacket::GetProtocolVersion() const +AbilitySyncAckPacket::AbilitySyncAckPacket(const std::string &schema, int32_t ackCode, const DbAbility &dbAbility) + : ackCode_(ackCode), + permitSync_(0), + requirePeerConvert_(0) { - return protocolVersion_; + schema_ = schema; + dbAbility_ = dbAbility; } -void AbilitySyncAckPacket::SetAckCode(int32_t ackCode) +AbilitySyncAckPacket::~AbilitySyncAckPacket() { - ackCode_ = ackCode; } -int32_t AbilitySyncAckPacket::GetAckCode() const +const int32_t &AbilitySyncAckPacket::GetAckCode() const { return ackCode_; } -void AbilitySyncAckPacket::SetSchema(const std::string &schema) -{ - schema_ = schema; -} - -std::string AbilitySyncAckPacket::GetSchema() const +const uint32_t &AbilitySyncAckPacket::GetPermitSync() const { - return schema_; -} - -void AbilitySyncAckPacket::SetSchemaType(uint32_t schemaType) -{ - schemaType_ = schemaType; + return permitSync_; } -uint32_t AbilitySyncAckPacket::GetSchemaType() const +const uint32_t &AbilitySyncAckPacket::GetRequirePeerConvert() const { - return schemaType_; + return requirePeerConvert_; } -void AbilitySyncAckPacket::SetSecLabel(int32_t secLabel) +const RelationalSyncOpinion &AbilitySyncAckPacket::GetRelationalSyncOpinion() const { - secLabel_ = secLabel; + return relationalSyncOpinion_; } -int32_t AbilitySyncAckPacket::GetSecLabel() const +void AbilitySyncAckPacket::GetAckCodeAndVersion(int &ack, uint32_t &swVersion) const { - return secLabel_; + ack = ackCode_; + swVersion = softwareVersion_; } -void AbilitySyncAckPacket::SetSecFlag(int32_t secFlag) +void AbilitySyncAckPacket::SetRelationalSyncOpinion(const RelationalSyncOpinion &relationalSyncOpinion) { - secFlag_ = secFlag; + relationalSyncOpinion_ = relationalSyncOpinion; } -int32_t AbilitySyncAckPacket::GetSecFlag() const +void AbilitySyncAckPacket::SetNonNotifyAckBody(const SecurityOption &option, + uint64_t dbCreateTime, const DbAbility &dbAbility) { - return secFlag_; + secLabel_ = option.securityLabel; + secFlag_ = option.securityFlag; + dbCreateTime_ = dbCreateTime; + dbAbility_ = dbAbility; } -void AbilitySyncAckPacket::SetPermitSync(uint32_t permitSync) +void AbilitySyncAckPacket::SetOpinionInfo(uint32_t permitSync, uint32_t requirePeerConvert) { permitSync_ = permitSync; -} - -uint32_t AbilitySyncAckPacket::GetPermitSync() const -{ - return permitSync_; -} - -void AbilitySyncAckPacket::SetRequirePeerConvert(uint32_t requirePeerConvert) -{ requirePeerConvert_ = requirePeerConvert; } -uint32_t AbilitySyncAckPacket::GetRequirePeerConvert() const -{ - return requirePeerConvert_; -} - -void AbilitySyncAckPacket::SetDbCreateTime(uint64_t dbCreateTime) +void AbilitySyncAckPacket::SetCodeAndSchema(int32_t code, const std::string &schema) { - dbCreateTime_ = dbCreateTime; + ackCode_ = code; + schema_ = schema; } -uint64_t AbilitySyncAckPacket::GetDbCreateTime() const +int AbilitySyncAckPacket::SetBodyInfo(int32_t ackCode, std::shared_ptr metadata) { - return dbCreateTime_; -} + protocolVersion_ = ABILITY_SYNC_VERSION_V1; + softwareVersion_ = SOFTWARE_VERSION_CURRENT; -uint64_t AbilitySyncAckPacket::GetSchemaVersion() const -{ - return schemaVersion_; -} + auto [ret, schemaVersion] = metadata->GetLocalSchemaVersion(); + if (ret != E_OK) { + return ret; + } -void AbilitySyncAckPacket::SetSchemaVersion(uint64_t schemaVersion) -{ + ackCode_ = ackCode; schemaVersion_ = schemaVersion; + return E_OK; } uint32_t AbilitySyncAckPacket::CalculateLen() const @@ -329,24 +386,88 @@ uint32_t AbilitySyncAckPacket::CalculateLen() const return len; } -DbAbility AbilitySyncAckPacket::GetDbAbility() const -{ - return dbAbility_; -} - -void AbilitySyncAckPacket::SetDbAbility(const DbAbility &dbAbility) +int AbilitySyncAckPacket::DeSerialization(Parcel &parcel, Message *inMsg, AbilitySyncAckPacket *packet) { - dbAbility_ = dbAbility; + uint32_t version = 0; + uint32_t softwareVersion = 0; + int32_t ackCode = E_OK; + std::string schema; + int errCode; + parcel.ReadUInt32(version); + if (parcel.IsError()) { + LOGE("[AbilitySyncAckPacket][DeSerialization] read version failed!"); + errCode = -E_PARSE_FAIL; + return errCode; + } + protocolVersion_ = version; + if (version > ABILITY_SYNC_VERSION_V1) { + ackCode_ = -E_VERSION_NOT_SUPPORT; + errCode = inMsg->SetExternalObject<>(packet); + return errCode; + } + parcel.ReadUInt32(softwareVersion); + parcel.ReadInt(ackCode); + parcel.ReadString(schema); + errCode = DeSerializationTailPart(parcel, softwareVersion); + if (parcel.IsError() || errCode != E_OK) { + LOGE("[AbilitySyncAckPacket][DeSerialization] DeSerialization failed!"); + errCode = -E_PARSE_FAIL; + return errCode; + } + softwareVersion_ = softwareVersion; + SetCodeAndSchema(ackCode_, schema); + errCode = inMsg->SetExternalObject<>(packet); + return errCode; } -void AbilitySyncAckPacket::SetRelationalSyncOpinion(const RelationalSyncOpinion &relationalSyncOpinion) +int AbilitySyncAckPacket::DeSerializationTailPart(Parcel &parcel, uint32_t version) { + if (!parcel.IsError() && version > SOFTWARE_VERSION_RELEASE_2_0) { + int32_t secLabel = 0; + int32_t secFlag = 0; + uint32_t schemaType = 0; + uint32_t permitSync = 0; + uint32_t requirePeerConvert = 0; + parcel.ReadInt(secLabel); + parcel.ReadInt(secFlag); + parcel.ReadUInt32(schemaType); + parcel.ReadUInt32(permitSync); + parcel.ReadUInt32(requirePeerConvert); + secLabel_ = secLabel; + secFlag_ = secFlag; + schemaType_ = schemaType; + permitSync_ = permitSync; + requirePeerConvert_ = requirePeerConvert; + } + if (!parcel.IsError() && version > SOFTWARE_VERSION_RELEASE_3_0) { + uint64_t dbCreateTime = 0; + parcel.ReadUInt64(dbCreateTime); + dbCreateTime_ = dbCreateTime; + } + DbAbility remoteDbAbility; + int errCode = DbAbility::DeSerialize(parcel, remoteDbAbility); + if (errCode != E_OK) { + LOGE("[AbilitySync] ack packet DeSerializ failed."); + return errCode; + } + dbAbility_ = remoteDbAbility; + RelationalSyncOpinion relationalSyncOpinion; + errCode = SchemaNegotiate::DeserializeData(parcel, relationalSyncOpinion); + if (errCode != E_OK) { + LOGE("[AbilitySync] ack packet DeSerializ RelationalSyncOpinion failed."); + return errCode; + } relationalSyncOpinion_ = relationalSyncOpinion; -} - -RelationalSyncOpinion AbilitySyncAckPacket::GetRelationalSyncOpinion() const -{ - return relationalSyncOpinion_; + if (version >= SOFTWARE_VERSION_RELEASE_9_0) { + uint64_t schemaVersion = 0; + parcel.ReadUInt64(schemaVersion); + if (parcel.IsError()) { + LOGW("[AbilitySync] ack packet read schema version failed."); + return -E_PARSE_FAIL; + } + schemaVersion_ = schemaVersion; + } + return E_OK; } AbilitySync::AbilitySync() @@ -779,132 +900,11 @@ int AbilitySync::RequestPacketDeSerialization(const uint8_t *buffer, uint32_t le } Parcel parcel(const_cast(buffer), length); - uint32_t version = 0; - uint32_t softwareVersion = 0; - std::string schema; - int32_t sendCode = 0; - int errCode = -E_PARSE_FAIL; - - parcel.ReadUInt32(version); - if (parcel.IsError()) { - goto ERROR_OUT; - } - packet->SetProtocolVersion(version); - if (version > ABILITY_SYNC_VERSION_V1) { - packet->SetSendCode(-E_VERSION_NOT_SUPPORT); - errCode = inMsg->SetExternalObject<>(packet); - if (errCode != E_OK) { - goto ERROR_OUT; - } - return errCode; - } - parcel.ReadInt(sendCode); - parcel.ReadUInt32(softwareVersion); - parcel.ReadString(schema); - errCode = RequestPacketDeSerializationTailPart(parcel, packet, softwareVersion); - if (parcel.IsError() || errCode != E_OK) { - goto ERROR_OUT; - } - packet->SetSendCode(sendCode); - packet->SetSoftwareVersion(softwareVersion); - packet->SetSchema(schema); - - errCode = inMsg->SetExternalObject<>(packet); - if (errCode == E_OK) { - return E_OK; - } - -ERROR_OUT: - delete packet; - return errCode; -} - -int AbilitySync::RequestPacketDeSerializationTailPart(Parcel &parcel, AbilitySyncRequestPacket *packet, - uint32_t version) -{ - if (!parcel.IsError() && version > SOFTWARE_VERSION_RELEASE_2_0) { - int32_t secLabel = 0; - int32_t secFlag = 0; - uint32_t schemaType = 0; - parcel.ReadInt(secLabel); - parcel.ReadInt(secFlag); - parcel.ReadUInt32(schemaType); - packet->SetSecLabel(secLabel); - packet->SetSecFlag(secFlag); - packet->SetSchemaType(schemaType); - } - if (!parcel.IsError() && version > SOFTWARE_VERSION_RELEASE_3_0) { - uint64_t dbCreateTime = 0; - parcel.ReadUInt64(dbCreateTime); - packet->SetDbCreateTime(dbCreateTime); - } - DbAbility remoteDbAbility; - int errCode = DbAbility::DeSerialize(parcel, remoteDbAbility); - if (errCode != E_OK) { - LOGE("[AbilitySync] request packet DeSerializ failed."); - return errCode; - } - packet->SetDbAbility(remoteDbAbility); - if (version >= SOFTWARE_VERSION_RELEASE_9_0) { - uint64_t schemaVersion = 0u; - parcel.ReadUInt64(schemaVersion); - if (parcel.IsError()) { - LOGW("[AbilitySync] request packet read schema version failed"); - return -E_PARSE_FAIL; - } - packet->SetSchemaVersion(schemaVersion); - } - return E_OK; -} - -int AbilitySync::AckPacketDeSerializationTailPart(Parcel &parcel, AbilitySyncAckPacket *packet, uint32_t version) -{ - if (!parcel.IsError() && version > SOFTWARE_VERSION_RELEASE_2_0) { - int32_t secLabel = 0; - int32_t secFlag = 0; - uint32_t schemaType = 0; - uint32_t permitSync = 0; - uint32_t requirePeerConvert = 0; - parcel.ReadInt(secLabel); - parcel.ReadInt(secFlag); - parcel.ReadUInt32(schemaType); - parcel.ReadUInt32(permitSync); - parcel.ReadUInt32(requirePeerConvert); - packet->SetSecLabel(secLabel); - packet->SetSecFlag(secFlag); - packet->SetSchemaType(schemaType); - packet->SetPermitSync(permitSync); - packet->SetRequirePeerConvert(requirePeerConvert); - } - if (!parcel.IsError() && version > SOFTWARE_VERSION_RELEASE_3_0) { - uint64_t dbCreateTime = 0; - parcel.ReadUInt64(dbCreateTime); - packet->SetDbCreateTime(dbCreateTime); - } - DbAbility remoteDbAbility; - int errCode = DbAbility::DeSerialize(parcel, remoteDbAbility); - if (errCode != E_OK) { - LOGE("[AbilitySync] ack packet DeSerializ failed."); - return errCode; - } - packet->SetDbAbility(remoteDbAbility); - RelationalSyncOpinion relationalSyncOpinion; - errCode = SchemaNegotiate::DeserializeData(parcel, relationalSyncOpinion); + int errCode = packet->DeSerialization(parcel, inMsg, packet); if (errCode != E_OK) { - LOGE("[AbilitySync] ack packet DeSerializ RelationalSyncOpinion failed."); - return errCode; + delete packet; } - packet->SetRelationalSyncOpinion(relationalSyncOpinion); - if (version >= SOFTWARE_VERSION_RELEASE_9_0) { - uint64_t schemaVersion = 0; - parcel.ReadUInt64(schemaVersion); - if (parcel.IsError()) { - LOGW("[AbilitySync] ack packet read schema version failed."); - return -E_PARSE_FAIL; - } - packet->SetSchemaVersion(schemaVersion); - } - return E_OK; + return errCode; } int AbilitySync::AckPacketDeSerialization(const uint8_t *buffer, uint32_t length, Message *inMsg) @@ -915,45 +915,10 @@ int AbilitySync::AckPacketDeSerialization(const uint8_t *buffer, uint32_t length } Parcel parcel(const_cast(buffer), length); - uint32_t version = 0; - uint32_t softwareVersion = 0; - int32_t ackCode = E_OK; - std::string schema; - int errCode; - parcel.ReadUInt32(version); - if (parcel.IsError()) { - LOGE("[AbilitySync][RequestDeSerialization] read version failed!"); - errCode = -E_PARSE_FAIL; - goto ERROR_OUT; - } - packet->SetProtocolVersion(version); - if (version > ABILITY_SYNC_VERSION_V1) { - packet->SetAckCode(-E_VERSION_NOT_SUPPORT); - errCode = inMsg->SetExternalObject<>(packet); - if (errCode != E_OK) { - goto ERROR_OUT; - } - return errCode; - } - parcel.ReadUInt32(softwareVersion); - parcel.ReadInt(ackCode); - parcel.ReadString(schema); - errCode = AckPacketDeSerializationTailPart(parcel, packet, softwareVersion); - if (parcel.IsError() || errCode != E_OK) { - LOGE("[AbilitySync][RequestDeSerialization] DeSerialization failed!"); - errCode = -E_PARSE_FAIL; - goto ERROR_OUT; - } - packet->SetSoftwareVersion(softwareVersion); - packet->SetAckCode(ackCode); - packet->SetSchema(schema); - errCode = inMsg->SetExternalObject<>(packet); - if (errCode == E_OK) { - return E_OK; + int errCode = packet->DeSerialization(parcel, inMsg, packet); + if (errCode != E_OK) { + delete packet; } - -ERROR_OUT: - delete packet; return errCode; } @@ -994,19 +959,11 @@ int AbilitySync::SetAbilityRequestBodyInfo(uint16_t remoteCommunicatorVersion, c // 102 version is forbidden to sync with 103 json-schema or flatbuffer-schema // so schema should put null string while remote is 102 version to avoid this bug. if (remoteCommunicatorVersion == 1) { - packet.SetSchema(""); - packet.SetSchemaType(0); + packet.SetSchemaInfo("", 0); } else { - packet.SetSchema(schemaStr); - packet.SetSchemaType(schemaType); - } - packet.SetProtocolVersion(ABILITY_SYNC_VERSION_V1); - packet.SetSoftwareVersion(SOFTWARE_VERSION_CURRENT); - packet.SetSecLabel(option.securityLabel); - packet.SetSecFlag(option.securityFlag); - packet.SetDbCreateTime(dbCreateTime); - packet.SetDbAbility(dbAbility); - packet.SetSchemaVersion(localSchemaVer); + packet.SetSchemaInfo(schemaStr, schemaType); + } + packet.SetBodyInfo(option, dbCreateTime, dbAbility, localSchemaVer); LOGI("[AbilitySync][FillRequest] ver=%u,Lab=%d,Flag=%d,dbCreateTime=%" PRId64 ",schemaVer=%" PRId64, SOFTWARE_VERSION_CURRENT, option.securityLabel, option.securityFlag, dbCreateTime, localSchemaVer); return E_OK; @@ -1015,13 +972,9 @@ int AbilitySync::SetAbilityRequestBodyInfo(uint16_t remoteCommunicatorVersion, c int AbilitySync::SetAbilityAckBodyInfo(const ISyncTaskContext *context, int ackCode, bool isAckNotify, AbilitySyncAckPacket &ackPacket) const { - ackPacket.SetProtocolVersion(ABILITY_SYNC_VERSION_V1); - ackPacket.SetSoftwareVersion(SOFTWARE_VERSION_CURRENT); if (!isAckNotify) { SecurityOption option; GetPacketSecOption(context, option); - ackPacket.SetSecLabel(option.securityLabel); - ackPacket.SetSecFlag(option.securityFlag); uint64_t dbCreateTime = 0; int errCode = (static_cast(storageInterface_))->GetDatabaseCreateTimestamp(dbCreateTime); @@ -1035,28 +988,19 @@ int AbilitySync::SetAbilityAckBodyInfo(const ISyncTaskContext *context, int ackC LOGE("[AbilitySync][FillAbilityRequest] GetDbAbility failed, err %d", errCode); return errCode; } - ackPacket.SetDbCreateTime(dbCreateTime); - ackPacket.SetDbAbility(dbAbility); + ackPacket.SetNonNotifyAckBody(option, dbCreateTime, dbAbility); } - auto [ret, schemaVersion] = metadata_->GetLocalSchemaVersion(); - if (ret != E_OK) { - return ret; - } - ackPacket.SetAckCode(ackCode); - ackPacket.SetSchemaVersion(schemaVersion); - return E_OK; + return ackPacket.SetBodyInfo(ackCode, metadata_); } void AbilitySync::SetAbilityAckSchemaInfo(AbilitySyncAckPacket &ackPacket, const ISchema &schemaObj) { - ackPacket.SetSchema(schemaObj.ToSchemaString()); - ackPacket.SetSchemaType(static_cast(schemaObj.GetSchemaType())); + ackPacket.SetSchemaInfo(schemaObj.ToSchemaString(), static_cast(schemaObj.GetSchemaType())); } void AbilitySync::SetAbilityAckSyncOpinionInfo(AbilitySyncAckPacket &ackPacket, SyncOpinion localOpinion) { - ackPacket.SetPermitSync(localOpinion.permitSync); - ackPacket.SetRequirePeerConvert(localOpinion.requirePeerConvert); + ackPacket.SetOpinionInfo(localOpinion.permitSync, localOpinion.requirePeerConvert); } int AbilitySync::GetDbAbilityInfo(DbAbility &dbAbility) diff --git a/frameworks/libs/distributeddb/syncer/src/device/ability_sync.h b/frameworks/libs/distributeddb/syncer/src/device/ability_sync.h index 0e9535adac2b96723e06a2926d19adb3dc4edcdc..63c376aaadf0dd005edd2b502fb39a2b247d5abe 100644 --- a/frameworks/libs/distributeddb/syncer/src/device/ability_sync.h +++ b/frameworks/libs/distributeddb/syncer/src/device/ability_sync.h @@ -30,121 +30,96 @@ #include "schema_negotiate.h" namespace DistributedDB { -class AbilitySyncRequestPacket { -public: - AbilitySyncRequestPacket(); - ~AbilitySyncRequestPacket(); - - void SetProtocolVersion(uint32_t protocolVersion); - uint32_t GetProtocolVersion() const; - - void SetSendCode(int32_t sendCode); - int32_t GetSendCode() const; - - void SetSoftwareVersion(uint32_t swVersion); - uint32_t GetSoftwareVersion() const; - - void SetSchema(const std::string &schema); - std::string GetSchema() const; - - void SetSchemaType(uint32_t schemaType); - uint32_t GetSchemaType() const; - - void SetSecLabel(int32_t secLabel); - int32_t GetSecLabel() const; - - void SetSecFlag(int32_t secFlag); - int32_t GetSecFlag() const; - - void SetDbCreateTime(uint64_t dbCreateTime); - uint64_t GetDbCreateTime() const; - - uint32_t CalculateLen() const; - - DbAbility GetDbAbility() const; - - void SetDbAbility(const DbAbility &dbAbility); - void SetSchemaVersion(uint64_t schemaVersion); - - uint64_t GetSchemaVersion() const; - -private: +class AbilitySyncPacket { +public: + AbilitySyncPacket(); + virtual ~AbilitySyncPacket(); + + // getters + const uint32_t &GetProtocolVersion() const; + const uint32_t &GetSoftwareVersion() const; + const std::string &GetSchema() const; + const uint32_t &GetSchemaType() const; + const uint64_t &GetSchemaVersion() const; + const int32_t &GetSecLabel() const; + const int32_t &GetSecFlag() const; + const uint64_t &GetDbCreateTime() const; + const DbAbility &GetDbAbility() const; + + virtual uint32_t CalculateLen() const = 0; + void SetSchemaInfo(const std::string &schema, uint32_t schemaType); + + friend class AibiltySync; + +protected: uint32_t protocolVersion_; - int32_t sendCode_; uint32_t softwareVersion_; std::string schema_; + uint32_t schemaType_; + uint64_t schemaVersion_; int32_t secLabel_; int32_t secFlag_; - uint32_t schemaType_; uint64_t dbCreateTime_; DbAbility dbAbility_; - uint64_t schemaVersion_; + + virtual int DeSerializationTailPart(Parcel &parcel, uint32_t version) = 0; }; -class AbilitySyncAckPacket { +class AbilitySyncRequestPacket : public AbilitySyncPacket { public: - AbilitySyncAckPacket(); - ~AbilitySyncAckPacket(); - - void SetProtocolVersion(uint32_t protocolVersion); - uint32_t GetProtocolVersion() const; - - void SetSoftwareVersion(uint32_t swVersion); - uint32_t GetSoftwareVersion() const; - - void SetAckCode(int32_t ackCode); - int32_t GetAckCode() const; - - void SetSchema(const std::string &schema); - std::string GetSchema() const; - - void SetSchemaType(uint32_t schemaType); - uint32_t GetSchemaType() const; - - void SetSecLabel(int32_t secLabel); - int32_t GetSecLabel() const; - - void SetSecFlag(int32_t secFlag); - int32_t GetSecFlag() const; - - void SetPermitSync(uint32_t permitSync); - uint32_t GetPermitSync() const; - - void SetRequirePeerConvert(uint32_t requirePeerConvert); - uint32_t GetRequirePeerConvert() const; + AbilitySyncRequestPacket(); + AbilitySyncRequestPacket(uint32_t protocolVersion, uint32_t swVersion, const std::string &schema); + AbilitySyncRequestPacket(uint32_t swVersion, const std::string &schema, const DbAbility &dbAbility); + AbilitySyncRequestPacket(uint32_t swVersion, const std::string &schema, int32_t secLabel, int32_t secFlag); - void SetDbCreateTime(uint64_t dbCreateTime); - uint64_t GetDbCreateTime() const; + ~AbilitySyncRequestPacket() override; + + const int32_t &GetSendCode() const; - uint32_t CalculateLen() const; + void SetCodeAndSchema(int32_t code, const std::string &schema); + void SetBodyInfo(const SecurityOption &option, uint64_t dbCreateTime, + const DbAbility &dbAbility, uint64_t schemaVer); - DbAbility GetDbAbility() const; + int DeSerialization(Parcel &parcel, Message *inMsg, AbilitySyncRequestPacket *packet); + uint32_t CalculateLen() const override; + +private: + int32_t sendCode_; + int DeSerializationTailPart(Parcel &parcel, uint32_t version) override; +}; - void SetDbAbility(const DbAbility &dbAbility); +class AbilitySyncAckPacket : public AbilitySyncPacket { +public: + AbilitySyncAckPacket(); + explicit AbilitySyncAckPacket(int32_t ackCode); + AbilitySyncAckPacket(uint32_t swVersion, int32_t ackCode); + AbilitySyncAckPacket(const std::string &schema, int32_t ackCode, const DbAbility &dbAbility); - void SetSchemaVersion(uint64_t schemaVersion); + ~AbilitySyncAckPacket() override; - uint64_t GetSchemaVersion() const; + const int32_t &GetAckCode() const; + const uint32_t &GetPermitSync() const; + const uint32_t &GetRequirePeerConvert() const; + const RelationalSyncOpinion &GetRelationalSyncOpinion() const; + void GetAckCodeAndVersion(int &ack, uint32_t &swVersion) const; void SetRelationalSyncOpinion(const RelationalSyncOpinion &relationalSyncOpinion); - - RelationalSyncOpinion GetRelationalSyncOpinion() const; + void SetNonNotifyAckBody(const SecurityOption &option, uint64_t dbCreateTime, const DbAbility &dbAbility); + void SetOpinionInfo(uint32_t permitSync, uint32_t requirePeerConvert); + void SetCodeAndSchema(int32_t code, const std::string &schema); + int SetBodyInfo(int32_t ackCode, std::shared_ptr metadata); + + uint32_t CalculateLen() const override; + int DeSerialization(Parcel &parcel, Message *inMsg, AbilitySyncAckPacket *packet); private: - uint32_t protocolVersion_; - uint32_t softwareVersion_; int32_t ackCode_; - std::string schema_; - int32_t secLabel_; - int32_t secFlag_; - uint32_t schemaType_; uint32_t permitSync_; uint32_t requirePeerConvert_; - uint64_t dbCreateTime_; - uint64_t schemaVersion_; - DbAbility dbAbility_; RelationalSyncOpinion relationalSyncOpinion_; + + int DeSerializationTailPart(Parcel &parcel, uint32_t version) override; }; class AbilitySync { @@ -194,11 +169,6 @@ private: static int AckPacketCalculateLen(const Message *inMsg, uint32_t &len); - static int RequestPacketDeSerializationTailPart(Parcel &parcel, AbilitySyncRequestPacket *packet, - uint32_t version); - - static int AckPacketDeSerializationTailPart(Parcel &parcel, AbilitySyncAckPacket *packet, uint32_t version); - bool SecLabelCheck(const AbilitySyncRequestPacket *packet) const; static void HandleVersionV3RequestParam(const AbilitySyncRequestPacket *packet, ISyncTaskContext *context); diff --git a/frameworks/libs/distributeddb/syncer/src/device/commit_history_sync.cpp b/frameworks/libs/distributeddb/syncer/src/device/commit_history_sync.cpp index 4d8a5802879869d6f2b578291331d1b6bb1afc4b..c187892b6c3e4b854f1efe2827200faa28586d09 100644 --- a/frameworks/libs/distributeddb/syncer/src/device/commit_history_sync.cpp +++ b/frameworks/libs/distributeddb/syncer/src/device/commit_history_sync.cpp @@ -60,7 +60,7 @@ void CommitHistorySyncRequestPacket::SetVersion(uint32_t version) version_ = version; } -uint32_t CommitHistorySyncRequestPacket::GetVersion() const +const uint32_t &CommitHistorySyncRequestPacket::GetVersion() const { return version_; } @@ -70,7 +70,7 @@ void CommitHistorySyncRequestPacket::SetReserved(std::vector &reserved reserved_ = std::move(reserved); } -std::vector CommitHistorySyncRequestPacket::GetReserved() const +const std::vector &CommitHistorySyncRequestPacket::GetReserved() const { return reserved_; } @@ -417,14 +417,14 @@ int CommitHistorySync::RequestPacketDeSerialization(const uint8_t *buffer, uint3 ", cac len = %" PRIu64, length, packLen); return -E_INVALID_ARGS; } - CommitHistorySyncRequestPacket *packet = new (std::nothrow) CommitHistorySyncRequestPacket(); + + CommitHistorySyncRequestPacket *packet = + new (std::nothrow) CommitHistorySyncRequestPacket(commitMap, version, reserved); if (packet == nullptr) { LOGE("CommitHistorySync::RequestPacketDeSerialization : new packet error"); return -E_OUT_OF_MEMORY; } - packet->SetCommitMap(commitMap); - packet->SetVersion(version); - packet->SetReserved(reserved); + int errCode = inMsg->SetExternalObject<>(packet); if (errCode != E_OK) { delete packet; @@ -616,13 +616,13 @@ int CommitHistorySync::GetCommitTree(const std::map &commitMap) { - CommitHistorySyncRequestPacket *packet = new (std::nothrow) CommitHistorySyncRequestPacket(); + CommitHistorySyncRequestPacket *packet = + new (std::nothrow) CommitHistorySyncRequestPacket(commitMap, SOFTWARE_VERSION_CURRENT); if (packet == nullptr) { LOGE("CommitHistorySync::SendRequestPacket : new packet error"); return -E_OUT_OF_MEMORY; } - packet->SetCommitMap(commitMap); - packet->SetVersion(SOFTWARE_VERSION_CURRENT); + Message *message = new (std::nothrow) Message(COMMIT_HISTORY_SYNC_MESSAGE); if (message == nullptr) { LOGE("CommitHistorySync::SendRequestPacket : new message error"); diff --git a/frameworks/libs/distributeddb/syncer/src/device/commit_history_sync.h b/frameworks/libs/distributeddb/syncer/src/device/commit_history_sync.h index 3804bafe7474cb812caa723016bf000002638d9f..60a15e2a1879c307882042a83dc9c5787e883152 100644 --- a/frameworks/libs/distributeddb/syncer/src/device/commit_history_sync.h +++ b/frameworks/libs/distributeddb/syncer/src/device/commit_history_sync.h @@ -32,26 +32,31 @@ namespace DistributedDB { class CommitHistorySyncRequestPacket { public: CommitHistorySyncRequestPacket() {}; + CommitHistorySyncRequestPacket(std::map commitMap, uint32_t version) + :commitMap_(commitMap), version_(version) {}; + CommitHistorySyncRequestPacket(std::map commitMap, uint32_t version, + std::vector reserved) + :commitMap_(commitMap), version_(version), reserved_(reserved){}; ~CommitHistorySyncRequestPacket() {}; - uint32_t CalculateLen() const; - - void SetCommitMap(std::map &inMap); - + // getters + const std::vector &GetReserved() const; + const uint32_t &GetVersion() const; void GetCommitMap(std::map &outMap) const; - void SetVersion(uint32_t version); - - uint32_t GetVersion() const; - - void SetReserved(std::vector &reserved); + // setters + void SetCommitMap(std::map &inMap); - std::vector GetReserved() const; + // other public methods + uint32_t CalculateLen() const; private: std::map commitMap_; uint32_t version_ = SOFTWARE_VERSION_CURRENT; std::vector reserved_; + + void SetVersion(uint32_t version); + void SetReserved(std::vector &reserved); }; class CommitHistorySyncAckPacket { diff --git a/frameworks/libs/distributeddb/syncer/src/device/singlever/single_ver_data_packet.cpp b/frameworks/libs/distributeddb/syncer/src/device/singlever/single_ver_data_packet.cpp index c2a7f12bf259ec269362a3280f15d5b6c56e075b..d32b93a1731ac8858fcbe2d51e2ea3876bb0c4fe 100644 --- a/frameworks/libs/distributeddb/syncer/src/device/singlever/single_ver_data_packet.cpp +++ b/frameworks/libs/distributeddb/syncer/src/device/singlever/single_ver_data_packet.cpp @@ -372,7 +372,7 @@ void DataAckPacket::SetData(const uint64_t data) data_ = data; } -uint64_t DataAckPacket::GetData() const +const uint64_t &DataAckPacket::GetData() const { return data_; } @@ -382,7 +382,7 @@ void DataAckPacket::SetRecvCode(int32_t errorCode) recvCode_ = errorCode; } -int32_t DataAckPacket::GetRecvCode() const +const int32_t &DataAckPacket::GetRecvCode() const { return recvCode_; } @@ -392,7 +392,7 @@ void DataAckPacket::SetVersion(uint32_t version) version_ = version; } -uint32_t DataAckPacket::GetVersion() const +const uint32_t &DataAckPacket::GetVersion() const { return version_; } @@ -402,7 +402,7 @@ void DataAckPacket::SetReserved(std::vector &reserved) reserved_ = std::move(reserved); } -std::vector DataAckPacket::GetReserved() const +const std::vector &DataAckPacket::GetReserved() const { return reserved_; } @@ -441,6 +441,34 @@ uint32_t DataAckPacket::CalculateLen() const return len; } +void DataAckPacket::ChangeData(int32_t recvCode, uint32_t version) +{ + SetRecvCode(recvCode); + SetVersion(version); +} + +void DataAckPacket::ChangeData(int32_t recvCode, uint32_t version, uint64_t data) +{ + SetRecvCode(recvCode); + SetVersion(version); + SetData(data); +} + +void DataAckPacket::ChangeData(int32_t recvCode, uint64_t data, std::vector &reserved) +{ + SetRecvCode(recvCode); + SetData(data); + SetReserved(reserved); +} + +void DataAckPacket::ChangeData(int32_t recvCode, uint32_t version, uint64_t data, std::vector &reserved) +{ + SetRecvCode(recvCode); + SetVersion(version); + SetData(data); + SetReserved(reserved); +} + void ControlRequestPacket::SetPacketHead(int sendCode, uint32_t version, int32_t controlCmd, uint32_t flag) { sendCode_ = sendCode; diff --git a/frameworks/libs/distributeddb/syncer/src/device/singlever/single_ver_data_packet.h b/frameworks/libs/distributeddb/syncer/src/device/singlever/single_ver_data_packet.h index 0f0d54333c27612ceaaffa26047882670316b766..7fcc56ef0ed92859f2352475486a5423354a1bbe 100644 --- a/frameworks/libs/distributeddb/syncer/src/device/singlever/single_ver_data_packet.h +++ b/frameworks/libs/distributeddb/syncer/src/device/singlever/single_ver_data_packet.h @@ -164,19 +164,19 @@ public: void SetData(const uint64_t data); - uint64_t GetData() const; + const uint64_t &GetData() const; void SetRecvCode(int32_t errorCode); - int32_t GetRecvCode() const; + const int32_t &GetRecvCode() const; void SetVersion(uint32_t version); - uint32_t GetVersion() const; + const uint32_t &GetVersion() const; void SetReserved(std::vector &reserved); - std::vector GetReserved() const; + const std::vector &GetReserved() const; uint64_t GetPacketId() const; @@ -184,6 +184,11 @@ public: uint32_t CalculateLen() const; + void ChangeData(int32_t recvCode, uint32_t version); + void ChangeData(int32_t recvCode, uint32_t version, uint64_t data); + void ChangeData(int32_t recvCode, uint64_t data, std::vector &reserved); + void ChangeData(int32_t recvCode, uint32_t version, uint64_t data, std::vector &reserved); + private: /* * data_ is waterMark when revCode_ == LOCAL_WATER_MARK_NOT_INIT || revCode_ == E_OK; diff --git a/frameworks/libs/distributeddb/syncer/src/device/singlever/single_ver_data_sync.cpp b/frameworks/libs/distributeddb/syncer/src/device/singlever/single_ver_data_sync.cpp index 7048bb2d33bda6d63d95b2fc05004e81aba01a3d..abca36398a1859b6b1636f798c522a3507580e43 100644 --- a/frameworks/libs/distributeddb/syncer/src/device/singlever/single_ver_data_sync.cpp +++ b/frameworks/libs/distributeddb/syncer/src/device/singlever/single_ver_data_sync.cpp @@ -1258,8 +1258,7 @@ void SingleVerDataSync::SendSaveDataNotifyPacket(SingleVerSyncTaskContext *conte } DataAckPacket ack; - ack.SetRecvCode(-E_SAVE_DATA_NOTIFY); - ack.SetVersion(pktVersion); + ack.ChangeData(-E_SAVE_DATA_NOTIFY, pktVersion); int errCode = ackMessage->SetCopiedObject(ack); if (errCode != E_OK) { delete ackMessage; @@ -1376,9 +1375,7 @@ void SingleVerDataSync::SendResetWatchDogPacket(SingleVerSyncTaskContext *contex } DataAckPacket ack; - ack.SetData(data); - ack.SetRecvCode(-E_SAVE_DATA_NOTIFY); - ack.SetVersion(std::min(context->GetRemoteSoftwareVersion(), SOFTWARE_VERSION_CURRENT)); + ack.ChangeData(-E_SAVE_DATA_NOTIFY, std::min(context->GetRemoteSoftwareVersion(), SOFTWARE_VERSION_CURRENT), data); int errCode = ackMessage->SetCopiedObject(ack); if (errCode != E_OK) { delete ackMessage; @@ -1603,12 +1600,13 @@ void SingleVerDataSync::SetAckPacket(DataAckPacket &ackPacket, SingleVerSyncTask GetLocalWaterMark(curType, packet->GetQueryId(), context, localMark); ackPacket.SetRecvCode(recvCode); // send ack packet + uint64_t newData = ackPacket.GetData(); if ((recvCode == E_OK) && (maxSendDataTime != 0)) { - ackPacket.SetData(maxSendDataTime + 1); // + 1 to next start + newData = maxSendDataTime + 1; // + 1 to next start } else if (recvCode != WATER_MARK_INVALID) { WaterMark mark = 0; GetPeerWaterMark(curType, packet->GetQueryId(), context->GetDeviceId(), mark); - ackPacket.SetData(mark); + newData = mark; } std::vector reserved {localMark}; uint32_t version = std::min(context->GetRemoteSoftwareVersion(), SOFTWARE_VERSION_CURRENT); @@ -1625,8 +1623,8 @@ void SingleVerDataSync::SetAckPacket(DataAckPacket &ackPacket, SingleVerSyncTask GetPeerDeleteSyncWaterMark(context->GetDeleteSyncId(), deletedPeerMark); reserved.push_back(deletedPeerMark); // query sync mode, reserve[2] store deletedPeerMark value } - ackPacket.SetReserved(reserved); - ackPacket.SetVersion(version); + + ackPacket.ChangeData(recvCode, version, newData, reserved); } int SingleVerDataSync::GetReSendData(SyncEntry &syncData, SingleVerSyncTaskContext *context, diff --git a/frameworks/libs/distributeddb/syncer/src/device/singlever/single_ver_serialize_manager.cpp b/frameworks/libs/distributeddb/syncer/src/device/singlever/single_ver_serialize_manager.cpp index 186c9f779e5ceff0262c5208e5596faa408d5c1f..b90b444a8c740bc5a1b74b6d7d18aaa88ea9e6db 100644 --- a/frameworks/libs/distributeddb/syncer/src/device/singlever/single_ver_serialize_manager.cpp +++ b/frameworks/libs/distributeddb/syncer/src/device/singlever/single_ver_serialize_manager.cpp @@ -503,11 +503,10 @@ int SingleVerSerializeManager::AckPacketDeSerialization(const uint8_t *buffer, u return -E_INVALID_ARGS; } if (version > SOFTWARE_VERSION_CURRENT) { - packet.SetVersion(version); - packet.SetRecvCode(-E_VERSION_NOT_SUPPORT); + packet.ChangeData(-E_VERSION_NOT_SUPPORT, version); return inMsg->SetCopiedObject<>(packet); } - packet.SetVersion(version); + packet.ChangeData(packet.GetRecvCode(), version); // now V1 compatible for softWareVersion :{101, 102} int errCode = AckPacketSyncerPartDeSerializationV1(parcel, packet); if (errCode != E_OK) { @@ -530,9 +529,7 @@ int SingleVerSerializeManager::AckPacketSyncerPartDeSerializationV1(Parcel &parc LOGE("[AckPacketSyncerPartDeSerializationV1] DeSerialization failed"); return -E_INVALID_ARGS; } - packet.SetData(mark); - packet.SetRecvCode(errCode); - packet.SetReserved(reserved); + packet.ChangeData(errCode, mark, reserved); return E_OK; } diff --git a/frameworks/libs/distributeddb/syncer/src/device/singlever/single_ver_sync_state_machine.cpp b/frameworks/libs/distributeddb/syncer/src/device/singlever/single_ver_sync_state_machine.cpp index aaeb802ba651075eb6f12ac422cd784c9312ae53..eaf9ff07f9ddcbe0e0f8edc3cb07624e11496410 100644 --- a/frameworks/libs/distributeddb/syncer/src/device/singlever/single_ver_sync_state_machine.cpp +++ b/frameworks/libs/distributeddb/syncer/src/device/singlever/single_ver_sync_state_machine.cpp @@ -1011,14 +1011,11 @@ void SingleVerSyncStateMachine::AddPullResponseTarget(const Message *inMsg, Wate LOGE("[StateMachine][AddPullResponseTarget] add failed, may oom"); return; } - targetTmp->SetTaskType(ISyncTarget::RESPONSE); - if (messageType == QUERY_SYNC_MESSAGE) { - targetTmp->SetQuery(packet->GetQuery()); - targetTmp->SetQuerySync(true); - } - targetTmp->SetMode(SyncModeType::RESPONSE_PULL); - targetTmp->SetEndWaterMark(pullEndWatermark); - targetTmp->SetResponseSessionId(sessionId); + targetTmp->BuildResponseTarget( + packet->GetQuery(), + SyncModeType::RESPONSE_PULL, + pullEndWatermark, sessionId, + messageType == QUERY_SYNC_MESSAGE); if (context_->AddSyncTarget(targetTmp) != E_OK) { delete targetTmp; return; @@ -1287,7 +1284,9 @@ int SingleVerSyncStateMachine::AbilitySyncNotifyRecv(const Message *inMsg) if (packet == nullptr) { return -E_INVALID_ARGS; } - int ackCode = packet->GetAckCode(); + int ackCode; + uint32_t swVersion; + packet->GetAckCodeAndVersion(ackCode, swVersion); if (ackCode != AbilitySync::CHECK_SUCCESS && ackCode != AbilitySync::LAST_NOTIFY) { LOGE("[StateMachine][AbilitySyncRecv] ackCode check failed,ackCode=%d", ackCode); context_->SetTaskErrCode(ackCode); @@ -1300,7 +1299,7 @@ int SingleVerSyncStateMachine::AbilitySyncNotifyRecv(const Message *inMsg) // while recv last notify means ability sync finished,it is better to reset watchDog to avoid timeout. LOGI("[StateMachine][AbilitySyncRecv] ability sync finished,label=%s,dev=%s", dataSync_->GetLabel().c_str(), STR_MASK(context_->GetDeviceId())); - context_->SetRemoteSoftwareVersion(packet->GetSoftwareVersion()); + context_->SetRemoteSoftwareVersion(swVersion); currentRemoteVersionId_ = context_->GetRemoteSoftwareVersionId(); std::lock_guard lock(stateMachineLock_); (void)ResetWatchDog(); diff --git a/frameworks/libs/distributeddb/syncer/src/device/singlever/single_ver_sync_target.cpp b/frameworks/libs/distributeddb/syncer/src/device/singlever/single_ver_sync_target.cpp index 1a03df1bd49923dea3aaf38dfa58b70c1c4a9097..08e464397776877a13098aa70c5397b759d5867f 100644 --- a/frameworks/libs/distributeddb/syncer/src/device/singlever/single_ver_sync_target.cpp +++ b/frameworks/libs/distributeddb/syncer/src/device/singlever/single_ver_sync_target.cpp @@ -45,7 +45,7 @@ void SingleVerSyncTarget::SetEndWaterMark(WaterMark waterMark) endWaterMark_ = waterMark; } -WaterMark SingleVerSyncTarget::GetEndWaterMark() const +const WaterMark &SingleVerSyncTarget::GetEndWaterMark() const { return endWaterMark_; } @@ -65,7 +65,7 @@ void SingleVerSyncTarget::SetQuery(const QuerySyncObject &query) query_ = query; } -QuerySyncObject SingleVerSyncTarget::GetQuery() const +const QuerySyncObject &SingleVerSyncTarget::GetQuery() const { return query_; } @@ -79,4 +79,17 @@ bool SingleVerSyncTarget::IsQuerySync() const { return isQuerySync_; } + +void SingleVerSyncTarget::BuildResponseTarget(const QuerySyncObject &query, int mode, WaterMark waterMark, + uint32_t responseSessionId, bool isSyncMessage) +{ + SetTaskType(ISyncTarget::RESPONSE); + if (isSyncMessage) { + SetQuery(query); + SetQuerySync(true); + } + SetMode(mode); + SetEndWaterMark(waterMark); + SetResponseSessionId(responseSessionId); +} } // namespace DistributedDB \ No newline at end of file diff --git a/frameworks/libs/distributeddb/syncer/src/device/singlever/single_ver_sync_target.h b/frameworks/libs/distributeddb/syncer/src/device/singlever/single_ver_sync_target.h index 7f8020274de616b7a5918e7c7893f3794c14cf20..d7a50119895bd5ca90f6e7a75bb74d81d0d80b2a 100644 --- a/frameworks/libs/distributeddb/syncer/src/device/singlever/single_ver_sync_target.h +++ b/frameworks/libs/distributeddb/syncer/src/device/singlever/single_ver_sync_target.h @@ -34,22 +34,27 @@ public: void SetEndWaterMark(WaterMark waterMark); // Get the end water mark of this task - WaterMark GetEndWaterMark() const; + const WaterMark &GetEndWaterMark() const; // For pull response sync - void SetResponseSessionId(uint32_t responseSessionId); uint32_t GetResponseSessionId() const override; // For query sync - void SetQuery(const QuerySyncObject &query); - QuerySyncObject GetQuery() const; - void SetQuerySync(bool isQuerySync); + const QuerySyncObject &GetQuery() const; bool IsQuerySync() const; + + void BuildResponseTarget(const QuerySyncObject &query, int mode, WaterMark waterMark, + uint32_t responseSessionId, bool isSyncMessage); private: WaterMark endWaterMark_; uint32_t responseSessionId_ = 0; QuerySyncObject query_; bool isQuerySync_ = false; + + // private setters + void SetResponseSessionId(uint32_t responseSessionId); + void SetQuery(const QuerySyncObject &query); + void SetQuerySync(bool isQuerySync); }; } // namespace DistributedDB diff --git a/frameworks/libs/distributeddb/syncer/src/device/time_sync.cpp b/frameworks/libs/distributeddb/syncer/src/device/time_sync.cpp index 55b903c610d1adf65aceafe589bfb5f06f99e788..0e61c8e937a76b22e8a6b950829b6155b2d042c7 100644 --- a/frameworks/libs/distributeddb/syncer/src/device/time_sync.cpp +++ b/frameworks/libs/distributeddb/syncer/src/device/time_sync.cpp @@ -49,12 +49,20 @@ TimeSyncPacket::~TimeSyncPacket() { } +bool TimeSyncPacket::IsEqual(const TimeSyncPacket &inPacketA, const TimeSyncPacket &inPacketB) +{ + return inPacketA.GetSourceTimeBegin() == inPacketB.GetSourceTimeBegin() && + inPacketA.GetSourceTimeEnd() == inPacketB.GetSourceTimeEnd() && + inPacketA.GetTargetTimeBegin() == inPacketB.GetTargetTimeBegin() && + inPacketA.GetTargetTimeEnd() == inPacketB.GetTargetTimeEnd(); +} + void TimeSyncPacket::SetSourceTimeBegin(Timestamp sourceTimeBegin) { sourceTimeBegin_ = sourceTimeBegin; } -Timestamp TimeSyncPacket::GetSourceTimeBegin() const +const Timestamp &TimeSyncPacket::GetSourceTimeBegin() const { return sourceTimeBegin_; } @@ -64,7 +72,7 @@ void TimeSyncPacket::SetSourceTimeEnd(Timestamp sourceTimeEnd) sourceTimeEnd_ = sourceTimeEnd; } -Timestamp TimeSyncPacket::GetSourceTimeEnd() const +const Timestamp &TimeSyncPacket::GetSourceTimeEnd() const { return sourceTimeEnd_; } @@ -74,7 +82,7 @@ void TimeSyncPacket::SetTargetTimeBegin(Timestamp targetTimeBegin) targetTimeBegin_ = targetTimeBegin; } -Timestamp TimeSyncPacket::GetTargetTimeBegin() const +const Timestamp &TimeSyncPacket::GetTargetTimeBegin() const { return targetTimeBegin_; } @@ -84,7 +92,7 @@ void TimeSyncPacket::SetTargetTimeEnd(Timestamp targetTimeEnd) targetTimeEnd_ = targetTimeEnd; } -Timestamp TimeSyncPacket::GetTargetTimeEnd() const +const Timestamp &TimeSyncPacket::GetTargetTimeEnd() const { return targetTimeEnd_; } @@ -94,12 +102,12 @@ void TimeSyncPacket::SetVersion(uint32_t version) version_ = version; } -uint32_t TimeSyncPacket::GetVersion() const +const uint32_t &TimeSyncPacket::GetVersion() const { return version_; } -TimeOffset TimeSyncPacket::GetRequestLocalOffset() const +const TimeOffset &TimeSyncPacket::GetRequestLocalOffset() const { return requestLocalOffset_; } @@ -109,7 +117,7 @@ void TimeSyncPacket::SetRequestLocalOffset(TimeOffset offset) requestLocalOffset_ = offset; } -TimeOffset TimeSyncPacket::GetResponseLocalOffset() const +const TimeOffset &TimeSyncPacket::GetResponseLocalOffset() const { return responseLocalOffset_; } @@ -132,6 +140,14 @@ uint32_t TimeSyncPacket::CalculateLen() return len; } +void TimeSyncPacket::ChangeTime(Timestamp sourceBegin, Timestamp sourceEnd, Timestamp targetBegin, Timestamp targetEnd) +{ + sourceTimeBegin_ = sourceBegin; + sourceTimeEnd_ = sourceEnd; + targetTimeBegin_ = targetBegin; + targetTimeEnd_ = targetEnd; +} + // Class TimeSync TimeSync::TimeSync() : communicateHandle_(nullptr), diff --git a/frameworks/libs/distributeddb/syncer/src/device/time_sync.h b/frameworks/libs/distributeddb/syncer/src/device/time_sync.h index b8ec799191ed1a955b9c370aa7339d94cb96a7aa..8f1d4ade3e7dfec558833d0bba502987bf8b2867 100644 --- a/frameworks/libs/distributeddb/syncer/src/device/time_sync.h +++ b/frameworks/libs/distributeddb/syncer/src/device/time_sync.h @@ -27,35 +27,30 @@ public: TimeSyncPacket(); ~TimeSyncPacket(); + static bool IsEqual(const TimeSyncPacket &inPacketA, const TimeSyncPacket &inPacketB); + + // getters + const Timestamp &GetSourceTimeBegin() const; + const Timestamp &GetSourceTimeEnd() const; + const Timestamp &GetTargetTimeBegin() const; + const Timestamp &GetTargetTimeEnd() const; + const uint32_t &GetVersion() const; + const TimeOffset &GetRequestLocalOffset() const; + const TimeOffset &GetResponseLocalOffset() const; + + // setters void SetSourceTimeBegin(Timestamp sourceTimeBegin); - - Timestamp GetSourceTimeBegin() const; - void SetSourceTimeEnd(Timestamp sourceTimeEnd); - - Timestamp GetSourceTimeEnd() const; - void SetTargetTimeBegin(Timestamp targetTimeBegin); - - Timestamp GetTargetTimeBegin() const; - void SetTargetTimeEnd(Timestamp targetTimeEnd); - - Timestamp GetTargetTimeEnd() const; - void SetVersion(uint32_t version); - - uint32_t GetVersion() const; - void SetRequestLocalOffset(TimeOffset offset); - - TimeOffset GetRequestLocalOffset() const; - void SetResponseLocalOffset(TimeOffset offset); - TimeOffset GetResponseLocalOffset() const; - + // public methods static uint32_t CalculateLen(); + void ChangeTime(Timestamp sourceBegin, Timestamp sourceEnd, + Timestamp targetBegin, Timestamp targetEnd); private: Timestamp sourceTimeBegin_; // start point time on peer Timestamp sourceTimeEnd_; // end point time on local diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_ability_sync_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_ability_sync_test.cpp index 7cc95252983355534a628d1d22ef687679cb50ce..c822b0c0ab7f3f5e91df2f7c3a74e2ebf7b216d8 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_ability_sync_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_ability_sync_test.cpp @@ -121,16 +121,11 @@ HWTEST_F(DistributedDBAbilitySyncTest, RequestPacketTest001, TestSize.Level0) * @tc.steps: step1. create a AbilityRequestPacket packet1 * @tc.steps: step2. set version = ABILITY_SYNC_VERSION_V1. schema = TEST_SCHEMA. */ - AbilitySyncRequestPacket packet1; DbAbility ability1; #ifndef OMIT_ZLIB ability1.SetAbilityItem(SyncConfig::DATABASE_COMPRESSION_ZLIB, SUPPORT_MARK); #endif - packet1.SetProtocolVersion(ABILITY_SYNC_VERSION_V1); - packet1.SetSoftwareVersion(SOFTWARE_VERSION_CURRENT); - packet1.SetSchema(TEST_SCHEMA); - packet1.SetSendCode(E_OK); - packet1.SetDbAbility(ability1); + AbilitySyncRequestPacket packet1(ABILITY_SYNC_VERSION_V1, TEST_SCHEMA, ability1); Message msg1(ABILITY_SYNC_MESSAGE); msg1.SetMessageType(TYPE_REQUEST); msg1.SetCopiedObject(packet1); @@ -178,10 +173,7 @@ HWTEST_F(DistributedDBAbilitySyncTest, RequestPacketTest002, TestSize.Level0) * @tc.steps: step1. create a AbilityRequestPacket packet1 * @tc.steps: step2. set version = ABILITY_SYNC_VERSION_V1 + 1. schema = TEST_SCHEMA. */ - AbilitySyncRequestPacket packet1; - packet1.SetProtocolVersion(ABILITY_SYNC_VERSION_V1 + 1); - packet1.SetSoftwareVersion(SOFTWARE_VERSION_CURRENT); - packet1.SetSchema(""); + AbilitySyncRequestPacket packet1(ABILITY_SYNC_VERSION_V1 + 1, SOFTWARE_VERSION_CURRENT, ""); Message msg1(ABILITY_SYNC_MESSAGE); msg1.SetMessageType(TYPE_REQUEST); msg1.SetCopiedObject(packet1); @@ -224,15 +216,10 @@ HWTEST_F(DistributedDBAbilitySyncTest, RequestPacketTest003, TestSize.Level0) * @tc.steps: step1. create a AbilityRequestPacket packet1 * @tc.steps: step2. set version = ABILITY_SYNC_VERSION_V1. schema = TEST_SCHEMA. */ - AbilitySyncRequestPacket packet1; - packet1.SetProtocolVersion(ABILITY_SYNC_VERSION_V1); - packet1.SetSoftwareVersion(SOFTWARE_VERSION_CURRENT); - packet1.SetSchema(TEST_SCHEMA); - packet1.SetSendCode(E_OK); + int secLabel = 3; // label 3 int secFlag = 1; // flag 1 - packet1.SetSecLabel(secLabel); - packet1.SetSecFlag(secFlag); + AbilitySyncRequestPacket packet1(SOFTWARE_VERSION_CURRENT, TEST_SCHEMA, secLabel, secFlag); Message msg1(ABILITY_SYNC_MESSAGE); msg1.SetMessageType(TYPE_REQUEST); msg1.SetCopiedObject(packet1); @@ -281,15 +268,9 @@ HWTEST_F(DistributedDBAbilitySyncTest, RequestPacketTest004, TestSize.Level0) * @tc.steps: step1. create a AbilityRequestPacket packet1 * @tc.steps: step2. set version = ABILITY_SYNC_VERSION_V1. schema = TEST_SCHEMA. */ - AbilitySyncRequestPacket packet1; - packet1.SetProtocolVersion(ABILITY_SYNC_VERSION_V1); - packet1.SetSoftwareVersion(SOFTWARE_VERSION_RELEASE_2_0); - packet1.SetSchema(TEST_SCHEMA); - packet1.SetSendCode(E_OK); int secLabel = 3; // label 3 int secFlag = 1; // flag 1 - packet1.SetSecLabel(secLabel); - packet1.SetSecFlag(secFlag); + AbilitySyncRequestPacket packet1(SOFTWARE_VERSION_RELEASE_2_0, TEST_SCHEMA, secLabel, secFlag); Message msg1(ABILITY_SYNC_MESSAGE); msg1.SetMessageType(TYPE_REQUEST); msg1.SetCopiedObject(packet1); @@ -338,16 +319,11 @@ HWTEST_F(DistributedDBAbilitySyncTest, AckPacketTest001, TestSize.Level0) * @tc.steps: step1. create a AbilityAckPacket packet1 * @tc.steps: step2. set version = ABILITY_SYNC_VERSION_V1. schema = TEST_SCHEMA. */ - AbilitySyncAckPacket packet1; DbAbility ability1; #ifndef OMIT_ZLIB ability1.SetAbilityItem(SyncConfig::DATABASE_COMPRESSION_ZLIB, SUPPORT_MARK); #endif - packet1.SetProtocolVersion(ABILITY_SYNC_VERSION_V1); - packet1.SetSoftwareVersion(SOFTWARE_VERSION_CURRENT); - packet1.SetSchema(TEST_SCHEMA); - packet1.SetAckCode(E_VERSION_NOT_SUPPORT); - packet1.SetDbAbility(ability1); + AbilitySyncAckPacket packet1(TEST_SCHEMA, E_VERSION_NOT_SUPPORT, ability1); Message msg1(ABILITY_SYNC_MESSAGE); msg1.SetMessageType(TYPE_RESPONSE); msg1.SetCopiedObject(packet1); @@ -450,10 +426,7 @@ HWTEST_F(DistributedDBAbilitySyncTest, RequestReceiveTest001, TestSize.Level0) /** * @tc.steps: step4. create a AbilityRequestkPacket packet1 */ - AbilitySyncRequestPacket packet1; - packet1.SetProtocolVersion(ABILITY_SYNC_VERSION_V1); - packet1.SetSoftwareVersion(SOFTWARE_VERSION_CURRENT); - packet1.SetSchema(TEST_SCHEMA); + AbilitySyncRequestPacket packet1(ABILITY_SYNC_VERSION_V1, SOFTWARE_VERSION_CURRENT, TEST_SCHEMA); msg1.SetCopiedObject(packet1); /** @@ -470,7 +443,7 @@ HWTEST_F(DistributedDBAbilitySyncTest, RequestReceiveTest001, TestSize.Level0) * @tc.steps: step6. call RequestRecv, set inMsg sendCode -E_VERSION_NOT_SUPPORT * @tc.expected: step6. RequestRecv return E_VERSION_NOT_SUPPORT */ - packet1.SetSendCode(-E_VERSION_NOT_SUPPORT); + packet1.SetCodeAndSchema(-E_VERSION_NOT_SUPPORT, packet1.GetSchema()); msg1.SetCopiedObject(packet1); EXPECT_EQ(async.RequestRecv(&msg1, context), -E_VERSION_NOT_SUPPORT); @@ -478,8 +451,7 @@ HWTEST_F(DistributedDBAbilitySyncTest, RequestReceiveTest001, TestSize.Level0) * @tc.steps: step7. call RequestRecv, SetSchema "" * @tc.expected: step7. IsSchemaCompatible false */ - packet1.SetSchema(""); - packet1.SetSendCode(E_OK); + packet1.SetCodeAndSchema(E_OK, ""); msg1.SetCopiedObject(packet1); EXPECT_EQ(async.RequestRecv(&msg1, context), -E_SECURITY_OPTION_CHECK_ERROR); EXPECT_FALSE(context->GetTaskErrCode() != -E_SCHEMA_MISMATCH); @@ -523,10 +495,8 @@ HWTEST_F(DistributedDBAbilitySyncTest, AckReceiveTest001, TestSize.Level0) * @tc.steps: step4. create a AbilityAckPacket packet1 */ AbilitySyncAckPacket packet1; - packet1.SetProtocolVersion(ABILITY_SYNC_VERSION_V1); - packet1.SetSoftwareVersion(SOFTWARE_VERSION_CURRENT); - packet1.SetAckCode(E_OK); - packet1.SetSchema(TEST_SCHEMA); + packet1.SetCodeAndSchema(E_OK, TEST_SCHEMA); + msg1.SetCopiedObject(packet1); /** @@ -542,7 +512,7 @@ HWTEST_F(DistributedDBAbilitySyncTest, AckReceiveTest001, TestSize.Level0) * @tc.steps: step6. call RequestRecv, SetSchema "" * @tc.expected: step6. IsSchemaCompatible false */ - packet1.SetSchema(""); + packet1.SetCodeAndSchema(E_OK, ""); msg1.SetCopiedObject(packet1); EXPECT_EQ(async.AckRecv(&msg1, context), E_OK); @@ -550,8 +520,7 @@ HWTEST_F(DistributedDBAbilitySyncTest, AckReceiveTest001, TestSize.Level0) * @tc.steps: step7. call AckRecv, set inMsg sendCode -E_VERSION_NOT_SUPPORT * @tc.expected: step7. return -E_VERSION_NOT_SUPPORT */ - packet1.SetSchema(TEST_SCHEMA); - packet1.SetAckCode(-E_VERSION_NOT_SUPPORT); + packet1.SetCodeAndSchema(-E_VERSION_NOT_SUPPORT, TEST_SCHEMA); msg1.SetCopiedObject(packet1); EXPECT_EQ(async.AckRecv(&msg1, context), -E_VERSION_NOT_SUPPORT); RefObject::KillAndDecObjRef(context); @@ -587,11 +556,7 @@ HWTEST_F(DistributedDBAbilitySyncTest, AckReceiveTest002, TestSize.Level1) Message msg1(ABILITY_SYNC_MESSAGE); msg1.SetMessageType(TYPE_RESPONSE); AbilitySyncAckPacket packet; - packet.SetProtocolVersion(ABILITY_SYNC_VERSION_V1); - packet.SetSoftwareVersion(SOFTWARE_VERSION_CURRENT); - packet.SetAckCode(E_OK); - packet.SetSchema(RDB_SCHEMA); - packet.SetSchemaType(static_cast(SchemaType::RELATIVE)); + packet.SetSchemaInfo(RDB_SCHEMA, static_cast(SchemaType::RELATIVE)); msg1.SetCopiedObject(packet); EXPECT_EQ(async.AckRecv(&msg1, context), -E_NOT_SUPPORT); EXPECT_EQ(context->GetTaskErrCode(), -E_NOT_SUPPORT); diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_mock_sync_module_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_mock_sync_module_test.cpp index c8b74bea09b5d23900166b694d1cdfaa93df5620..3acf933357ed15eb306b57da8912eba864b092fb 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_mock_sync_module_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_mock_sync_module_test.cpp @@ -687,10 +687,7 @@ HWTEST_F(DistributedDBMockSyncModuleTest, StateMachineCheck012, TestSize.Level1) EXPECT_CALL(stateMachine, SwitchStateAndStep(_)).WillOnce(Return()); DistributedDB::Message msg(ABILITY_SYNC_MESSAGE); msg.SetMessageType(TYPE_NOTIFY); - AbilitySyncAckPacket packet; - packet.SetProtocolVersion(ABILITY_SYNC_VERSION_V1); - packet.SetSoftwareVersion(SOFTWARE_VERSION_CURRENT); - packet.SetAckCode(-E_BUSY); + AbilitySyncAckPacket packet(-E_BUSY); msg.SetCopiedObject(packet); EXPECT_EQ(stateMachine.ReceiveMessageCallback(&msg), E_OK); EXPECT_EQ(syncTaskContext.GetTaskErrCode(), -E_BUSY); @@ -960,8 +957,7 @@ HWTEST_F(DistributedDBMockSyncModuleTest, AbilitySync001, TestSize.Level1) DistributedDB::Message *message = new (std::nothrow) DistributedDB::Message(); ASSERT_TRUE(message != nullptr); - AbilitySyncAckPacket packet; - packet.SetAckCode(-E_BUSY); + AbilitySyncAckPacket packet(-E_BUSY); message->SetCopiedObject(packet); EXPECT_EQ(abilitySync.AckRecv(message, &syncTaskContext), -E_BUSY); delete message; @@ -990,10 +986,8 @@ HWTEST_F(DistributedDBMockSyncModuleTest, AbilitySync002, TestSize.Level1) */ DistributedDB::Message *message = new (std::nothrow) DistributedDB::Message(); ASSERT_TRUE(message != nullptr); - AbilitySyncAckPacket packet; - packet.SetAckCode(E_OK); // should set version less than 108 avoid ability sync with 2 packet - packet.SetSoftwareVersion(SOFTWARE_VERSION_RELEASE_7_0); + AbilitySyncAckPacket packet(SOFTWARE_VERSION_RELEASE_7_0, E_OK); message->SetCopiedObject(packet); /** * @tc.steps: step2. set syncDBInterface busy for save data return -E_BUSY diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_multi_ver_p2p_sync_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_multi_ver_p2p_sync_test.cpp index 82172bb5b5b857d250d271bcd978fdc0bac916bf..ee70bf4e4a695e004bebd202149f8ed2b7ab8a26 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_multi_ver_p2p_sync_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/distributeddb_multi_ver_p2p_sync_test.cpp @@ -607,20 +607,12 @@ HWTEST_F(DistributedDBMultiVerP2PSyncTest, IsolationSync003, TestSize.Level2) static void SetTimeSyncPacketField(TimeSyncPacket &inPacket, Timestamp sourceBegin, Timestamp sourceEnd, Timestamp targetBegin, Timestamp targetEnd) { - inPacket.SetSourceTimeBegin(sourceBegin); - inPacket.SetSourceTimeEnd(sourceEnd); - inPacket.SetTargetTimeBegin(targetBegin); - inPacket.SetTargetTimeEnd(targetEnd); + inPacket.ChangeTime(sourceBegin, sourceEnd, targetBegin, targetEnd); } static bool IsTimeSyncPacketEqual(const TimeSyncPacket &inPacketA, const TimeSyncPacket &inPacketB) { - bool equal = true; - equal = inPacketA.GetSourceTimeBegin() == inPacketB.GetSourceTimeBegin() ? equal : false; - equal = inPacketA.GetSourceTimeEnd() == inPacketB.GetSourceTimeEnd() ? equal : false; - equal = inPacketA.GetTargetTimeBegin() == inPacketB.GetTargetTimeBegin() ? equal : false; - equal = inPacketA.GetTargetTimeEnd() == inPacketB.GetTargetTimeEnd() ? equal : false; - return equal; + return TimeSyncPacket::IsEqual(inPacketA, inPacketB); } /**