1 /*
2  * Copyright (c) 2024 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_CLOUD_STORE_H
16 #define SQLITE_CLOUD_STORE_H
17 
18 #include "icloud_sync_storage_interface.h"
19 #include "cloud/cloud_upload_recorder.h"
20 #include "kv_storage_handle.h"
21 
22 namespace DistributedDB {
23 class SqliteCloudKvStore : public ICloudSyncStorageInterface, public RefObject {
24 public:
25     explicit SqliteCloudKvStore(KvStorageHandle *handle);
26     ~SqliteCloudKvStore() override = default;
27 
28     int GetMetaData(const Key &key, Value &value) const override;
29 
30     int PutMetaData(const Key &key, const Value &value) override;
31 
32     int ChkSchema(const TableName &tableName) override;
33 
34     int SetCloudDbSchema(const DataBaseSchema &schema) override;
35 
36     int GetCloudDbSchema(std::shared_ptr<DataBaseSchema> &cloudSchema) override;
37 
38     int GetCloudTableSchema(const TableName &tableName, TableSchema &tableSchema) override;
39 
40     int StartTransaction(TransactType type) override;
41 
42     int Commit() override;
43 
44     int Rollback() override;
45 
46     int GetUploadCount(const QuerySyncObject &query, const Timestamp &timestamp, bool isCloudForcePush,
47         bool isCompensatedTask, int64_t &count) override;
48 
49     int GetAllUploadCount(const QuerySyncObject &query, const std::vector<Timestamp> &timestampVec,
50         bool isCloudForcePush, bool isCompensatedTask, int64_t &count) override;
51 
52     int GetCloudData(const TableSchema &tableSchema, const QuerySyncObject &object, const Timestamp &beginTime,
53         ContinueToken &continueStmtToken, CloudSyncData &cloudDataResult) override;
54 
55     int GetCloudDataNext(ContinueToken &continueStmtToken, CloudSyncData &cloudDataResult) override;
56 
57     int GetCloudGid(const TableSchema &tableSchema, const QuerySyncObject &querySyncObject, bool isCloudForcePush,
58         bool isCompensatedTask, std::vector<std::string> &cloudGid) override;
59 
60     int ReleaseCloudDataToken(ContinueToken &continueStmtToken) override;
61 
62     int GetInfoByPrimaryKeyOrGid(const std::string &tableName, const VBucket &vBucket, DataInfoWithLog &dataInfoWithLog,
63         VBucket &assetInfo) override;
64 
65     int PutCloudSyncData(const std::string &tableName, DownloadData &downloadData) override;
66 
67     void TriggerObserverAction(const std::string &deviceName, ChangedData &&changedData, bool isChangedData) override;
68 
69     int FillCloudAssetForDownload(const std::string &tableName, VBucket &asset, bool isDownloadSuccess) override;
70 
71     int SetLogTriggerStatus(bool status) override;
72     int SetCursorIncFlag(bool status) override;
73 
74     int FillCloudLogAndAsset(OpType opType, const CloudSyncData &data, bool fillAsset, bool ignoreEmptyGid) override;
75 
76     std::string GetIdentify() const override;
77 
78     int CheckQueryValid(const QuerySyncObject &query) override;
79 
80     bool IsSharedTable(const std::string &tableName) override;
81 
82     void SetUser(const std::string &user) override;
83 
84     int SetCloudDbSchema(const std::map<std::string, DataBaseSchema> &schema);
85 
86     void RegisterObserverAction(const KvStoreObserver *observer, const ObserverAction &action);
87 
88     void UnRegisterObserverAction(const KvStoreObserver *observer);
89 
90     int GetCloudVersion(const std::string &device, std::map<std::string, std::string> &versionMap);
91 
92     std::pair<int, CloudSyncData> GetLocalCloudVersion() override;
93 
94     void SetCloudSyncConfig(const CloudSyncConfig &config);
95 
96     CloudSyncConfig GetCloudSyncConfig() const override;
97 
98     std::map<std::string, DataBaseSchema> GetDataBaseSchemas();
99 
100     void ReleaseUploadRecord(const std::string &tableName, const CloudWaterType &type, Timestamp localMark) override;
101 
102     bool IsTagCloudUpdateLocal(const LogInfo &localInfo, const LogInfo &cloudInfo,
103         SingleVerConflictResolvePolicy policy) override;
104 
105     int GetCompensatedSyncQuery(std::vector<QuerySyncObject> &syncQuery, std::vector<std::string> &users) override;
106 
107 private:
108     std::pair<sqlite3 *, bool> GetTransactionDbHandleAndMemoryStatus();
109 
110     static void FillTimestamp(Timestamp rawSystemTime, Timestamp virtualTime, CloudSyncBatch &syncBatch);
111 
112     static void FilterCloudVersionPrefixKey(std::vector<std::vector<Type>> &changeValList);
113 
114     bool CheckSchema(std::map<std::string, DataBaseSchema> schema);
115 
116     KvStorageHandle *storageHandle_;
117 
118     std::mutex schemaMutex_;
119     std::map<std::string, DataBaseSchema> schema_;
120 
121     mutable std::mutex transactionMutex_;
122     SQLiteSingleVerStorageExecutor *transactionHandle_;
123 
124     std::string user_;
125 
126     std::mutex observerMapMutex_;
127     std::map<const KvStoreObserver *, ObserverAction> cloudObserverMap_;
128 
129     mutable std::mutex configMutex_;
130     CloudSyncConfig config_;
131 
132     CloudUploadRecorder recorder_;
133 };
134 }
135 #endif // SQLITE_CLOUD_STORE_H