• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {
20 const std::string DBProperties::CREATE_IF_NECESSARY = "createIfNecessary";
21 const std::string DBProperties::DATABASE_TYPE = "databaseType";
22 const std::string DBProperties::DATA_DIR = "dataDir";
23 const std::string DBProperties::USER_ID = "userId";
24 const std::string DBProperties::APP_ID = "appId";
25 const std::string DBProperties::STORE_ID = "storeId";
26 const std::string DBProperties::INSTANCE_ID = "instanceId";
27 const std::string DBProperties::IDENTIFIER_DATA = "identifier";
28 const std::string DBProperties::IDENTIFIER_DIR = "identifierDir";
29 const std::string DBProperties::DUAL_TUPLE_IDENTIFIER_DATA = "dualTupleIdentifier";
30 const std::string DBProperties::SYNC_DUAL_TUPLE_MODE = "syncDualTuple";
31 const std::string DBProperties::AUTO_LAUNCH_ID = "autoLaunchID";
32 
GetStringProp(const std::string & name,const std::string & defaultValue) const33 std::string DBProperties::GetStringProp(const std::string &name, const std::string &defaultValue) const
34 {
35     auto iter = stringProperties_.find(name);
36     if (iter != stringProperties_.end()) {
37         return iter->second;
38     }
39     return defaultValue;
40 }
41 
SetStringProp(const std::string & name,const std::string & value)42 void DBProperties::SetStringProp(const std::string &name, const std::string &value)
43 {
44     stringProperties_[name] = value;
45 }
46 
GetBoolProp(const std::string & name,bool defaultValue) const47 bool DBProperties::GetBoolProp(const std::string &name, bool defaultValue) const
48 {
49     auto iter = boolProperties_.find(name);
50     if (iter != boolProperties_.end()) {
51         return iter->second;
52     }
53     return defaultValue;
54 }
55 
SetBoolProp(const std::string & name,bool value)56 void DBProperties::SetBoolProp(const std::string &name, bool value)
57 {
58     boolProperties_[name] = value;
59 }
60 
GetIntProp(const std::string & name,int defaultValue) const61 int DBProperties::GetIntProp(const std::string &name, int defaultValue) const
62 {
63     auto iter = intProperties_.find(name);
64     if (iter != intProperties_.end()) {
65         return iter->second;
66     }
67     return defaultValue;
68 }
69 
SetIntProp(const std::string & name,int value)70 void DBProperties::SetIntProp(const std::string &name, int value)
71 {
72     intProperties_[name] = value;
73 }
74 
SetIdentifier(const std::string & userId,const std::string & appId,const std::string & storeId,int32_t instanceId)75 void DBProperties::SetIdentifier(const std::string &userId, const std::string &appId, const std::string &storeId,
76     int32_t instanceId)
77 {
78     SetStringProp(DBProperties::APP_ID, appId);
79     SetStringProp(DBProperties::USER_ID, userId);
80     SetStringProp(DBProperties::STORE_ID, storeId);
81     SetIntProp(DBProperties::INSTANCE_ID, instanceId);
82     std::string hashIdentifier = DBCommon::TransferHashString(
83         DBCommon::GenerateIdentifierId(storeId, appId, userId, instanceId));
84     SetStringProp(DBProperties::IDENTIFIER_DATA, hashIdentifier);
85     std::string dualIdentifier = DBCommon::TransferHashString(DBCommon::GenerateDualTupleIdentifierId(storeId, appId));
86     SetStringProp(DBProperties::DUAL_TUPLE_IDENTIFIER_DATA, dualIdentifier);
87 }
88 }