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 VALUE_SLICE_SYNC_H
17 #define VALUE_SLICE_SYNC_H
18 
19 #ifndef OMIT_MULTI_VER
20 #include <vector>
21 
22 #include "icommunicator.h"
23 #include "multi_ver_kvdb_sync_interface.h"
24 #include "multi_ver_sync_task_context.h"
25 
26 namespace DistributedDB {
27 class ValueSliceHashPacket {
28 public:
ValueSliceHashPacket()29     ValueSliceHashPacket() : errCode_(E_OK) {};
~ValueSliceHashPacket()30     ~ValueSliceHashPacket() {};
31 
32     uint32_t CalculateLen() const;
33 
34     void SetValueSliceHash(ValueSliceHash &hash);
35 
36     void GetValueSliceHash(ValueSliceHash &hash) const;
37 
38     void SetErrCode(int32_t errCode);
39 
40     int32_t GetErrCode() const;
41 private:
42     ValueSliceHash valueSliceHash_;
43     int32_t errCode_;
44 };
45 
46 class ValueSlicePacket {
47 public:
ValueSlicePacket()48     ValueSlicePacket() : errorCode_(0) {};
~ValueSlicePacket()49     ~ValueSlicePacket() {};
50 
51     uint32_t CalculateLen() const;
52 
53     void SetData(const ValueSlice &data);
54 
55     void GetData(ValueSlice &data) const;
56 
57     void SetErrorCode(int32_t errCode);
58 
59     void GetErrorCode(int32_t &errCode) const;
60 private:
61     ValueSlice valueSlice_;
62     int32_t errorCode_;
63 };
64 
65 class ValueSliceSync {
66 public:
ValueSliceSync()67     ValueSliceSync() : storagePtr_(nullptr), communicateHandle_(nullptr) {};
68     ~ValueSliceSync();
69     DISABLE_COPY_ASSIGN_MOVE(ValueSliceSync);
70 
71     static int RegisterTransformFunc();
72 
73     int Initialize(MultiVerKvDBSyncInterface *storagePtr, ICommunicator *communicateHandle);
74 
75     static int Serialization(uint8_t *buffer, uint32_t length, const Message *inMsg);
76 
77     static int DeSerialization(const uint8_t *buffer, uint32_t length, Message *inMsg);
78 
79     static uint32_t CalculateLen(const Message *inMsg);
80 
81     int SyncStart(MultiVerSyncTaskContext *context);
82 
83     int RequestRecvCallback(const MultiVerSyncTaskContext *context, const Message *message);
84 
85     int AckRecvCallback(const MultiVerSyncTaskContext *context, const Message *message);
86 
87     void SendFinishedRequest(const MultiVerSyncTaskContext *context);
88 
89 private:
90     static int RequestPacketCalculateLen(const Message *inMsg, uint32_t &len);
91 
92     static int RequestPacketSerialization(uint8_t *buffer, uint32_t length, const Message *inMsg);
93 
94     static int RequestPacketDeSerialization(const uint8_t *buffer, uint32_t length, Message *inMsg);
95 
96     static int AckPacketCalculateLen(const Message *inMsg, uint32_t &len);
97 
98     static int AckPacketSerialization(uint8_t *buffer, uint32_t length, const Message *inMsg);
99 
100     static int AckPacketDeSerialization(const uint8_t *buffer, uint32_t length, Message *inMsg);
101 
102     static bool IsPacketValid(const Message *inMsg, uint16_t messageType);
103 
104     int GetValidValueSliceHashNode(MultiVerSyncTaskContext *context, ValueSliceHash &valueHashNode);
105 
106     int Send(const DeviceID &deviceId, const Message *inMsg);
107 
108     int SendRequestPacket(const MultiVerSyncTaskContext *context, ValueSliceHash &valueSliceHash);
109 
110     int SendAckPacket(const MultiVerSyncTaskContext *context, const ValueSlice &value, int ackCode,
111         const Message *message);
112 
113     bool IsValueSliceExisted(const ValueSliceHash &value);
114 
115     int GetValueSlice(const ValueSliceHash &hashValue, ValueSlice &sliceValue);
116 
117     int PutValueSlice(const ValueSliceHash &hashValue, const ValueSlice &sliceValue);
118 
119     static const int MAX_VALUE_NODE_SIZE;
120     MultiVerKvDBSyncInterface *storagePtr_;
121     ICommunicator *communicateHandle_;
122 };
123 }
124 
125 #endif
126 #endif