Skip to content

Commit

Permalink
dpu db support by cli
Browse files Browse the repository at this point in the history
Signed-off-by: Ze Gan <[email protected]>
  • Loading branch information
Pterosaur committed Nov 21, 2023
1 parent 05e024e commit cc25de8
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 23 deletions.
2 changes: 1 addition & 1 deletion common/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ common_libswsscommon_la_SOURCES = \

common_libswsscommon_la_CXXFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(LIBNL_CFLAGS) $(CODE_COVERAGE_CXXFLAGS)
common_libswsscommon_la_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(LIBNL_CPPFLAGS) $(CODE_COVERAGE_CPPFLAGS)
common_libswsscommon_la_LIBADD = -lpthread $(LIBNL_LIBS) $(CODE_COVERAGE_LIBS) -lzmq -lboost_serialization -luuid -lyang
common_libswsscommon_la_LIBADD = -lpthread $(LIBNL_LIBS) $(CODE_COVERAGE_LIBS) -lzmq -lboost_serialization -luuid -lyang -ldashapi
common_libswsscommon_la_LDFLAGS = -Wl,-z,now $(LDFLAGS)

common_swssloglevel_SOURCES = \
Expand Down
61 changes: 56 additions & 5 deletions common/redisreply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <sstream>
#include <system_error>
#include <functional>
#include <memory>
#include <boost/algorithm/string.hpp>

#include "common/logger.h"
Expand All @@ -14,6 +15,8 @@
#include "common/rediscommand.h"
#include "common/stringutility.h"

#include <dash_api/utils.h>

using namespace std;
using namespace boost;

Expand Down Expand Up @@ -276,7 +279,7 @@ string RedisReply::to_string()
return RedisReply::to_string(this->getContext());
}

string RedisReply::to_string(redisReply *reply, string command)
string RedisReply::to_string(redisReply *reply, const string &command)
{
/*
Response format need keep as same as redis-py, redis-py using a command to result type mapping to convert result:
Expand Down Expand Up @@ -308,7 +311,31 @@ string RedisReply::to_string(redisReply *reply, string command)
}
}

string RedisReply::formatReply(string command, long long integer)
string RedisReply::to_string(redisReply *reply,const vector<string> &commands)
{
if (commands.empty())
{
return to_string(reply);
}

auto command = boost::to_upper_copy<string>(commands[0]);

if (commands.size() > 1)
{
if (reply->type == REDIS_REPLY_ARRAY
&& command == "HGETALL"
&& reply->elements > 1
&& to_string(reply->element[0]) == PROTOBUF_TYPE_TAG
&& reply->element[1]->type != REDIS_REPLY_STRING)
{
return formatPbReply(reply->element, reply->elements, commands[1]);
}
}

return to_string(reply, command);
}

string RedisReply::formatReply(const string &command, long long integer)
{
if (g_intToBoolCommands.find(command) != g_intToBoolCommands.end())
{
Expand All @@ -332,7 +359,7 @@ string RedisReply::formatReply(string command, long long integer)
return std::to_string(integer);
}

string RedisReply::formatReply(string command, const char* str, size_t len)
string RedisReply::formatReply(const string &command, const char* str, size_t len)
{
string result = string(str, len);
if (g_strToBoolCommands.find(command) != g_strToBoolCommands.end()
Expand All @@ -357,7 +384,7 @@ string RedisReply::formatReply(string command, const char* str, size_t len)
return result;
}

string RedisReply::formatReply(string command, struct redisReply **element, size_t elements)
string RedisReply::formatReply(const string &command, struct redisReply **element, size_t elements)
{
if (command == "HGETALL")
{
Expand Down Expand Up @@ -475,7 +502,31 @@ string RedisReply::formatTupleReply(struct redisReply **element, size_t elements
return swss::join(", ", '(', ')', elementvector.begin(), elementvector.end());
}

string RedisReply::formatStringWithQuot(string str)
string RedisReply::formatPbReply(struct redisReply **element, size_t elements, const string &key)
{
if (
elements != 2
|| to_string(element[0]) != PROTOBUF_TYPE_TAG
|| element[1]->type != REDIS_REPLY_STRING)
{
throw system_error(make_error_code(errc::io_error),
"Invalid result");
}

std::string pb_buffer(element[1]->str, element[1]->len);

// Extract table name
std::string table_name(
key.begin(),
find_if(
key.begin(),
key.end(),
[](char c){ return c == ':' || c == '|';}));

return dash::PbBinaryToJsonString(table_name, pb_buffer);
}

string RedisReply::formatStringWithQuot(const string &str)
{
if (str.find('\'') != std::string::npos)
{
Expand Down
12 changes: 7 additions & 5 deletions common/redisreply.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,22 +94,24 @@ class RedisReply

std::string to_string();

static std::string to_string(redisReply *reply, std::string command = std::string());
static std::string to_string(redisReply *reply,const std::string &command = std::string());
static std::string to_string(redisReply *reply,const std::vector<std::string> &commands);

private:
void checkStatus(const char *status);
void checkReply();

static std::string formatReply(std::string command, struct redisReply **element, size_t elements);
static std::string formatReply(std::string command, long long integer);
static std::string formatReply(std::string command, const char* str, size_t len);
static std::string formatReply(const std::string &command, struct redisReply **element, size_t elements);
static std::string formatReply(const std::string &command, long long integer);
static std::string formatReply(const std::string &command, const char* str, size_t len);
static std::string formatSscanReply(struct redisReply **element, size_t elements);
static std::string formatHscanReply(struct redisReply **element, size_t elements);
static std::string formatDictReply(struct redisReply **element, size_t elements);
static std::string formatArrayReply(struct redisReply **element, size_t elements);
static std::string formatListReply(struct redisReply **element, size_t elements);
static std::string formatTupleReply(struct redisReply **element, size_t elements);
static std::string formatStringWithQuot(std::string str);
static std::string formatPbReply(struct redisReply **element, size_t elements, const std::string &key);
static std::string formatStringWithQuot(const std::string &str);

redisReply *m_reply;
};
Expand Down
2 changes: 2 additions & 0 deletions common/schema.h
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,8 @@ namespace swss {
#define DEL_COMMAND "DEL"
#define EMPTY_PREFIX ""

#define PROTOBUF_TYPE_TAG "pb"

#define CFG_DTEL_TABLE_NAME "DTEL"
#define CFG_DTEL_REPORT_SESSION_TABLE_NAME "DTEL_REPORT_SESSION"
#define CFG_DTEL_INT_SESSION_TABLE_NAME "DTEL_INT_SESSION"
Expand Down
13 changes: 1 addition & 12 deletions sonic-db-cli/sonic-db-cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,7 @@ int executeCommands(
based on our usage in SONiC, None and list type output from python API needs to be modified
with these changes, it is enough for us to mimic redis-cli in SONiC so far since no application uses tty mode redis-cli output
*/
auto commandName = getCommandName(commands);
cout << RedisReply::to_string(reply.getContext(), commandName) << endl;
cout << RedisReply::to_string(reply.getContext(), commands) << endl;
}
catch (const std::system_error& e)
{
Expand Down Expand Up @@ -371,13 +370,3 @@ int cli_exception_wrapper(
return 1;
}
}

string getCommandName(vector<string>& commands)
{
if (commands.size() == 0)
{
return string();
}

return boost::to_upper_copy<string>(commands[0]);
}

0 comments on commit cc25de8

Please sign in to comment.