1 /*
2  * Copyright (c) 2021 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 "db_common.h"
17 #include "db_properties.h"
18 
19 namespace DistributedDB {
GetStringProp(const std::string & name,const std::string & defaultValue) const20 std::string DBProperties::GetStringProp(const std::string &name, const std::string &defaultValue) const
21 {
22     auto iter = stringProperties_.find(name);
23     if (iter != stringProperties_.end()) {
24         return iter->second;
25     }
26     return defaultValue;
27 }
28 
SetStringProp(const std::string & name,const std::string & value)29 void DBProperties::SetStringProp(const std::string &name, const std::string &value)
30 {
31     stringProperties_[name] = value;
32 }
33 
GetBoolProp(const std::string & name,bool defaultValue) const34 bool DBProperties::GetBoolProp(const std::string &name, bool defaultValue) const
35 {
36     auto iter = boolProperties_.find(name);
37     if (iter != boolProperties_.end()) {
38         return iter->second;
39     }
40     return defaultValue;
41 }
42 
SetBoolProp(const std::string & name,bool value)43 void DBProperties::SetBoolProp(const std::string &name, bool value)
44 {
45     boolProperties_[name] = value;
46 }
47 
GetIntProp(const std::string & name,int defaultValue) const48 int DBProperties::GetIntProp(const std::string &name, int defaultValue) const
49 {
50     auto iter = intProperties_.find(name);
51     if (iter != intProperties_.end()) {
52         return iter->second;
53     }
54     return defaultValue;
55 }
56 
SetIntProp(const std::string & name,int value)57 void DBProperties::SetIntProp(const std::string &name, int value)
58 {
59     intProperties_[name] = value;
60 }
61 
GetUIntProp(const std::string & name,uint32_t defaultValue) const62 uint32_t DBProperties::GetUIntProp(const std::string &name, uint32_t defaultValue) const
63 {
64     auto iter = uintProperties_.find(name);
65     if (iter != uintProperties_.end()) {
66         return iter->second;
67     }
68     return defaultValue;
69 }
70 
SetUIntProp(const std::string & name,uint32_t value)71 void DBProperties::SetUIntProp(const std::string &name, uint32_t value)
72 {
73     uintProperties_[name] = value;
74 }
75 
SetIdentifier(const std::string & userId,const std::string & appId,const std::string & storeId,const std::string & subUser,int32_t instanceId)76 void DBProperties::SetIdentifier(const std::string &userId, const std::string &appId, const std::string &storeId,
77     const std::string &subUser, int32_t instanceId)
78 {
79     SetStringProp(DBProperties::APP_ID, appId);
80     SetStringProp(DBProperties::USER_ID, userId);
81     SetStringProp(DBProperties::STORE_ID, storeId);
82     SetStringProp(DBProperties::SUB_USER, subUser);
83     SetIntProp(DBProperties::INSTANCE_ID, instanceId);
84     std::string hashIdentifier = DBCommon::TransferHashString(
85         DBCommon::GenerateIdentifierId(storeId, appId, userId, subUser, instanceId));
86     SetStringProp(DBProperties::IDENTIFIER_DATA, hashIdentifier);
87     std::string dualIdentifier = DBCommon::TransferHashString(DBCommon::GenerateDualTupleIdentifierId(storeId, appId));
88     SetStringProp(DBProperties::DUAL_TUPLE_IDENTIFIER_DATA, dualIdentifier);
89 }
90 }