Skip to content

Commit

Permalink
[dbconnector]: Add API getDbKeys in dbconnector (sonic-net#854)
Browse files Browse the repository at this point in the history
Get a new API getDbKeys in dbconnector to fetch all DB keys.

---------

Signed-off-by: Ze Gan <[email protected]>
  • Loading branch information
Pterosaur authored Feb 22, 2024
1 parent 3c3ae57 commit 9330086
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 0 deletions.
16 changes: 16 additions & 0 deletions common/dbconnector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,22 @@ vector<string> SonicDBConfig::getNamespaces()
return vector<string>(list.begin(), list.end());
}

vector<SonicDBKey> SonicDBConfig::getDbKeys()
{
vector<SonicDBKey> keys;
std::lock_guard<std::recursive_mutex> guard(m_db_info_mutex);

if (!m_init)
initialize(DEFAULT_SONIC_DB_CONFIG_FILE);

// This API returns back all db keys.
for (auto it = m_inst_info.cbegin(); it != m_inst_info.cend(); ++it) {
keys.push_back(it->first);
}

return keys;
}

std::vector<std::string> SonicDBConfig::getDbList(const std::string &netns, const std::string &containerName)
{
SonicDBKey key;
Expand Down
1 change: 1 addition & 0 deletions common/dbconnector.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ class SonicDBConfig
static int getDbPort(const std::string &dbName, const std::string &netns = EMPTY_NAMESPACE, const std::string &containerName=EMPTY_CONTAINERNAME);
static int getDbPort(const std::string &dbName, const SonicDBKey &key);
static std::vector<std::string> getNamespaces();
static std::vector<SonicDBKey> getDbKeys();
#if defined(SWIG) && defined(SWIGPYTHON)
%pythoncode %{
## TODO: the python function and C++ one is not on-par
Expand Down
1 change: 1 addition & 0 deletions pyext/swsscommon.i
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
%template(GetConfigResult) std::map<std::string, std::map<std::string, std::map<std::string, std::string>>>;
%template(GetInstanceListResult) std::map<std::string, swss::RedisInstInfo>;
%template(KeyOpFieldsValuesQueue) std::deque<std::tuple<std::string, std::string, std::vector<std::pair<std::string, std::string>>>>;
%template(VectorSonicDbKey) std::vector<swss::SonicDBKey>;

#ifdef SWIGPYTHON
%exception {
Expand Down
19 changes: 19 additions & 0 deletions tests/redis_multi_ns_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ TEST(DBConnector, multi_ns_test)
std::string local_file, dir_name;
set<string> namespaces;
vector<string> ns_names;
vector<SonicDBKey> dbkeys;
vector<SonicDBKey> target_dbkeys;

// load global config file again, should throw exception with init already done
try
Expand Down Expand Up @@ -67,7 +69,12 @@ TEST(DBConnector, multi_ns_test)
{
key.netns = element["namespace"];
}
if (!element["container_name"].empty())
{
key.containerName = element["container_name"];
}
namespaces.insert(key.netns);
target_dbkeys.push_back(key);

if (!element["container_name"].empty())
{
Expand Down Expand Up @@ -136,6 +143,18 @@ TEST(DBConnector, multi_ns_test)
sort (ns_names.begin(), ns_names.end());
EXPECT_EQ(ns_names.size(), namespaces.size());
EXPECT_TRUE(std::equal(namespaces.begin(), namespaces.end(), ns_names.begin()));

dbkeys = SonicDBConfig::getDbKeys();
EXPECT_EQ(dbkeys.size(), target_dbkeys.size());
for (auto &key : target_dbkeys)
{
cout<< "target key " << key.toString() <<endl;
}
for (auto &key : dbkeys)
{
cout<< "Check key " << key.toString() <<endl;
EXPECT_TRUE(std::find(target_dbkeys.begin(), target_dbkeys.end(), key) != target_dbkeys.end());
}
}

void clearNetnsDB()
Expand Down
2 changes: 2 additions & 0 deletions tests/test_redis_ut.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,7 @@ def test_SmartSwitchDBConnector():
test_dir = os.path.dirname(os.path.abspath(__file__))
global_db_config = os.path.join(test_dir, 'redis_multi_db_ut_config', 'database_global.json')
SonicDBConfig.load_sonic_global_db_config(global_db_config)
global_db_config_json = json.load(open(global_db_config))
db_key = SonicDBKey()
db_key.containerName = "dpu0"
db = swsscommon.DBConnector("DPU_APPL_DB", 0, True, db_key)
Expand All @@ -848,4 +849,5 @@ def test_SmartSwitchDBConnector():
assert "dputest2" in keys
assert tbl.get("dputest1")[1][0] == ("dashfield1", "dashvalue1")
assert tbl.get("dputest2")[1][1] == ("dashfield2", "dashvalue2")
assert len(SonicDBConfig.getDbKeys()) == len(global_db_config_json["INCLUDES"])

0 comments on commit 9330086

Please sign in to comment.