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