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 #include "cloud/cloud_info.h"
17
18 #include "utils/constant.h"
19
20 namespace OHOS::DistributedData {
Marshal(Serializable::json & node) const21 bool CloudInfo::Marshal(Serializable::json &node) const
22 {
23 SetValue(node[GET_NAME(user)], user);
24 SetValue(node[GET_NAME(id)], id);
25 SetValue(node[GET_NAME(totalSpace)], totalSpace);
26 SetValue(node[GET_NAME(remainSpace)], remainSpace);
27 SetValue(node[GET_NAME(enableCloud)], enableCloud);
28 SetValue(node[GET_NAME(apps)], apps);
29 SetValue(node[GET_NAME(maxNumber)], maxNumber);
30 SetValue(node[GET_NAME(maxSize)], maxSize);
31 return true;
32 }
33
Unmarshal(const Serializable::json & node)34 bool CloudInfo::Unmarshal(const Serializable::json &node)
35 {
36 GetValue(node, GET_NAME(user), user);
37 GetValue(node, GET_NAME(id), id);
38 GetValue(node, GET_NAME(totalSpace), totalSpace);
39 GetValue(node, GET_NAME(remainSpace), remainSpace);
40 GetValue(node, GET_NAME(enableCloud), enableCloud);
41 GetValue(node, GET_NAME(apps), apps);
42 if (!GetValue(node, GET_NAME(maxNumber), maxNumber) || maxNumber == 0) {
43 maxNumber = DEFAULT_BATCH_NUMBER;
44 }
45 if (!GetValue(node, GET_NAME(maxSize), maxSize) || maxSize == 0) {
46 maxSize = DEFAULT_BATCH_SIZE;
47 }
48 return true;
49 }
50
Marshal(Serializable::json & node) const51 bool CloudInfo::AppInfo::Marshal(Serializable::json &node) const
52 {
53 SetValue(node[GET_NAME(bundleName)], bundleName);
54 SetValue(node[GET_NAME(appId)], appId);
55 SetValue(node[GET_NAME(version)], version);
56 SetValue(node[GET_NAME(instanceId)], instanceId);
57 SetValue(node[GET_NAME(cloudSwitch)], cloudSwitch);
58 return true;
59 }
60
Unmarshal(const Serializable::json & node)61 bool CloudInfo::AppInfo::Unmarshal(const Serializable::json &node)
62 {
63 GetValue(node, GET_NAME(bundleName), bundleName);
64 GetValue(node, GET_NAME(appId), appId);
65 GetValue(node, GET_NAME(version), version);
66 GetValue(node, GET_NAME(instanceId), instanceId);
67 GetValue(node, GET_NAME(cloudSwitch), cloudSwitch);
68 return true;
69 }
70
GetKey() const71 std::string CloudInfo::GetKey() const
72 {
73 return GetKey(INFO_PREFIX, { std::to_string(user) });
74 }
75
GetSchemaKey() const76 std::map<std::string, std::string> CloudInfo::GetSchemaKey() const
77 {
78 std::map<std::string, std::string> keys;
79 for (const auto &[bundle, app] : apps) {
80 const auto key = GetKey(SCHEMA_PREFIX, { std::to_string(user), bundle, std::to_string(app.instanceId) });
81 keys.insert_or_assign(app.bundleName, key);
82 }
83 return keys;
84 }
85
GetSchemaKey(const std::string & bundleName,int32_t instanceId) const86 std::string CloudInfo::GetSchemaKey(const std::string &bundleName, int32_t instanceId) const
87 {
88 return GetKey(SCHEMA_PREFIX, { std::to_string(user), bundleName, std::to_string(instanceId) });
89 }
90
GetSchemaKey(int32_t user,const std::string & bundleName,int32_t instanceId)91 std::string CloudInfo::GetSchemaKey(int32_t user, const std::string &bundleName, int32_t instanceId)
92 {
93 return GetKey(SCHEMA_PREFIX, { std::to_string(user), bundleName, std::to_string(instanceId) });
94 }
95
GetSchemaPrefix(const std::string & bundleName) const96 std::string CloudInfo::GetSchemaPrefix(const std::string &bundleName) const
97 {
98 if (bundleName.empty()) {
99 return GetKey(SCHEMA_PREFIX, { std::to_string(user) });
100 }
101 return GetKey(SCHEMA_PREFIX, { std::to_string(user), bundleName });
102 }
103
GetSchemaKey(const StoreMetaData & meta)104 std::string CloudInfo::GetSchemaKey(const StoreMetaData &meta)
105 {
106 return GetKey(SCHEMA_PREFIX, { meta.user, meta.bundleName, std::to_string(meta.instanceId) });
107 }
108
IsValid() const109 bool CloudInfo::IsValid() const
110 {
111 return !id.empty();
112 }
113
Exist(const std::string & bundleName,int32_t instanceId) const114 bool CloudInfo::Exist(const std::string &bundleName, int32_t instanceId) const
115 {
116 if (bundleName.empty()) {
117 return false;
118 }
119 auto it = apps.find(bundleName);
120 return it != apps.end() && it->second.bundleName == bundleName && it->second.instanceId == instanceId;
121 }
122
IsOn(const std::string & bundleName,int32_t instanceId) const123 bool CloudInfo::IsOn(const std::string &bundleName, int32_t instanceId) const
124 {
125 if (bundleName.empty()) {
126 return false;
127 }
128 auto it = apps.find(bundleName);
129 return it != apps.end() && it->second.instanceId == instanceId && it->second.cloudSwitch;
130 }
131
IsAllSwitchOff() const132 bool CloudInfo::IsAllSwitchOff() const
133 {
134 for (auto &[bundle, app] : apps) {
135 if (app.cloudSwitch) {
136 return false;
137 }
138 }
139 return true;
140 }
141
GetPrefix(const std::initializer_list<std::string> & fields)142 std::string CloudInfo::GetPrefix(const std::initializer_list<std::string> &fields)
143 {
144 return GetKey(INFO_PREFIX, fields).append(Constant::KEY_SEPARATOR);
145 }
146
GetKey(const std::string & prefix,const std::initializer_list<std::string> & fields)147 std::string CloudInfo::GetKey(const std::string &prefix, const std::initializer_list<std::string> &fields)
148 {
149 return Constant::Join(prefix, Constant::KEY_SEPARATOR, fields);
150 }
151 } // namespace OHOS::DistributedData