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 specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #define LOG_TAG "ValueProxy"
17 #include "value_proxy.h"
18 namespace OHOS::DistributedRdb {
19 using namespace OHOS::DistributedData;
Convert(DistributedData::Value && value)20 ValueProxy::Value ValueProxy::Convert(DistributedData::Value &&value)
21 {
22 Value proxy;
23 DistributedData::Convert(std::move(value), proxy.value_);
24 return proxy;
25 }
26
Convert(NativeRdb::ValueObject && value)27 ValueProxy::Value ValueProxy::Convert(NativeRdb::ValueObject &&value)
28 {
29 Value proxy;
30 DistributedData::Convert(std::move(value.value), proxy.value_);
31 return proxy;
32 }
33
Convert(DistributedDB::Type && value)34 ValueProxy::Value ValueProxy::Convert(DistributedDB::Type &&value)
35 {
36 Value proxy;
37 DistributedData::Convert(std::move(value), proxy.value_);
38 return proxy;
39 }
40
Convert(DistributedData::Values && values)41 ValueProxy::Values ValueProxy::Convert(DistributedData::Values &&values)
42 {
43 Values proxy;
44 proxy.value_.reserve(values.size());
45 for (auto &value : values) {
46 proxy.value_.emplace_back(Convert(std::move(value)));
47 }
48 return proxy;
49 }
50
Convert(std::vector<NativeRdb::ValueObject> && values)51 ValueProxy::Values ValueProxy::Convert(std::vector<NativeRdb::ValueObject> &&values)
52 {
53 Values proxy;
54 proxy.value_.reserve(values.size());
55 for (auto &value : values) {
56 proxy.value_.emplace_back(Convert(std::move(value)));
57 }
58 return proxy;
59 }
60
Convert(DistributedData::VBucket && bucket)61 ValueProxy::Bucket ValueProxy::Convert(DistributedData::VBucket &&bucket)
62 {
63 ValueProxy::Bucket proxy;
64 for (auto &[key, value] : bucket) {
65 proxy.value_.insert_or_assign(key, Convert(std::move(value)));
66 }
67 return proxy;
68 }
69
Convert(NativeRdb::ValuesBucket && bucket)70 ValueProxy::Bucket ValueProxy::Convert(NativeRdb::ValuesBucket &&bucket)
71 {
72 ValueProxy::Bucket proxy;
73 for (auto &[key, value] : bucket.values_) {
74 proxy.value_.insert_or_assign(key, Convert(std::move(value)));
75 }
76 return proxy;
77 }
78
Convert(DistributedDB::VBucket && bucket)79 ValueProxy::Bucket ValueProxy::Convert(DistributedDB::VBucket &&bucket)
80 {
81 ValueProxy::Bucket proxy;
82 for (auto &[key, value] : bucket) {
83 proxy.value_.insert_or_assign(key, Convert(std::move(value)));
84 }
85 return proxy;
86 }
87
Convert(std::vector<NativeRdb::ValuesBucket> && buckets)88 ValueProxy::Buckets ValueProxy::Convert(std::vector<NativeRdb::ValuesBucket> &&buckets)
89 {
90 ValueProxy::Buckets proxy;
91 proxy.value_.reserve(buckets.size());
92 for (auto &bucket : buckets) {
93 proxy.value_.emplace_back(Convert(std::move(bucket)));
94 }
95 return proxy;
96 }
97
Convert(std::vector<DistributedDB::VBucket> && buckets)98 ValueProxy::Buckets ValueProxy::Convert(std::vector<DistributedDB::VBucket> &&buckets)
99 {
100 ValueProxy::Buckets proxy;
101 proxy.value_.reserve(buckets.size());
102 for (auto &bucket : buckets) {
103 proxy.value_.emplace_back(Convert(std::move(bucket)));
104 }
105 return proxy;
106 }
107
Convert(DistributedData::VBuckets && buckets)108 ValueProxy::Buckets ValueProxy::Convert(DistributedData::VBuckets &&buckets)
109 {
110 ValueProxy::Buckets proxy;
111 proxy.value_.reserve(buckets.size());
112 for (auto &bucket : buckets) {
113 proxy.value_.emplace_back(Convert(std::move(bucket)));
114 }
115 return proxy;
116 }
117
Convert(DistributedDB::VariantData && value)118 ValueProxy::Value ValueProxy::Convert(DistributedDB::VariantData &&value)
119 {
120 Value proxy;
121 DistributedData::Convert(std::move(value), proxy.value_);
122 return proxy;
123 }
124
Convert(std::map<std::string,DistributedDB::VariantData> && value)125 ValueProxy::Bucket ValueProxy::Convert(std::map<std::string, DistributedDB::VariantData> &&value)
126 {
127 ValueProxy::Bucket proxy;
128 for (auto &[key, value] : value) {
129 proxy.value_.insert_or_assign(key, Convert(std::move(value)));
130 }
131 return proxy;
132 }
133
Asset(DistributedData::Asset asset)134 ValueProxy::Asset::Asset(DistributedData::Asset asset)
135 {
136 asset_ = std::move(asset);
137 }
138
Asset(NativeRdb::AssetValue asset)139 ValueProxy::Asset::Asset(NativeRdb::AssetValue asset)
140 {
141 asset_ = DistributedData::Asset { .version = asset.version,
142 .status = asset.status,
143 .expiresTime = asset.expiresTime,
144 .id = std::move(asset.id),
145 .name = std::move(asset.name),
146 .uri = std::move(asset.uri),
147 .createTime = std::move(asset.createTime),
148 .modifyTime = std::move(asset.modifyTime),
149 .size = std::move(asset.size),
150 .hash = std::move(asset.hash),
151 .path = std::move(asset.path) };
152 }
153
Asset(DistributedDB::Asset asset)154 ValueProxy::Asset::Asset(DistributedDB::Asset asset)
155 {
156 asset_ = DistributedData::Asset { .version = asset.version,
157 .status = ConvertToDataStatus(asset),
158 .expiresTime = DistributedData::Asset::NO_EXPIRES_TIME,
159 .id = std::move(asset.assetId),
160 .name = std::move(asset.name),
161 .uri = std::move(asset.uri),
162 .createTime = std::move(asset.createTime),
163 .modifyTime = std::move(asset.modifyTime),
164 .size = std::move(asset.size),
165 .hash = std::move(asset.hash),
166 .path = std::move(asset.subpath) };
167 }
168
operator =(const Asset & proxy)169 ValueProxy::Asset &ValueProxy::Asset::operator=(const Asset &proxy)
170 {
171 if (this == &proxy) {
172 return *this;
173 }
174 asset_ = proxy.asset_;
175 return *this;
176 }
177
operator =(Asset && proxy)178 ValueProxy::Asset &ValueProxy::Asset::operator=(Asset &&proxy) noexcept
179 {
180 if (this == &proxy) {
181 return *this;
182 }
183 asset_ = std::move(proxy);
184 return *this;
185 }
186
operator NativeRdb::AssetValue()187 ValueProxy::Asset::operator NativeRdb::AssetValue()
188 {
189 return NativeRdb::AssetValue { .version = asset_.version,
190 .status = asset_.status,
191 .expiresTime = asset_.expiresTime,
192 .id = std::move(asset_.id),
193 .name = std::move(asset_.name),
194 .uri = std::move(asset_.uri),
195 .createTime = std::move(asset_.createTime),
196 .modifyTime = std::move(asset_.modifyTime),
197 .size = std::move(asset_.size),
198 .hash = std::move(asset_.hash),
199 .path = std::move(asset_.path) };
200 }
201
operator DistributedData::Asset()202 ValueProxy::Asset::operator DistributedData::Asset()
203 {
204 return std::move(asset_);
205 }
206
operator DistributedDB::Asset()207 ValueProxy::Asset::operator DistributedDB::Asset()
208 {
209 return DistributedDB::Asset { .version = asset_.version,
210 .name = std::move(asset_.name),
211 .assetId = std::move(asset_.id),
212 .subpath = std::move(asset_.path),
213 .uri = std::move(asset_.uri),
214 .modifyTime = std::move(asset_.modifyTime),
215 .createTime = std::move(asset_.createTime),
216 .size = std::move(asset_.size),
217 .hash = std::move(asset_.hash),
218 .status = ConvertToDBStatus(asset_) };
219 }
220
ConvertToDataStatus(const DistributedDB::Asset & asset)221 uint32_t ValueProxy::Asset::ConvertToDataStatus(const DistributedDB::Asset &asset)
222 {
223 if (asset.status == DistributedDB::AssetStatus::DOWNLOADING) {
224 if (asset.flag == static_cast<uint32_t>(DistributedDB::AssetOpType::DELETE)) {
225 return DistributedData::Asset::STATUS_DELETE;
226 }
227 return DistributedData::Asset::STATUS_DOWNLOADING;
228 } else if (asset.status == DistributedDB::AssetStatus::ABNORMAL) {
229 return DistributedData::Asset::STATUS_ABNORMAL;
230 } else {
231 switch (asset.flag) {
232 case static_cast<uint32_t>(DistributedDB::AssetOpType::INSERT):
233 return DistributedData::Asset::STATUS_INSERT;
234 case static_cast<uint32_t>(DistributedDB::AssetOpType::UPDATE):
235 return DistributedData::Asset::STATUS_UPDATE;
236 case static_cast<uint32_t>(DistributedDB::AssetOpType::DELETE):
237 return DistributedData::Asset::STATUS_DELETE;
238 default:
239 return DistributedData::Asset::STATUS_NORMAL;
240 }
241 }
242 return DistributedData::Asset::STATUS_UNKNOWN;
243 }
244
ConvertToDBStatus(const DistributedData::Asset & asset)245 uint32_t ValueProxy::Asset::ConvertToDBStatus(const DistributedData::Asset &asset)
246 {
247 switch (asset.status) {
248 case DistributedData::Asset::STATUS_NORMAL:
249 return static_cast<uint32_t>(DistributedDB::AssetStatus::NORMAL);
250 case DistributedData::Asset::STATUS_ABNORMAL:
251 return static_cast<uint32_t>(DistributedDB::AssetStatus::ABNORMAL);
252 case DistributedData::Asset::STATUS_INSERT:
253 return static_cast<uint32_t>(DistributedDB::AssetStatus::INSERT);
254 case DistributedData::Asset::STATUS_UPDATE:
255 return static_cast<uint32_t>(DistributedDB::AssetStatus::UPDATE);
256 case DistributedData::Asset::STATUS_DELETE:
257 return static_cast<uint32_t>(DistributedDB::AssetStatus::DELETE);
258 case DistributedData::Asset::STATUS_DOWNLOADING:
259 return static_cast<uint32_t>(DistributedDB::AssetStatus::DOWNLOADING);
260 default:
261 return static_cast<uint32_t>(DistributedDB::AssetStatus::NORMAL);
262 }
263 }
264
Assets(DistributedData::Assets assets)265 ValueProxy::Assets::Assets(DistributedData::Assets assets)
266 {
267 assets_.clear();
268 assets_.reserve(assets.size());
269 for (auto &asset : assets) {
270 assets_.emplace_back(std::move(asset));
271 }
272 }
273
Assets(NativeRdb::ValueObject::Assets assets)274 ValueProxy::Assets::Assets(NativeRdb::ValueObject::Assets assets)
275 {
276 assets_.clear();
277 assets_.reserve(assets.size());
278 for (auto &asset : assets) {
279 assets_.emplace_back(std::move(asset));
280 }
281 }
282
Assets(DistributedDB::Assets assets)283 ValueProxy::Assets::Assets(DistributedDB::Assets assets)
284 {
285 assets_.clear();
286 assets_.reserve(assets.size());
287 for (auto &asset : assets) {
288 assets_.emplace_back(std::move(asset));
289 }
290 }
291
operator =(const Assets & proxy)292 ValueProxy::Assets &ValueProxy::Assets::operator=(const Assets &proxy)
293 {
294 if (this == &proxy) {
295 return *this;
296 }
297 assets_ = proxy.assets_;
298 return *this;
299 }
300
operator =(Assets && proxy)301 ValueProxy::Assets &ValueProxy::Assets::operator=(Assets &&proxy) noexcept
302 {
303 if (this == &proxy) {
304 return *this;
305 }
306 assets_ = std::move(proxy.assets_);
307 return *this;
308 }
309
operator NativeRdb::ValueObject::Assets()310 ValueProxy::Assets::operator NativeRdb::ValueObject::Assets()
311 {
312 NativeRdb::ValueObject::Assets assets;
313 assets.reserve(assets_.size());
314 for (auto &asset : assets_) {
315 assets.push_back(std::move(asset));
316 }
317 return assets;
318 }
319
operator DistributedData::Assets()320 ValueProxy::Assets::operator DistributedData::Assets()
321 {
322 DistributedData::Assets assets;
323 assets.reserve(assets_.size());
324 for (auto &asset : assets_) {
325 assets.push_back(std::move(asset));
326 }
327 return assets;
328 }
329
operator DistributedDB::Assets()330 ValueProxy::Assets::operator DistributedDB::Assets()
331 {
332 DistributedDB::Assets assets;
333 assets.reserve(assets_.size());
334 for (auto &asset : assets_) {
335 assets.push_back(std::move(asset));
336 }
337 return assets;
338 }
339
operator =(ValueProxy::Value && value)340 ValueProxy::Value &ValueProxy::Value::operator=(ValueProxy::Value &&value) noexcept
341 {
342 if (this == &value) {
343 return *this;
344 }
345 value_ = std::move(value.value_);
346 return *this;
347 }
348
operator NativeRdb::ValueObject()349 ValueProxy::Value::operator NativeRdb::ValueObject()
350 {
351 NativeRdb::ValueObject object;
352 DistributedData::Convert(std::move(value_), object.value);
353 return object;
354 }
355
operator DistributedData::Value()356 ValueProxy::Value::operator DistributedData::Value()
357 {
358 DistributedData::Value value;
359 DistributedData::Convert(std::move(value_), value);
360 return value;
361 }
362
operator DistributedDB::Type()363 ValueProxy::Value::operator DistributedDB::Type()
364 {
365 DistributedDB::Type value;
366 DistributedData::Convert(std::move(value_), value);
367 return value;
368 }
369
operator =(ValueProxy::Values && values)370 ValueProxy::Values &ValueProxy::Values::operator=(ValueProxy::Values &&values) noexcept
371 {
372 if (this == &values) {
373 return *this;
374 }
375 value_ = std::move(values.value_);
376 return *this;
377 }
378
operator =(Bucket && bucket)379 ValueProxy::Bucket &ValueProxy::Bucket::operator=(Bucket &&bucket) noexcept
380 {
381 if (this == &bucket) {
382 return *this;
383 }
384 value_ = std::move(bucket.value_);
385 return *this;
386 }
387
operator NativeRdb::ValuesBucket()388 ValueProxy::Bucket::operator NativeRdb::ValuesBucket()
389 {
390 NativeRdb::ValuesBucket bucket;
391 for (auto &[key, value] : value_) {
392 bucket.values_.insert_or_assign(key, std::move(value));
393 }
394 value_.clear();
395 return bucket;
396 }
397
operator =(Buckets && buckets)398 ValueProxy::Buckets &ValueProxy::Buckets::operator=(Buckets &&buckets) noexcept
399 {
400 if (this == &buckets) {
401 return *this;
402 }
403 value_ = std::move(buckets.value_);
404 return *this;
405 }
406 } // namespace OHOS::DistributedRdb