1 /*
2  * Copyright (c) 2022 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 REMOTE_EXECUTOR_PACKET_H
17 #define REMOTE_EXECUTOR_PACKET_H
18 
19 #include "isync_packet.h"
20 #include "message.h"
21 #include "prepared_stmt.h"
22 #include "relational_row_data_set.h"
23 #include "sync_types.h"
24 #include "version.h"
25 
26 namespace DistributedDB {
27 class RemoteExecutorRequestPacket : public ISyncPacket {
28 public:
29     RemoteExecutorRequestPacket() = default;
30     ~RemoteExecutorRequestPacket() override = default;
31 
32     uint32_t GetVersion() const;
33 
34     void SetVersion(uint32_t version);
35 
36     uint32_t GetFlag() const;
37 
38     void SetFlag(uint32_t flag);
39 
40     const PreparedStmt &GetPreparedStmt() const;
41 
42     bool IsNeedResponse() const;
43 
44     void SetNeedResponse();
45 
46     void SetExtraConditions(const std::map<std::string, std::string> &extraConditions);
47 
48     std::map<std::string, std::string> GetExtraConditions() const;
49 
50     uint32_t CalculateLen() const override;
51 
52     int Serialization(Parcel &parcel) const override;
53 
54     int DeSerialization(Parcel &parcel) override;
55 
56     void SetOpCode(PreparedStmt::ExecutorOperation opCode);
57 
58     void SetSql(const std::string &sql);
59 
60     void SetBindArgs(const std::vector<std::string> &bindArgs);
61 
62     void SetSecLabel(int32_t secLabel);
63 
64     int32_t GetSecLabel() const;
65 
66     static RemoteExecutorRequestPacket* Create();
67 
68     static void Release(RemoteExecutorRequestPacket *&packet);
69 
70     static constexpr uint32_t REQUEST_PACKET_VERSION_V1 = SOFTWARE_VERSION_RELEASE_6_0;
71     static constexpr uint32_t REQUEST_PACKET_VERSION_V2 = SOFTWARE_VERSION_RELEASE_6_0 + 1; // 1 is version 107
72     static constexpr uint32_t REQUEST_PACKET_VERSION_V3 = SOFTWARE_VERSION_RELEASE_6_0 + 2; // 2 is version 108
73     // abandon not set security label in v4
74     static constexpr uint32_t REQUEST_PACKET_VERSION_V4 = SOFTWARE_VERSION_RELEASE_6_0 + 3; // 3 is version 109
75     static constexpr uint32_t REQUEST_PACKET_VERSION_CURRENT = REQUEST_PACKET_VERSION_V4;
76 private:
77     uint32_t version_ = 0u;
78     uint32_t flag_ = 0u; // 0x01 mean need reply ack
79     PreparedStmt preparedStmt_;
80     std::map<std::string, std::string> extraConditions_;
81     int32_t secLabel_ = UNKNOWN_SECURITY_LABEL; // source sec label
82 };
83 
84 class RemoteExecutorAckPacket : public ISyncPacket {
85 public:
86     RemoteExecutorAckPacket() = default;
87 
88     ~RemoteExecutorAckPacket() override = default;
89 
90     uint32_t GetVersion() const;
91 
92     void SetVersion(uint32_t version);
93 
94     uint32_t GetFlag() const;
95 
96     void SetFlag(uint32_t flag);
97 
98     int32_t GetAckCode() const;
99 
100     void SetAckCode(int32_t ackCode);
101 
102     void MoveInRowDataSet(RelationalRowDataSet &&rowDataSet);
103 
104     RelationalRowDataSet &&MoveOutRowDataSet() const;
105 
106     bool IsLastAck() const;
107 
108     void SetLastAck();
109 
110     SecurityOption GetSecurityOption() const;
111 
112     void SetSecurityOption(const SecurityOption &option);
113 
114     uint32_t CalculateLen() const override;
115 
116     int Serialization(Parcel &parcel) const override;
117 
118     int DeSerialization(Parcel &parcel) override;
119 
120     static const uint32_t RESPONSE_PACKET_VERSION_V1 = SOFTWARE_VERSION_RELEASE_6_0;
121     static const uint32_t RESPONSE_PACKET_VERSION_CURRENT = RESPONSE_PACKET_VERSION_V1;
122 private:
123     uint32_t version_ = 0u;
124     int32_t ackCode_ = 0;
125     uint32_t flag_ = 0u; // 0x01 mean last one
126     mutable RelationalRowDataSet rowDataSet_;
127     int32_t secLabel_ = 0;
128     int32_t secFlag_ = 0;
129 };
130 }
131 #endif