• 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_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 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 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 
78     // For migrate.
79     int MigrateLocalData(SQLiteSingleVerStorageExecutor *handle) const;
80     int MigrateSyncDataByVersion(SQLiteSingleVerStorageExecutor *&handle,
81         NotifyMigrateSyncData &syncData, uint64_t &curMigrateVer);
82     int MigrateSyncData(SQLiteSingleVerStorageExecutor *&handle, bool &isNeedTriggerSync);
83     int FinishMigrateData(SQLiteSingleVerStorageExecutor *&handle, EngineState stateBeforeMigrate);
84     int InitExecuteMigrate(SQLiteSingleVerStorageExecutor *handle, EngineState preMigrateState);
85     void EndMigrate(SQLiteSingleVerStorageExecutor *&handle, EngineState stateBeforeMigrate, int errCode,
86         bool isNeedTriggerSync);
87     void ResetCacheRecordVersion();
88     void SetMaxTimestamp(Timestamp maxTimestamp) const;
89     int EraseDeviceWaterMark(SQLiteSingleVerStorageExecutor *&handle, const std::vector<DataItem> &dataItems);
90 
91     // For db.
92     int TryToOpenMainDatabase(bool isWrite, sqlite3 *&db);
93     int GetCacheDbHandle(sqlite3 *&db);
94     int GetDbHandle(bool isWrite, const SecurityOption &secOpt, sqlite3 *&dbHandle);
95     int AttachMetaDatabase(sqlite3 *dbHandle, const OpenDbProperties &option) const;
96     int AttachMainDbAndCacheDb(SQLiteSingleVerStorageExecutor *handle, EngineState stateBeforeMigrate);
97     int AttachMainDbAndCacheDb(sqlite3 *db, EngineState stateBeforeMigrate) const;
98     void RegisterFunctionIfNeed(sqlite3 *dbHandle) const;
99     int TryAttachMetaDb(sqlite3 *&dbHandle, bool &isAttachMeta);
100 
101     // For secOpt.
102     int CreateNewDirsAndSetSecOpt() const;
103     int CheckDatabaseSecOpt(const SecurityOption &secOption) const;
104     int GetExistedSecOption(SecurityOption &secOption) const;
105 
106     void ClearCorruptedFlag() override;
107 
108     // For commit notify.
109     void CommitAndReleaseNotifyData(SingleVerNaturalStoreCommitNotifyData *&committedData, int eventType) const;
110     void InitConflictNotifiedFlag(SingleVerNaturalStoreCommitNotifyData *&committedData) const;
111     void CommitNotifyForMigrateCache(NotifyMigrateSyncData &syncData) const;
112 
113     // For subscribe
114     int AddSubscribeToMainDBInMigrate();
115 
116     mutable std::mutex migrateLock_;
117     std::atomic<uint64_t> cacheRecordVersion_;
118     ExecutorState executorState_;
119     bool isCorrupted_;
120     bool isNeedUpdateSecOpt_; // update the option_
121 
122     std::mutex subscribeMutex_;
123     std::map<std::string, QueryObject> subscribeQuery_;
124 };
125 } // namespace DistributedDB
126 
127 #endif // SQLITE_SINGLE_VER_STORAGE_ENGINE_H
128