1 /*
2  * Copyright (c) 2022 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 #include "metadata/store_meta_data_local.h"
16 #include "utils/constant.h"
17 
18 namespace OHOS {
19 namespace DistributedData {
Marshal(json & node) const20 bool PolicyValue::Marshal(json &node) const
21 {
22     SetValue(node[GET_NAME(type)], type);
23     SetValue(node[GET_NAME(valueUint)], valueUint);
24     SetValue(node[GET_NAME(index)], index);
25     return true;
26 }
27 
Unmarshal(const json & node)28 bool PolicyValue::Unmarshal(const json &node)
29 {
30     GetValue(node, GET_NAME(type), type);
31     GetValue(node, GET_NAME(valueUint), valueUint);
32     GetValue(node, GET_NAME(index), index);
33     return true;
34 }
35 
IsValueEffect() const36 bool PolicyValue::IsValueEffect() const
37 {
38     return (index > 0);
39 }
40 
HasPolicy(uint32_t type)41 bool StoreMetaDataLocal::HasPolicy(uint32_t type)
42 {
43     for (const auto &policy : policies) {
44         if (policy.type == type) {
45             return true;
46         }
47     }
48     return false;
49 }
50 
GetPolicy(uint32_t type)51 PolicyValue StoreMetaDataLocal::GetPolicy(uint32_t type)
52 {
53     for (const auto &policy : policies) {
54         if (policy.type == type) {
55             return policy;
56         }
57     }
58     return PolicyValue();
59 }
60 
Marshal(json & node) const61 bool StoreMetaDataLocal::Marshal(json &node) const
62 {
63     SetValue(node[GET_NAME(isAutoSync)], isAutoSync);
64     SetValue(node[GET_NAME(isBackup)], isBackup);
65     SetValue(node[GET_NAME(isDirty)], isDirty);
66     SetValue(node[GET_NAME(isEncrypt)], isEncrypt);
67     SetValue(node[GET_NAME(dataDir)], dataDir);
68     SetValue(node[GET_NAME(schema)], schema);
69     SetValue(node[GET_NAME(policies)], policies);
70     SetValue(node[GET_NAME(isPublic)], isPublic);
71     return true;
72 }
73 
Unmarshal(const json & node)74 bool StoreMetaDataLocal::Unmarshal(const json &node)
75 {
76     GetValue(node, GET_NAME(isAutoSync), isAutoSync);
77     GetValue(node, GET_NAME(isBackup), isBackup);
78     GetValue(node, GET_NAME(isDirty), isDirty);
79     GetValue(node, GET_NAME(isEncrypt), isEncrypt);
80     GetValue(node, GET_NAME(dataDir), dataDir);
81     GetValue(node, GET_NAME(schema), schema);
82     GetValue(node, GET_NAME(policies), policies);
83     GetValue(node, GET_NAME(isPublic), isPublic);
84     return true;
85 }
86 
StoreMetaDataLocal()87 StoreMetaDataLocal::StoreMetaDataLocal()
88 {
89 }
90 
~StoreMetaDataLocal()91 StoreMetaDataLocal::~StoreMetaDataLocal()
92 {
93 }
94 
operator ==(const StoreMetaDataLocal & metaData) const95 bool StoreMetaDataLocal::operator==(const StoreMetaDataLocal &metaData) const
96 {
97     if (Constant::NotEqual(isAutoSync, metaData.isAutoSync) || Constant::NotEqual(isBackup, metaData.isBackup) ||
98         Constant::NotEqual(isDirty, metaData.isDirty) || Constant::NotEqual(isEncrypt, metaData.isEncrypt) ||
99         Constant::NotEqual(isPublic, metaData.isPublic)) {
100         return false;
101     }
102     return dataDir == metaData.dataDir;
103 }
104 
operator !=(const StoreMetaDataLocal & metaData) const105 bool StoreMetaDataLocal::operator!=(const StoreMetaDataLocal &metaData) const
106 {
107     return !(*this == metaData);
108 }
109 
GetKey(const std::initializer_list<std::string> & fields)110 std::string StoreMetaDataLocal::GetKey(const std::initializer_list<std::string> &fields)
111 {
112     return Constant::Join(KEY_PREFIX, Constant::KEY_SEPARATOR, fields);
113 }
114 
GetPrefix(const std::initializer_list<std::string> & fields)115 std::string StoreMetaDataLocal::GetPrefix(const std::initializer_list<std::string> &fields)
116 {
117     auto prefix = Constant::Join(KEY_PREFIX, Constant::KEY_SEPARATOR, fields);
118     prefix.append(Constant::KEY_SEPARATOR);
119     return prefix;
120 }
121 } // namespace DistributedData
122 } // namespace OHOS
123