Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance on table set command #13

Merged
merged 2 commits into from
Apr 29, 2016
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
37 changes: 31 additions & 6 deletions common/table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,14 @@ bool Table::get(std::string key, vector<FieldValueTuple> &values)
void Table::set(std::string key, std::vector<FieldValueTuple> &values,
std::string /*op*/)
{
/* We are doing transaction for AON (All or nothing) */
multi();
for (FieldValueTuple &i : values)
enqueue(formatHSET(getKeyName(key), fvField(i), fvValue(i)),
REDIS_REPLY_INTEGER, true);
if (values.size() == 0)
return;

exec();
const std::string &cmd = formatHMSET(getKeyName(key), values);

RedisReply r(m_db, cmd, REDIS_REPLY_STATUS);

r.checkStatusOK();
}

void Table::del(std::string key, std::string /* op */)
Expand Down Expand Up @@ -199,6 +200,30 @@ void Table::enqueue(std::string command, int exepectedResult, bool isFormatted)
m_expectedResults.push(exepectedResult);
}

string Table::formatHMSET(const std::string &key,
const std::vector<FieldValueTuple> &values)
{
if (values.size() == 0)
throw system_error(make_error_code(errc::io_error),
"HMSET must have some arguments");

const char* cmd = "HMSET";

std::vector<const char*> args = { cmd, key.c_str() };

for (const auto &fvt: values)
{
args.push_back(fvField(fvt).c_str());
args.push_back(fvValue(fvt).c_str());
}

char *temp;
int len = redisFormatCommandArgv(&temp, args.size(), args.data(), NULL);
string hmset(temp, len);
free(temp);
return hmset;
}

string Table::formatHSET(const string& key, const string& field,
const string& value)
{
Expand Down
4 changes: 4 additions & 0 deletions common/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ class Table {
redisReply* queueResultsFront();
void queueResultsPop();

/* Format HMSET key multiple field value command */
static std::string formatHMSET(const std::string &key,
const std::vector<FieldValueTuple> &values);

/* Format HSET key field value command */
static std::string formatHSET(const std::string &key,
const std::string &field,
Expand Down