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_NATURAL_STORE_CONNECTION_H
17 #define SQLITE_SINGLE_VER_NATURAL_STORE_CONNECTION_H
18 #include <atomic>
19 #include "single_ver_natural_store_connection.h"
20 #include "sync_able_kvdb_connection.h"
21 #include "sqlite_single_ver_storage_executor.h"
22 #include "db_types.h"
23 #include "runtime_context.h"
24 
25 namespace DistributedDB {
26 class SQLiteSingleVerNaturalStore;
27 
28 class SQLiteSingleVerNaturalStoreConnection : public SingleVerNaturalStoreConnection {
29 public:
30     explicit SQLiteSingleVerNaturalStoreConnection(SQLiteSingleVerNaturalStore *kvDB);
31     ~SQLiteSingleVerNaturalStoreConnection() override;
32 
33     // Delete the copy and assign constructors
34     DISABLE_COPY_ASSIGN_MOVE(SQLiteSingleVerNaturalStoreConnection);
35 
36     // Get the value from the database
37     int Get(const IOption &option, const Key &key, Value &value) const override;
38 
39     // Clear all the data from the database
40     int Clear(const IOption &option) override;
41 
42     // Get all the data from the database
43     int GetEntries(const IOption &option, const Key &keyPrefix, std::vector<Entry> &entries) const override;
44 
45     int GetEntries(const IOption &option, const Query &query, std::vector<Entry> &entries) const override;
46 
47     int GetCount(const IOption &option, const Query &query, int &count) const override;
48 
49     // Put the batch values to the database.
50     int PutBatch(const IOption &option, const std::vector<Entry> &entries) override;
51 
52     // Delete the batch values from the database.
53     int DeleteBatch(const IOption &option, const std::vector<Key> &keys) override;
54 
55     // Get the snapshot
56     int GetSnapshot(IKvDBSnapshot *&snapshot) const override;
57 
58     // Release the created snapshot
59     void ReleaseSnapshot(IKvDBSnapshot *&snapshot) override;
60 
61     // Start the transaction
62     int StartTransaction() override;
63 
64     // Commit the transaction
65     int Commit() override;
66 
67     // Roll back the transaction
68     int RollBack() override;
69 
70     // Check if the transaction already started manually
71     bool IsTransactionStarted() const override;
72 
73     // Pragma interface.
74     int Pragma(int cmd, void *parameter) override;
75 
76     // Parse event types(from observer mode).
77     int TranslateObserverModeToEventTypes(unsigned mode, std::list<int> &eventTypes) const override;
78 
79     // Register a conflict notifier.
80     int SetConflictNotifier(int conflictType, const KvDBConflictAction &action) override;
81 
82     int Rekey(const CipherPassword &passwd) override;
83 
84     int Export(const std::string &filePath, const CipherPassword &passwd) override;
85 
86     int Import(const std::string &filePath, const CipherPassword &passwd) override;
87 
88     // Get the result set
89     int GetResultSet(const IOption &option, const Key &keyPrefix, IKvDBResultSet *&resultSet) const override;
90 
91     int GetResultSet(const IOption &option, const Query &query, IKvDBResultSet *&resultSet) const override;
92 
93     // Release the result set
94     void ReleaseResultSet(IKvDBResultSet *&resultSet) override;
95 
96     int RegisterLifeCycleCallback(const DatabaseLifeCycleNotifier &notifier) override;
97 
98     // Called when Close and delete the connection.
99     int PreClose() override;
100 
101     int CheckIntegrity() const override;
102 
103     int GetKeys(const IOption &option, const Key &keyPrefix, std::vector<Key> &keys) const override;
104 
105     int UpdateKey(const UpdateKeyCallback &callback) override;
106 
107     int SetCloudDbSchema(const std::map<std::string, DataBaseSchema> &schema) override;
108 
109     int RegisterObserverAction(const KvStoreObserver *observer, const ObserverAction &action) override;
110 
111     int UnRegisterObserverAction(const KvStoreObserver *observer) override;
112 
113     int RemoveDeviceData(const std::string &device, ClearMode mode) override;
114 
115     int RemoveDeviceData(const std::string &device, const std::string &user, ClearMode mode) override;
116 
117     int GetCloudVersion(const std::string &device, std::map<std::string, std::string> &versionMap) override;
118 
119     int SetCloudSyncConfig(const CloudSyncConfig &config) override;
120 
121     int GetEntries(const std::string &device, std::vector<Entry> &entries) const override;
122 private:
123     int CheckMonoStatus(OperatePerm perm);
124 
125     int GetDeviceIdentifier(PragmaEntryDeviceIdentifier *identifier);
126 
127     void ClearConflictNotifierCount();
128 
129     int PutBatchInner(const IOption &option, const std::vector<Entry> &entries) override;
130     int DeleteBatchInner(const IOption &option, const std::vector<Key> &keys) override;
131 
132     int SaveSyncEntries(const std::vector<Entry> &entries);
133     int SaveLocalEntries(const std::vector<Entry> &entries);
134     int DeleteSyncEntries(const std::vector<Key> &keys);
135     int DeleteLocalEntries(const std::vector<Key> &keys);
136 
137     int SaveEntry(const Entry &entry, bool isDelete, Timestamp timestamp = 0);
138 
139     int CheckDataStatus(const Key &key, const Value &value, bool isDelete) const;
140 
141     int CheckWritePermission() const override;
142 
143     int CheckSyncEntriesValid(const std::vector<Entry> &entries) const override;
144 
145     int CheckSyncKeysValid(const std::vector<Key> &keys) const override;
146 
147     int CheckLocalEntriesValid(const std::vector<Entry> &entries) const;
148 
149     int CheckLocalKeysValid(const std::vector<Key> &keys) const;
150 
151     void CommitAndReleaseNotifyData(SingleVerNaturalStoreCommitNotifyData *&committedData,
152         bool isNeedCommit, int eventType);
153 
154     int StartTransactionInner(TransactType transType = TransactType::DEFERRED);
155 
156     int CommitInner();
157 
158     int RollbackInner();
159 
160     int PublishLocal(const Key &key, bool deleteLocal, bool updateTimestamp,
161         const KvStoreNbPublishAction &onConflict);
162 
163     int PublishLocalCallback(bool updateTimestamp, const SingleVerRecord &localRecord,
164         const SingleVerRecord &syncRecord, const KvStoreNbPublishAction &onConflict);
165 
166     int PublishInner(SingleVerNaturalStoreCommitNotifyData *committedData, bool updateTimestamp,
167         SingleVerRecord &localRecord, SingleVerRecord &syncRecord, bool &isNeedCallback);
168 
169     int UnpublishToLocal(const Key &key, bool deletePublic, bool updateTimestamp);
170 
171     int UnpublishInner(std::pair<Key, Key> &keyPair, SingleVerNaturalStoreCommitNotifyData *&committedData,
172         SingleVerRecord &syncRecord, bool updateTimestamp, int &innerErrCode);
173 
174     int UnpublishOper(SingleVerNaturalStoreCommitNotifyData *&committedData, const SingleVerRecord &syncRecord,
175         bool updateTimestamp, int operType);
176 
177     void ReleaseCommitData(SingleVerNaturalStoreCommitNotifyData *&committedData);
178 
179     int PragmaPublish(void *parameter);
180 
181     int PragmaUnpublish(void *parameter);
182 
183     SQLiteSingleVerStorageExecutor *GetExecutor(bool isWrite, int &errCode) const;
184 
185     void ReleaseExecutor(SQLiteSingleVerStorageExecutor *&executor) const;
186 
187     int PragmaSetAutoLifeCycle(const uint32_t *lifeTime);
188     void InitConflictNotifiedFlag();
189     void AddConflictNotifierCount(int target);
190     void ResetConflictNotifierCount(int target);
191 
192     int PragmaResultSetCacheMode(PragmaData inMode);
193     int PragmaResultSetCacheMaxSize(PragmaData inSize);
194 
195     // use for getkvstore migrating cache data
196     int PragmaTriggerToMigrateData(const SecurityOption &secOption) const;
197     int CheckAmendValueContentForLocalProcedure(const Value &oriValue, Value &amendValue) const;
198 
199     int SaveLocalEntry(const Entry &entry, bool isDelete);
200     int SaveLocalItem(const LocalDataItem &dataItem) const;
201     int SaveLocalItemInCacheMode(const LocalDataItem &dataItem) const;
202     int SaveEntryNormally(DataItem &dataItem);
203     int SaveEntryInCacheMode(DataItem &dataItem, uint64_t recordVersion);
204 
205     int StartTransactionInCacheMode(TransactType transType = TransactType::DEFERRED);
206     int StartTransactionNormally(TransactType transType = TransactType::DEFERRED);
207 
208     bool IsCacheDBMode() const;
209     bool IsExtendedCacheDBMode() const override;
210     int CheckReadDataControlled() const;
211     bool IsFileAccessControlled() const;
212 
213     int PragmaSetMaxLogSize(uint64_t *limit);
214     int ForceCheckPoint() const;
215 
216     bool CheckLogOverLimit(SQLiteSingleVerStorageExecutor *executor) const;
217     int CalcHashDevID(PragmaDeviceIdentifier &pragmaDev);
218 
219     int GetEntriesInner(bool isGetValue, const IOption &option,
220         const Key &keyPrefix, std::vector<Entry> &entries) const;
221 
222     void RecordTimeIntoDataItem(Timestamp existCreateTime, DataItem &dataItem,
223         SQLiteSingleVerNaturalStore &naturalStore);
224 
225     DECLARE_OBJECT_TAG(SQLiteSingleVerNaturalStoreConnection);
226 
227     // ResultSet Related Info
228     static constexpr std::size_t MAX_RESULT_SET_SIZE = 8; // Max 8 ResultSet At The Same Time
229     std::atomic<ResultSetCacheMode> cacheModeForNewResultSet_{ResultSetCacheMode::CACHE_FULL_ENTRY};
230     std::atomic<int> cacheMaxSizeForNewResultSet_{0}; // Will be init to default value in constructor
231 
232     int conflictType_;
233     uint32_t transactionEntryLen_; // used for transaction
234     Timestamp currentMaxTimestamp_; // used for transaction
235     SingleVerNaturalStoreCommitNotifyData *committedData_; // used for transaction
236     SingleVerNaturalStoreCommitNotifyData *localCommittedData_;
237     std::atomic<bool> transactionExeFlag_;
238 
239     NotificationChain::Listener *conflictListener_;
240     SQLiteSingleVerStorageExecutor *writeHandle_; // only existed while in transaction.
241     mutable std::set<IKvDBResultSet *> kvDbResultSets_;
242     std::mutex conflictMutex_;
243     std::mutex rekeyMutex_;
244     std::mutex importMutex_;
245     mutable std::mutex kvDbResultSetsMutex_;
246     mutable std::mutex transactionMutex_; // used for transaction
247 };
248 }
249 
250 #endif