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::DistributedData {
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(CommonType::Value && value)34 ValueProxy::Value ValueProxy::Convert(CommonType::Value &&value)
35 {
36 Value proxy;
37 DistributedData::Convert(std::move(value), proxy.value_);
38 return proxy;
39 }
40
Convert(DistributedDB::Type && value)41 ValueProxy::Value ValueProxy::Convert(DistributedDB::Type &&value)
42 {
43 Value proxy;
44 DistributedData::Convert(std::move(value), proxy.value_);
45 return proxy;
46 }
47
Convert(DistributedData::Values && values)48 ValueProxy::Values ValueProxy::Convert(DistributedData::Values &&values)
49 {
50 Values proxy;
51 proxy.value_.reserve(values.size());
52 for (auto &value : values) {
53 proxy.value_.emplace_back(Convert(std::move(value)));
54 }
55 return proxy;
56 }
57
Convert(std::vector<NativeRdb::ValueObject> && values)58 ValueProxy::Values ValueProxy::Convert(std::vector<NativeRdb::ValueObject> &&values)
59 {
60 Values proxy;
61 proxy.value_.reserve(values.size());
62 for (auto &value : values) {
63 proxy.value_.emplace_back(Convert(std::move(value)));
64 }
65 return proxy;
66 }
67
Convert(std::vector<CommonType::Value> && values)68 ValueProxy::Values ValueProxy::Convert(std::vector<CommonType::Value> &&values)
69 {
70 Values proxy;
71 proxy.value_.reserve(values.size());
72 for (auto &value : values) {
73 proxy.value_.emplace_back(Convert(std::move(value)));
74 }
75 return proxy;
76 }
77
Convert(std::vector<DistributedDB::Type> && values)78 ValueProxy::Values ValueProxy::Convert(std::vector<DistributedDB::Type> &&values)
79 {
80 Values proxy;
81 proxy.value_.reserve(values.size());
82 for (auto &value : values) {
83 proxy.value_.emplace_back(Convert(std::move(value)));
84 }
85 return proxy;
86 }
87
Convert(DistributedData::VBucket && bucket)88 ValueProxy::Bucket ValueProxy::Convert(DistributedData::VBucket &&bucket)
89 {
90 ValueProxy::Bucket proxy;
91 for (auto &[key, value] : bucket) {
92 proxy.value_.insert_or_assign(key, Convert(std::move(value)));
93 }
94 return proxy;
95 }
96
Convert(NativeRdb::ValuesBucket && bucket)97 ValueProxy::Bucket ValueProxy::Convert(NativeRdb::ValuesBucket &&bucket)
98 {
99 ValueProxy::Bucket proxy;
100 for (auto &[key, value] : bucket.values_) {
101 proxy.value_.insert_or_assign(key, Convert(std::move(value)));
102 }
103 return proxy;
104 }
105
Convert(CommonType::ValuesBucket && bucket)106 ValueProxy::Bucket ValueProxy::Convert(CommonType::ValuesBucket &&bucket)
107 {
108 ValueProxy::Bucket proxy;
109 for (auto &[key, value] : bucket) {
110 proxy.value_.insert_or_assign(key, Convert(std::move(value)));
111 }
112 return proxy;
113 }
114
Convert(DistributedDB::VBucket && bucket)115 ValueProxy::Bucket ValueProxy::Convert(DistributedDB::VBucket &&bucket)
116 {
117 ValueProxy::Bucket proxy;
118 for (auto &[key, value] : bucket) {
119 proxy.value_.insert_or_assign(key, Convert(std::move(value)));
120 }
121 return proxy;
122 }
123
Convert(std::vector<NativeRdb::ValuesBucket> && buckets)124 ValueProxy::Buckets ValueProxy::Convert(std::vector<NativeRdb::ValuesBucket> &&buckets)
125 {
126 ValueProxy::Buckets proxy;
127 proxy.value_.reserve(buckets.size());
128 for (auto &bucket : buckets) {
129 proxy.value_.emplace_back(Convert(std::move(bucket)));
130 }
131 return proxy;
132 }
133
Convert(std::vector<CommonType::ValuesBucket> && buckets)134 ValueProxy::Buckets ValueProxy::Convert(std::vector<CommonType::ValuesBucket> &&buckets)
135 {
136 ValueProxy::Buckets proxy;
137 proxy.value_.reserve(buckets.size());
138 for (auto &bucket : buckets) {
139 proxy.value_.emplace_back(Convert(std::move(bucket)));
140 }
141 return proxy;
142 }
143
Convert(std::vector<DistributedDB::VBucket> && buckets)144 ValueProxy::Buckets ValueProxy::Convert(std::vector<DistributedDB::VBucket> &&buckets)
145 {
146 ValueProxy::Buckets proxy;
147 proxy.value_.reserve(buckets.size());
148 for (auto &bucket : buckets) {
149 proxy.value_.emplace_back(Convert(std::move(bucket)));
150 }
151 return proxy;
152 }
153
Convert(DistributedData::VBuckets && buckets)154 ValueProxy::Buckets ValueProxy::Convert(DistributedData::VBuckets &&buckets)
155 {
156 ValueProxy::Buckets proxy;
157 proxy.value_.reserve(buckets.size());
158 for (auto &bucket : buckets) {
159 proxy.value_.emplace_back(Convert(std::move(bucket)));
160 }
161 return proxy;
162 }
163
Convert(DistributedDB::VariantData && value)164 ValueProxy::Value ValueProxy::Convert(DistributedDB::VariantData &&value)
165 {
166 Value proxy;
167 DistributedData::Convert(std::move(value), proxy.value_);
168 return proxy;
169 }
170
Convert(std::map<std::string,DistributedDB::VariantData> && value)171 ValueProxy::Bucket ValueProxy::Convert(std::map<std::string, DistributedDB::VariantData> &&value)
172 {
173 ValueProxy::Bucket proxy;
174 for (auto &[key, value] : value) {
175 proxy.value_.insert_or_assign(key, Convert(std::move(value)));
176 }
177 return proxy;
178 }
179
Asset(DistributedData::Asset asset)180 ValueProxy::Asset::Asset(DistributedData::Asset asset)
181 {
182 asset_ = std::move(asset);
183 }
184
Asset(NativeRdb::AssetValue asset)185 ValueProxy::Asset::Asset(NativeRdb::AssetValue asset)
186 {
187 asset_ = DistributedData::Asset { .version = asset.version,
188 .status = asset.status,
189 .expiresTime = asset.expiresTime,
190 .id = std::move(asset.id),
191 .name = std::move(asset.name),
192 .uri = std::move(asset.uri),
193 .createTime = std::move(asset.createTime),
194 .modifyTime = std::move(asset.modifyTime),
195 .size = std::move(asset.size),
196 .hash = std::move(asset.hash),
197 .path = std::move(asset.path) };
198 }
199
Asset(CommonType::AssetValue asset)200 ValueProxy::Asset::Asset(CommonType::AssetValue asset)
201 {
202 asset_ = DistributedData::Asset { .version = asset.version,
203 .status = asset.status,
204 .expiresTime = asset.expiresTime,
205 .id = std::move(asset.id),
206 .name = std::move(asset.name),
207 .uri = std::move(asset.uri),
208 .createTime = std::move(asset.createTime),
209 .modifyTime = std::move(asset.modifyTime),
210 .size = std::move(asset.size),
211 .hash = std::move(asset.hash),
212 .path = std::move(asset.path) };
213 }
214
Asset(DistributedDB::Asset asset)215 ValueProxy::Asset::Asset(DistributedDB::Asset asset)
216 {
217 asset_ = DistributedData::Asset { .version = asset.version,
218 .status = ConvertToDataStatus(asset),
219 .expiresTime = DistributedData::Asset::NO_EXPIRES_TIME,
220 .id = std::move(asset.assetId),
221 .name = std::move(asset.name),
222 .uri = std::move(asset.uri),
223 .createTime = std::move(asset.createTime),
224 .modifyTime = std::move(asset.modifyTime),
225 .size = std::move(asset.size),
226 .hash = std::move(asset.hash),
227 .path = std::move(asset.subpath) };
228 }
229
operator =(const Asset & proxy)230 ValueProxy::Asset &ValueProxy::Asset::operator=(const Asset &proxy)
231 {
232 if (this == &proxy) {
233 return *this;
234 }
235 asset_ = proxy.asset_;
236 return *this;
237 }
238
operator =(Asset && proxy)239 ValueProxy::Asset &ValueProxy::Asset::operator=(Asset &&proxy) noexcept
240 {
241 if (this == &proxy) {
242 return *this;
243 }
244 asset_ = std::move(proxy);
245 return *this;
246 }
247
248 ValueProxy::Asset::operator NativeRdb::AssetValue()
249 {
250 return NativeRdb::AssetValue { .version = asset_.version,
251 .status = asset_.status,
252 .expiresTime = asset_.expiresTime,
253 .id = std::move(asset_.id),
254 .name = std::move(asset_.name),
255 .uri = std::move(asset_.uri),
256 .createTime = std::move(asset_.createTime),
257 .modifyTime = std::move(asset_.modifyTime),
258 .size = std::move(asset_.size),
259 .hash = std::move(asset_.hash),
260 .path = std::move(asset_.path) };
261 }
262
263 ValueProxy::Asset::operator CommonType::AssetValue()
264 {
265 return CommonType::AssetValue { .version = asset_.version,
266 .status = asset_.status,
267 .expiresTime = asset_.expiresTime,
268 .id = std::move(asset_.id),
269 .name = std::move(asset_.name),
270 .uri = std::move(asset_.uri),
271 .createTime = std::move(asset_.createTime),
272 .modifyTime = std::move(asset_.modifyTime),
273 .size = std::move(asset_.size),
274 .hash = std::move(asset_.hash),
275 .path = std::move(asset_.path) };
276 }
277
278 ValueProxy::Asset::operator DistributedData::Asset()
279 {
280 return std::move(asset_);
281 }
282
283 ValueProxy::Asset::operator DistributedDB::Asset()
284 {
285 return DistributedDB::Asset { .version = asset_.version,
286 .name = std::move(asset_.name),
287 .assetId = std::move(asset_.id),
288 .subpath = std::move(asset_.path),
289 .uri = std::move(asset_.uri),
290 .modifyTime = std::move(asset_.modifyTime),
291 .createTime = std::move(asset_.createTime),
292 .size = std::move(asset_.size),
293 .hash = std::move(asset_.hash),
294 .status = ConvertToDBStatus(asset_.status) };
295 }
296
ConvertToDataStatus(const DistributedDB::Asset & asset)297 uint32_t ValueProxy::Asset::ConvertToDataStatus(const DistributedDB::Asset &asset)
298 {
299 auto highStatus = GetHighStatus(asset.status);
300 auto lowStatus = GetLowStatus(asset.status);
301 if (lowStatus == DistributedDB::AssetStatus::DOWNLOADING) {
302 if (asset.flag == static_cast<uint32_t>(DistributedDB::AssetOpType::DELETE)) {
303 lowStatus = DistributedData::Asset::STATUS_DELETE;
304 } else {
305 lowStatus = DistributedData::Asset::STATUS_DOWNLOADING;
306 }
307 } else if (lowStatus == DistributedDB::AssetStatus::ABNORMAL) {
308 lowStatus = DistributedData::Asset::STATUS_ABNORMAL;
309 } else {
310 switch (asset.flag) {
311 case static_cast<uint32_t>(DistributedDB::AssetOpType::INSERT):
312 lowStatus = DistributedData::Asset::STATUS_INSERT;
313 break;
314 case static_cast<uint32_t>(DistributedDB::AssetOpType::UPDATE):
315 lowStatus = DistributedData::Asset::STATUS_UPDATE;
316 break;
317 case static_cast<uint32_t>(DistributedDB::AssetOpType::DELETE):
318 lowStatus = DistributedData::Asset::STATUS_DELETE;
319 break;
320 default:
321 lowStatus = DistributedData::Asset::STATUS_NORMAL;
322 }
323 }
324 return lowStatus | highStatus;
325 }
326
ConvertToDBStatus(const uint32_t & status)327 uint32_t ValueProxy::Asset::ConvertToDBStatus(const uint32_t &status)
328 {
329 auto highStatus = GetHighStatus(status);
330 auto lowStatus = GetLowStatus(status);
331 switch (lowStatus) {
332 case DistributedData::Asset::STATUS_NORMAL:
333 lowStatus = static_cast<uint32_t>(DistributedDB::AssetStatus::NORMAL);
334 break;
335 case DistributedData::Asset::STATUS_ABNORMAL:
336 lowStatus = static_cast<uint32_t>(DistributedDB::AssetStatus::ABNORMAL);
337 break;
338 case DistributedData::Asset::STATUS_INSERT:
339 lowStatus = static_cast<uint32_t>(DistributedDB::AssetStatus::INSERT);
340 break;
341 case DistributedData::Asset::STATUS_UPDATE:
342 lowStatus = static_cast<uint32_t>(DistributedDB::AssetStatus::UPDATE);
343 break;
344 case DistributedData::Asset::STATUS_DELETE:
345 lowStatus = static_cast<uint32_t>(DistributedDB::AssetStatus::DELETE);
346 break;
347 case DistributedData::Asset::STATUS_DOWNLOADING:
348 lowStatus = static_cast<uint32_t>(DistributedDB::AssetStatus::DOWNLOADING);
349 break;
350 default:
351 lowStatus = static_cast<uint32_t>(DistributedDB::AssetStatus::NORMAL);
352 }
353 return lowStatus | highStatus;
354 }
355
Assets(DistributedData::Assets assets)356 ValueProxy::Assets::Assets(DistributedData::Assets assets)
357 {
358 assets_.clear();
359 assets_.reserve(assets.size());
360 for (auto &asset : assets) {
361 assets_.emplace_back(std::move(asset));
362 }
363 }
364
Assets(NativeRdb::ValueObject::Assets assets)365 ValueProxy::Assets::Assets(NativeRdb::ValueObject::Assets assets)
366 {
367 assets_.clear();
368 assets_.reserve(assets.size());
369 for (auto &asset : assets) {
370 assets_.emplace_back(std::move(asset));
371 }
372 }
373
Assets(CommonType::Assets assets)374 ValueProxy::Assets::Assets(CommonType::Assets assets)
375 {
376 assets_.clear();
377 assets_.reserve(assets.size());
378 for (auto &asset : assets) {
379 assets_.emplace_back(std::move(asset));
380 }
381 }
382
Assets(DistributedDB::Assets assets)383 ValueProxy::Assets::Assets(DistributedDB::Assets assets)
384 {
385 assets_.clear();
386 assets_.reserve(assets.size());
387 for (auto &asset : assets) {
388 assets_.emplace_back(std::move(asset));
389 }
390 }
391
operator =(const Assets & proxy)392 ValueProxy::Assets &ValueProxy::Assets::operator=(const Assets &proxy)
393 {
394 if (this == &proxy) {
395 return *this;
396 }
397 assets_ = proxy.assets_;
398 return *this;
399 }
400
operator =(Assets && proxy)401 ValueProxy::Assets &ValueProxy::Assets::operator=(Assets &&proxy) noexcept
402 {
403 if (this == &proxy) {
404 return *this;
405 }
406 assets_ = std::move(proxy.assets_);
407 return *this;
408 }
409
410 ValueProxy::Assets::operator NativeRdb::ValueObject::Assets()
411 {
412 NativeRdb::ValueObject::Assets assets;
413 assets.reserve(assets_.size());
414 for (auto &asset : assets_) {
415 assets.push_back(std::move(asset));
416 }
417 return assets;
418 }
419
420 ValueProxy::Assets::operator CommonType::Assets()
421 {
422 CommonType::Assets assets;
423 assets.reserve(assets_.size());
424 for (auto &asset : assets_) {
425 assets.push_back(std::move(asset));
426 }
427 return assets;
428 }
429
430 ValueProxy::Assets::operator DistributedData::Assets()
431 {
432 DistributedData::Assets assets;
433 assets.reserve(assets_.size());
434 for (auto &asset : assets_) {
435 assets.push_back(std::move(asset));
436 }
437 return assets;
438 }
439
440 ValueProxy::Assets::operator DistributedDB::Assets()
441 {
442 DistributedDB::Assets assets;
443 assets.reserve(assets_.size());
444 for (auto &asset : assets_) {
445 assets.push_back(std::move(asset));
446 }
447 return assets;
448 }
449
operator =(ValueProxy::Value && value)450 ValueProxy::Value &ValueProxy::Value::operator=(ValueProxy::Value &&value) noexcept
451 {
452 if (this == &value) {
453 return *this;
454 }
455 value_ = std::move(value.value_);
456 return *this;
457 }
458
459 ValueProxy::Value::operator NativeRdb::ValueObject()
460 {
461 NativeRdb::ValueObject object;
462 DistributedData::Convert(std::move(value_), object.value);
463 return object;
464 }
465
466 ValueProxy::Value::operator CommonType::Value()
467 {
468 CommonType::Value object;
469 DistributedData::Convert(std::move(value_), object);
470 return object;
471 }
472
473 ValueProxy::Value::operator DistributedData::Value()
474 {
475 DistributedData::Value value;
476 DistributedData::Convert(std::move(value_), value);
477 return value;
478 }
479
480 ValueProxy::Value::operator DistributedDB::Type()
481 {
482 DistributedDB::Type value;
483 DistributedData::Convert(std::move(value_), value);
484 return value;
485 }
486
operator =(ValueProxy::Values && values)487 ValueProxy::Values &ValueProxy::Values::operator=(ValueProxy::Values &&values) noexcept
488 {
489 if (this == &values) {
490 return *this;
491 }
492 value_ = std::move(values.value_);
493 return *this;
494 }
495
operator =(Bucket && bucket)496 ValueProxy::Bucket &ValueProxy::Bucket::operator=(Bucket &&bucket) noexcept
497 {
498 if (this == &bucket) {
499 return *this;
500 }
501 value_ = std::move(bucket.value_);
502 return *this;
503 }
504
505 ValueProxy::Bucket::operator NativeRdb::ValuesBucket()
506 {
507 NativeRdb::ValuesBucket bucket;
508 for (auto &[key, value] : value_) {
509 bucket.values_.insert_or_assign(key, std::move(value));
510 }
511 value_.clear();
512 return bucket;
513 }
514
515 ValueProxy::Bucket::operator CommonType::ValuesBucket()
516 {
517 CommonType::ValuesBucket bucket;
518 for (auto &[key, value] : value_) {
519 bucket.insert_or_assign(key, std::move(value));
520 }
521 value_.clear();
522 return bucket;
523 }
524
operator =(Buckets && buckets)525 ValueProxy::Buckets &ValueProxy::Buckets::operator=(Buckets &&buckets) noexcept
526 {
527 if (this == &buckets) {
528 return *this;
529 }
530 value_ = std::move(buckets.value_);
531 return *this;
532 }
533
TempAsset(DistributedDB::Asset asset)534 ValueProxy::TempAsset::TempAsset(DistributedDB::Asset asset)
535 {
536 asset_ = DistributedData::Asset { .version = asset.version,
537 .status = ConvertToDataStatus(asset.status),
538 .expiresTime = DistributedData::Asset::NO_EXPIRES_TIME,
539 .id = std::move(asset.assetId),
540 .name = std::move(asset.name),
541 .uri = std::move(asset.uri),
542 .createTime = std::move(asset.createTime),
543 .modifyTime = std::move(asset.modifyTime),
544 .size = std::move(asset.size),
545 .hash = std::move(asset.hash),
546 .path = std::move(asset.subpath) };
547 }
548
549 ValueProxy::TempAsset::operator NativeRdb::AssetValue()
550 {
551 return NativeRdb::AssetValue { .version = asset_.version,
552 .status = asset_.status,
553 .expiresTime = asset_.expiresTime,
554 .id = std::move(asset_.id),
555 .name = std::move(asset_.name),
556 .uri = std::move(asset_.uri),
557 .createTime = std::move(asset_.createTime),
558 .modifyTime = std::move(asset_.modifyTime),
559 .size = std::move(asset_.size),
560 .hash = std::move(asset_.hash),
561 .path = std::move(asset_.path) };
562 }
563
ConvertToDataStatus(const uint32_t & status)564 uint32_t ValueProxy::TempAsset::ConvertToDataStatus(const uint32_t &status)
565 {
566 auto highStatus = GetHighStatus(status);
567 auto lowStatus = GetLowStatus(status);
568 switch (lowStatus) {
569 case DistributedDB::AssetStatus::NORMAL:
570 lowStatus = static_cast<uint32_t>(DistributedData::Asset::STATUS_NORMAL);
571 break;
572 case DistributedDB::AssetStatus::ABNORMAL:
573 lowStatus = static_cast<uint32_t>(DistributedData::Asset::STATUS_ABNORMAL);
574 break;
575 case DistributedDB::AssetStatus::INSERT:
576 lowStatus = static_cast<uint32_t>(DistributedData::Asset::STATUS_INSERT);
577 break;
578 case DistributedDB::AssetStatus::UPDATE:
579 lowStatus = static_cast<uint32_t>(DistributedData::Asset::STATUS_UPDATE);
580 break;
581 case DistributedDB::AssetStatus::DELETE:
582 lowStatus = static_cast<uint32_t>(DistributedData::Asset::STATUS_DELETE);
583 break;
584 case DistributedDB::AssetStatus::DOWNLOADING:
585 lowStatus = static_cast<uint32_t>(DistributedData::Asset::STATUS_DOWNLOADING);
586 break;
587 default:
588 lowStatus = static_cast<uint32_t>(DistributedData::Asset::STATUS_NORMAL);
589 }
590 return lowStatus | highStatus;
591 }
592 } // namespace OHOS::DistributedData