1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef COMMIT_HISTORY_SYNC_H 17 #define COMMIT_HISTORY_SYNC_H 18 19 #ifndef OMIT_MULTI_VER 20 #include <cstdint> 21 #include <string> 22 #include <map> 23 #include <vector> 24 25 #include "icommunicator.h" 26 #include "multi_ver_kvdb_sync_interface.h" 27 #include "multi_ver_sync_task_context.h" 28 #include "sync_types.h" 29 #include "version.h" 30 31 namespace DistributedDB { 32 class CommitHistorySyncRequestPacket { 33 public: CommitHistorySyncRequestPacket()34 CommitHistorySyncRequestPacket() {}; ~CommitHistorySyncRequestPacket()35 ~CommitHistorySyncRequestPacket() {}; 36 37 uint32_t CalculateLen() const; 38 39 void SetCommitMap(std::map<std::string, MultiVerCommitNode> &inMap); 40 41 void GetCommitMap(std::map<std::string, MultiVerCommitNode> &outMap) const; 42 43 void SetVersion(uint32_t version); 44 45 uint32_t GetVersion() const; 46 47 void SetReserved(std::vector<uint64_t> &reserved); 48 49 std::vector<uint64_t> GetReserved() const; 50 51 private: 52 std::map<std::string, MultiVerCommitNode> commitMap_; 53 uint32_t version_ = SOFTWARE_VERSION_CURRENT; 54 std::vector<uint64_t> reserved_; 55 }; 56 57 class CommitHistorySyncAckPacket { 58 public: CommitHistorySyncAckPacket()59 CommitHistorySyncAckPacket() : errorCode_(0) {}; ~CommitHistorySyncAckPacket()60 ~CommitHistorySyncAckPacket() {}; 61 62 uint32_t CalculateLen() const; 63 64 void SetData(std::vector<MultiVerCommitNode> &inData); 65 66 void GetData(std::vector<MultiVerCommitNode> &outData) const; 67 68 void SetErrorCode(int32_t errCode); 69 70 void GetErrorCode(int32_t &errCode) const; 71 72 void SetVersion(uint32_t version); 73 74 uint32_t GetVersion() const; 75 76 void SetReserved(std::vector<uint64_t> &reserved); 77 78 std::vector<uint64_t> GetReserved() const; 79 80 private: 81 int32_t errorCode_; 82 uint32_t version_ = SOFTWARE_VERSION_CURRENT; 83 std::vector<MultiVerCommitNode> commits_; 84 std::vector<uint64_t> reserved_; 85 }; 86 87 class CommitHistorySync { 88 public: CommitHistorySync()89 CommitHistorySync() : storagePtr_(nullptr), communicateHandle_(nullptr) {}; 90 ~CommitHistorySync(); 91 DISABLE_COPY_ASSIGN_MOVE(CommitHistorySync); 92 93 static int RegisterTransformFunc(); 94 95 int Initialize(MultiVerKvDBSyncInterface *storagePtr, ICommunicator *communicateHandle); 96 97 static int Serialization(uint8_t *buffer, uint32_t length, const Message *inMsg); 98 99 static int DeSerialization(const uint8_t *buffer, uint32_t length, Message *inMsg); 100 101 static uint32_t CalculateLen(const Message *inMsg); 102 103 void TimeOutCallback(MultiVerSyncTaskContext *context, const Message *message) const; 104 105 int SyncStart(MultiVerSyncTaskContext *context); 106 107 int RequestRecvCallback(const MultiVerSyncTaskContext *context, const Message *message); 108 109 int AckRecvCallback(MultiVerSyncTaskContext *context, const Message *message); 110 111 private: 112 static int RequestPacketCalculateLen(const Message *inMsg, uint32_t &len); 113 114 static int RequestPacketSerialization(uint8_t *buffer, uint32_t length, const Message *inMsg); 115 116 static int RequestPacketDeSerialization(const uint8_t *buffer, uint32_t length, Message *inMsg); 117 118 static int AckPacketCalculateLen(const Message *inMsg, uint32_t &len); 119 120 static int AckPacketSerialization(uint8_t *buffer, uint32_t length, const Message *inMsg); 121 122 static int AckPacketDeSerialization(const uint8_t *buffer, uint32_t length, Message *inMsg); 123 124 static bool IsPacketValid(const Message *inMsg, uint16_t messageType); 125 126 int Send(const DeviceID &deviceId, const Message *inMsg); 127 128 int GetDeviceLatestCommit(std::map<std::string, MultiVerCommitNode> &); 129 130 int GetCommitTree(const std::map<std::string, MultiVerCommitNode> &, std::vector<MultiVerCommitNode> &); 131 132 int SendRequestPacket(const MultiVerSyncTaskContext *context, 133 std::map<std::string, MultiVerCommitNode> &commitMap); 134 135 int SendAckPacket(const MultiVerSyncTaskContext *context, std::vector<MultiVerCommitNode> &commits, 136 int ackCode, const Message *message); 137 138 int GetLocalDeviceInfo(std::string &deviceInfo); 139 140 int RunPermissionCheck(const std::string &deviceId) const; 141 142 MultiVerKvDBSyncInterface *storagePtr_; 143 ICommunicator *communicateHandle_; 144 }; 145 } // namespace DistributedDB 146 147 #endif 148 #endif