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/subscription.h"
17
18 #include "utils/constant.h"
19 namespace OHOS::DistributedData {
Marshal(json & node) const20 bool Subscription::Relation::Marshal(json &node) const
21 {
22 SetValue(node[GET_NAME(id)], id);
23 SetValue(node[GET_NAME(bundleName)], bundleName);
24 SetValue(node[GET_NAME(relations)], relations);
25 return true;
26 }
27
Unmarshal(const json & node)28 bool Subscription::Relation::Unmarshal(const json &node)
29 {
30 GetValue(node, GET_NAME(id), id);
31 GetValue(node, GET_NAME(bundleName), bundleName);
32 GetValue(node, GET_NAME(relations), relations);
33 return true;
34 }
35
Marshal(json & node) const36 bool Subscription::Marshal(json &node) const
37 {
38 SetValue(node[GET_NAME(userId)], userId);
39 SetValue(node[GET_NAME(id)], id);
40 SetValue(node[GET_NAME(expiresTime)], expiresTime);
41 return true;
42 }
43
Unmarshal(const json & node)44 bool Subscription::Unmarshal(const json &node)
45 {
46 GetValue(node, GET_NAME(userId), userId);
47 GetValue(node, GET_NAME(id), id);
48 GetValue(node, GET_NAME(expiresTime), expiresTime);
49 return true;
50 }
51
GetKey()52 std::string Subscription::GetKey()
53 {
54 return GetKey(userId);
55 }
56
GetRelationKey(const std::string & bundleName)57 std::string Subscription::GetRelationKey(const std::string &bundleName)
58 {
59 return GetRelationKey(userId, bundleName);
60 }
61
GetMinExpireTime() const62 uint64_t Subscription::GetMinExpireTime() const
63 {
64 if (expiresTime.empty()) {
65 return 0;
66 }
67 auto it = expiresTime.begin();
68 uint64_t expire = it->second;
69 it++;
70 for (; it != expiresTime.end(); it++) {
71 if (it->second == 0) {
72 continue;
73 }
74 if (it->second < expire) {
75 expire = it->second;
76 }
77 }
78 return expire;
79 }
80
GetKey(int32_t userId)81 std::string Subscription::GetKey(int32_t userId)
82 {
83 return Constant::Join(PREFIX, Constant::KEY_SEPARATOR, { std::to_string(userId) });
84 }
85
GetRelationKey(int32_t userId,const std::string & bundleName)86 std::string Subscription::GetRelationKey(int32_t userId, const std::string &bundleName)
87 {
88 return Constant::Join(RELATION_PREFIX, Constant::KEY_SEPARATOR, { std::to_string(userId), bundleName });
89 }
90
GetPrefix(const std::initializer_list<std::string> & fields)91 std::string Subscription::GetPrefix(const std::initializer_list<std::string> &fields)
92 {
93 return Constant::Join(PREFIX, Constant::KEY_SEPARATOR, fields);
94 }
95 } // namespace OHOS::DistributedData