Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions common/table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<FieldValueTuple> 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()
{
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need empty destructor?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since declared as virtual

void Table::multi()
{
while (!m_expectedResults.empty())
Expand Down Expand Up @@ -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;
}

}
14 changes: 14 additions & 0 deletions common/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;

Expand Down