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 <map>
21  #include <vector>
22  
23  #include "icommunicator.h"
24  #include "multi_ver_kvdb_sync_interface.h"
25  #include "multi_ver_sync_task_context.h"
26  #include "sync_types.h"
27  #include "version.h"
28  
29  namespace DistributedDB {
30  class CommitHistorySyncRequestPacket {
31  public:
CommitHistorySyncRequestPacket()32      CommitHistorySyncRequestPacket() {};
~CommitHistorySyncRequestPacket()33      ~CommitHistorySyncRequestPacket() {};
34  
35      uint32_t CalculateLen() const;
36  
37      void SetCommitMap(std::map<std::string, MultiVerCommitNode> &inMap);
38  
39      void GetCommitMap(std::map<std::string, MultiVerCommitNode> &outMap) const;
40  
41      void SetVersion(uint32_t version);
42  
43      uint32_t GetVersion() const;
44  
45      void SetReserved(std::vector<uint64_t> &reserved);
46  
47      std::vector<uint64_t> GetReserved() const;
48  
49  private:
50      std::map<std::string, MultiVerCommitNode> commitMap_;
51      uint32_t version_ = SOFTWARE_VERSION_CURRENT;
52      std::vector<uint64_t> reserved_;
53  };
54  
55  class CommitHistorySyncAckPacket {
56  public:
CommitHistorySyncAckPacket()57      CommitHistorySyncAckPacket() : errorCode_(0) {};
~CommitHistorySyncAckPacket()58      ~CommitHistorySyncAckPacket() {};
59  
60      uint32_t CalculateLen() const;
61  
62      void SetData(std::vector<MultiVerCommitNode> &inData);
63  
64      void GetData(std::vector<MultiVerCommitNode> &outData) const;
65  
66      void SetErrorCode(int32_t errCode);
67  
68      void GetErrorCode(int32_t &errCode) const;
69  
70      void SetVersion(uint32_t version);
71  
72      uint32_t GetVersion() const;
73  
74      void SetReserved(std::vector<uint64_t> &reserved);
75  
76      std::vector<uint64_t> GetReserved() const;
77  
78  private:
79      int32_t errorCode_;
80      uint32_t version_ = SOFTWARE_VERSION_CURRENT;
81      std::vector<MultiVerCommitNode> commits_;
82      std::vector<uint64_t> reserved_;
83  };
84  
85  class CommitHistorySync {
86  public:
CommitHistorySync()87      CommitHistorySync() : storagePtr_(nullptr), communicateHandle_(nullptr) {};
88      ~CommitHistorySync();
89      DISABLE_COPY_ASSIGN_MOVE(CommitHistorySync);
90  
91      static int RegisterTransformFunc();
92  
93      int Initialize(MultiVerKvDBSyncInterface *storagePtr, ICommunicator *communicateHandle);
94  
95      static int Serialization(uint8_t *buffer, uint32_t length, const Message *inMsg);
96  
97      static int DeSerialization(const uint8_t *buffer, uint32_t length, Message *inMsg);
98  
99      static uint32_t CalculateLen(const Message *inMsg);
100  
101      void TimeOutCallback(MultiVerSyncTaskContext *context, const Message *message) const;
102  
103      int SyncStart(MultiVerSyncTaskContext *context);
104  
105      int RequestRecvCallback(const MultiVerSyncTaskContext *context, const Message *message);
106  
107      int AckRecvCallback(MultiVerSyncTaskContext *context, const Message *message);
108  
109  private:
110      static int RequestPacketCalculateLen(const Message *inMsg, uint32_t &len);
111  
112      static int RequestPacketSerialization(uint8_t *buffer, uint32_t length, const Message *inMsg);
113  
114      static int RequestPacketDeSerialization(const uint8_t *buffer, uint32_t length, Message *inMsg);
115  
116      static int AckPacketCalculateLen(const Message *inMsg, uint32_t &len);
117  
118      static int AckPacketSerialization(uint8_t *buffer, uint32_t length, const Message *inMsg);
119  
120      static int AckPacketDeSerialization(const uint8_t *buffer, uint32_t length, Message *inMsg);
121  
122      static bool IsPacketValid(const Message *inMsg, uint16_t messageType);
123  
124      int Send(const DeviceID &deviceId, const Message *inMsg);
125  
126      int GetDeviceLatestCommit(std::map<std::string, MultiVerCommitNode> &);
127  
128      int GetCommitTree(const std::map<std::string, MultiVerCommitNode> &, std::vector<MultiVerCommitNode> &);
129  
130      int SendRequestPacket(const MultiVerSyncTaskContext *context,
131          std::map<std::string, MultiVerCommitNode> &commitMap);
132  
133      int SendAckPacket(const MultiVerSyncTaskContext *context, std::vector<MultiVerCommitNode> &commits,
134          int ackCode, const Message *message);
135  
136      int GetLocalDeviceInfo(std::string &deviceInfo);
137  
138      int RunPermissionCheck(const std::string &deviceId) const;
139  
140      MultiVerKvDBSyncInterface *storagePtr_;
141      ICommunicator *communicateHandle_;
142  };
143  }  // namespace DistributedDB
144  
145  #endif
146  #endif