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 SQLITE_RELATIONAL_ENGINE_H 16 #define SQLITE_RELATIONAL_ENGINE_H 17 #ifdef RELATIONAL_STORE 18 19 #include "macro_utils.h" 20 #include "relationaldb_properties.h" 21 #include "sqlite_storage_engine.h" 22 #include "sqlite_single_ver_relational_storage_executor.h" 23 #include "tracker_table.h" 24 25 namespace DistributedDB { 26 class SQLiteSingleRelationalStorageEngine : public SQLiteStorageEngine { 27 public: 28 explicit SQLiteSingleRelationalStorageEngine(RelationalDBProperties properties); 29 ~SQLiteSingleRelationalStorageEngine() override; 30 31 // Delete the copy and assign constructors 32 DISABLE_COPY_ASSIGN_MOVE(SQLiteSingleRelationalStorageEngine); 33 34 void SetSchema(const RelationalSchemaObject &schema); 35 36 RelationalSchemaObject GetSchema() const; 37 38 int CreateDistributedTable(const std::string &tableName, const std::string &identity, bool &schemaChanged, 39 TableSyncType syncType, bool trackerSchemaChanged); 40 41 int CleanDistributedDeviceTable(std::vector<std::string> &missingTables); 42 43 const RelationalDBProperties &GetProperties() const; 44 const RelationalDBProperties GetRelationalProperties() const; 45 void SetProperties(const RelationalDBProperties &properties); 46 47 int SetTrackerTable(const TrackerSchema &schema, const TableInfo &tableInfo, bool isFirstCreate); 48 void CacheTrackerSchema(const TrackerSchema &schema); 49 int GetOrInitTrackerSchemaFromMeta(); 50 int SaveTrackerSchema(const std::string &tableName, bool isFirstCreate); 51 52 int ExecuteSql(const SqlCondition &condition, std::vector<VBucket> &records); 53 RelationalSchemaObject GetTrackerSchema() const; 54 void SetTrackerSchema(const RelationalSchemaObject &trackerSchema); 55 int CleanTrackerData(const std::string &tableName, int64_t cursor); 56 57 int SetReference(const std::vector<TableReferenceProperty> &tableReferenceProperty, 58 SQLiteSingleVerRelationalStorageExecutor *handle, std::set<std::string> &clearWaterMarkTables, 59 RelationalSchemaObject &schema); 60 int UpgradeSharedTable(const DataBaseSchema &cloudSchema, const std::vector<std::string> &deleteTableNames, 61 const std::map<std::string, std::vector<Field>> &updateTableNames, 62 const std::map<std::string, std::string> &alterTableNames); 63 std::pair<std::vector<std::string>, int> CalTableRef(const std::vector<std::string> &tableNames, 64 const std::map<std::string, std::string> &sharedTableOriginNames); 65 int UpdateExtendField(const TrackerSchema &schema); 66 67 std::pair<int, bool> SetDistributedSchema(const DistributedSchema &schema, const std::string &localIdentity, 68 bool isForceUpgrade); 69 protected: 70 StorageExecutor *NewSQLiteStorageExecutor(sqlite3 *dbHandle, bool isWrite, bool isMemDb) override; 71 int Upgrade(sqlite3 *db) override; 72 int CreateNewExecutor(bool isWrite, StorageExecutor *&handle) override; 73 private: 74 // For executor. 75 void ReleaseExecutor(SQLiteSingleVerRelationalStorageExecutor *&handle, bool isExternal = false); 76 77 // For db. 78 int RegisterFunction(sqlite3 *db) const; 79 80 int UpgradeDistributedTable(const std::string &tableName, bool &schemaChanged, TableSyncType syncType); 81 82 int CreateDistributedTable(SQLiteSingleVerRelationalStorageExecutor *&handle, bool isUpgraded, 83 const std::string &identity, TableInfo &table, RelationalSchemaObject &schema); 84 85 int CreateDistributedTable(const std::string &tableName, bool isUpgraded, const std::string &identity, 86 RelationalSchemaObject &schema, TableSyncType tableSyncType); 87 88 int CreateDistributedSharedTable(SQLiteSingleVerRelationalStorageExecutor *&handle, const std::string &tableName, 89 const std::string &sharedTableName, TableSyncType syncType, RelationalSchemaObject &schema); 90 91 int CleanTrackerDeviceTable(const std::vector<std::string> &tableNames, RelationalSchemaObject &trackerSchemaObj, 92 SQLiteSingleVerRelationalStorageExecutor *&handle); 93 94 int GenLogInfoForUpgrade(const std::string &tableName, RelationalSchemaObject &schema, bool schemaChanged); 95 96 static std::map<std::string, std::map<std::string, bool>> GetReachableWithShared( 97 const std::map<std::string, std::map<std::string, bool>> &reachableReference, 98 const std::map<std::string, std::string> &tableToShared); 99 100 static std::map<std::string, int> GetTableWeightWithShared(const std::map<std::string, int> &tableWeight, 101 const std::map<std::string, std::string> &tableToShared); 102 103 int UpgradeSharedTableInner(SQLiteSingleVerRelationalStorageExecutor *&handle, 104 const DataBaseSchema &cloudSchema, const std::vector<std::string> &deleteTableNames, 105 const std::map<std::string, std::vector<Field>> &updateTableNames, 106 const std::map<std::string, std::string> &alterTableNames); 107 108 int DoDeleteSharedTable(SQLiteSingleVerRelationalStorageExecutor *&handle, 109 const std::vector<std::string> &deleteTableNames, RelationalSchemaObject &schema); 110 111 int DoUpdateSharedTable(SQLiteSingleVerRelationalStorageExecutor *&handle, 112 const std::map<std::string, std::vector<Field>> &updateTableNames, const DataBaseSchema &cloudSchema, 113 RelationalSchemaObject &localSchema); 114 115 int DoAlterSharedTableName(SQLiteSingleVerRelationalStorageExecutor *&handle, 116 const std::map<std::string, std::string> &alterTableNames, RelationalSchemaObject &schema); 117 118 int DoCreateSharedTable(SQLiteSingleVerRelationalStorageExecutor *&handle, 119 const DataBaseSchema &cloudSchema, const std::map<std::string, std::vector<Field>> &updateTableNames, 120 const std::map<std::string, std::string> &alterTableNames, RelationalSchemaObject &schema); 121 122 int UpdateKvData(SQLiteSingleVerRelationalStorageExecutor *&handle, 123 const std::map<std::string, std::string> &alterTableNames); 124 125 int CheckIfExistUserTable(SQLiteSingleVerRelationalStorageExecutor *&handle, const DataBaseSchema &cloudSchema, 126 const std::map<std::string, std::string> &alterTableNames, const RelationalSchemaObject &schema); 127 128 int SetDistributedSchemaInner(RelationalSchemaObject &schemaObj, const DistributedSchema &schema, 129 const std::string &localIdentity, bool isForceUpgrade); 130 131 int SetDistributedSchemaInTraction(RelationalSchemaObject &schemaObj, const DistributedSchema &schema, 132 const std::string &localIdentity, bool isForceUpgrade, SQLiteSingleVerRelationalStorageExecutor &handle); 133 134 RelationalSchemaObject schema_; 135 RelationalSchemaObject trackerSchema_; 136 mutable std::mutex schemaMutex_; 137 mutable std::mutex trackerSchemaMutex_; 138 139 RelationalDBProperties properties_; 140 std::mutex createDistributedTableMutex_; 141 mutable std::mutex propertiesMutex_; 142 }; 143 } // namespace DistributedDB 144 #endif 145 #endif // SQLITE_RELATIONAL_ENGINE_H