• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 #include "cloud/schema_mgr.h"
17 
18 #include <unordered_set>
19 
20 #include "cloud/cloud_db_constant.h"
21 #include "cloud/cloud_store_types.h"
22 #include "db_common.h"
23 #include "db_errno.h"
24 namespace DistributedDB {
SchemaMgr()25 SchemaMgr::SchemaMgr()
26 {
27 }
28 
ChkSchema(const TableName & tableName,RelationalSchemaObject & localSchema)29 int SchemaMgr::ChkSchema(const TableName &tableName, RelationalSchemaObject &localSchema)
30 {
31     if (cloudSchema_ == nullptr) {
32         LOGE("Cloud schema has not been set");
33         return -E_SCHEMA_MISMATCH;
34     }
35     TableInfo tableInfo = localSchema.GetTable(tableName);
36     if (tableInfo.Empty()) {
37         LOGE("Local schema does not contain certain table");
38         return -E_SCHEMA_MISMATCH;
39     }
40     if (tableInfo.GetTableSyncType() != TableSyncType::CLOUD_COOPERATION) {
41         LOGE("Sync type of local table is not CLOUD_COOPERATION");
42         return -E_NOT_SUPPORT;
43     }
44     TableSchema cloudTableSchema;
45     int ret = GetCloudTableSchema(tableName, cloudTableSchema);
46     if (ret != E_OK) {
47         LOGE("Cloud schema does not contain certain table:%d", ret);
48         return -E_SCHEMA_MISMATCH;
49     }
50     std::map<int, FieldName> primaryKeys = tableInfo.GetPrimaryKey();
51     FieldInfoMap localFields = tableInfo.GetFields();
52     return CompareFieldSchema(primaryKeys, localFields, cloudTableSchema.fields);
53 }
54 
CompareFieldSchema(std::map<int,FieldName> & primaryKeys,FieldInfoMap & localFields,std::vector<Field> & cloudFields)55 int SchemaMgr::CompareFieldSchema(std::map<int, FieldName> &primaryKeys, FieldInfoMap &localFields,
56     std::vector<Field> &cloudFields)
57 {
58     std::unordered_set<std::string> cloudColNames;
59     for (const Field &cloudField : cloudFields) {
60         if (localFields.find(cloudField.colName) == localFields.end()) {
61             LOGE("Column name mismatch between local and cloud schema");
62             return -E_SCHEMA_MISMATCH;
63         }
64         if (IsAssetPrimaryField(cloudField)) {
65             LOGE("Asset type can not be primary field");
66             return -E_SCHEMA_MISMATCH;
67         }
68         FieldInfo &localField = localFields[cloudField.colName];
69         if (!CompareType(localField, cloudField)) {
70             LOGE("Type mismatch between local and cloud schema");
71             return -E_SCHEMA_MISMATCH;
72         }
73         if (!CompareNullable(localField, cloudField)) {
74             LOGE("The nullable property is mismatched between local and cloud schema");
75             return -E_SCHEMA_MISMATCH;
76         }
77         if (!ComparePrimaryField(primaryKeys, cloudField)) {
78             LOGE("The primary key property is mismatched between local and cloud schema");
79             return -E_SCHEMA_MISMATCH;
80         }
81         cloudColNames.emplace(cloudField.colName);
82     }
83     if (primaryKeys.size() > 0 && !(primaryKeys.size() == 1 && primaryKeys[0] == CloudDbConstant::ROW_ID_FIELD_NAME)) {
84         LOGE("Local schema contain extra primary key:%d", -E_SCHEMA_MISMATCH);
85         return -E_SCHEMA_MISMATCH;
86     }
87     for (const auto &[fieldName, fieldInfo] : localFields) {
88         if (!fieldInfo.HasDefaultValue() &&
89             fieldInfo.IsNotNull() &&
90             cloudColNames.find(fieldName) == cloudColNames.end()) {
91             LOGE("Column from local schema is not within cloud schema but doesn't have default value");
92             return -E_SCHEMA_MISMATCH;
93         }
94     }
95     return E_OK;
96 }
97 
IsAssetPrimaryField(const Field & cloudField)98 bool SchemaMgr::IsAssetPrimaryField(const Field &cloudField)
99 {
100     return cloudField.primary && (cloudField.type == TYPE_INDEX<Assets> || cloudField.type == TYPE_INDEX<Asset>);
101 }
102 
CompareType(const FieldInfo & localField,const Field & cloudField)103 bool SchemaMgr::CompareType(const FieldInfo &localField, const Field &cloudField)
104 {
105     StorageType localType = localField.GetStorageType();
106     switch (cloudField.type) {
107         case TYPE_INDEX<std::monostate>:
108         case TYPE_INDEX<bool>:
109             // BOOL type should be stored as NUMERIC type,
110             // but we regard it as NULL type for historic reason
111             return localType == StorageType::STORAGE_TYPE_NULL;
112         case TYPE_INDEX<int64_t>:
113             return localType == StorageType::STORAGE_TYPE_INTEGER;
114         case TYPE_INDEX<double>:
115             return localType == StorageType::STORAGE_TYPE_REAL;
116         case TYPE_INDEX<std::string>:
117             return localType == StorageType::STORAGE_TYPE_TEXT;
118         case TYPE_INDEX<Bytes>:
119         case TYPE_INDEX<Asset>:
120         case TYPE_INDEX<Assets>:
121             return localType == StorageType::STORAGE_TYPE_BLOB;
122         default:
123             return false;
124     }
125     return false;
126 }
127 
CompareNullable(const FieldInfo & localField,const Field & cloudField)128 bool SchemaMgr::CompareNullable(const FieldInfo &localField, const Field &cloudField)
129 {
130     return localField.IsNotNull() == !cloudField.nullable;
131 }
132 
ComparePrimaryField(std::map<int,FieldName> & localPrimaryKeys,const Field & cloudField)133 bool SchemaMgr::ComparePrimaryField(std::map<int, FieldName> &localPrimaryKeys, const Field &cloudField)
134 {
135     // whether the corresponding field in local schema is primary key
136     bool isLocalFieldPrimary = false;
137     for (const auto &kvPair : localPrimaryKeys) {
138         if (kvPair.second == cloudField.colName) {
139             isLocalFieldPrimary = true;
140             localPrimaryKeys.erase(kvPair.first);
141             break;
142         }
143     }
144     return isLocalFieldPrimary == cloudField.primary;
145 }
146 
SetCloudDbSchema(const DataBaseSchema & schema)147 void SchemaMgr::SetCloudDbSchema(const DataBaseSchema &schema)
148 {
149     cloudSchema_ = std::make_shared<DataBaseSchema>(schema);
150 }
151 
GetCloudDbSchema()152 std::shared_ptr<DataBaseSchema> SchemaMgr::GetCloudDbSchema()
153 {
154     return cloudSchema_;
155 }
156 
GetCloudTableSchema(const TableName & tableName,TableSchema & retSchema)157 int SchemaMgr::GetCloudTableSchema(const TableName &tableName, TableSchema &retSchema)
158 {
159     if (cloudSchema_ == nullptr) {
160         return -E_SCHEMA_MISMATCH;
161     }
162     for (const TableSchema &tableSchema : cloudSchema_->tables) {
163         if (tableSchema.name == tableName) {
164             retSchema = tableSchema;
165             return E_OK;
166         }
167     }
168     return -E_NOT_FOUND;
169 }
170 
171 } // namespace DistributedDB