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