• 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_SINGLE_VER_RELATIONAL_STORAGE_EXECUTOR_H
16 #define SQLITE_SINGLE_VER_RELATIONAL_STORAGE_EXECUTOR_H
17 #ifdef RELATIONAL_STORE
18 
19 #include "cloud/cloud_db_constant.h"
20 #include "cloud/cloud_store_types.h"
21 #include "data_transformer.h"
22 #include "db_types.h"
23 #include "icloud_sync_storage_interface.h"
24 #include "macro_utils.h"
25 #include "query_object.h"
26 #include "relational_row_data.h"
27 #include "relational_store_delegate.h"
28 #include "relational_sync_data_inserter.h"
29 #include "sqlite_single_ver_relational_continue_token.h"
30 #include "sqlite_storage_executor.h"
31 #include "sqlite_utils.h"
32 
33 namespace DistributedDB {
34 class SQLiteSingleVerRelationalStorageExecutor : public SQLiteStorageExecutor {
35 public:
36     SQLiteSingleVerRelationalStorageExecutor(sqlite3 *dbHandle, bool writable, DistributedTableMode mode);
37     ~SQLiteSingleVerRelationalStorageExecutor() override = default;
38 
39     // Delete the copy and assign constructors
40     DISABLE_COPY_ASSIGN_MOVE(SQLiteSingleVerRelationalStorageExecutor);
41 
42     // The parameter "identity" is a hash string that identifies a device
43     int CreateDistributedTable(DistributedTableMode mode, bool isUpgraded, const std::string &identity,
44         TableInfo &table, TableSyncType syncType);
45 
46     int UpgradeDistributedTable(const std::string &tableName, DistributedTableMode mode, bool &schemaChanged,
47         RelationalSchemaObject &schema, TableSyncType syncType);
48 
49     int StartTransaction(TransactType type);
50     int Commit();
51     int Rollback();
52 
53     // For Get sync data
54     int GetSyncDataByQuery(std::vector<DataItem> &dataItems, size_t appendLength, const DataSizeSpecInfo &sizeInfo,
55         std::function<int(sqlite3 *, sqlite3_stmt *&, sqlite3_stmt *&, bool &)> getStmt, const TableInfo &tableInfo);
56 
57     // operation of meta data
58     int GetKvData(const Key &key, Value &value) const;
59     int PutKvData(const Key &key, const Value &value) const;
60     int DeleteMetaData(const std::vector<Key> &keys) const;
61     int DeleteMetaDataByPrefixKey(const Key &keyPrefix) const;
62     int GetAllMetaKeys(std::vector<Key> &keys) const;
63 
64     // For Put sync data
65     int SaveSyncItems(RelationalSyncDataInserter &inserter, bool useTrans = true);
66 
67     int CheckDBModeForRelational();
68 
69     int DeleteDistributedDeviceTable(const std::string &device, const std::string &tableName);
70 
71     int DeleteDistributedAllDeviceTableLog(const std::string &tableName);
72 
73     int DeleteDistributedDeviceTableLog(const std::string &device, const std::string &tableName);
74 
75     int DeleteDistributedLogTable(const std::string &tableName);
76 
77     int CheckAndCleanDistributedTable(const std::vector<std::string> &tableNames,
78         std::vector<std::string> &missingTables);
79 
80     int CreateDistributedDeviceTable(const std::string &device, const TableInfo &baseTbl, const StoreInfo &info);
81 
82     int CheckQueryObjectLegal(const TableInfo &table, QueryObject &query, const std::string &schemaVersion);
83 
84     int GetMaxTimestamp(const std::vector<std::string> &tablesName, Timestamp &maxTimestamp) const;
85 
86     int ExecuteQueryBySqlStmt(const std::string &sql, const std::vector<std::string> &bindArgs, int packetSize,
87         std::vector<std::string> &colNames, std::vector<RelationalRowData *> &data);
88 
89     int SaveSyncDataItems(RelationalSyncDataInserter &inserter);
90 
91     int CheckEncryptedOrCorrupted() const;
92 
93     int GetExistsDeviceList(std::set<std::string> &devices) const;
94 
95     int GetUploadCount(const std::string &tableName, const Timestamp &timestamp, const bool isCloudForcePush,
96         int64_t &count);
97 
98     int UpdateCloudLogGid(const CloudSyncData &cloudDataResult);
99 
100     int GetSyncCloudData(CloudSyncData &cloudDataResult, const uint32_t &maxSize,
101         SQLiteSingleVerRelationalContinueToken &token);
102 
103     int GetInfoByPrimaryKeyOrGid(const TableSchema &tableSchema, const VBucket &vBucket,
104         DataInfoWithLog &dataInfoWithLog, VBucket &assetInfo);
105 
106     int PutCloudSyncData(const std::string &tableName, const TableSchema &tableSchema, DownloadData &downloadData);
107 
108     int FillCloudAssetForDownload(const TableSchema &tableSchema, VBucket &vBucket, bool isDownloadSuccess);
109     int DoCleanInner(ClearMode mode, const std::vector<std::string> &tableNameList,
110         const RelationalSchemaObject &localSchema, std::vector<Asset> &assets);
111 
112     int FillCloudAssetForUpload(const std::string &tableName, const CloudSyncBatch &data);
113 
114     int SetLogTriggerStatus(bool status);
115 
116 private:
117     int DoCleanLogs(const std::vector<std::string> &tableNameList);
118 
119     int DoCleanLogAndData(const std::vector<std::string> &tableNameList,
120         const RelationalSchemaObject &localSchema, std::vector<Asset> &assets);
121 
122     int CleanCloudDataOnLogTable(const std::string &logTableName);
123 
124     int CleanCloudDataAndLogOnUserTable(const std::string &tableName, const std::string &logTableName);
125 
126     int GetCleanCloudDataKeys(const std::string &logTableName, std::vector<int64_t> &dataKeys);
127 
128     int GetCloudAssets(const std::string &tableName, const std::vector<FieldInfo> &fieldInfos,
129         const std::vector<int64_t> &dataKeys, std::vector<Asset> &assets);
130 
131     int GetCloudAssetOnTable(const std::string &tableName, const std::string &fieldName,
132         const std::vector<int64_t> &dataKeys, std::vector<Asset> &assets);
133 
134     int GetCloudAssetsOnTable(const std::string &tableName, const std::string &fieldName,
135         const std::vector<int64_t> &dataKeys, std::vector<Asset> &assets);
136 
137     int GetDataItemForSync(sqlite3_stmt *statement, DataItem &dataItem, bool isGettingDeletedData) const;
138 
139     int GetSyncDataPre(const DataItem &dataItem, sqlite3_stmt *queryStmt, DataItem &itemGet);
140 
141     int CheckDataConflictDefeated(const DataItem &item, sqlite3_stmt *queryStmt,  bool &isDefeated);
142 
143     int SaveSyncDataItem(RelationalSyncDataInserter &inserter, SaveSyncDataStmt &saveStmt, DataItem &item);
144 
145     int SaveSyncDataItem(const DataItem &dataItem, SaveSyncDataStmt &saveStmt, RelationalSyncDataInserter &inserter,
146         int64_t &rowid);
147 
148     int DeleteSyncDataItem(const DataItem &dataItem, RelationalSyncDataInserter &inserter, sqlite3_stmt *&rmDataStmt);
149 
150     int GetLogInfoPre(sqlite3_stmt *queryStmt, const DataItem &dataItem, LogInfo &logInfoGet);
151 
152     int SaveSyncLog(sqlite3_stmt *statement, sqlite3_stmt *queryStmt, const DataItem &dataItem, int64_t rowid);
153 
154     int AlterAuxTableForUpgrade(const TableInfo &oldTableInfo, const TableInfo &newTableInfo);
155 
156     int DeleteSyncLog(const DataItem &item, RelationalSyncDataInserter &inserter, sqlite3_stmt *&rmLogStmt);
157     int ProcessMissQueryData(const DataItem &item, RelationalSyncDataInserter &inserter, sqlite3_stmt *&rmDataStmt,
158         sqlite3_stmt *&rmLogStmt);
159     int GetMissQueryData(sqlite3_stmt *fullStmt, DataItem &item);
160     int GetQueryDataAndStepNext(bool isFirstTime, bool isGettingDeletedData, sqlite3_stmt *queryStmt, DataItem &item,
161         Timestamp &queryTime);
162     int GetMissQueryDataAndStepNext(sqlite3_stmt *fullStmt, DataItem &item, Timestamp &missQueryTime);
163 
164     void SetTableInfo(const TableInfo &tableInfo);  // When put or get sync data, must call the func first.
165 
166     int GeneLogInfoForExistedData(sqlite3 *db, const std::string &tableName, const TableInfo &table,
167         const std::string &calPrimaryKeyHash);
168 
169     int GetCloudDataForSync(sqlite3_stmt *statement, CloudSyncData &cloudDataResult, uint32_t stepNum,
170         uint32_t &totalSize, const uint32_t &maxSize);
171 
172     int PutVBucketByType(VBucket &vBucket, const Field &field, Type &cloudValue);
173 
174     int ExecutePutCloudData(const std::string &tableName, const TableSchema &tableSchema, DownloadData &downloadData,
175         std::map<int, int> &statisticMap);
176 
177     std::string GetInsertSqlForCloudSync(const TableSchema &tableSchema);
178 
179     int GetPrimaryKeyHashValue(const VBucket &vBucket, const TableSchema &tableSchema, std::vector<uint8_t> &hashValue,
180         bool allowEmpty = false);
181 
182     int GetQueryLogStatement(const TableSchema &tableSchema, const VBucket &vBucket, const std::string &querySql,
183         std::set<std::string> &pkSet, sqlite3_stmt *&selectStmt);
184 
185     int GetQueryLogSql(const std::string &tableName, const VBucket &vBucket, std::set<std::string> &pkSet,
186         std::string &querySql);
187 
188     int GetQueryInfoSql(const std::string &tableName, const VBucket &vBucket, std::set<std::string> &pkSet,
189         std::vector<Field> &assetFields, std::string &querySql);
190 
191     int GetFillDownloadAssetStatement(const std::string &tableName, const VBucket &vBucket,
192         const std::vector<Field> &fields, sqlite3_stmt *&statement);
193 
194     int InitFillUploadAssetStatement(const std::string &tableName, const CloudSyncBatch &data,
195         const int &index, sqlite3_stmt *&statement);
196 
197     void GetLogInfoByStatement(sqlite3_stmt *statement, LogInfo &logInfo);
198 
199     int GetInfoByStatement(sqlite3_stmt *statement, std::vector<Field> &assetFields,
200         const std::map<std::string, Field> &pkMap, DataInfoWithLog &dataInfoWithLog, VBucket &assetInfo);
201 
202     int InsertCloudData(const std::string &tableName, VBucket &vBucket, const TableSchema &tableSchema);
203 
204     int InsertLogRecord(const TableSchema &tableSchema, VBucket &vBucket);
205 
206     int BindOneField(int index, const VBucket &vBucket, const Field &field, sqlite3_stmt *updateStmt);
207 
208     int BindValueToUpsertStatement(const VBucket &vBucket,  const std::vector<Field> &fields, sqlite3_stmt *upsertStmt);
209 
210     int BindHashKeyAndGidToInsertLogStatement(const VBucket &vBucket, const TableSchema &tableSchema,
211         sqlite3_stmt *insertLogStmt);
212 
213     int BindValueToInsertLogStatement(VBucket &vBucket, const TableSchema &tableSchema, sqlite3_stmt *insertLogStmt);
214 
215     std::string GetWhereConditionForDataTable(const std::string &gidStr, const std::set<std::string> &pkSet,
216         const std::string &tableName, bool queryByPk = true);
217 
218     int GetUpdateSqlForCloudSync(const TableSchema &tableSchema, const VBucket &vBucket, const std::string &gidStr,
219         const std::set<std::string> &pkSet, std::string &updateSql);
220 
221     int GetUpdateDataTableStatement(const VBucket &vBucket, const TableSchema &tableSchema, sqlite3_stmt *&updateStmt);
222 
223     int UpdateCloudData(const std::string &tableName, VBucket &vBucket, const TableSchema &tableSchema);
224 
225     int GetUpdateLogRecordStatement(const TableSchema &tableSchema, const VBucket &vBucket, OpType opType,
226         std::vector<std::string> &updateColName, sqlite3_stmt *&updateLogStmt);
227 
228     int UpdateLogRecord(const VBucket &vBucket, const TableSchema &tableSchema, OpType opType);
229 
230     int BindValueToUpdateLogStatement(const VBucket &vBucket, const TableSchema &tableSchema,
231         const std::vector<std::string> &colNames, bool allowPrimaryKeyEmpty, sqlite3_stmt *updateLogStmt);
232 
233     int GetDeleteStatementForCloudSync(const TableSchema &tableSchema, const std::set<std::string> &pkSet,
234         const VBucket &vBucket, sqlite3_stmt *&deleteStmt);
235 
236     int DeleteCloudData(const std::string &tableName, const VBucket &vBucket, const TableSchema &tableSchema);
237 
238     int OnlyUpdateLogTable(const VBucket &vBucket, const TableSchema &tableSchema, OpType opType);
239 
240     bool IsGetCloudDataContinue(uint32_t curNum, uint32_t curSize, uint32_t maxSize);
241 
242     std::string baseTblName_;
243     TableInfo table_;  // Always operating table, user table when get, device table when put.
244     TableSchema tableSchema_; // for cloud table
245 
246     DistributedTableMode mode_;
247 
248     std::map<int32_t, std::function<int(int, const VBucket &, const Field &, sqlite3_stmt *)>> bindCloudFieldFuncMap_;
249     std::map<int32_t, std::function<int(const VBucket &, const Field &, std::vector<uint8_t> &)>> toVectorFuncMap_;
250 };
251 } // namespace DistributedDB
252 #endif
253 #endif // SQLITE_SINGLE_VER_RELATIONAL_STORAGE_EXECUTOR_H