7
7
#include < utility>
8
8
9
9
#include < hiredis/hiredis.h>
10
+ #define EMPTY_NAMESPACE std::string ()
10
11
11
12
namespace swss {
12
13
13
14
class DBConnector ;
14
15
16
+ class RedisInstInfo
17
+ {
18
+ public:
19
+ std::string unixSocketPath;
20
+ std::string hostname;
21
+ int port;
22
+ };
23
+
15
24
class SonicDBInfo
16
25
{
17
26
public:
@@ -24,25 +33,35 @@ class SonicDBConfig
24
33
{
25
34
public:
26
35
static void initialize (const std::string &file = DEFAULT_SONIC_DB_CONFIG_FILE);
27
- static std::string getDbInst (const std::string &dbName);
28
- static int getDbId (const std::string &dbName);
29
- static std::string getSeparator (const std::string &dbName);
30
- static std::string getSeparator (int dbId);
36
+ static void initializeGlobalConfig (const std::string &file = DEFAULT_SONIC_DB_GLOBAL_CONFIG_FILE);
37
+ static void validateNamespace (const std::string &nameSpace);
38
+ static std::string getDbInst (const std::string &dbName, const std::string &nameSpace = EMPTY_NAMESPACE);
39
+ static int getDbId (const std::string &dbName, const std::string &nameSpace = EMPTY_NAMESPACE);
40
+ static std::string getSeparator (const std::string &dbName, const std::string &nameSpace = EMPTY_NAMESPACE);
41
+ static std::string getSeparator (int dbId, const std::string &nameSpace = EMPTY_NAMESPACE);
31
42
static std::string getSeparator (const DBConnector* db);
32
- static std::string getDbSock (const std::string &dbName);
33
- static std::string getDbHostname (const std::string &dbName);
34
- static int getDbPort (const std::string &dbName);
43
+ static std::string getDbSock (const std::string &dbName, const std::string &nameSpace = EMPTY_NAMESPACE);
44
+ static std::string getDbHostname (const std::string &dbName, const std::string &nameSpace = EMPTY_NAMESPACE);
45
+ static int getDbPort (const std::string &dbName, const std::string &nameSpace = EMPTY_NAMESPACE);
46
+ static std::vector<std::string> getNamespaces ();
35
47
static bool isInit () { return m_init; };
48
+ static bool isGlobalInit () { return m_global_init; };
36
49
37
50
private:
38
51
static constexpr const char *DEFAULT_SONIC_DB_CONFIG_FILE = " /var/run/redis/sonic-db/database_config.json" ;
39
- // { instName, { unix_socket_path, {hostname, port} } }
40
- static std::unordered_map<std::string, std::pair<std::string, std::pair<std::string, int >>> m_inst_info;
41
- // { dbName, {instName, dbId} }
42
- static std::unordered_map<std::string, SonicDBInfo> m_db_info;
43
- // { dbIp, separator }
44
- static std::unordered_map<int , std::string> m_db_separator;
52
+ static constexpr const char *DEFAULT_SONIC_DB_GLOBAL_CONFIG_FILE = " /var/run/redis/sonic-db/database_global.json" ;
53
+ // { namespace { instName, { unix_socket_path, hostname, port } } }
54
+ static std::unordered_map<std::string, std::unordered_map<std::string, RedisInstInfo>> m_inst_info;
55
+ // { namespace, { dbName, {instName, dbId, separator} } }
56
+ static std::unordered_map<std::string, std::unordered_map<std::string, SonicDBInfo>> m_db_info;
57
+ // { namespace, { dbId, separator } }
58
+ static std::unordered_map<std::string, std::unordered_map<int , std::string>> m_db_separator;
45
59
static bool m_init;
60
+ static bool m_global_init;
61
+ static void parseDatabaseConfig (const std::string &file,
62
+ std::unordered_map<std::string, RedisInstInfo> &inst_entry,
63
+ std::unordered_map<std::string, SonicDBInfo> &db_entry,
64
+ std::unordered_map<int , std::string> &separator_entry);
46
65
};
47
66
48
67
class DBConnector
@@ -60,12 +79,14 @@ class DBConnector
60
79
DBConnector (int dbId, const std::string &hostname, int port, unsigned int timeout);
61
80
DBConnector (int dbId, const std::string &unixPath, unsigned int timeout);
62
81
DBConnector (const std::string &dbName, unsigned int timeout, bool isTcpConn = false );
82
+ DBConnector (const std::string &dbName, unsigned int timeout, bool isTcpConn, const std::string &nameSpace);
63
83
64
84
~DBConnector ();
65
85
66
86
redisContext *getContext () const ;
67
87
int getDbId () const ;
68
88
std::string getDbName () const ;
89
+ std::string getNamespace () const ;
69
90
70
91
static void select (DBConnector *db);
71
92
@@ -84,6 +105,7 @@ class DBConnector
84
105
redisContext *m_conn;
85
106
int m_dbId;
86
107
std::string m_dbName;
108
+ std::string m_namespace;
87
109
};
88
110
89
111
}
0 commit comments