• 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 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     int UpgradeLocalMetaData();
64 
65     void SetMaxValueSize(uint32_t maxValueSize);
66 
67     uint32_t GetMaxValueSize();
68 
69 protected:
70     virtual StorageExecutor *NewSQLiteStorageExecutor(sqlite3 *dbHandle, bool isWrite, bool isMemDb) override;
71 
72     int Upgrade(sqlite3 *db) override;
73 
74     int CreateNewExecutor(bool isWrite, StorageExecutor *&handle) override;
75 
76     ExecutorState executorState_;
77 
78 private:
79     // For executor.
80     int PreCreateExecutor(bool isWrite, SecurityOption &existedSecOpt);
81     int EndCreateExecutor(sqlite3 *db, SecurityOption existedSecOpt, bool isWrite, bool isDetachMeta);
82     int ReInit() override;
83     int ReleaseExecutor(SQLiteSingleVerStorageExecutor *&handle);
84     int ReleaseHandleTransiently(SQLiteSingleVerStorageExecutor *&handle, uint64_t idleTime,
85         NotifyMigrateSyncData &syncData);
86 
87     // For migrate.
88     int MigrateLocalData(SQLiteSingleVerStorageExecutor *handle) const;
89     int MigrateSyncDataByVersion(SQLiteSingleVerStorageExecutor *&handle,
90         NotifyMigrateSyncData &syncData, uint64_t &curMigrateVer);
91     int MigrateSyncData(SQLiteSingleVerStorageExecutor *&handle, bool &isNeedTriggerSync);
92     int FinishMigrateData(SQLiteSingleVerStorageExecutor *&handle, EngineState stateBeforeMigrate);
93     int InitExecuteMigrate(SQLiteSingleVerStorageExecutor *handle, EngineState preMigrateState);
94     void EndMigrate(SQLiteSingleVerStorageExecutor *&handle, EngineState stateBeforeMigrate, int errCode,
95         bool isNeedTriggerSync);
96     void ResetCacheRecordVersion();
97     int EraseDeviceWaterMark(SQLiteSingleVerStorageExecutor *&handle, const std::vector<DataItem> &dataItems);
98     int EraseDeviceWaterMark(const std::set<std::string> &removeDevices, bool isNeedHash);
99     int GetRemoveDataDevices(SQLiteSingleVerStorageExecutor *handle, const DataItem &item,
100         std::set<std::string> &removeDevices, bool &isNeedHash) const;
101 
102     // For db.
103     int TryToOpenMainDatabase(bool isWrite, sqlite3 *&db);
104     int GetCacheDbHandle(sqlite3 *&db);
105     int GetDbHandle(bool isWrite, const SecurityOption &secOpt, sqlite3 *&dbHandle);
106     int AttachMetaDatabase(sqlite3 *dbHandle, const OpenDbProperties &option) const;
107     int AttachMainDbAndCacheDb(SQLiteSingleVerStorageExecutor *handle, EngineState stateBeforeMigrate);
108     int AttachMainDbAndCacheDb(sqlite3 *dbHandle, EngineState stateBeforeMigrate) const;
109     void RegisterFunctionIfNeed(sqlite3 *dbHandle) const;
110     int TryAttachMetaDb(const SecurityOption &existedSecOpt, sqlite3 *&dbHandle, bool &isAttachMeta,
111         bool &isNeedDetachMeta);
112 
113     // For secOpt.
114     int CreateNewDirsAndSetSecOpt() const;
115     void CheckDatabaseSecOpt(const SecurityOption &secOption) const;
116     int GetExistedSecOption(SecurityOption &secOption) const;
117 
118     void ClearCorruptedFlag() override;
119 
120     // For commit notify.
121     void CommitAndReleaseNotifyData(SingleVerNaturalStoreCommitNotifyData *&committedData, int eventType) const;
122     void InitConflictNotifiedFlag(SingleVerNaturalStoreCommitNotifyData *&committedData) const;
123     void CommitNotifyForMigrateCache(NotifyMigrateSyncData &syncData) const;
124 
125     // For subscribe
126     int AddSubscribeToMainDBInMigrate();
127 
128     bool IsUseExistedSecOption(const SecurityOption &existedSecOpt, const SecurityOption &openSecOpt);
129 
130     mutable std::mutex migrateLock_;
131     std::atomic<uint64_t> cacheRecordVersion_;
132     bool isCorrupted_;
133     bool isNeedUpdateSecOpt_; // update the option_
134 
135     std::mutex subscribeMutex_;
136     std::map<std::string, QueryObject> subscribeQuery_;
137     uint32_t maxValueSize_;
138 };
139 } // namespace DistributedDB
140 
141 #endif // SQLITE_SINGLE_VER_STORAGE_ENGINE_H
142