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