@@ -16,11 +16,17 @@ using namespace std;
16
16
17
17
namespace swss {
18
18
19
- void SonicDBConfig::parseDatabaseConfig (const string &file,
20
- std::unordered_map<std::string, RedisInstInfo> &inst_entry,
21
- std::unordered_map<std::string, SonicDBInfo> &db_entry,
22
- std::unordered_map<int , std::string> &separator_entry)
19
+ void SonicDBConfig::initialize (const string &file)
23
20
{
21
+
22
+ SWSS_LOG_ENTER ();
23
+
24
+ if (m_init)
25
+ {
26
+ SWSS_LOG_ERROR (" SonicDBConfig already initialized" );
27
+ throw runtime_error (" SonicDBConfig already initialized" );
28
+ }
29
+
24
30
ifstream i (file);
25
31
if (i.good ())
26
32
{
@@ -34,7 +40,7 @@ void SonicDBConfig::parseDatabaseConfig(const string &file,
34
40
string socket = it.value ().at (" unix_socket_path" );
35
41
string hostname = it.value ().at (" hostname" );
36
42
int port = it.value ().at (" port" );
37
- inst_entry [instName] = {socket, hostname, port};
43
+ m_inst_info [instName] = {socket, { hostname, port} };
38
44
}
39
45
40
46
for (auto it = j[" DATABASES" ].begin (); it!= j[" DATABASES" ].end (); it++)
@@ -43,12 +49,12 @@ void SonicDBConfig::parseDatabaseConfig(const string &file,
43
49
string instName = it.value ().at (" instance" );
44
50
int dbId = it.value ().at (" id" );
45
51
string separator = it.value ().at (" separator" );
46
- db_entry [dbName] = {instName, dbId, separator};
52
+ m_db_info [dbName] = {instName, dbId, separator};
47
53
48
- separator_entry .emplace (dbId, separator);
54
+ m_db_separator .emplace (dbId, separator);
49
55
}
56
+ m_init = true ;
50
57
}
51
-
52
58
catch (domain_error& e)
53
59
{
54
60
SWSS_LOG_ERROR (" key doesn't exist in json object, NULL value has no iterator >> %s\n " , e.what ());
@@ -67,152 +73,32 @@ void SonicDBConfig::parseDatabaseConfig(const string &file,
67
73
}
68
74
}
69
75
70
- void SonicDBConfig::initializeGlobalConfig (const string &file)
71
- {
72
- std::string local_file, dir_name, ns_name;
73
- std::unordered_map<std::string, SonicDBInfo> db_entry;
74
- std::unordered_map<std::string, RedisInstInfo> inst_entry;
75
- std::unordered_map<int , std::string> separator_entry;
76
-
77
- SWSS_LOG_ENTER ();
78
-
79
- if (m_global_init)
80
- {
81
- SWSS_LOG_ERROR (" SonicDBConfig Global config is already initialized" );
82
- return ;
83
- }
84
-
85
-
86
- ifstream i (file);
87
- if (i.good ())
88
- {
89
- local_file = dir_name = std::string ();
90
-
91
- // Get the directory name from the file path given as input.
92
- std::string::size_type pos = file.rfind (" /" );
93
- if ( pos != std::string::npos)
94
- {
95
- dir_name = file.substr (0 ,pos+1 );
96
- }
97
-
98
- try
99
- {
100
- json j;
101
- i >> j;
102
-
103
- for (auto & element : j[" INCLUDES" ])
104
- {
105
- local_file.append (dir_name);
106
- local_file.append (element[" include" ]);
107
-
108
- if (element[" namespace" ].empty ())
109
- {
110
- // If database_config.json is already initlized via SonicDBConfig::initialize
111
- // skip initializing it here again.
112
- if (m_init)
113
- {
114
- local_file.clear ();
115
- continue ;
116
- }
117
- ns_name = EMPTY_NAMESPACE;
118
- }
119
- else
120
- {
121
- ns_name = element[" namespace" ];
122
- }
123
-
124
- parseDatabaseConfig (local_file, inst_entry, db_entry, separator_entry);
125
- m_inst_info[ns_name] = inst_entry;
126
- m_db_info[ns_name] = db_entry;
127
- m_db_separator[ns_name] = separator_entry;
128
-
129
- inst_entry.clear ();
130
- db_entry.clear ();
131
- separator_entry.clear ();
132
- local_file.clear ();
133
- }
134
- }
135
-
136
- catch (domain_error& e)
137
- {
138
- SWSS_LOG_ERROR (" key doesn't exist in json object, NULL value has no iterator >> %s\n " , e.what ());
139
- throw runtime_error (" key doesn't exist in json object, NULL value has no iterator >> " + string (e.what ()));
140
- }
141
- catch (exception &e)
142
- {
143
- SWSS_LOG_ERROR (" Sonic database config file syntax error >> %s\n " , e.what ());
144
- throw runtime_error (" Sonic database config file syntax error >> " + string (e.what ()));
145
- }
146
- }
147
- else
148
- {
149
- SWSS_LOG_ERROR (" Sonic database global config file doesn't exist at %s\n " , file.c_str ());
150
- throw runtime_error (" Sonic database global config file doesn't exist at " + file);
151
- }
152
-
153
- // Set it as the global config file is already parsed and init done.
154
- m_global_init = true ;
155
-
156
- // Make regular init also done
157
- m_init = true ;
158
- }
159
-
160
- void SonicDBConfig::initialize (const string &file, const string &nameSpace)
161
- {
162
- std::unordered_map<std::string, SonicDBInfo> db_entry;
163
- std::unordered_map<std::string, RedisInstInfo> inst_entry;
164
- std::unordered_map<int , std::string> separator_entry;
165
-
166
- SWSS_LOG_ENTER ();
167
-
168
- if (m_init)
169
- {
170
- SWSS_LOG_ERROR (" SonicDBConfig already initialized" );
171
- throw runtime_error (" SonicDBConfig already initialized" );
172
- }
173
-
174
- // namespace string is empty, use the file given as input to parse.
175
- if (nameSpace.empty ())
176
- {
177
- parseDatabaseConfig (file, inst_entry, db_entry, separator_entry);
178
- m_inst_info[EMPTY_NAMESPACE] = inst_entry;
179
- m_db_info[EMPTY_NAMESPACE] = db_entry;
180
- m_db_separator[EMPTY_NAMESPACE] = separator_entry;
181
- }
182
- else
183
- // namespace is not empty, use DEFAULT_SONIC_DB_GLOBAL_CONFIG_FILE.
184
- initializeGlobalConfig ();
185
-
186
- // Set it as the config file is already parsed and init done.
187
- m_init = true ;
188
- }
189
-
190
- string SonicDBConfig::getDbInst (const string &dbName, const string &nameSpace)
76
+ string SonicDBConfig::getDbInst (const string &dbName)
191
77
{
192
78
if (!m_init)
193
- initialize (DEFAULT_SONIC_DB_CONFIG_FILE, nameSpace );
194
- return m_db_info[nameSpace] .at (dbName).instName ;
79
+ initialize ();
80
+ return m_db_info.at (dbName).instName ;
195
81
}
196
82
197
- int SonicDBConfig::getDbId (const string &dbName, const string &nameSpace )
83
+ int SonicDBConfig::getDbId (const string &dbName)
198
84
{
199
85
if (!m_init)
200
- initialize (DEFAULT_SONIC_DB_CONFIG_FILE, nameSpace );
201
- return m_db_info[nameSpace] .at (dbName).dbId ;
86
+ initialize ();
87
+ return m_db_info.at (dbName).dbId ;
202
88
}
203
89
204
- string SonicDBConfig::getSeparator (const string &dbName, const string &nameSpace )
90
+ string SonicDBConfig::getSeparator (const string &dbName)
205
91
{
206
92
if (!m_init)
207
- initialize (DEFAULT_SONIC_DB_CONFIG_FILE, nameSpace );
208
- return m_db_info[nameSpace] .at (dbName).separator ;
93
+ initialize ();
94
+ return m_db_info.at (dbName).separator ;
209
95
}
210
96
211
- string SonicDBConfig::getSeparator (int dbId, const string &nameSpace )
97
+ string SonicDBConfig::getSeparator (int dbId)
212
98
{
213
99
if (!m_init)
214
- initialize (DEFAULT_SONIC_DB_CONFIG_FILE, nameSpace );
215
- return m_db_separator[nameSpace] .at (dbId);
100
+ initialize ();
101
+ return m_db_separator.at (dbId);
216
102
}
217
103
218
104
string SonicDBConfig::getSeparator (const DBConnector* db)
@@ -223,61 +109,42 @@ string SonicDBConfig::getSeparator(const DBConnector* db)
223
109
}
224
110
225
111
string dbName = db->getDbName ();
226
- string nameSpace = db->getNamespace ();
227
112
if (dbName.empty ())
228
113
{
229
- return getSeparator (db->getDbId (), nameSpace );
114
+ return getSeparator (db->getDbId ());
230
115
}
231
116
else
232
117
{
233
- return getSeparator (dbName, nameSpace );
118
+ return getSeparator (dbName);
234
119
}
235
120
}
236
121
237
- string SonicDBConfig::getDbSock (const string &dbName, const string &nameSpace )
122
+ string SonicDBConfig::getDbSock (const string &dbName)
238
123
{
239
124
if (!m_init)
240
- initialize (DEFAULT_SONIC_DB_CONFIG_FILE, nameSpace );
241
- return m_inst_info[nameSpace] .at (getDbInst (dbName)).unixSocketPath ;
125
+ initialize ();
126
+ return m_inst_info.at (getDbInst (dbName)).first ;
242
127
}
243
128
244
- string SonicDBConfig::getDbHostname (const string &dbName, const string &nameSpace )
129
+ string SonicDBConfig::getDbHostname (const string &dbName)
245
130
{
246
131
if (!m_init)
247
- initialize (DEFAULT_SONIC_DB_CONFIG_FILE, nameSpace );
248
- return m_inst_info[nameSpace] .at (getDbInst (dbName)).hostname ;
132
+ initialize ();
133
+ return m_inst_info.at (getDbInst (dbName)).second . first ;
249
134
}
250
135
251
- int SonicDBConfig::getDbPort (const string &dbName, const string &nameSpace )
136
+ int SonicDBConfig::getDbPort (const string &dbName)
252
137
{
253
138
if (!m_init)
254
- initialize (DEFAULT_SONIC_DB_CONFIG_FILE, nameSpace);
255
- return m_inst_info[nameSpace].at (getDbInst (dbName)).port ;
256
- }
257
-
258
- vector<string> SonicDBConfig::getNamespaces ()
259
- {
260
- vector<string> list;
261
-
262
- if (!m_global_init)
263
- initializeGlobalConfig ();
264
-
265
- // This API returns back non-empty namespaces.
266
- for (auto it = m_inst_info.cbegin (); it != m_inst_info.cend (); ++it) {
267
- if (!((it->first ).empty ()))
268
- list.push_back (it->first );
269
- }
270
-
271
- return list;
139
+ initialize ();
140
+ return m_inst_info.at (getDbInst (dbName)).second .second ;
272
141
}
273
142
274
143
constexpr const char *SonicDBConfig::DEFAULT_SONIC_DB_CONFIG_FILE;
275
- constexpr const char *SonicDBConfig::DEFAULT_SONIC_DB_GLOBAL_CONFIG_FILE;
276
- unordered_map<string, unordered_map<string, RedisInstInfo>> SonicDBConfig::m_inst_info;
277
- unordered_map<string, unordered_map<string, SonicDBInfo>> SonicDBConfig::m_db_info;
278
- unordered_map<string, unordered_map<int , string>> SonicDBConfig::m_db_separator;
144
+ unordered_map<string, pair<string, pair<string, int >>> SonicDBConfig::m_inst_info;
145
+ unordered_map<string, SonicDBInfo> SonicDBConfig::m_db_info;
146
+ unordered_map<int , string> SonicDBConfig::m_db_separator;
279
147
bool SonicDBConfig::m_init = false ;
280
- bool SonicDBConfig::m_global_init = false ;
281
148
282
149
constexpr const char *DBConnector::DEFAULT_UNIXSOCKET;
283
150
@@ -297,8 +164,7 @@ DBConnector::~DBConnector()
297
164
298
165
DBConnector::DBConnector (int dbId, const string& hostname, int port,
299
166
unsigned int timeout) :
300
- m_dbId (dbId),
301
- m_namespace (EMPTY_NAMESPACE)
167
+ m_dbId (dbId)
302
168
{
303
169
struct timeval tv = {0 , (suseconds_t )timeout * 1000 };
304
170
@@ -315,8 +181,7 @@ DBConnector::DBConnector(int dbId, const string& hostname, int port,
315
181
}
316
182
317
183
DBConnector::DBConnector (int dbId, const string& unixPath, unsigned int timeout) :
318
- m_dbId (dbId),
319
- m_namespace (EMPTY_NAMESPACE)
184
+ m_dbId (dbId)
320
185
{
321
186
struct timeval tv = {0 , (suseconds_t )timeout * 1000 };
322
187
@@ -327,31 +192,30 @@ DBConnector::DBConnector(int dbId, const string& unixPath, unsigned int timeout)
327
192
328
193
if (m_conn->err )
329
194
throw system_error (make_error_code (errc::address_not_available),
330
- " Unable to connect to redis (unix -socket)" );
195
+ " Unable to connect to redis (unixs -socket)" );
331
196
332
197
select (this );
333
198
}
334
199
335
- DBConnector::DBConnector (const string& dbName, unsigned int timeout, bool isTcpConn, const string& nameSpace )
336
- : m_dbId(SonicDBConfig::getDbId(dbName, nameSpace ))
200
+ DBConnector::DBConnector (const string& dbName, unsigned int timeout, bool isTcpConn)
201
+ : m_dbId(SonicDBConfig::getDbId(dbName))
337
202
, m_dbName(dbName)
338
- , m_namespace(nameSpace)
339
203
{
340
204
struct timeval tv = {0 , (suseconds_t )timeout * 1000 };
341
205
342
206
if (timeout)
343
207
{
344
208
if (isTcpConn)
345
- m_conn = redisConnectWithTimeout (SonicDBConfig::getDbHostname (dbName, nameSpace ).c_str (), SonicDBConfig::getDbPort (dbName, nameSpace ), tv);
209
+ m_conn = redisConnectWithTimeout (SonicDBConfig::getDbHostname (dbName).c_str (), SonicDBConfig::getDbPort (dbName), tv);
346
210
else
347
- m_conn = redisConnectUnixWithTimeout (SonicDBConfig::getDbSock (dbName, nameSpace ).c_str (), tv);
211
+ m_conn = redisConnectUnixWithTimeout (SonicDBConfig::getDbSock (dbName).c_str (), tv);
348
212
}
349
213
else
350
214
{
351
215
if (isTcpConn)
352
- m_conn = redisConnect (SonicDBConfig::getDbHostname (dbName, nameSpace ).c_str (), SonicDBConfig::getDbPort (dbName, nameSpace ));
216
+ m_conn = redisConnect (SonicDBConfig::getDbHostname (dbName).c_str (), SonicDBConfig::getDbPort (dbName));
353
217
else
354
- m_conn = redisConnectUnix (SonicDBConfig::getDbSock (dbName, nameSpace ).c_str ());
218
+ m_conn = redisConnectUnix (SonicDBConfig::getDbSock (dbName).c_str ());
355
219
}
356
220
357
221
if (m_conn->err )
@@ -376,15 +240,19 @@ string DBConnector::getDbName() const
376
240
return m_dbName;
377
241
}
378
242
379
- string DBConnector::getNamespace () const
380
- {
381
- return m_namespace;
382
- }
383
-
384
243
DBConnector *DBConnector::newConnector (unsigned int timeout) const
385
244
{
386
245
DBConnector *ret;
387
- ret = new DBConnector (getDbName (), timeout, (getContext ()->connection_type == REDIS_CONN_TCP), getNamespace ());
246
+ if (getContext ()->connection_type == REDIS_CONN_TCP)
247
+ ret = new DBConnector (getDbId (),
248
+ getContext ()->tcp .host ,
249
+ getContext ()->tcp .port ,
250
+ timeout);
251
+ else
252
+ ret = new DBConnector (getDbId (),
253
+ getContext ()->unix_sock .path ,
254
+ timeout);
255
+ ret->m_dbName = m_dbName;
388
256
return ret;
389
257
}
390
258
0 commit comments