• 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 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, const TableInfo &tableInfo, bool isFirstCreate);
47     void CacheTrackerSchema(const TrackerSchema &schema);
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     int UpdateExtendField(const TrackerSchema &schema);
64 
65     std::pair<int, bool> SetDistributedSchema(const DistributedSchema &schema, bool isForceUpgrade);
66 protected:
67     StorageExecutor *NewSQLiteStorageExecutor(sqlite3 *dbHandle, bool isWrite, bool isMemDb) override;
68     int Upgrade(sqlite3 *db) override;
69     int CreateNewExecutor(bool isWrite, StorageExecutor *&handle) override;
70 private:
71     // For executor.
72     void ReleaseExecutor(SQLiteSingleVerRelationalStorageExecutor *&handle, bool isExternal = false);
73 
74     // For db.
75     int RegisterFunction(sqlite3 *db) const;
76 
77     int UpgradeDistributedTable(const std::string &tableName, bool &schemaChanged, TableSyncType syncType);
78 
79     int CreateDistributedTable(SQLiteSingleVerRelationalStorageExecutor *&handle, bool isUpgraded,
80         const std::string &identity, TableInfo &table, RelationalSchemaObject &schema);
81 
82     int CreateDistributedTable(const std::string &tableName, bool isUpgraded, const std::string &identity,
83         RelationalSchemaObject &schema, TableSyncType tableSyncType);
84 
85     int CreateDistributedSharedTable(SQLiteSingleVerRelationalStorageExecutor *&handle, const std::string &tableName,
86         const std::string &sharedTableName, TableSyncType syncType, RelationalSchemaObject &schema);
87 
88     int CreateRelationalMetaTable(sqlite3 *db);
89 
90     int CleanTrackerDeviceTable(const std::vector<std::string> &tableNames, RelationalSchemaObject &trackerSchemaObj,
91         SQLiteSingleVerRelationalStorageExecutor *&handle);
92 
93     int GenLogInfoForUpgrade(const std::string &tableName, RelationalSchemaObject &schema, bool schemaChanged);
94 
95     static std::map<std::string, std::map<std::string, bool>> GetReachableWithShared(
96         const std::map<std::string, std::map<std::string, bool>> &reachableReference,
97         const std::map<std::string, std::string> &tableToShared);
98 
99     static std::map<std::string, int> GetTableWeightWithShared(const std::map<std::string, int> &tableWeight,
100         const std::map<std::string, std::string> &tableToShared);
101 
102     int UpgradeSharedTableInner(SQLiteSingleVerRelationalStorageExecutor *&handle,
103         const DataBaseSchema &cloudSchema, const std::vector<std::string> &deleteTableNames,
104         const std::map<std::string, std::vector<Field>> &updateTableNames,
105         const std::map<std::string, std::string> &alterTableNames);
106 
107     int DoDeleteSharedTable(SQLiteSingleVerRelationalStorageExecutor *&handle,
108         const std::vector<std::string> &deleteTableNames, RelationalSchemaObject &schema);
109 
110     int DoUpdateSharedTable(SQLiteSingleVerRelationalStorageExecutor *&handle,
111         const std::map<std::string, std::vector<Field>> &updateTableNames, const DataBaseSchema &cloudSchema,
112         RelationalSchemaObject &localSchema);
113 
114     int DoAlterSharedTableName(SQLiteSingleVerRelationalStorageExecutor *&handle,
115         const std::map<std::string, std::string> &alterTableNames, RelationalSchemaObject &schema);
116 
117     int DoCreateSharedTable(SQLiteSingleVerRelationalStorageExecutor *&handle,
118         const DataBaseSchema &cloudSchema, const std::map<std::string, std::vector<Field>> &updateTableNames,
119         const std::map<std::string, std::string> &alterTableNames, RelationalSchemaObject &schema);
120 
121     int UpdateKvData(SQLiteSingleVerRelationalStorageExecutor *&handle,
122         const std::map<std::string, std::string> &alterTableNames);
123 
124     int CheckIfExistUserTable(SQLiteSingleVerRelationalStorageExecutor *&handle, const DataBaseSchema &cloudSchema,
125         const std::map<std::string, std::string> &alterTableNames, const RelationalSchemaObject &schema);
126 
127     int SetDistributedSchemaInner(RelationalSchemaObject &schemaObj, const DistributedSchema &schema,
128         bool isForceUpgrade);
129 
130     RelationalSchemaObject schema_;
131     RelationalSchemaObject trackerSchema_;
132     mutable std::mutex schemaMutex_;
133 
134     RelationalDBProperties properties_;
135     std::mutex createDistributedTableMutex_;
136 };
137 } // namespace DistributedDB
138 #endif
139 #endif // SQLITE_RELATIONAL_ENGINE_H