• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
70     bool CheckDistributedSchemaChange(const DistributedSchema &schema);
71     void SetDistributedSchema(const DistributedSchema &schema);
72     DistributedSchema GetDistributedSchema() const;
73 
74     bool IsNeedSkipSyncField(const FieldInfo &fieldInfo, const std::string &tableName,
75         bool ignoreTableNonExist = true) const;
76 
77     std::vector<FieldInfo> GetSyncFieldInfo(const std::string &tableName, bool ignoreTableNonExist = true) const;
78 
79     DistributedTable GetDistributedTable(const std::string &table) const;
80 private:
81     int CompareAgainstSchemaObject(const std::string &inSchemaString, std::map<std::string, int> &cmpRst) const;
82 
83     int CompareAgainstSchemaObject(const RelationalSchemaObject &inSchemaObject,
84         std::map<std::string, int> &cmpRst) const;
85 
86     int ParseRelationalSchema(const JsonObject &inJsonObject);
87     int ParseCheckSchemaType(const JsonObject &inJsonObject);
88     int ParseCheckTableMode(const JsonObject &inJsonObject);
89     int ParseCheckSchemaVersion(const JsonObject &inJsonObject);
90     int ParseCheckSchemaTableDefine(const JsonObject &inJsonObject);
91     int ParseCheckTableInfo(const JsonObject &inJsonObject);
92     int ParseCheckTableName(const JsonObject &inJsonObject, TableInfo &resultTable);
93     int ParseCheckOriginTableName(const JsonObject &inJsonObject, TableInfo &resultTable);
94     int ParseCheckTableDefine(const JsonObject &inJsonObject, TableInfo &resultTable);
95     int ParseCheckTableFieldInfo(const JsonObject &inJsonObject, const FieldPath &path, FieldInfo &table);
96     int ParseCheckTableAutoInc(const JsonObject &inJsonObject, TableInfo &resultTable);
97     int ParseCheckSharedTableMark(const JsonObject &inJsonObject, TableInfo &resultTable);
98     int ParseCheckTableSyncType(const JsonObject &inJsonObject, TableInfo &resultTable);
99     int ParseCheckTableIndex(const JsonObject &inJsonObject, TableInfo &resultTable);
100     int ParseCheckTableUnique(const JsonObject &inJsonObject, TableInfo &resultTable);
101     int ParseCheckTablePrimaryKey(const JsonObject &inJsonObject, TableInfo &resultTable);
102     int ParseCheckReferenceProperty(const JsonObject &inJsonObject); // parse all reference
103     int ParseCheckReference(const JsonObject &inJsonObject); // parse one reference
104     // parse reference columns
105     int ParseCheckReferenceColumns(const JsonObject &inJsonObject, TableReferenceProperty &tableReferenceProperty);
106     // parse one reference column pair
107     int ParseCheckReferenceColumn(const JsonObject &inJsonObject, TableReferenceProperty &tableReferenceProperty);
108     int ParseDistributedVersion(const JsonObject &inJsonObject); // parse distributed version if need
109     int ParseDistributedSchema(const JsonObject &inJsonObject); // parse distributed schema if need
110     int ParseDistributedTables(const JsonObject &inJsonObject); // parse distributed tables if need
111     int ParseDistributedTable(const JsonObject &inJsonObject); // parse distributed table if need
112 
113     void GenerateSchemaString();
114     void GenerateTrackerSchemaString();
115     std::string GetReferencePropertyString();
116     std::string GetOneReferenceString(const TableReferenceProperty &reference);
117     int ParseTrackerSchema(const JsonObject &inJsonObject);
118     int ParseCheckTrackerTable(const JsonObject &inJsonObject);
119     int ParseCheckTrackerTableName(const JsonObject &inJsonObject, TrackerTable &resultTable);
120     int ParseCheckTrackerExtendName(const JsonObject &inJsonObject, TrackerTable &resultTable);
121     int ParseCheckTrackerName(const JsonObject &inJsonObject, TrackerTable &resultTable);
122     int ParseCheckTrackerAction(const JsonObject &inJsonObject, TrackerTable &resultTable);
123     void GenerateReachableRef();
124     void GenerateTableInfoReferenced();
125     void RefreshReachableRef(const TableReferenceProperty &referenceProperty);
126     void CalculateTableWeight(const std::set<std::string> &startNodes,
127         const std::map<std::string, std::set<std::string>> &nextNodes);
128     std::string GetDistributedSchemaString();
129 
130     static bool CheckDistributedFieldChange(const std::vector<DistributedField> &source,
131         const std::vector<DistributedField> &target);
132     static std::string GetOneDistributedTableString(const DistributedTable &table);
133     static int ParseDistributedFields(const JsonObject &inJsonObject, std::vector<DistributedField> &fields);
134     static int ParseDistributedField(const JsonObject &inJsonObject, DistributedField &field);
135 
136     bool isValid_ = false; // set to true after parse success from string or add at least one relational table
137     SchemaType schemaType_ = SchemaType::RELATIVE; // Default RELATIVE
138     std::string schemaString_; // The minified and valid schemaString
139     std::string schemaVersion_ = SchemaConstant::SCHEMA_SUPPORT_VERSION_V2; // Default version 2.0
140     TableInfoMap tables_;
141     TableInfoMap trackerTables_;
142     std::vector<TableReferenceProperty> referenceProperty_;
143     std::map<std::string, std::map<std::string, bool>> reachableReference_;
144     std::map<std::string, int> tableWeight_;
145     DistributedSchema dbSchema_;
146 
147     DistributedTableMode tableMode_ = DistributedTableMode::SPLIT_BY_DEVICE;
148 };
149 } // namespace DistributedDB
150 #endif // RELATIONAL_STORE
151 #endif // RELATIONAL_SCHEMA_OBJECT_H