• 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 #ifndef SQLITE_SINGLE_VER_NATURAL_STORE_H
16 #define SQLITE_SINGLE_VER_NATURAL_STORE_H
17 #include <atomic>
18 #include <mutex>
19 
20 #include "isyncer.h"
21 #include "kv_store_nb_conflict_data_impl.h"
22 #include "runtime_context.h"
23 #include "single_ver_kvdb_sync_interface.h"
24 #include "single_ver_natural_store_commit_notify_data.h"
25 #include "sqlite_single_ver_continue_token.h"
26 #include "sqlite_single_ver_storage_engine.h"
27 #include "sqlite_utils.h"
28 #include "sync_able_kvdb.h"
29 
30 namespace DistributedDB {
31 class SQLiteSingleVerNaturalStore : public SyncAbleKvDB, public SingleVerKvDBSyncInterface {
32 public:
33     SQLiteSingleVerNaturalStore();
34     ~SQLiteSingleVerNaturalStore() override;
35 
36     // Delete the copy and assign constructors
37     DISABLE_COPY_ASSIGN_MOVE(SQLiteSingleVerNaturalStore);
38 
39     // Open the database
40     int Open(const KvDBProperties &kvDBProp) override;
41 
42     // Invoked automatically when connection count is zero
43     void Close() override;
44 
45     // Create a connection object.
46     GenericKvDBConnection *NewConnection(int &errCode) override;
47 
48     // Get interface type of this kvdb.
49     int GetInterfaceType() const override;
50 
51     // Get the interface ref-count, in order to access asynchronously.
52     void IncRefCount() override;
53 
54     // Drop the interface ref-count.
55     void DecRefCount() override;
56 
57     // Get the identifier of this kvdb.
58     std::vector<uint8_t> GetIdentifier() const override;
59     // Get the dual tuple identifier of this kvdb.
60     std::vector<uint8_t> GetDualTupleIdentifier() const override;
61 
62     // Get interface for syncer.
63     IKvDBSyncInterface *GetSyncInterface() override;
64 
65     int GetMetaData(const Key &key, Value &value) const override;
66 
67     int PutMetaData(const Key &key, const Value &value) override;
68 
69     // Delete multiple meta data records in a transaction.
70     int DeleteMetaData(const std::vector<Key> &keys) override;
71     // Delete multiple meta data records with key prefix in a transaction.
72     int DeleteMetaDataByPrefixKey(const Key &keyPrefix) const override;
73 
74     int GetAllMetaKeys(std::vector<Key> &keys) const override;
75 
76     int GetSyncData(Timestamp begin, Timestamp end, std::vector<DataItem> &dataItems, ContinueToken &continueStmtToken,
77         const DataSizeSpecInfo &dataSizeInfo) const override;
78 
79     int GetSyncData(Timestamp begin, Timestamp end, std::vector<SingleVerKvEntry *> &entries,
80         ContinueToken &continueStmtToken, const DataSizeSpecInfo &dataSizeInfo) const override;
81 
82     int GetSyncData(QueryObject &query, const SyncTimeRange &timeRange, const DataSizeSpecInfo &dataSizeInfo,
83         ContinueToken &continueStmtToken, std::vector<SingleVerKvEntry *> &entries) const override;
84 
85     int GetSyncDataNext(std::vector<DataItem> &dataItems, ContinueToken &continueStmtToken,
86         const DataSizeSpecInfo &dataSizeInfo) const override;
87 
88     int GetSyncDataNext(std::vector<SingleVerKvEntry *> &entries, ContinueToken &continueStmtToken,
89         const DataSizeSpecInfo &dataSizeInfo) const override;
90 
91     void ReleaseContinueToken(ContinueToken &continueStmtToken) const override;
92 
93     int PutSyncDataWithQuery(const QueryObject &query, const std::vector<SingleVerKvEntry *> &entries,
94         const std::string &deviceName) override;
95 
96     void GetMaxTimestamp(Timestamp &stamp) const override;
97 
98     void SetMaxTimestamp(Timestamp timestamp);
99 
100     int Rekey(const CipherPassword &passwd) override;
101 
102     int Export(const std::string &filePath, const CipherPassword &passwd) override;
103 
104     int Import(const std::string &filePath, const CipherPassword &passwd) override;
105 
106     // In sync procedure, call this function
107     int RemoveDeviceData(const std::string &deviceName, bool isNeedNotify) override;
108 
109     // In local procedure, call this function
110     int RemoveDeviceData(const std::string &deviceName, bool isNeedNotify, bool isInSync);
111 
112     SQLiteSingleVerStorageExecutor *GetHandle(bool isWrite, int &errCode,
113         OperatePerm perm = OperatePerm::NORMAL_PERM) const;
114 
115     void ReleaseHandle(SQLiteSingleVerStorageExecutor *&handle) const;
116 
117     int TransObserverTypeToRegisterFunctionType(int observerType, RegisterFuncType &type) const override;
118 
119     int TransConflictTypeToRegisterFunctionType(int conflictType, RegisterFuncType &type) const override;
120 
121     bool CheckWritePermission() const override;
122 
123     SchemaObject GetSchemaInfo() const override;
124 
125     bool CheckCompatible(const std::string &schema, uint8_t type) const override;
126 
127     Timestamp GetCurrentTimestamp();
128 
129     SchemaObject GetSchemaObject() const;
130 
131     const SchemaObject &GetSchemaObjectConstRef() const;
132 
133     const KvDBProperties &GetDbProperties() const override;
134 
135     int RemoveKvDB(const KvDBProperties &properties) override;
136 
137     int GetKvDBSize(const KvDBProperties &properties, uint64_t &size) const override;
138     KvDBProperties &GetDbPropertyForUpdate();
139 
140     int InitDatabaseContext(const KvDBProperties &kvDBProp, bool isNeedUpdateSecOpt = false);
141 
142     int RegisterLifeCycleCallback(const DatabaseLifeCycleNotifier &notifier);
143 
144     int SetAutoLifeCycleTime(uint32_t time);
145 
146     int GetSecurityOption(SecurityOption &option) const override;
147 
148     bool IsDataMigrating() const override;
149 
150     void SetConnectionFlag(bool isExisted) const override;
151 
152     int TriggerToMigrateData() const;
153 
154     int CheckValueAndAmendIfNeed(ValueSource sourceType, const Value &oriValue, Value &amendValue,
155         bool &useAmendValue) const;
156 
157     int CheckReadDataControlled() const;
158     bool IsCacheDBMode() const;
159     bool IsExtendedCacheDBMode() const;
160 
161     void IncreaseCacheRecordVersion() const;
162     uint64_t GetCacheRecordVersion() const;
163     uint64_t GetAndIncreaseCacheRecordVersion() const;
164 
165     void NotifyRemotePushFinished(const std::string &targetId) const override;
166 
167     int GetDatabaseCreateTimestamp(Timestamp &outTime) const override;
168 
169     int CheckIntegrity() const override;
170 
171     int GetCompressionOption(bool &needCompressOnSync, uint8_t &compressionRate) const override;
172     int GetCompressionAlgo(std::set<CompressAlgorithm> &algorithmSet) const override;
173 
174     // Check and init query object for query sync and subscribe, flatbuffer schema will always return E_NOT_SUPPORT.
175     // return E_OK if subscribe is legal, ERROR on exception.
176     int CheckAndInitQueryCondition(QueryObject &query) const override;
177 
178     int InterceptData(std::vector<SingleVerKvEntry *> &entries, const std::string &sourceID,
179         const std::string &targetID) const override;
180 
181     void SetDataInterceptor(const PushDataInterceptor &interceptor) override;
182 
183     int AddSubscribe(const std::string &subscribeId, const QueryObject &query, bool needCacheSubscribe) override;
184 
185     int RemoveSubscribe(const std::string &subscribeId) override;
186 
187     int RemoveSubscribe(const std::vector<std::string> &subscribeIds) override;
188 
189     int SetMaxLogSize(uint64_t limit);
190 
191     uint64_t GetMaxLogSize() const;
192 
193     void Dump(int fd) override;
194 
195     int IsSupportSubscribe() const override;
196 
197     void AbortHandle();
198 
199     void EnableHandle();
200 
201     int TryHandle() const override;
202 
203 protected:
204     void AsyncDataMigration() const;
205 
206     void ReleaseResources();
207 
208 private:
209     struct TransPair {
210         int index;
211         RegisterFuncType funcType;
212     };
213     static RegisterFuncType GetFuncType(int index, const TransPair *transMap, int32_t len);
214     int CheckDatabaseRecovery(const KvDBProperties &kvDBProp);
215 
216     void CommitAndReleaseNotifyData(SingleVerNaturalStoreCommitNotifyData *&committedData,
217         bool isNeedCommit, int eventType);
218 
219     int RegisterNotification();
220 
221     void InitCurrentMaxStamp();
222 
223     int SaveSyncDataItems(const QueryObject &query, std::vector<DataItem> &dataItems, const DeviceInfo &deviceInfo,
224         bool checkValueContent);
225 
226     int InitStorageEngine(const KvDBProperties &kvDBProp, bool isNeedUpdateSecOpt);
227 
228     void InitialLocalDataTimestamp();
229 
230     int GetSchema(SchemaObject &schema) const;
231 
232     static void InitDataBaseOption(const KvDBProperties &kvDBProp, OpenDbProperties &option);
233 
234     static int SetUserVer(const KvDBProperties &kvDBProp, int version);
235 
236     static std::string GetDatabasePath(const KvDBProperties &kvDBProp);
237     static std::string GetSubDirPath(const KvDBProperties &kvDBProp);
238     void NotifyRemovedData(std::vector<Entry> &entries);
239 
240     // Decide read only based on schema situation
241     int DecideReadOnlyBaseOnSchema(const KvDBProperties &kvDBProp, bool &isReadOnly,
242         SchemaObject &savedSchemaObj) const;
243 
244     void HeartBeatForLifeCycle() const;
245 
246     int StartLifeCycleTimer(const DatabaseLifeCycleNotifier &notifier) const;
247 
248     int ResetLifeCycleTimer() const;
249 
250     int StopLifeCycleTimer() const;
251     void InitConflictNotifiedFlag(SingleVerNaturalStoreCommitNotifyData *committedData);
252 
253     // Change value that should be amended, and neglect value that is incompatible
254     void CheckAmendValueContentForSyncProcedure(std::vector<DataItem> &dataItems) const;
255 
256     int RemoveDeviceDataInCacheMode(const std::string &hashDev, bool isNeedNotify) const;
257 
258     int RemoveDeviceDataNormally(const std::string &hashDev, bool isNeedNotify);
259 
260     int SaveSyncDataToMain(const QueryObject &query, std::vector<DataItem> &dataItems, const DeviceInfo &deviceInfo);
261 
262     // Currently, this function only suitable to be call from sync in insert_record_from_sync procedure
263     // Take attention if future coder attempt to call it in other situation procedure
264     int SaveSyncItems(const QueryObject& query, std::vector<DataItem> &dataItems, const DeviceInfo &deviceInfo,
265         Timestamp &maxTimestamp, SingleVerNaturalStoreCommitNotifyData *commitData) const;
266 
267     int SaveSyncDataToCacheDB(const QueryObject &query, std::vector<DataItem> &dataItems,
268         const DeviceInfo &deviceInfo);
269 
270     int SaveSyncItemsInCacheMode(SQLiteSingleVerStorageExecutor *handle, const QueryObject &query,
271         std::vector<DataItem> &dataItems, const DeviceInfo &deviceInfo, Timestamp &maxTimestamp) const;
272 
273     int ClearIncompleteDatabase(const KvDBProperties &kvDBPro) const;
274 
275     int GetSyncDataForQuerySync(std::vector<DataItem> &dataItems, SQLiteSingleVerContinueToken *&continueStmtToken,
276         const DataSizeSpecInfo &dataSizeInfo) const;
277 
278     int SaveCreateDBTime();
279     int SaveCreateDBTimeIfNotExisted();
280 
281     int GetAndInitStorageEngine(const KvDBProperties &kvDBProp);
282 
283     int RemoveAllSubscribe();
284 
285     int GetExistsDeviceList(std::set<std::string> &devices) const;
286 
287     int RemoveDeviceDataInner(const std::string &hashDev, bool isNeedNotify);
288 
289     int GetAndResizeLocalIdentity(std::string &outTarget) const;
290 
291     DECLARE_OBJECT_TAG(SQLiteSingleVerNaturalStore);
292 
293     Timestamp currentMaxTimestamp_ = 0;
294 
295     mutable std::shared_mutex engineMutex_;
296     mutable std::mutex migrateMutex_;
297     mutable std::condition_variable migrateCv_;
298     mutable int migrateCount_;
299     SQLiteSingleVerStorageEngine *storageEngine_;
300 
301     bool notificationEventsRegistered_;
302     bool notificationConflictEventsRegistered_;
303     bool isInitialized_;
304     bool isReadOnly_;
305     mutable std::mutex initialMutex_;
306     mutable std::mutex maxTimestampMutex_;
307     mutable std::mutex lifeCycleMutex_;
308     mutable DatabaseLifeCycleNotifier lifeCycleNotifier_;
309     mutable TimerId lifeTimerId_;
310     uint32_t autoLifeTime_;
311     mutable Timestamp createDBTime_;
312     mutable std::mutex createDBTimeMutex_;
313 
314     mutable std::shared_mutex dataInterceptorMutex_;
315     PushDataInterceptor dataInterceptor_;
316     std::atomic<uint64_t> maxLogSize_;
317 
318     mutable std::shared_mutex abortHandleMutex_;
319     OperatePerm abortPerm_;
320 };
321 } // namespace DistributedDB
322 #endif // SQLITE_SINGLE_VER_NATURAL_STORE_H
323