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_ENGINE_H 17 #define SQLITE_SINGLE_VER_STORAGE_ENGINE_H 18 19 #include "macro_utils.h" 20 #include "sqlite_storage_engine.h" 21 #include "sqlite_single_ver_storage_executor.h" 22 23 namespace DistributedDB { 24 enum class SQLiteGeneralNSNotificationEventType { 25 SQLITE_GENERAL_NS_PUT_EVENT = 0x01, 26 SQLITE_GENERAL_NS_SYNC_EVENT = 0x02, 27 SQLITE_GENERAL_NS_LOCAL_PUT_EVENT = 0x04, 28 SQLITE_GENERAL_CONFLICT_EVENT = 0x08, // Conflict event 29 SQLITE_GENERAL_FINISH_MIGRATE_EVENT = 0x10, // Only trigger sync event 30 }; 31 enum class SQLiteGeneralNSConflictType { 32 SQLITE_GENERAL_NS_FOREIGN_KEY_ONLY = 0x01, // sync conflict for same origin dev 33 SQLITE_GENERAL_NS_FOREIGN_KEY_ORIG = 0x02, // sync conflict for different origin dev 34 SQLITE_GENERAL_NS_NATIVE_ALL = 0x0c, // native conflict. 35 }; 36 class SQLiteSingleVerStorageEngine : public SQLiteStorageEngine { 37 public: 38 SQLiteSingleVerStorageEngine(); 39 ~SQLiteSingleVerStorageEngine() override; 40 41 // Delete the copy and assign constructors 42 DISABLE_COPY_ASSIGN_MOVE(SQLiteSingleVerStorageEngine); 43 44 void IncreaseCacheRecordVersion() override; 45 uint64_t GetCacheRecordVersion() const override; 46 uint64_t GetAndIncreaseCacheRecordVersion() override; 47 48 int ExecuteMigrate() override; 49 bool IsEngineCorrupted() const override; 50 GetSecurityOption()51 const SecurityOption &GetSecurityOption() const 52 { 53 return option_.securityOpt; 54 } 55 SetNeedUpdateSecOption(bool flag)56 void SetNeedUpdateSecOption(bool flag) 57 { 58 isNeedUpdateSecOpt_ = flag; 59 } 60 61 void CacheSubscribe(const std::string &subscribeId, const QueryObject &query); 62 63 protected: 64 StorageExecutor *NewSQLiteStorageExecutor(sqlite3 *dbHandle, bool isWrite, bool isMemDb) override; 65 66 int Upgrade(sqlite3 *db) override; 67 68 int CreateNewExecutor(bool isWrite, StorageExecutor *&handle) override; 69 70 private: 71 // For executor. 72 int PreCreateExecutor(bool isWrite); 73 int EndCreateExecutor(bool isWrite); 74 int ReInit() override; 75 int ReleaseExecutor(SQLiteSingleVerStorageExecutor *&handle); 76 int ReleaseHandleTransiently(SQLiteSingleVerStorageExecutor *&handle, uint64_t idleTime, 77 NotifyMigrateSyncData &syncData); 78 79 // For migrate. 80 int MigrateLocalData(SQLiteSingleVerStorageExecutor *handle) const; 81 int MigrateSyncDataByVersion(SQLiteSingleVerStorageExecutor *&handle, 82 NotifyMigrateSyncData &syncData, uint64_t &curMigrateVer); 83 int MigrateSyncData(SQLiteSingleVerStorageExecutor *&handle, bool &isNeedTriggerSync); 84 int FinishMigrateData(SQLiteSingleVerStorageExecutor *&handle, EngineState stateBeforeMigrate); 85 int InitExecuteMigrate(SQLiteSingleVerStorageExecutor *handle, EngineState preMigrateState); 86 void EndMigrate(SQLiteSingleVerStorageExecutor *&handle, EngineState stateBeforeMigrate, int errCode, 87 bool isNeedTriggerSync); 88 void ResetCacheRecordVersion(); 89 void SetMaxTimestamp(Timestamp maxTimestamp) const; 90 int EraseDeviceWaterMark(SQLiteSingleVerStorageExecutor *&handle, const std::vector<DataItem> &dataItems); 91 int EraseDeviceWaterMark(const std::set<std::string> &removeDevices, bool isNeedHash); 92 int GetRemoveDataDevices(SQLiteSingleVerStorageExecutor *handle, const DataItem &item, 93 std::set<std::string> &removeDevices, bool &isNeedHash) const; 94 95 // For db. 96 int TryToOpenMainDatabase(bool isWrite, sqlite3 *&db); 97 int GetCacheDbHandle(sqlite3 *&db); 98 int GetDbHandle(bool isWrite, const SecurityOption &secOpt, sqlite3 *&dbHandle); 99 int AttachMetaDatabase(sqlite3 *dbHandle, const OpenDbProperties &option) const; 100 int AttachMainDbAndCacheDb(SQLiteSingleVerStorageExecutor *handle, EngineState stateBeforeMigrate); 101 int AttachMainDbAndCacheDb(sqlite3 *dbHandle, EngineState stateBeforeMigrate) const; 102 void RegisterFunctionIfNeed(sqlite3 *dbHandle) const; 103 int TryAttachMetaDb(sqlite3 *&dbHandle, bool &isAttachMeta); 104 105 // For secOpt. 106 int CreateNewDirsAndSetSecOpt() const; 107 int CheckDatabaseSecOpt(const SecurityOption &secOption) const; 108 int GetExistedSecOption(SecurityOption &secOption) const; 109 110 void ClearCorruptedFlag() override; 111 112 // For commit notify. 113 void CommitAndReleaseNotifyData(SingleVerNaturalStoreCommitNotifyData *&committedData, int eventType) const; 114 void InitConflictNotifiedFlag(SingleVerNaturalStoreCommitNotifyData *&committedData) const; 115 void CommitNotifyForMigrateCache(NotifyMigrateSyncData &syncData) const; 116 117 // For subscribe 118 int AddSubscribeToMainDBInMigrate(); 119 120 mutable std::mutex migrateLock_; 121 std::atomic<uint64_t> cacheRecordVersion_; 122 ExecutorState executorState_; 123 bool isCorrupted_; 124 bool isNeedUpdateSecOpt_; // update the option_ 125 126 std::mutex subscribeMutex_; 127 std::map<std::string, QueryObject> subscribeQuery_; 128 }; 129 } // namespace DistributedDB 130 131 #endif // SQLITE_SINGLE_VER_STORAGE_ENGINE_H 132