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