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 #ifndef OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_CLOUD_DATA_TRASLATE_H
16 #define OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_CLOUD_DATA_TRASLATE_H
17 
18 #include "cloud/icloud_data_translate.h"
19 #include "serializable/serializable.h"
20 #include "value_object.h"
21 namespace OHOS::DistributedRdb {
22 class RdbCloudDataTranslate : public DistributedDB::ICloudDataTranslate {
23 public:
24     using Asset = DistributedDB::Asset;
25     using Assets = DistributedDB::Assets;
26     using DataAsset = NativeRdb::ValueObject::Asset;
27     using DataAssets = NativeRdb::ValueObject::Assets;
28     RdbCloudDataTranslate() = default;
29     ~RdbCloudDataTranslate() = default;
30     std::vector<uint8_t> AssetToBlob(const Asset &asset) override;
31     std::vector<uint8_t> AssetsToBlob(const Assets &assets) override;
32     Asset BlobToAsset(const std::vector<uint8_t> &blob) override;
33     Assets BlobToAssets(const std::vector<uint8_t> &blob) override;
34 
35 private:
36     using Serializable = DistributedData::Serializable;
37 
38     static constexpr const uint32_t ASSET_MAGIC = 0x41534554;
39     static constexpr const uint32_t ASSETS_MAGIC = 0x41534553;
40     struct InnerAsset : public Serializable {
41         DataAsset &asset_;
InnerAssetInnerAsset42         explicit InnerAsset(DataAsset &asset) : asset_(asset) {}
43 
44         bool Marshal(json &node) const override;
45         bool Unmarshal(const json &node) override;
46     };
47     size_t ParserRawData(const uint8_t *data, size_t length, DataAsset &asset);
48     size_t ParserRawData(const uint8_t *data, size_t length, DataAssets &assets);
49 };
50 } // namespace OHOS::DistributedRdb
51 #endif // OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_RDB_CLOUD_DATA_TRASLATE_H
52