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
GetStringProp(const std::string & name,const std::string & defaultValue) const32 std::string DBProperties::GetStringProp(const std::string &name, const std::string &defaultValue) const
33 {
34 auto iter = stringProperties_.find(name);
35 if (iter != stringProperties_.end()) {
36 return iter->second;
37 }
38 return defaultValue;
39 }
40
SetStringProp(const std::string & name,const std::string & value)41 void DBProperties::SetStringProp(const std::string &name, const std::string &value)
42 {
43 stringProperties_[name] = value;
44 }
45
GetBoolProp(const std::string & name,bool defaultValue) const46 bool DBProperties::GetBoolProp(const std::string &name, bool defaultValue) const
47 {
48 auto iter = boolProperties_.find(name);
49 if (iter != boolProperties_.end()) {
50 return iter->second;
51 }
52 return defaultValue;
53 }
54
SetBoolProp(const std::string & name,bool value)55 void DBProperties::SetBoolProp(const std::string &name, bool value)
56 {
57 boolProperties_[name] = value;
58 }
59
GetIntProp(const std::string & name,int defaultValue) const60 int DBProperties::GetIntProp(const std::string &name, int defaultValue) const
61 {
62 auto iter = intProperties_.find(name);
63 if (iter != intProperties_.end()) {
64 return iter->second;
65 }
66 return defaultValue;
67 }
68
SetIntProp(const std::string & name,int value)69 void DBProperties::SetIntProp(const std::string &name, int value)
70 {
71 intProperties_[name] = value;
72 }
73
SetIdentifier(const std::string & userId,const std::string & appId,const std::string & storeId,int32_t instanceId)74 void DBProperties::SetIdentifier(const std::string &userId, const std::string &appId, const std::string &storeId,
75 int32_t instanceId)
76 {
77 SetStringProp(DBProperties::APP_ID, appId);
78 SetStringProp(DBProperties::USER_ID, userId);
79 SetStringProp(DBProperties::STORE_ID, storeId);
80 SetIntProp(DBProperties::INSTANCE_ID, instanceId);
81 std::string hashIdentifier = DBCommon::TransferHashString(
82 DBCommon::GenerateIdentifierId(storeId, appId, userId, instanceId));
83 SetStringProp(DBProperties::IDENTIFIER_DATA, hashIdentifier);
84 std::string dualIdentifier = DBCommon::TransferHashString(DBCommon::GenerateDualTupleIdentifierId(storeId, appId));
85 SetStringProp(DBProperties::DUAL_TUPLE_IDENTIFIER_DATA, dualIdentifier);
86 }
87 }