diff --git a/common/redisclient.cpp b/common/redisclient.cpp deleted file mode 100644 index c996356a3570..000000000000 --- a/common/redisclient.cpp +++ /dev/null @@ -1,269 +0,0 @@ -#include "redisclient.h" -#include "swss/logger.h" - -namespace swss -{ - -RedisClient::RedisClient(swss::DBConnector *db): - m_db(db) -{ -} - -int64_t RedisClient::del(std::string key) -{ - char *temp; - int len = redisFormatCommand(&temp, "DEL %s", key.c_str()); - std::string sdel(temp, len); - free(temp); - - RedisReply r(m_db, sdel, REDIS_REPLY_INTEGER, true); - - if (r.getContext()->type != REDIS_REPLY_INTEGER) - throw std::runtime_error("DEL operation failed"); - - return r.getContext()->integer; -} - -int64_t RedisClient::hdel(std::string key, std::string field) -{ - char *temp; - int len = redisFormatCommand(&temp, "HDEL %s %s", key.c_str(), field.c_str()); - std::string shdel(temp, len); - free(temp); - - RedisReply r(m_db, shdel, REDIS_REPLY_INTEGER, true); - - if (r.getContext()->type != REDIS_REPLY_INTEGER) - throw std::runtime_error("HDEL operation failed"); - - return r.getContext()->integer; -} - -void RedisClient::hset(std::string key, std::string field, std::string value) -{ - char *temp; - int len = redisFormatCommand(&temp, "HSET %s %s %s", key.c_str(), field.c_str(), value.c_str()); - std::string shset(temp, len); - free(temp); - - RedisReply r(m_db, shset, REDIS_REPLY_INTEGER, true); - - if (r.getContext()->type != REDIS_REPLY_INTEGER) - throw std::runtime_error("HSET operation failed"); -} - -void RedisClient::set(std::string key, std::string value) -{ - char *temp; - int len = redisFormatCommand(&temp, "SET %s %s", key.c_str(), value.c_str()); - std::string sset(temp, len); - free(temp); - - RedisReply r(m_db, sset, REDIS_REPLY_STATUS, true); - - if (r.getContext()->type != REDIS_REPLY_STATUS) - throw std::runtime_error("SET operation failed"); -} - -std::unordered_map RedisClient::hgetall(std::string key) -{ - std::unordered_map map; - - char *temp; - int len = redisFormatCommand(&temp, "HGETALL %s", key.c_str()); - - std::string sincr(temp, len); - free(temp); - - RedisReply r(m_db, sincr, REDIS_REPLY_ARRAY, true); - - if (r.getContext()->type != REDIS_REPLY_ARRAY) - throw std::runtime_error("HGETALL operation failed"); - - auto ctx = r.getContext(); - - for (unsigned int i = 0; i < ctx->elements; i += 2) - map[std::string(ctx->element[i]->str)] = std::string(ctx->element[i+1]->str); - - return map; -} - -std::vector RedisClient::keys(std::string key) -{ - std::vector list; - - char *temp; - int len = redisFormatCommand(&temp, "KEYS %s", key.c_str()); - - std::string skeys(temp, len); - free(temp); - - RedisReply r(m_db, skeys, REDIS_REPLY_ARRAY, true); - - if (r.getContext()->type != REDIS_REPLY_ARRAY) - throw std::runtime_error("KEYS operation failed"); - - auto ctx = r.getContext(); - - for (unsigned int i = 0; i < ctx->elements; i++) - list.push_back(ctx->element[i]->str); - - return list; -} - -int64_t RedisClient::incr(std::string key) -{ - char *temp; - int len = redisFormatCommand(&temp, "INCR %s", key.c_str()); - - std::string sincr(temp, len); - free(temp); - - RedisReply r(m_db, sincr, REDIS_REPLY_INTEGER, true); - - if (r.getContext()->type != REDIS_REPLY_INTEGER) - throw std::runtime_error("INCR command failed"); - - return r.getContext()->integer; -} - -int64_t RedisClient::decr(std::string key) -{ - char *temp; - int len = redisFormatCommand(&temp, "DECR %s", key.c_str()); - - std::string sdecr(temp, len); - free(temp); - - RedisReply r(m_db, sdecr, REDIS_REPLY_INTEGER, true); - - if (r.getContext()->type != REDIS_REPLY_INTEGER) - throw std::runtime_error("DECR command failed"); - - return r.getContext()->integer; -} - -std::shared_ptr RedisClient::get(std::string key) -{ - char *temp; - int len = redisFormatCommand(&temp, "GET %s", key.c_str()); - - std::string sget(temp, len); - free(temp); - - redisReply *reply; - - redisAppendFormattedCommand(m_db->getContext(), sget.c_str(), sget.length()); - redisGetReply(m_db->getContext(), (void**)&reply); - - if (!reply) - throw std::runtime_error("GET failed, memory exception"); - - if (reply->type == REDIS_REPLY_NIL) - { - freeReplyObject(reply); - return std::shared_ptr(NULL); - } - - if (reply->type == REDIS_REPLY_STRING) - { - std::shared_ptr ptr(new std::string(reply->str)); - freeReplyObject(reply); - return ptr; - } - - freeReplyObject(reply); - - throw std::runtime_error("GET failed, memory exception"); -} - -std::shared_ptr RedisClient::hget(std::string key, std::string field) -{ - char *temp; - int len = redisFormatCommand(&temp, "HGET %s %s", key.c_str(), field.c_str()); - - std::string shget(temp, len); - free(temp); - - redisReply *reply; - - redisAppendFormattedCommand(m_db->getContext(), shget.c_str(), shget.length()); - redisGetReply(m_db->getContext(), (void**)&reply); - - if (!reply) - { - SWSS_LOG_ERROR("HGET failed, null reply, memory exception: %s: %s", key.c_str(), field.c_str()); - - throw std::runtime_error("HGET failed, null reply, memory exception"); - } - - if (reply->type == REDIS_REPLY_NIL) - { - freeReplyObject(reply); - return std::shared_ptr(NULL); - } - - if (reply->type == REDIS_REPLY_STRING) - { - std::shared_ptr ptr(new std::string(reply->str)); - freeReplyObject(reply); - return ptr; - } - - SWSS_LOG_ERROR("HGET failed, reply-type: %d, %s: %s", reply->type, key.c_str(), field.c_str()); - - freeReplyObject(reply); - - throw std::runtime_error("HGET failed, unexpected reply type, memory exception"); -} - -int64_t RedisClient::rpush(std::string list, std::string item) -{ - char *temp; - int len = redisFormatCommand(&temp, "RPUSH %s %s", list.c_str(), item.c_str()); - - std::string srpush(temp, len); - free(temp); - - RedisReply r(m_db, srpush, REDIS_REPLY_INTEGER, true); - - if (r.getContext()->type != REDIS_REPLY_INTEGER) - throw std::runtime_error("RPUSH command failed"); - - return r.getContext()->integer; -} - -std::shared_ptr RedisClient::blpop(std::string list, int timeout) -{ - char *temp; - int len = redisFormatCommand(&temp, "BLPOP %s %d", list.c_str(), timeout); - - std::string sblpop(temp, len); - free(temp); - - redisReply *reply; - - redisAppendFormattedCommand(m_db->getContext(), sblpop.c_str(), sblpop.length()); - redisGetReply(m_db->getContext(), (void**)&reply); - - if (!reply) - throw std::runtime_error("BLPOP failed, memory exception"); - - if (reply->type == REDIS_REPLY_NIL) - { - freeReplyObject(reply); - return std::shared_ptr(NULL); - } - - if (reply->type == REDIS_REPLY_STRING) - { - std::shared_ptr ptr(new std::string(reply->str)); - freeReplyObject(reply); - return ptr; - } - - freeReplyObject(reply); - - throw std::runtime_error("GET failed, memory exception"); -} -} diff --git a/common/redisclient.h b/common/redisclient.h deleted file mode 100644 index 378099dc500d..000000000000 --- a/common/redisclient.h +++ /dev/null @@ -1,67 +0,0 @@ -#ifndef __REDISCLIENT_H__ -#define __REDISCLIENT_H__ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "swss/dbconnector.h" -#include "swss/redisreply.h" - -namespace swss -{ - -class RedisClient -{ - public: - - RedisClient(swss::DBConnector *db); - - int64_t del(std::string key); - - int64_t hdel(std::string key, std::string field); - - std::unordered_map hgetall(std::string key); - - std::vector keys(std::string key); - - std::vector hkeys(std::string key); - - void set(std::string key, std::string value); - - void hset(std::string key, std::string field, std::string value); - - void mset(std::unordered_map map); - - void hmset(std::string key, std::unordered_map map); - - std::shared_ptr get(std::string key); - - std::shared_ptr hget(std::string key, std::string field); - - std::vector> mget(std::vector keys); - - std::vector> hmget(std::string key, std::vector fields); - - int64_t incr(std::string key); - - int64_t decr(std::string key); - - int64_t rpush(std::string list, std::string item); - - std::shared_ptr blpop(std::string list, int timeout); - - private: - swss::DBConnector *m_db; -}; - -} - -#endif // __REDISCLIENT_H__ diff --git a/lib/inc/sai_redis.h b/lib/inc/sai_redis.h index a54677d9ef5f..928c16454842 100644 --- a/lib/inc/sai_redis.h +++ b/lib/inc/sai_redis.h @@ -11,8 +11,8 @@ extern "C" { #include "sai.h" } -#include "common/redisclient.h" +#include "swss/redisclient.h" #include "swss/dbconnector.h" #include "swss/producertable.h" #include "swss/consumertable.h" diff --git a/lib/src/Makefile.am b/lib/src/Makefile.am index f29881ea494d..8a827c85078b 100644 --- a/lib/src/Makefile.am +++ b/lib/src/Makefile.am @@ -41,8 +41,7 @@ libsairedis_la_SOURCES = \ sai_redis_generic_set.cpp \ sai_redis_generic_get.cpp \ sai_redis_notifications.cpp \ - sai_redis_record.cpp \ - ../../common/redisclient.cpp + sai_redis_record.cpp libsairedis_la_CPPFLAGS = $(DBGFLAGS) $(AM_CPPFLAGS) $(CFLAGS_COMMON) libsairedis_la_LIBADD = -lhiredis -lswsscommon diff --git a/syncd/Makefile.am b/syncd/Makefile.am index c1725189ec08..ef5026fa0693 100644 --- a/syncd/Makefile.am +++ b/syncd/Makefile.am @@ -26,8 +26,7 @@ syncd_SOURCES = \ syncd_reinit.cpp \ syncd_hard_reinit.cpp \ syncd_notifications.cpp \ - syncd_counters.cpp \ - ../common/redisclient.cpp + syncd_counters.cpp syncd_CPPFLAGS = $(DBGFLAGS) $(AM_CPPFLAGS) $(CFLAGS_COMMON) $(SAIFLAGS) syncd_LDADD = -lhiredis -lswsscommon -lsai -lpthread -L$(top_srcdir)/meta/.libs -lsaimetadata diff --git a/syncd/syncd.h b/syncd/syncd.h index 5d800f3f8cef..05d0779597aa 100644 --- a/syncd/syncd.h +++ b/syncd/syncd.h @@ -25,9 +25,9 @@ extern "C" { #include "sai.h" } -#include "common/redisclient.h" #include "meta/saiserialize.h" #include "meta/saiattributelist.h" +#include "swss/redisclient.h" #include "swss/dbconnector.h" #include "swss/producertable.h" #include "swss/consumertable.h"