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 
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 
operator NativeRdb::AssetValue()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 
operator CommonType::AssetValue()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 
operator DistributedData::Asset()278 ValueProxy::Asset::operator DistributedData::Asset()
279 {
280     return std::move(asset_);
281 }
282 
operator DistributedDB::Asset()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     switch (lowStatus) {
302         case static_cast<uint32_t>(DistributedDB::AssetStatus::NORMAL):
303             lowStatus = DistributedData::Asset::STATUS_NORMAL;
304             break;
305         case static_cast<uint32_t>(DistributedDB::AssetStatus::DOWNLOADING):
306             lowStatus = DistributedData::Asset::STATUS_DOWNLOADING;
307             break;
308         case static_cast<uint32_t>(DistributedDB::AssetStatus::ABNORMAL):
309             lowStatus = DistributedData::Asset::STATUS_ABNORMAL;
310             break;
311         case static_cast<uint32_t>(DistributedDB::AssetStatus::INSERT):
312             lowStatus = DistributedData::Asset::STATUS_INSERT;
313             break;
314         case static_cast<uint32_t>(DistributedDB::AssetStatus::UPDATE):
315             lowStatus = DistributedData::Asset::STATUS_UPDATE;
316             break;
317         case static_cast<uint32_t>(DistributedDB::AssetStatus::DELETE):
318             lowStatus = DistributedData::Asset::STATUS_DELETE;
319             break;
320         default:
321             lowStatus = DistributedData::Asset::STATUS_NORMAL;
322     }
323     return lowStatus | highStatus;
324 }
325 
ConvertToDBStatus(const uint32_t &status)326 uint32_t ValueProxy::Asset::ConvertToDBStatus(const uint32_t &status)
327 {
328     auto highStatus = GetHighStatus(status);
329     auto lowStatus = GetLowStatus(status);
330     switch (lowStatus) {
331         case DistributedData::Asset::STATUS_NORMAL:
332             lowStatus = static_cast<uint32_t>(DistributedDB::AssetStatus::NORMAL);
333             break;
334         case DistributedData::Asset::STATUS_ABNORMAL:
335             lowStatus = static_cast<uint32_t>(DistributedDB::AssetStatus::ABNORMAL);
336             break;
337         case DistributedData::Asset::STATUS_INSERT:
338             lowStatus = static_cast<uint32_t>(DistributedDB::AssetStatus::INSERT);
339             break;
340         case DistributedData::Asset::STATUS_UPDATE:
341             lowStatus = static_cast<uint32_t>(DistributedDB::AssetStatus::UPDATE);
342             break;
343         case DistributedData::Asset::STATUS_DELETE:
344             lowStatus = static_cast<uint32_t>(DistributedDB::AssetStatus::DELETE);
345             break;
346         case DistributedData::Asset::STATUS_DOWNLOADING:
347             lowStatus = static_cast<uint32_t>(DistributedDB::AssetStatus::DOWNLOADING);
348             break;
349         default:
350             lowStatus = static_cast<uint32_t>(DistributedDB::AssetStatus::NORMAL);
351     }
352     return lowStatus | highStatus;
353 }
354 
Assets(DistributedData::Assets assets)355 ValueProxy::Assets::Assets(DistributedData::Assets assets)
356 {
357     assets_.clear();
358     assets_.reserve(assets.size());
359     for (auto &asset : assets) {
360         assets_.emplace_back(std::move(asset));
361     }
362 }
363 
Assets(NativeRdb::ValueObject::Assets assets)364 ValueProxy::Assets::Assets(NativeRdb::ValueObject::Assets assets)
365 {
366     assets_.clear();
367     assets_.reserve(assets.size());
368     for (auto &asset : assets) {
369         assets_.emplace_back(std::move(asset));
370     }
371 }
372 
Assets(CommonType::Assets assets)373 ValueProxy::Assets::Assets(CommonType::Assets assets)
374 {
375     assets_.clear();
376     assets_.reserve(assets.size());
377     for (auto &asset : assets) {
378         assets_.emplace_back(std::move(asset));
379     }
380 }
381 
Assets(DistributedDB::Assets assets)382 ValueProxy::Assets::Assets(DistributedDB::Assets assets)
383 {
384     assets_.clear();
385     assets_.reserve(assets.size());
386     for (auto &asset : assets) {
387         assets_.emplace_back(std::move(asset));
388     }
389 }
390 
operator =(const Assets &proxy)391 ValueProxy::Assets &ValueProxy::Assets::operator=(const Assets &proxy)
392 {
393     if (this == &proxy) {
394         return *this;
395     }
396     assets_ = proxy.assets_;
397     return *this;
398 }
399 
400 ValueProxy::Assets &ValueProxy::Assets::operator=(Assets &&proxy) noexcept
401 {
402     if (this == &proxy) {
403         return *this;
404     }
405     assets_ = std::move(proxy.assets_);
406     return *this;
407 }
408 
operator NativeRdb::ValueObject::Assets()409 ValueProxy::Assets::operator NativeRdb::ValueObject::Assets()
410 {
411     NativeRdb::ValueObject::Assets assets;
412     assets.reserve(assets_.size());
413     for (auto &asset : assets_) {
414         assets.push_back(std::move(asset));
415     }
416     return assets;
417 }
418 
operator CommonType::Assets()419 ValueProxy::Assets::operator CommonType::Assets()
420 {
421     CommonType::Assets assets;
422     assets.reserve(assets_.size());
423     for (auto &asset : assets_) {
424         assets.push_back(std::move(asset));
425     }
426     return assets;
427 }
428 
operator DistributedData::Assets()429 ValueProxy::Assets::operator DistributedData::Assets()
430 {
431     DistributedData::Assets assets;
432     assets.reserve(assets_.size());
433     for (auto &asset : assets_) {
434         assets.push_back(std::move(asset));
435     }
436     return assets;
437 }
438 
operator DistributedDB::Assets()439 ValueProxy::Assets::operator DistributedDB::Assets()
440 {
441     DistributedDB::Assets assets;
442     assets.reserve(assets_.size());
443     for (auto &asset : assets_) {
444         assets.push_back(std::move(asset));
445     }
446     return assets;
447 }
448 
449 ValueProxy::Value &ValueProxy::Value::operator=(ValueProxy::Value &&value) noexcept
450 {
451     if (this == &value) {
452         return *this;
453     }
454     value_ = std::move(value.value_);
455     return *this;
456 }
457 
operator NativeRdb::ValueObject()458 ValueProxy::Value::operator NativeRdb::ValueObject()
459 {
460     NativeRdb::ValueObject object;
461     DistributedData::Convert(std::move(value_), object.value);
462     return object;
463 }
464 
operator CommonType::Value()465 ValueProxy::Value::operator CommonType::Value()
466 {
467     CommonType::Value object;
468     DistributedData::Convert(std::move(value_), object);
469     return object;
470 }
471 
operator DistributedData::Value()472 ValueProxy::Value::operator DistributedData::Value()
473 {
474     DistributedData::Value value;
475     DistributedData::Convert(std::move(value_), value);
476     return value;
477 }
478 
operator DistributedDB::Type()479 ValueProxy::Value::operator DistributedDB::Type()
480 {
481     DistributedDB::Type value;
482     DistributedData::Convert(std::move(value_), value);
483     return value;
484 }
485 
486 ValueProxy::Values &ValueProxy::Values::operator=(ValueProxy::Values &&values) noexcept
487 {
488     if (this == &values) {
489         return *this;
490     }
491     value_ = std::move(values.value_);
492     return *this;
493 }
494 
495 ValueProxy::Bucket &ValueProxy::Bucket::operator=(Bucket &&bucket) noexcept
496 {
497     if (this == &bucket) {
498         return *this;
499     }
500     value_ = std::move(bucket.value_);
501     return *this;
502 }
503 
operator NativeRdb::ValuesBucket()504 ValueProxy::Bucket::operator NativeRdb::ValuesBucket()
505 {
506     NativeRdb::ValuesBucket bucket;
507     for (auto &[key, value] : value_) {
508         bucket.values_.insert_or_assign(key, std::move(value));
509     }
510     value_.clear();
511     return bucket;
512 }
513 
operator CommonType::ValuesBucket()514 ValueProxy::Bucket::operator CommonType::ValuesBucket()
515 {
516     CommonType::ValuesBucket bucket;
517     for (auto &[key, value] : value_) {
518         bucket.insert_or_assign(key, std::move(value));
519     }
520     value_.clear();
521     return bucket;
522 }
523 
524 ValueProxy::Buckets &ValueProxy::Buckets::operator=(Buckets &&buckets) noexcept
525 {
526     if (this == &buckets) {
527         return *this;
528     }
529     value_ = std::move(buckets.value_);
530     return *this;
531 }
532 
TempAsset(DistributedDB::Asset asset)533 ValueProxy::TempAsset::TempAsset(DistributedDB::Asset asset)
534 {
535     asset_ = DistributedData::Asset { .version = asset.version,
536         .status = ConvertToDataStatus(asset.status),
537         .expiresTime = DistributedData::Asset::NO_EXPIRES_TIME,
538         .id = std::move(asset.assetId),
539         .name = std::move(asset.name),
540         .uri = std::move(asset.uri),
541         .createTime = std::move(asset.createTime),
542         .modifyTime = std::move(asset.modifyTime),
543         .size = std::move(asset.size),
544         .hash = std::move(asset.hash),
545         .path = std::move(asset.subpath) };
546 }
547 
operator NativeRdb::AssetValue()548 ValueProxy::TempAsset::operator NativeRdb::AssetValue()
549 {
550     return NativeRdb::AssetValue { .version = asset_.version,
551         .status = asset_.status,
552         .expiresTime = asset_.expiresTime,
553         .id = std::move(asset_.id),
554         .name = std::move(asset_.name),
555         .uri = std::move(asset_.uri),
556         .createTime = std::move(asset_.createTime),
557         .modifyTime = std::move(asset_.modifyTime),
558         .size = std::move(asset_.size),
559         .hash = std::move(asset_.hash),
560         .path = std::move(asset_.path) };
561 }
562 
ConvertToDataStatus(const uint32_t &status)563 uint32_t ValueProxy::TempAsset::ConvertToDataStatus(const uint32_t &status)
564 {
565     auto highStatus = GetHighStatus(status);
566     auto lowStatus = GetLowStatus(status);
567     switch (lowStatus) {
568         case DistributedDB::AssetStatus::NORMAL:
569             lowStatus = static_cast<uint32_t>(DistributedData::Asset::STATUS_NORMAL);
570             break;
571         case DistributedDB::AssetStatus::ABNORMAL:
572             lowStatus = static_cast<uint32_t>(DistributedData::Asset::STATUS_ABNORMAL);
573             break;
574         case DistributedDB::AssetStatus::INSERT:
575             lowStatus = static_cast<uint32_t>(DistributedData::Asset::STATUS_INSERT);
576             break;
577         case DistributedDB::AssetStatus::UPDATE:
578             lowStatus = static_cast<uint32_t>(DistributedData::Asset::STATUS_UPDATE);
579             break;
580         case DistributedDB::AssetStatus::DELETE:
581             lowStatus = static_cast<uint32_t>(DistributedData::Asset::STATUS_DELETE);
582             break;
583         case DistributedDB::AssetStatus::DOWNLOADING:
584             lowStatus = static_cast<uint32_t>(DistributedData::Asset::STATUS_DOWNLOADING);
585             break;
586         default:
587             lowStatus = static_cast<uint32_t>(DistributedData::Asset::STATUS_NORMAL);
588     }
589     return lowStatus | highStatus;
590 }
591 } // namespace OHOS::DistributedData