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     void SetProperties(const RelationalDBProperties &properties);
45 
46     int SetTrackerTable(const TrackerSchema &schema);
47     int CheckAndCacheTrackerSchema(const TrackerSchema &schema, TableInfo &tableInfo, bool &isFirstCreate);
48     int GetOrInitTrackerSchemaFromMeta();
49     int SaveTrackerSchema(const std::string &tableName, bool isFirstCreate);
50 
51     int ExecuteSql(const SqlCondition &condition, std::vector<VBucket> &records);
52     RelationalSchemaObject GetTrackerSchema() const;
53     int CleanTrackerData(const std::string &tableName, int64_t cursor);
54 
55     int SetReference(const std::vector<TableReferenceProperty> &tableReferenceProperty,
56         SQLiteSingleVerRelationalStorageExecutor *handle, std::set<std::string> &clearWaterMarkTables,
57         RelationalSchemaObject &schema);
58     int UpgradeSharedTable(const DataBaseSchema &cloudSchema, const std::vector<std::string> &deleteTableNames,
59         const std::map<std::string, std::vector<Field>> &updateTableNames,
60         const std::map<std::string, std::string> &alterTableNames);
61     std::pair<std::vector<std::string>, int> CalTableRef(const std::vector<std::string> &tableNames,
62         const std::map<std::string, std::string> &sharedTableOriginNames);
63 protected:
64     StorageExecutor *NewSQLiteStorageExecutor(sqlite3 *dbHandle, bool isWrite, bool isMemDb) override;
65     int Upgrade(sqlite3 *db) override;
66     int CreateNewExecutor(bool isWrite, StorageExecutor *&handle) override;
67 private:
68     // For executor.
69     void ReleaseExecutor(SQLiteSingleVerRelationalStorageExecutor *&handle, bool isExternal = false);
70 
71     // For db.
72     int RegisterFunction(sqlite3 *db) const;
73 
74     int UpgradeDistributedTable(const std::string &tableName, bool &schemaChanged, TableSyncType syncType);
75 
76     int CreateDistributedTable(SQLiteSingleVerRelationalStorageExecutor *&handle, bool isUpgraded,
77         const std::string &identity, TableInfo &table, RelationalSchemaObject &schema);
78 
79     int CreateDistributedTable(const std::string &tableName, bool isUpgraded, const std::string &identity,
80         RelationalSchemaObject &schema, TableSyncType tableSyncType);
81 
82     int CreateDistributedSharedTable(SQLiteSingleVerRelationalStorageExecutor *&handle, const std::string &tableName,
83         const std::string &sharedTableName, TableSyncType syncType, RelationalSchemaObject &schema);
84 
85     int CreateRelationalMetaTable(sqlite3 *db);
86 
87     int CleanTrackerDeviceTable(const std::vector<std::string> &tableNames, RelationalSchemaObject &trackerSchemaObj,
88         SQLiteSingleVerRelationalStorageExecutor *&handle);
89 
90     int GenLogInfoForUpgrade(const std::string &tableName, RelationalSchemaObject &schema, bool schemaChanged);
91 
92     static std::map<std::string, std::map<std::string, bool>> GetReachableWithShared(
93         const std::map<std::string, std::map<std::string, bool>> &reachableReference,
94         const std::map<std::string, std::string> &tableToShared);
95 
96     static std::map<std::string, int> GetTableWeightWithShared(const std::map<std::string, int> &tableWeight,
97         const std::map<std::string, std::string> &tableToShared);
98 
99     int UpgradeSharedTableInner(SQLiteSingleVerRelationalStorageExecutor *&handle,
100         const DataBaseSchema &cloudSchema, const std::vector<std::string> &deleteTableNames,
101         const std::map<std::string, std::vector<Field>> &updateTableNames,
102         const std::map<std::string, std::string> &alterTableNames);
103 
104     int DoDeleteSharedTable(SQLiteSingleVerRelationalStorageExecutor *&handle,
105         const std::vector<std::string> &deleteTableNames, RelationalSchemaObject &schema);
106 
107     int DoUpdateSharedTable(SQLiteSingleVerRelationalStorageExecutor *&handle,
108         const std::map<std::string, std::vector<Field>> &updateTableNames, const DataBaseSchema &cloudSchema,
109         RelationalSchemaObject &localSchema);
110 
111     int DoAlterSharedTableName(SQLiteSingleVerRelationalStorageExecutor *&handle,
112         const std::map<std::string, std::string> &alterTableNames, RelationalSchemaObject &schema);
113 
114     int DoCreateSharedTable(SQLiteSingleVerRelationalStorageExecutor *&handle,
115         const DataBaseSchema &cloudSchema, const std::map<std::string, std::vector<Field>> &updateTableNames,
116         const std::map<std::string, std::string> &alterTableNames, RelationalSchemaObject &schema);
117 
118     int UpdateKvData(SQLiteSingleVerRelationalStorageExecutor *&handle,
119         const std::map<std::string, std::string> &alterTableNames);
120 
121     int CheckIfExistUserTable(SQLiteSingleVerRelationalStorageExecutor *&handle, const DataBaseSchema &cloudSchema,
122         const std::map<std::string, std::string> &alterTableNames, const RelationalSchemaObject &schema);
123 
124     RelationalSchemaObject schema_;
125     RelationalSchemaObject trackerSchema_;
126     mutable std::mutex schemaMutex_;
127 
128     RelationalDBProperties properties_;
129     std::mutex createDistributedTableMutex_;
130 };
131 } // namespace DistributedDB
132 #endif
133 #endif // SQLITE_RELATIONAL_ENGINE_H