• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         ASSETS_SYNC_MODE = 0x20000,
58     };
59     enum CleanMode {
60         NEARBY_DATA = 0,
61         CLOUD_DATA,
62         CLOUD_INFO,
63         LOCAL_DATA,
64         CLEAN_WATER,
65         CLEAN_MODE_BUTT
66     };
67 
68     enum Area : int32_t {
69         EL0,
70         EL1,
71         EL2,
72         EL3,
73         EL4,
74         EL5
75     };
76 
MixMode(uint32_t syncMode,uint32_t highMode)77     static inline uint32_t MixMode(uint32_t syncMode, uint32_t highMode)
78     {
79         return syncMode | highMode;
80     }
81 
GetSyncMode(uint32_t mixMode)82     static inline uint32_t GetSyncMode(uint32_t mixMode)
83     {
84         return mixMode & 0xFFFF;
85     }
86 
GetHighMode(uint32_t mixMode)87     static inline uint32_t GetHighMode(uint32_t mixMode)
88     {
89         return mixMode & ~0xFFFF;
90     }
91 
GetPriorityLevel(uint32_t highMode)92     static inline uint32_t GetPriorityLevel(uint32_t highMode)
93     {
94         // shift right 16 bits
95         return highMode >> 16;
96     }
97 
98     struct BindInfo {
99         BindInfo(std::shared_ptr<CloudDB> db = nullptr, std::shared_ptr<AssetLoader> loader = nullptr)
db_BindInfo100             : db_(std::move(db)), loader_(std::move(loader))
101         {
102         }
103 
104         bool operator<(const BindInfo &bindInfo) const
105         {
106             return db_ < bindInfo.db_;
107         }
108 
109         std::shared_ptr<CloudDB> db_;
110         std::shared_ptr<AssetLoader> loader_;
111     };
112 
113     struct CloudConfig {
114         int32_t maxNumber = 30;
115         int32_t maxSize = 1024 * 512 * 3; // 1.5M
116         int32_t maxRetryConflictTimes = 3;     // default max retry 3 times when version conflict
117         bool isSupportEncrypt = false;
118     };
119 
120     enum class DistributedTableMode : int {
121         COLLABORATION = 0, // Save all devices data in user table
122         SPLIT_BY_DEVICE // Save device data in each table split by device
123     };
124 
125     struct StoreConfig {
126         bool enableCloud_ = false;
127         std::optional<DistributedTableMode> tableMode;
128     };
129 
130     enum ErrOffset {
131         DB_MODE_ID = 1,
132         CLOUD_MODE_ID = 10,
133     };
134     static const int32_t DB_ERR_OFFSET = ErrCodeOffset(SUBSYS_DISTRIBUTEDDATAMNG, DB_MODE_ID);
135     static const int32_t CLOUD_ERR_OFFSET = ErrCodeOffset(SUBSYS_DISTRIBUTEDDATAMNG, CLOUD_MODE_ID);
136 
137     virtual ~GeneralStore() = default;
138 
139     virtual void SetExecutor(std::shared_ptr<Executor> executor) = 0;
140 
141     virtual int32_t Bind(const Database &database, const std::map<uint32_t, BindInfo> &bindInfos,
142         const CloudConfig &config) = 0;
143 
144     virtual bool IsBound(uint32_t user) = 0;
145 
146     virtual int32_t Execute(const std::string &table, const std::string &sql) = 0;
147 
148     virtual int32_t SetDistributedTables(
149         const std::vector<std::string> &tables, int type, const std::vector<Reference> &references) = 0;
150 
151     virtual int32_t SetTrackerTable(const std::string &tableName, const std::set<std::string> &trackerColNames,
152         const std::set<std::string> &extendColNames, bool isForceUpgrade) = 0;
153 
154     virtual int32_t Insert(const std::string &table, VBuckets &&values) = 0;
155 
156     virtual int32_t Update(const std::string &table, const std::string &setSql, Values &&values,
157         const std::string &whereSql, Values &&conditions) = 0;
158 
159     virtual int32_t Replace(const std::string &table, VBucket &&value) = 0;
160 
161     virtual int32_t Delete(const std::string &table, const std::string &sql, Values &&args) = 0;
162 
163     virtual std::pair<int32_t, std::shared_ptr<Cursor>> Query(const std::string &table, const std::string &sql,
164         Values &&args) = 0;
165 
166     virtual std::pair<int32_t, std::shared_ptr<Cursor>> Query(const std::string &table, GenQuery &query) = 0;
167 
168     virtual std::pair<int32_t, int32_t> Sync(const Devices &devices, GenQuery &query,
169         DetailAsync async, const SyncParam &syncParam) = 0;
170 
171     virtual std::pair<int32_t, std::shared_ptr<Cursor>> PreSharing(GenQuery &query) = 0;
172 
173     virtual int32_t Clean(const std::vector<std::string> &devices, int32_t mode, const std::string &tableName) = 0;
174 
175     virtual int32_t Watch(int32_t origin, Watcher &watcher) = 0;
176 
177     virtual int32_t Unwatch(int32_t origin, Watcher &watcher) = 0;
178 
179     virtual int32_t RegisterDetailProgressObserver(DetailAsync async) = 0;
180 
181     virtual int32_t UnregisterDetailProgressObserver() = 0;
182 
183     virtual int32_t Close(bool isForce = false) = 0;
184 
185     virtual int32_t AddRef() = 0;
186 
187     virtual int32_t Release() = 0;
188 
189     virtual int32_t BindSnapshots(std::shared_ptr<std::map<std::string, std::shared_ptr<Snapshot>>> bindAssets) = 0;
190 
191     virtual int32_t MergeMigratedData(const std::string &tableName, VBuckets &&values) = 0;
192 
193     virtual int32_t CleanTrackerData(const std::string &tableName, int64_t cursor) = 0;
194 
195     virtual void SetEqualIdentifier(const std::string &appId, const std::string &storeId, std::string account = "") {};
196 
SetConfig(const StoreConfig & storeConfig)197     virtual void SetConfig(const StoreConfig &storeConfig) {};
198 
199     virtual std::pair<int32_t, uint32_t> LockCloudDB() = 0;
200 
201     virtual int32_t UnLockCloudDB() = 0;
202 
UpdateDBStatus()203     virtual int32_t UpdateDBStatus()
204     {
205         return 0;
206     }
207 };
208 } // namespace OHOS::DistributedData
209 #endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_GENERAL_STORE_H