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 #ifndef RELATIONAL_SCHEMA_OBJECT_H
16 #define RELATIONAL_SCHEMA_OBJECT_H
17 #ifdef RELATIONAL_STORE
18 #include <map>
19 #include "data_value.h"
20 #include "ischema.h"
21 #include "json_object.h"
22 #include "parcel.h"
23 #include "schema_constant.h"
24 #include "table_info.h"
25 
26 namespace DistributedDB {
27 using TableInfoMap = std::map<std::string, TableInfo, CaseInsensitiveComparator>;
28 class RelationalSchemaObject : public ISchema {
29 public:
30     RelationalSchemaObject() = default;
31     ~RelationalSchemaObject() override = default;
32 
33     bool IsSchemaValid() const override;
34 
35     SchemaType GetSchemaType() const override;
36 
37     std::string ToSchemaString() const override;
38 
39     // Should be called on an invalid SchemaObject, create new SchemaObject if need to reparse
40     int ParseFromSchemaString(const std::string &inSchemaString) override;
41 
42     void AddRelationalTable(const TableInfo &table);
43 
44     void RemoveRelationalTable(const std::string &tableName);
45 
46     const TableInfoMap &GetTables() const;
47 
48     std::vector<std::string> GetTableNames() const;
49 
50     TableInfo GetTable(const std::string& tableName) const;
51 
52     std::string GetSchemaVersion() const;
53 
54     DistributedTableMode GetTableMode() const;
55     void SetTableMode(DistributedTableMode mode);
56 
57     void InsertTrackerSchema(const TrackerSchema &schema);
58     void RemoveTrackerSchema(const TrackerSchema &schema);
59     TrackerTable GetTrackerTable(const std::string &tableName) const;
60     int ParseFromTrackerSchemaString(const std::string &inSchemaString);
61     const TableInfoMap &GetTrackerTables() const;
62 
63     void SetReferenceProperty(const std::vector<TableReferenceProperty> &referenceProperty);
64     const std::vector<TableReferenceProperty> &GetReferenceProperty() const;
65     std::set<std::string> GetSharedTableForChangeTable(std::set<std::string> &changeTables) const;
66     std::set<std::string> CompareReferenceProperty(const std::vector<TableReferenceProperty> &others) const;
67     std::map<std::string, std::map<std::string, bool>> GetReachableRef();
68     std::map<std::string, int> GetTableWeight();
69 private:
70     int CompareAgainstSchemaObject(const std::string &inSchemaString, std::map<std::string, int> &cmpRst) const;
71 
72     int CompareAgainstSchemaObject(const RelationalSchemaObject &inSchemaObject,
73         std::map<std::string, int> &cmpRst) const;
74 
75     int ParseRelationalSchema(const JsonObject &inJsonObject);
76     int ParseCheckSchemaType(const JsonObject &inJsonObject);
77     int ParseCheckTableMode(const JsonObject &inJsonObject);
78     int ParseCheckSchemaVersion(const JsonObject &inJsonObject);
79     int ParseCheckSchemaTableDefine(const JsonObject &inJsonObject);
80     int ParseCheckTableInfo(const JsonObject &inJsonObject);
81     int ParseCheckTableName(const JsonObject &inJsonObject, TableInfo &resultTable);
82     int ParseCheckOriginTableName(const JsonObject &inJsonObject, TableInfo &resultTable);
83     int ParseCheckTableDefine(const JsonObject &inJsonObject, TableInfo &resultTable);
84     int ParseCheckTableFieldInfo(const JsonObject &inJsonObject, const FieldPath &path, FieldInfo &table);
85     int ParseCheckTableAutoInc(const JsonObject &inJsonObject, TableInfo &resultTable);
86     int ParseCheckSharedTableMark(const JsonObject &inJsonObject, TableInfo &resultTable);
87     int ParseCheckTableSyncType(const JsonObject &inJsonObject, TableInfo &resultTable);
88     int ParseCheckTableIndex(const JsonObject &inJsonObject, TableInfo &resultTable);
89     int ParseCheckTableUnique(const JsonObject &inJsonObject, TableInfo &resultTable);
90     int ParseCheckTablePrimaryKey(const JsonObject &inJsonObject, TableInfo &resultTable);
91     int ParseCheckReferenceProperty(const JsonObject &inJsonObject); // parse all reference
92     int ParseCheckReference(const JsonObject &inJsonObject); // parse one reference
93     // parse reference columns
94     int ParseCheckReferenceColumns(const JsonObject &inJsonObject, TableReferenceProperty &tableReferenceProperty);
95     // parse one reference column pair
96     int ParseCheckReferenceColumn(const JsonObject &inJsonObject, TableReferenceProperty &tableReferenceProperty);
97 
98     void GenerateSchemaString();
99     void GenerateTrackerSchemaString();
100     std::string GetReferencePropertyString();
101     std::string GetOneReferenceString(const TableReferenceProperty &reference);
102     int ParseTrackerSchema(const JsonObject &inJsonObject);
103     int ParseCheckTrackerTable(const JsonObject &inJsonObject);
104     int ParseCheckTrackerTableName(const JsonObject &inJsonObject, TrackerTable &resultTable);
105     int ParseCheckTrackerExtendName(const JsonObject &inJsonObject, TrackerTable &resultTable);
106     int ParseCheckTrackerName(const JsonObject &inJsonObject, TrackerTable &resultTable);
107     void GenerateReachableRef();
108     void GenerateTableInfoReferenced();
109     void RefreshReachableRef(const TableReferenceProperty &referenceProperty);
110     void CalculateTableWeight(const std::set<std::string> &startNodes,
111         const std::map<std::string, std::set<std::string>> &nextNodes);
112 
113     bool isValid_ = false; // set to true after parse success from string or add at least one relational table
114     SchemaType schemaType_ = SchemaType::RELATIVE; // Default RELATIVE
115     std::string schemaString_; // The minified and valid schemaString
116     std::string schemaVersion_ = SchemaConstant::SCHEMA_SUPPORT_VERSION_V2; // Default version 2.0
117     TableInfoMap tables_;
118     TableInfoMap trackerTables_;
119     std::vector<TableReferenceProperty> referenceProperty_;
120     std::map<std::string, std::map<std::string, bool>> reachableReference_;
121     std::map<std::string, int> tableWeight_;
122 
123     DistributedTableMode tableMode_ = DistributedTableMode::SPLIT_BY_DEVICE;
124 };
125 } // namespace DistributedDB
126 #endif // RELATIONAL_STORE
127 #endif // RELATIONAL_SCHEMA_OBJECT_H