1 /*
2  * Copyright (c) 2023-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 
16 #ifndef OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_GENERAL_STORE_H
17 #define OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_GENERAL_STORE_H
18 #include <errors.h>
19 #include <functional>
20 #include <memory>
21 #include <set>
22 
23 #include "executor_pool.h"
24 #include "snapshot/snapshot.h"
25 #include "store/cursor.h"
26 #include "store/general_value.h"
27 #include "store/general_watcher.h"
28 
29 namespace OHOS::DistributedData {
30 class CloudDB;
31 class AssetLoader;
32 struct Database;
33 class GeneralStore {
34 public:
35     using Watcher = GeneralWatcher;
36     using DetailAsync = GenAsync;
37     using Devices = std::vector<std::string>;
38     using Executor = ExecutorPool;
39     enum SyncMode {
40         NEARBY_BEGIN,
41         NEARBY_PUSH = NEARBY_BEGIN,
42         NEARBY_PULL,
43         NEARBY_PULL_PUSH,
44         NEARBY_END,
45         CLOUD_BEGIN = 4,
46         CLOUD_TIME_FIRST = CLOUD_BEGIN,
47         CLOUD_NATIVE_FIRST,
48         CLOUD_ClOUD_FIRST,
49         CLOUD_END,
50         NEARBY_SUBSCRIBE_REMOTE,
51         NEARBY_UNSUBSCRIBE_REMOTE,
52         MODE_BUTT,
53     };
54     enum HighMode : uint32_t {
55         MANUAL_SYNC_MODE = 0x00000,
56         AUTO_SYNC_MODE = 0x10000,
57     };
58     enum CleanMode {
59         NEARBY_DATA = 0,
60         CLOUD_DATA,
61         CLOUD_INFO,
62         LOCAL_DATA,
63         CLEAN_MODE_BUTT
64     };
65 
66     enum Area : int32_t {
67         EL0,
68         EL1,
69         EL2,
70         EL3,
71         EL4,
72         EL5
73     };
74 
MixMode(uint32_t syncMode,uint32_t highMode)75     static inline uint32_t MixMode(uint32_t syncMode, uint32_t highMode)
76     {
77         return syncMode | highMode;
78     }
79 
GetSyncMode(uint32_t mixMode)80     static inline uint32_t GetSyncMode(uint32_t mixMode)
81     {
82         return mixMode & 0xFFFF;
83     }
84 
GetHighMode(uint32_t mixMode)85     static inline uint32_t GetHighMode(uint32_t mixMode)
86     {
87         return mixMode & ~0xFFFF;
88     }
89 
90     struct BindInfo {
91         BindInfo(std::shared_ptr<CloudDB> db = nullptr, std::shared_ptr<AssetLoader> loader = nullptr)
92             : db_(std::move(db)), loader_(std::move(loader))
93         {
94         }
95 
96         bool operator<(const BindInfo &bindInfo) const
97         {
98             return db_ < bindInfo.db_;
99         }
100 
101         std::shared_ptr<CloudDB> db_;
102         std::shared_ptr<AssetLoader> loader_;
103     };
104 
105     struct CloudConfig {
106         int32_t maxNumber = 30;
107         int32_t maxSize = 1024 * 512 * 3; // 1.5M
108         int32_t maxRetryConflictTimes = 3;     // default max retry 3 times when version conflict
109     };
110 
111     struct StoreConfig {
112         bool enableCloud_ = false;
113     };
114 
115     enum ErrOffset {
116         DB_MODE_ID = 1,
117         CLOUD_MODE_ID = 10,
118     };
119     static const int32_t DB_ERR_OFFSET = ErrCodeOffset(SUBSYS_DISTRIBUTEDDATAMNG, DB_MODE_ID);
120     static const int32_t CLOUD_ERR_OFFSET = ErrCodeOffset(SUBSYS_DISTRIBUTEDDATAMNG, CLOUD_MODE_ID);
121 
122     virtual ~GeneralStore() = default;
123 
124     virtual void SetExecutor(std::shared_ptr<Executor> executor) = 0;
125 
126     virtual int32_t Bind(Database &database, const std::map<uint32_t, BindInfo> &bindInfos,
127         const CloudConfig &config) = 0;
128 
129     virtual bool IsBound() = 0;
130 
131     virtual int32_t Execute(const std::string &table, const std::string &sql) = 0;
132 
133     virtual int32_t SetDistributedTables(
134         const std::vector<std::string> &tables, int type, const std::vector<Reference> &references) = 0;
135 
136     virtual int32_t SetTrackerTable(const std::string &tableName, const std::set<std::string> &trackerColNames,
137         const std::string &extendColName, bool isForceUpgrade) = 0;
138 
139     virtual int32_t Insert(const std::string &table, VBuckets &&values) = 0;
140 
141     virtual int32_t Update(const std::string &table, const std::string &setSql, Values &&values,
142         const std::string &whereSql, Values &&conditions) = 0;
143 
144     virtual int32_t Replace(const std::string &table, VBucket &&value) = 0;
145 
146     virtual int32_t Delete(const std::string &table, const std::string &sql, Values &&args) = 0;
147 
148     virtual std::pair<int32_t, std::shared_ptr<Cursor>> Query(const std::string &table, const std::string &sql,
149         Values &&args) = 0;
150 
151     virtual std::pair<int32_t, std::shared_ptr<Cursor>> Query(const std::string &table, GenQuery &query) = 0;
152 
153     virtual std::pair<int32_t, int32_t> Sync(const Devices &devices, GenQuery &query,
154         DetailAsync async, const SyncParam &syncParm) = 0;
155 
156     virtual std::pair<int32_t, std::shared_ptr<Cursor>> PreSharing(GenQuery &query) = 0;
157 
158     virtual int32_t Clean(const std::vector<std::string> &devices, int32_t mode, const std::string &tableName) = 0;
159 
160     virtual int32_t Watch(int32_t origin, Watcher &watcher) = 0;
161 
162     virtual int32_t Unwatch(int32_t origin, Watcher &watcher) = 0;
163 
164     virtual int32_t RegisterDetailProgressObserver(DetailAsync async) = 0;
165 
166     virtual int32_t UnregisterDetailProgressObserver() = 0;
167 
168     virtual int32_t Close(bool isForce = false) = 0;
169 
170     virtual int32_t AddRef() = 0;
171 
172     virtual int32_t Release() = 0;
173 
174     virtual int32_t BindSnapshots(std::shared_ptr<std::map<std::string, std::shared_ptr<Snapshot>>> bindAssets) = 0;
175 
176     virtual int32_t MergeMigratedData(const std::string &tableName, VBuckets &&values) = 0;
177 
178     virtual int32_t CleanTrackerData(const std::string &tableName, int64_t cursor) = 0;
179 
180     virtual std::vector<std::string> GetWaterVersion(const std::string &deviceId) = 0;
181 
182     virtual void SetEqualIdentifier(const std::string &appId, const std::string &storeId, std::string account = "") {};
183 
SetConfig(const StoreConfig & storeConfig)184     virtual void SetConfig(const StoreConfig &storeConfig) {};
185 
186     virtual std::pair<int32_t, uint32_t> LockCloudDB() = 0;
187 
188     virtual int32_t UnLockCloudDB() = 0;
189 };
190 } // namespace OHOS::DistributedData
191 #endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_GENERAL_STORE_H