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 #ifndef SCHEMA_NEGOTIATE_H
16 #define SCHEMA_NEGOTIATE_H
17 
18 #include "db_types.h"
19 #include "relational_schema_object.h"
20 #include "schema_object.h"
21 
22 namespace DistributedDB {
23 struct SyncOpinion {
24     bool permitSync = false;
25     bool requirePeerConvert = false;
26     bool checkOnReceive = false;
27 };
28 
29 struct SyncStrategy {
30     bool permitSync = false;
31     bool convertOnSend = false;
32     bool convertOnReceive = false;
33     bool checkOnReceive = false;
34 };
35 
36 using RelationalSyncOpinion = std::map<std::string, SyncOpinion, CaseInsensitiveComparator>;
37 using RelationalSyncStrategy = std::map<std::string, SyncStrategy, CaseInsensitiveComparator>;
38 
39 class SchemaNegotiate {
40 public:
41     // The remoteSchemaType may beyond local SchemaType definition
42     static SyncOpinion MakeLocalSyncOpinion(const SchemaObject &localSchema, const std::string &remoteSchema,
43         uint8_t remoteSchemaType);
44 
45     // The remoteOpinion.checkOnReceive is ignored
46     static SyncStrategy ConcludeSyncStrategy(const SyncOpinion &localOpinion, const SyncOpinion &remoteOpinion);
47 
48     static RelationalSyncOpinion MakeLocalSyncOpinion(const RelationalSchemaObject &localSchema,
49         const std::string &remoteSchema, uint8_t remoteSchemaType);
50 
51     // The remoteOpinion.checkOnReceive is ignored
52     static RelationalSyncStrategy ConcludeSyncStrategy(const RelationalSyncOpinion &localOpinion,
53         const RelationalSyncOpinion &remoteOpinion);
54 
55     static uint32_t CalculateParcelLen(const RelationalSyncOpinion &opinions);
56     static int SerializeData(const RelationalSyncOpinion &opinions, Parcel &parcel);
57     static int DeserializeData(Parcel &parcel, RelationalSyncOpinion &opinion);
58 private:
59     SchemaNegotiate() = default;
60     ~SchemaNegotiate() = default;
61 
62     static RelationalSyncOpinion MakeOpinionEachTable(const RelationalSchemaObject &localSchema,
63         const RelationalSchemaObject &remoteSchema);
64 };
65 }
66 
67 #endif // SCHEMA_NEGOTIATE_H