• 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 
16 #ifndef SQLITE_SINGLE_VER_STORAGE_EXECUTOR_H
17 #define SQLITE_SINGLE_VER_STORAGE_EXECUTOR_H
18 
19 #include "macro_utils.h"
20 #include "db_types.h"
21 #include "query_object.h"
22 #include "sqlite_utils.h"
23 #include "sqlite_storage_executor.h"
24 #include "single_ver_natural_store_commit_notify_data.h"
25 
26 namespace DistributedDB {
27 class SQLiteSingleVerStorageExecutor : public SQLiteStorageExecutor {
28 public:
29     SQLiteSingleVerStorageExecutor(sqlite3 *dbHandle, bool writable, bool isMemDb);
30     SQLiteSingleVerStorageExecutor(sqlite3 *dbHandle, bool writable, bool isMemDb, ExecutorState executorState);
31     ~SQLiteSingleVerStorageExecutor() override;
32 
33     // Delete the copy and assign constructors
34     DISABLE_COPY_ASSIGN_MOVE(SQLiteSingleVerStorageExecutor);
35 
36     // Get the Kv data according the type(sync, meta, local data).
37     virtual int GetKvData(SingleVerDataType type, const Key &key, Value &value, Timestamp &timestamp) const;
38 
39     // Get the sync data record by hash key.
40     int GetKvDataByHashKey(const Key &hashKey, SingleVerRecord &result) const;
41 
42     // Put the Kv data according the type(meta and the local data).
43     virtual int PutKvData(SingleVerDataType type, const Key &key, const Value &value,
44         Timestamp timestamp, SingleVerNaturalStoreCommitNotifyData *committedData);
45 
46     virtual int GetEntries(bool isGetValue, SingleVerDataType type, const Key &keyPrefix,
47         std::vector<Entry> &entries) const;
48 
49     int GetEntries(QueryObject &queryObj, std::vector<Entry> &entries) const;
50 
51     int GetCount(QueryObject &queryObj, int &count) const;
52 
53     // Get all the meta keys.
54     int GetAllMetaKeys(std::vector<Key> &keys) const;
55 
56     int GetAllSyncedEntries(const std::string &hashDev, std::vector<Entry> &entries) const;
57 
58     int SaveSyncDataItem(DataItem &dataItem, const DeviceInfo &deviceInfo,
59         Timestamp &maxStamp, SingleVerNaturalStoreCommitNotifyData *committedData, bool isPermitForceWrite = true);
60 
61     virtual int DeleteLocalKvData(const Key &key, SingleVerNaturalStoreCommitNotifyData *committedData, Value &value,
62         Timestamp &timestamp);
63 
64     // delete a row data by hashKey, with no tombstone left.
65     int EraseSyncData(const Key &hashKey);
66 
67     int RemoveDeviceData(const std::string &deviceName);
68 
69     int RemoveDeviceData(const std::string &deviceName, ClearMode mode);
70 
71     int RemoveDeviceData(const std::string &deviceName, const std::string &user, ClearMode mode);
72 
73     int RemoveDeviceDataInCacheMode(const std::string &hashDev, bool isNeedNotify, uint64_t recordVersion) const;
74 
75     void InitCurrentMaxStamp(Timestamp &maxStamp);
76 
77     void ReleaseContinueStatement();
78 
79     int GetSyncDataByTimestamp(std::vector<DataItem> &dataItems, size_t appendLength, Timestamp begin,
80         Timestamp end, const DataSizeSpecInfo &dataSizeInfo) const;
81     int GetDeletedSyncDataByTimestamp(std::vector<DataItem> &dataItems, size_t appendLength, Timestamp begin,
82         Timestamp end, const DataSizeSpecInfo &dataSizeInfo) const;
83 
84     int GetUnSyncTotalByTimestamp(Timestamp begin, Timestamp end, uint32_t &total) const;
85 
86     int GetDeletedSyncTotalByTimestamp(Timestamp begin, Timestamp end, uint32_t &total) const;
87 
88     int GetSyncTotalWithQuery(QueryObject &query, const std::pair<Timestamp, Timestamp> &timeRange,
89         uint32_t &total) const;
90 
91     int GetDeviceIdentifier(PragmaEntryDeviceIdentifier *identifier);
92 
93     int OpenResultSet(const Key &keyPrefix, int &count);
94 
95     int OpenResultSet(QueryObject &queryObj, int &count);
96 
97     int OpenResultSetForCacheRowIdMode(const Key &keyPrefix, std::vector<int64_t> &rowIdCache,
98         uint32_t cacheLimit, int &count);
99 
100     int OpenResultSetForCacheRowIdMode(QueryObject &queryObj, std::vector<int64_t> &rowIdCache,
101         uint32_t cacheLimit, int &count);
102 
103     int ReloadResultSet(const Key &keyPrefix);
104 
105     int ReloadResultSet(QueryObject &queryObj);
106 
107     int ReloadResultSetForCacheRowIdMode(const Key &keyPrefix, std::vector<int64_t> &rowIdCache,
108         uint32_t cacheLimit, uint32_t cacheStartPos);
109 
110     int ReloadResultSetForCacheRowIdMode(QueryObject &queryObj, std::vector<int64_t> &rowIdCache,
111         uint32_t cacheLimit, uint32_t cacheStartPos);
112 
113     int GetNextEntryFromResultSet(Key &key, Value &value, bool isCopy);
114 
115     int GetEntryByRowId(int64_t rowId, Entry &entry);
116 
117     void CloseResultSet();
118 
119     int StartTransaction(TransactType type);
120 
121     int Commit();
122 
123     int Rollback();
124 
125     bool CheckIfKeyExisted(const Key &key, bool isLocal, Value &value, Timestamp &timestamp) const;
126 
127     int PrepareForSavingData(SingleVerDataType type);
128 
129     int ResetForSavingData(SingleVerDataType type);
130 
131     int Reset() override;
132 
133     int UpdateLocalDataTimestamp(Timestamp timestamp);
134 
135     void SetAttachMetaMode(bool attachMetaMode);
136 
137     int PutLocalDataToCacheDB(const LocalDataItem &dataItem) const;
138 
139     int SaveSyncDataItemInCacheMode(DataItem &dataItem, const DeviceInfo &deviceInfo, Timestamp &maxStamp,
140         uint64_t recordVersion, const QueryObject &query);
141 
142     int PrepareForSavingCacheData(SingleVerDataType type);
143     int ResetForSavingCacheData(SingleVerDataType type);
144 
145     int MigrateLocalData();
146 
147     int MigrateSyncDataByVersion(uint64_t recordVer, NotifyMigrateSyncData &syncData,
148         std::vector<DataItem> &dataItems);
149     int GetMinVersionCacheData(std::vector<DataItem> &dataItems, uint64_t &minVerIncurCacheDb) const;
150 
151     int GetMaxVersionInCacheDb(uint64_t &maxVersion) const;
152     int AttachMainDbAndCacheDb(CipherType type, const CipherPassword &passwd,
153         const std::string &attachDbAbsPath, EngineState engineState);
154 
155     // Clear migrating data.
156     void ClearMigrateData();
157 
158     // Get current max timestamp.
159     int GetMaxTimestampDuringMigrating(Timestamp &maxTimestamp) const;
160 
161     void SetConflictResolvePolicy(int policy);
162 
163     // Delete multiple meta data records in a transaction.
164     int DeleteMetaData(const std::vector<Key> &keys);
165     // Delete multiple meta data records with key prefix in a transaction.
166     int DeleteMetaDataByPrefixKey(const Key &keyPrefix);
167 
168     int CheckIntegrity() const;
169 
170     int CheckQueryObjectLegal(QueryObject &queryObj) const;
171 
172     int CheckDataWithQuery(QueryObject query, std::vector<DataItem> &dataItems, const DeviceInfo &deviceInfo);
173 
174     static size_t GetDataItemSerialSize(const DataItem &item, size_t appendLen);
175 
176     int AddSubscribeTrigger(QueryObject &query, const std::string &subscribeId);
177 
178     int RemoveSubscribeTrigger(const std::vector<std::string> &subscribeIds);
179 
180     int RemoveSubscribeTriggerWaterMark(const std::vector<std::string> &subscribeIds);
181 
182     int GetTriggers(const std::string &namePreFix, std::vector<std::string> &triggerNames);
183 
184     int RemoveTrigger(const std::vector<std::string> &triggers);
185 
186     int GetSyncDataWithQuery(const QueryObject &query, size_t appendLength, const DataSizeSpecInfo &dataSizeInfo,
187         const std::pair<Timestamp, Timestamp> &timeRange, std::vector<DataItem> &dataItems) const;
188 
189     int ForceCheckPoint() const;
190 
191     uint64_t GetLogFileSize() const;
192 
193     int GetExistsDevicesFromMeta(std::set<std::string> &devices);
194 
195     int UpdateKey(const UpdateKeyCallback &callback);
196 
197     int CreateCloudLogTable();
198 
199     int GetEntries(const std::string &device, std::vector<Entry> &entries) const;
200 protected:
201     virtual int SaveKvData(SingleVerDataType type, const Key &key, const Value &value, Timestamp timestamp);
202 
203     virtual int DeleteLocalDataInner(SingleVerNaturalStoreCommitNotifyData *committedData,
204         const Key &key, const Value &value);
205 
206 private:
207     struct SaveRecordStatements {
208         sqlite3_stmt *queryStatement = nullptr;
209         sqlite3_stmt *insertStatement = nullptr;
210         sqlite3_stmt *updateStatement = nullptr;
211 
212         int ResetStatement();
213 
GetDataSaveStatementSaveRecordStatements214         inline sqlite3_stmt *GetDataSaveStatement(bool isUpdate) const
215         {
216             return isUpdate ? updateStatement : insertStatement;
217         }
218     };
219 
220     struct UpdateContext {
221         int errCode = E_OK;
222         Key newKey;
223         UpdateKeyCallback callback;
224     };
225 
226     void PutIntoCommittedData(const DataItem &itemPut, const DataItem &itemGet, const DataOperStatus &status,
227         SingleVerNaturalStoreCommitNotifyData *committedData);
228 
229     static int BindSavedSyncData(sqlite3_stmt *statement, const DataItem &dataItem, const Key &hashKey,
230         const SyncDataDevices &devices, bool isUpdate);
231 
232     static int BindDevForSavedSyncData(sqlite3_stmt *statement, const DataItem &dataItem, const std::string &origDev,
233         const std::string &deviceName);
234 
235     static void PutConflictData(const DataItem &itemPut, const DataItem &itemGet, const DeviceInfo &deviceInfo,
236         const DataOperStatus &dataStatus, SingleVerNaturalStoreCommitNotifyData *commitData);
237 
238     DataOperStatus JudgeSyncSaveType(DataItem &dataItem, const DataItem &itemGet,
239         const DeviceInfo &deviceInfo, bool isHashKeyExisted, bool isPermitForceWrite = true);
240 
241     static std::string GetOriginDevName(const DataItem &dataItem, const std::string &origDevGet);
242 
243     int GetSyncDataItemPre(const DataItem &itemPut, DataItem &itemGet, Key &hashKey) const;
244 
245     int GetSyncDataItemExt(const DataItem &dataItem, DataItem &itemGet, const DataOperStatus &dataStatus) const;
246 
247     int GetSyncDataPreByHashKey(const Key &hashKey, DataItem &itemGet) const;
248 
249     int PrepareForSyncDataByTime(Timestamp begin, Timestamp end, sqlite3_stmt *&statement, bool getDeletedData = false)
250         const;
251 
252     int PrepareForUnSyncTotalByTime(Timestamp begin, Timestamp end, sqlite3_stmt *&statement,
253         bool getDeletedData = false) const;
254 
255     int GetCountValue(sqlite3_stmt *&countStatement, uint32_t &total) const;
256 
257     int StepForResultEntries(bool isGetValue, sqlite3_stmt *statement, std::vector<Entry> &entries) const;
258 
259     int InitResultSet(const Key &keyPrefix, sqlite3_stmt *&countStmt);
260 
261     int InitResultSetCount(QueryObject &queryObj, sqlite3_stmt *&countStmt);
262 
263     int InitResultSetContent(QueryObject &queryObj);
264 
265     int InitResultSet(QueryObject &queryObj, sqlite3_stmt *&countStmt);
266 
267     int GetAllEntries(sqlite3_stmt *statement, std::vector<Entry> &entries) const;
268 
269     int BindPutKvData(sqlite3_stmt *statement, const Key &key, const Value &value, Timestamp timestamp,
270         SingleVerDataType type);
271 
272     int SaveSyncDataToDatabase(const DataItem &dataItem, const Key &hashKey, const std::string &origDev,
273         const std::string &deviceName, bool isUpdate);
274 
275     int PrepareForSavingData(const std::string &readSql, const std::string &insertSql,
276         const std::string &updateSql, SaveRecordStatements &statements) const;
277 
278     int OpenResultSetForCacheRowIdModeCommon(std::vector<int64_t> &rowIdCache, uint32_t cacheLimit, int &count);
279 
280     int ResultSetLoadRowIdCache(std::vector<int64_t> &rowIdCache, uint32_t cacheLimit,
281         uint32_t cacheStartPos, int &count);
282 
283     void FinalizeAllStatements();
284     int ResetSaveSyncStatements(int errCode);
285 
286     int BindSyncDataInCacheMode(sqlite3_stmt *statement,
287         const DataItem &dataItem, const Key &hashKey, uint64_t recordVersion) const;
288 
289     int BindPrimaryKeySyncDataInCacheMode(
290         sqlite3_stmt *statement, const Key &hashKey, uint64_t recordVersion) const;
291 
292     int BindTimestampSyncDataInCacheMode(sqlite3_stmt *statement, const DataItem &dataItem) const;
293 
294     int BindDevSyncDataInCacheMode(sqlite3_stmt *statement,
295         const std::string &origDev, const std::string &deviceName) const;
296 
297     int SaveSyncDataToCacheDatabase(const DataItem &dataItem, const Key &hashKey, uint64_t recordVersion) const;
298 
299     int GetOneRawDataItem(sqlite3_stmt *statement, DataItem &dataItem,
300         uint64_t &verInCurCacheDb, bool isCacheDb) const;
301     int GetAllDataItems(sqlite3_stmt *statement, std::vector<DataItem> &dataItems,
302         uint64_t &verInCurCacheDb, bool isCacheDb) const;
303     int DelCacheDbDataByVersion(uint64_t version) const;
304 
305     // use for migrating data
306     int BindLocalDataInCacheMode(sqlite3_stmt *statement, const LocalDataItem &dataItem) const;
307 
308     // Process timestamp for syncdata in cacheDB when migrating.
309     int ProcessTimestampForSyncDataInCacheDB(std::vector<DataItem> &dataItems);
310 
311     // Get migrateTimeOffset_.
312     int InitMigrateTimestampOffset();
313 
314     // Get min timestamp of local data in sync_data, cacheDB.
315     int GetMinTimestampInCacheDB(Timestamp &minStamp) const;
316 
317     // Prepare conflict notify and commit notify data.
318     int PrepareForNotifyConflictAndObserver(DataItem &dataItem, const DeviceInfo &deviceInfo,
319         NotifyConflictAndObserverData &notify, bool isPermitForceWrite = true);
320 
321     // Put observer and conflict data into commit notify when migrating cacheDB.
322     int PutIntoConflictAndCommitForMigrateCache(DataItem &dataItem, const DeviceInfo &deviceInfo,
323         NotifyConflictAndObserverData &notify, bool isPermitForceWrite);
324 
325     int MigrateDataItems(std::vector<DataItem> &dataItems, NotifyMigrateSyncData &syncData);
326 
327     int MigrateDataItem(DataItem &dataItem, const NotifyMigrateSyncData &syncData);
328 
329     int GetEntriesForNotifyRemoveDevData(const DataItem &item, std::vector<Entry> &entries) const;
330 
331     // Reset migrateSyncStatements_.
332     int ResetForMigrateCacheData();
333 
334     // Init migrating data.
335     int InitMigrateData();
336 
337     int MigrateRmDevData(const DataItem &dataItem) const;
338     int VacuumLocalData() const;
339 
340     int GetSyncDataItems(std::vector<DataItem> &dataItems, sqlite3_stmt *statement,
341         size_t appendLength, const DataSizeSpecInfo &dataSizeInfo) const;
342 
343     int GetSyncDataWithQuery(sqlite3_stmt *fullStmt, sqlite3_stmt *queryStmt,
344         size_t appendLength, const DataSizeSpecInfo &dataSizeInfo, std::vector<DataItem> &dataItems) const;
345 
346     int CheckMissQueryDataItems(sqlite3_stmt *&stmt, const SqliteQueryHelper &helper, const DeviceInfo &deviceInfo,
347         std::vector<DataItem> &dataItems);
348 
349     int CheckDataWithQuery(std::vector<DataItem> &dataItems);
350 
351     int GetExpandedCheckSql(QueryObject query, DataItem &dataItem);
352 
353     int CheckMissQueryDataItem(sqlite3_stmt *stmt, const std::string &deviceName, DataItem &item);
354 
355     int CreateFuncUpdateKey(UpdateContext &context,
356         void(*translateFunc)(sqlite3_context *ctx, int argc, sqlite3_value **argv),
357         void(*calHashFunc)(sqlite3_context *ctx, int argc, sqlite3_value **argv)) const;
358 
359     static void Translate(sqlite3_context *ctx, int argc, sqlite3_value **argv);
360 
361     static void CalHashKey(sqlite3_context *ctx, int argc, sqlite3_value **argv);
362 
363     static int BindSyncDataTime(sqlite3_stmt *statement, const DataItem &dataItem, bool isUpdate);
364 
365     int CloudExcuteRemoveOrUpdate(const std::string &sql, const std::string &deviceName, const std::string &user,
366         bool isUserBlobType = false);
367 
368     int CloudCheckDataExist(const std::string &sql, const std::string &deviceName, const std::string &user,
369         bool &isExist);
370 
371     int RemoveDeviceDataInner(ClearMode mode);
372 
373     int RemoveDeviceDataInner(const std::string &deviceName, ClearMode mode);
374 
375     int RemoveDeviceDataWithUserInner(const std::string &user, ClearMode mode);
376 
377     int RemoveDeviceDataWithUserInner(const std::string &deviceName, const std::string &user, ClearMode mode);
378 
379     int RemoveCloudUploadFlag(const std::vector<uint8_t> &hashKey);
380 
381     bool IsFromDataOwner(const DataItem &itemGet, const std::string &syncDev);
382     sqlite3_stmt *getSyncStatement_;
383     sqlite3_stmt *getResultRowIdStatement_;
384     sqlite3_stmt *getResultEntryStatement_;
385     SaveRecordStatements saveSyncStatements_;
386     SaveRecordStatements saveLocalStatements_;
387 
388     // Used for migrating sync_data.
389     SaveRecordStatements migrateSyncStatements_;
390     bool isTransactionOpen_;
391     bool attachMetaMode_; // true for attach meta mode
392     ExecutorState executorState_;
393 
394     // Max timestamp in mainDB. Used for migrating.
395     Timestamp maxTimestampInMainDB_;
396 
397     // The offset between min timestamp in cacheDB and max timestamp in mainDB. Used for migrating.
398     TimeOffset migrateTimeOffset_;
399 
400     // Migrating sync flag. When the flag is true, mainDB and cacheDB are attached, migrateSyncStatements_ is set,
401     // maxTimestampInMainDB_ and migrateTimeOffset_ is meaningful.
402     bool isSyncMigrating_;
403     int conflictResolvePolicy_;
404 };
405 } // namespace DistributedDB
406 
407 #endif // SQLITE_SINGLE_VER_STORAGE_EXECUTOR_H
408