diff --git a/common/table.cpp b/common/table.cpp index 86cd97c1718..1fccae17911 100644 --- a/common/table.cpp +++ b/common/table.cpp @@ -83,6 +83,47 @@ void Table::del(std::string key, std::string /* op */) "DEL operation failed"); } +bool Table::getField(std::string key, std::string field, std::string &value) +{ + std::string hget = formatHGET(getKeyName(key).c_str(), + field.c_str()); + + RedisReply r(m_db, hget, REDIS_REPLY_INTEGER); + + if (r.getContext()->type != REDIS_REPLY_STRING) + { + return false; + } + + value = std::string(r.getContext()->str); + + return true; +} + +void Table::setField(std::string key, std::string field, std::string value) +{ + FieldValueTuple entry(field, value); + + std::vector values { entry }; + + set(key, values); +} + +void Table::delField(std::string key, std::string field) +{ + std::string hdel = formatHDEL(getKeyName(key), field); + + RedisReply r(m_db, hdel, REDIS_REPLY_INTEGER); + + if (r.getContext()->type != REDIS_REPLY_INTEGER) + throw system_error(make_error_code(errc::io_error), + "DEL operation failed"); +} + +Table::~Table() +{ +} + void Table::multi() { while (!m_expectedResults.empty()) @@ -166,4 +207,26 @@ string Table::formatHSET(const string& key, const string& field, return hset; } +string Table::formatHGET(const string& key, const string& field) +{ + char *temp; + int len = redisFormatCommand(&temp, "HGET %s %s", + key.c_str(), + field.c_str()); + string hget(temp, len); + free(temp); + return hget; +} + +string Table::formatHDEL(const string& key, const string& field) +{ + char *temp; + int len = redisFormatCommand(&temp, "HDEL %s %s", + key.c_str(), + field.c_str()); + string hdel(temp, len); + free(temp); + return hdel; +} + } diff --git a/common/table.h b/common/table.h index 78d9893624a..d079341ff25 100644 --- a/common/table.h +++ b/common/table.h @@ -33,6 +33,12 @@ class Table { /* Delete an entry in the DB directly (op not in used) */ virtual void del(std::string key, std::string op = ""); + bool getField(std::string key, std::string field, std::string &value); + void setField(std::string key, std::string field, std::string value); + void delField(std::string key, std::string field); + + virtual ~Table(); + protected: /* Return the actual key name as a comibation of tableName:key */ std::string getKeyName(std::string key); @@ -57,6 +63,14 @@ class Table { const std::string &field, const std::string &value); + /* Format HGET key field command */ + static std::string formatHGET(const std::string& key, + const std::string& field); + + /* Format HDEL key field command */ + static std::string formatHDEL(const std::string& key, + const std::string& field); + DBConnector *m_db; std::string m_tableName;