1 /*
2  * Copyright (c) 2023 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 spRdSingleVerStorageExecutorecific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef RD_SINGLE_VER_STORAGE_EXECUTOR_H
17 #define RD_SINGLE_VER_STORAGE_EXECUTOR_H
18 #include "macro_utils.h"
19 #include "db_types.h"
20 #include "grd_kv_api.h"
21 #include "grd_resultset_api.h"
22 #include "query_object.h"
23 #include "sqlite_utils.h"
24 #include "sqlite_single_ver_storage_executor.h"
25 #include "single_ver_natural_store_commit_notify_data.h"
26 
27 namespace DistributedDB {
28 class RDStorageExecutor : public StorageExecutor {
29 public:
30     RDStorageExecutor(GRD_DB *db, bool isWrite);
31     ~RDStorageExecutor() override;
32 
33     // Delete the copy and assign constructors
34     DISABLE_COPY_ASSIGN_MOVE(RDStorageExecutor);
35 
36     int Reset() override;
37 
38     int GetDbHandle(GRD_DB *&dbHandle) const;
39 
40 protected:
41     GRD_DB *db_;
42 };
43 
44 class RdSingleVerStorageExecutor : public RDStorageExecutor {
45 public:
46     RdSingleVerStorageExecutor(GRD_DB *db, bool isWrite);
47     ~RdSingleVerStorageExecutor() override;
48 
49     // Delete the copy and assign constructors
50     DISABLE_COPY_ASSIGN_MOVE(RdSingleVerStorageExecutor);
51 
52     // Get the Kv data according the type(sync, meta, local data).
53     int GetKvData(SingleVerDataType type, const Key &key, Value &value, Timestamp &timestamp) const;
54 
55     int Backup(const std::string &filePath, uint8_t *encryptedKey, uint32_t encryptedKeyLen);
56 
57     // Get the sync data record by hash key.
58     int GetKvDataByHashKey(const Key &hashKey, SingleVerRecord &result) const;
59 
60     // Put the Kv data according the type(meta and the local data).
61     virtual int PutKvData(SingleVerDataType type, const Key &key, const Value &value,
62         Timestamp timestamp, SingleVerNaturalStoreCommitNotifyData *committedData);
63 
64     int GetEntries(const GRD_KvScanModeE mode, const std::pair<Key, Key> &pairKey,
65         std::vector<Entry> &entries) const;
66 
67     int GetCount(const Key &key, int &count, GRD_KvScanModeE kvScanMode);
68 
69     int GetCount(const Key &beginKey, const Key &endKey, int &count, GRD_KvScanModeE kvScanMode);
70 
71     // Get all the meta keys.
72     int GetAllMetaKeys(std::vector<Key> &keys) const;
73 
74     int GetAllSyncedEntries(const std::string &hashDev, std::vector<Entry> &entries) const;
75 
76     int BatchSaveEntries(const std::vector<Entry> &entries, bool isDelete,
77         SingleVerNaturalStoreCommitNotifyData *committedData);
78 
79     int SaveSyncDataItem(const Entry &entry, SingleVerNaturalStoreCommitNotifyData *committedData, bool isDelete);
80 
81     int PrepareNotifyForEntries(const std::vector<Entry> &entries,
82         SingleVerNaturalStoreCommitNotifyData *committedData, std::vector<NotifyConflictAndObserverData> &notifys,
83         bool isDelete);
84 
85     int DeleteLocalKvData(const Key &key, SingleVerNaturalStoreCommitNotifyData *committedData, Value &value,
86         Timestamp &timestamp);
87 
88     int EraseSyncData(const Key &hashKey);
89 
90     int RemoveDeviceData(const std::string &deviceName);
91     int RemoveDeviceDataInCacheMode(const std::string &hashDev, bool isNeedNotify, uint64_t recordVersion) const;
92 
93     void InitCurrentMaxStamp(Timestamp &maxStamp);
94 
95     void ReleaseContinueStatement();
96 
97     int GetSyncDataByTimestamp(std::vector<DataItem> &dataItems, size_t appendLength, Timestamp begin,
98         Timestamp end, const DataSizeSpecInfo &dataSizeInfo) const;
99 
100     int GetDeletedSyncDataByTimestamp(std::vector<DataItem> &dataItems, size_t appendLength, Timestamp begin,
101         Timestamp end, const DataSizeSpecInfo &dataSizeInfo) const;
102 
103     int GetDeviceIdentifier(PragmaEntryDeviceIdentifier *identifier);
104 
105     int OpenResultSet(const Key &key, GRD_KvScanModeE mode, GRD_ResultSet **resultSet);
106 
107     int OpenResultSet(const Key &beginKey, const Key &endKey, GRD_ResultSet **resultSet);
108 
109     int CloseResultSet(GRD_ResultSet *resultSet);
110 
111     int MoveTo(const int position, GRD_ResultSet *resultSet, int &currPosition);
112 
113     int MoveToNext(GRD_ResultSet *resultSet);
114 
115     int MoveToPrev(GRD_ResultSet *resultSet);
116 
117     int GetEntry(GRD_ResultSet *resultSet, Entry &entry);
118 
119     int OpenResultSet(QueryObject &queryObj, int &count);
120 
121     int StartTransaction(TransactType type);
122 
123     int Commit();
124 
125     int Rollback();
126 
127     bool CheckIfKeyExisted(const Key &key, bool isLocal, Value &value, Timestamp &timestamp) const;
128 
129     int ResetForSavingData(SingleVerDataType type);
130 
131     int UpdateLocalDataTimestamp(Timestamp timestamp);
132 
133     void SetAttachMetaMode(bool attachMetaMode);
134 
135     int PutLocalDataToCacheDB(const LocalDataItem &dataItem) const;
136 
137     int SaveSyncDataItemInCacheMode(DataItem &dataItem, const DeviceInfo &deviceInfo, Timestamp &maxStamp,
138         uint64_t recordVersion, const QueryObject &query);
139 
140     int PrepareForSavingCacheData(SingleVerDataType type);
141 
142     int ResetForSavingCacheData(SingleVerDataType type);
143 
144     int MigrateLocalData();
145 
146     int MigrateSyncDataByVersion(uint64_t recordVer, NotifyMigrateSyncData &syncData,
147         std::vector<DataItem> &dataItems);
148 
149     int GetMinVersionCacheData(std::vector<DataItem> &dataItems, uint64_t &minVerIncurCacheDb) const;
150 
151     int GetMaxVersionInCacheDb(uint64_t &maxVersion) const;
152 
153     int AttachMainDbAndCacheDb(CipherType type, const CipherPassword &passwd,
154         const std::string &attachDbAbsPath, EngineState engineState);
155 
156     // Clear migrating data.
157     void ClearMigrateData();
158 
159     // Get current max timestamp.
160     int GetMaxTimestampDuringMigrating(Timestamp &maxTimestamp) const;
161 
162     void SetConflictResolvePolicy(int policy);
163 
164     // Delete multiple meta data records in a transaction.
165     int DeleteMetaData(const std::vector<Key> &keys);
166 
167     // Delete multiple meta data records with key prefix in a transaction.
168     int DeleteMetaDataByPrefixKey(const Key &keyPrefix);
169 
170     int CheckIntegrity() const;
171 
172     int CheckQueryObjectLegal(QueryObject &queryObj) const;
173 
174     int CheckDataWithQuery(QueryObject query, std::vector<DataItem> &dataItems, const DeviceInfo &deviceInfo);
175 
176     static size_t GetDataItemSerialSize(const DataItem &item, size_t appendLen);
177 
178     int AddSubscribeTrigger(QueryObject &query, const std::string &subscribeId);
179 
180     int RemoveSubscribeTrigger(const std::vector<std::string> &subscribeIds);
181 
182     int RemoveSubscribeTriggerWaterMark(const std::vector<std::string> &subscribeIds);
183 
184     int GetTriggers(const std::string &namePreFix, std::vector<std::string> &triggerNames);
185 
186     int RemoveTrigger(const std::vector<std::string> &triggers);
187 
188     int GetSyncDataWithQuery(const QueryObject &query, size_t appendLength, const DataSizeSpecInfo &dataSizeInfo,
189         const std::pair<Timestamp, Timestamp> &timeRange, std::vector<DataItem> &dataItems) const;
190 
191     int ForceCheckPoint() const;
192 
193     uint64_t GetLogFileSize() const;
194 
195     int GetExistsDevicesFromMeta(std::set<std::string> &devices);
196 
197     int UpdateKey(const UpdateKeyCallback &callback);
198 protected:
199     int SaveKvData(SingleVerDataType type, const Key &key, const Value &value);
200 
201     int DeleteLocalDataInner(SingleVerNaturalStoreCommitNotifyData *committedData, const Key &key, const Value &value);
202 
203 private:
204     void PutIntoCommittedData(const Key &key, const Value &value, NotifyConflictAndObserverData &data);
205 
206     int GetSyncDataPreByKey(const Key &key, DataItem &itemGet) const;
207 
208     int PrepareForNotifyConflictAndObserver(const Entry &entry,
209         NotifyConflictAndObserverData &notify, bool isDelete = false);
210 
211     int DelKvData(const Key &key);
212 
213     int SaveSyncDataToDatabase(const Entry &entry, bool isDelete);
214 
215     int InnerMoveToHead(const int position, GRD_ResultSet *resultSet, int &currPosition);
216 
217     static int ClearEntriesAndFreeResultSet(std::vector<Entry> &entries, GRD_ResultSet *resultSet);
218 
219     static int GetEntriesPrepare(GRD_DB *db, const GRD_KvScanModeE mode, const std::pair<Key, Key> &pairKey,
220         std::vector<Entry> &entries, GRD_ResultSet **resultSet);
221 
222     int GetCountInner(GRD_ResultSet *tmpResultSet, int &count);
223 };
224 } // namespace DistributedDB
225 #endif // RD_SINGLE_VER_STORAGE_EXECUTOR_H
226