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

redisclient exists, hmset, hgetallordered function, warm restart table names and table getDbId() function #208

Merged
merged 11 commits into from
Jul 20, 2018
53 changes: 51 additions & 2 deletions common/redisclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ int64_t RedisClient::del(const string &key)
return r.getContext()->integer;
}

bool RedisClient::exists(const string &key)
{
RedisCommand rexists;
rexists.format("EXISTS %s", key.c_str());
RedisReply r(m_db, rexists, REDIS_REPLY_INTEGER);
return (r.getContext()->integer > 0);
}

int64_t RedisClient::hdel(const string &key, const string &field)
{
RedisCommand shdel;
Expand All @@ -35,6 +43,32 @@ void RedisClient::hset(const string &key, const string &field, const string &val
RedisReply r(m_db, shset, REDIS_REPLY_INTEGER);
}

void RedisClient::hmset(const string &key, const vector<FieldValueTuple> &values)
Copy link
Contributor

@pavel-shirshov pavel-shirshov Jul 20, 2018

Choose a reason for hiding this comment

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

implement both functions as a template? The code in them are identical #Resolved

Copy link
Contributor

Choose a reason for hiding this comment

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

Agree. I will follow up.


In reply to: 204127551 [](ancestors = 204127551)

{
if (values.empty())
return;

Copy link
Contributor

@qiluo-msft qiluo-msft Jul 17, 2018

Choose a reason for hiding this comment

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

You don't need this check. Already checked in RedisCommand::formatHMSET() #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, will remote the check in hmset()

RedisCommand shmset;
shmset.formatHMSET(key, values);
RedisReply r(m_db, shmset, REDIS_REPLY_STATUS);
}

void RedisClient::hmset(const string &key, const std::map<std::string, std::string> &vmap)
Copy link
Contributor

@qiluo-msft qiluo-msft Jul 17, 2018

Choose a reason for hiding this comment

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

RedisClient::hmset [](start = 5, length = 18)

You can overload formatHMSET(), and call it. #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure what change you mean here?

Copy link
Contributor

@qiluo-msft qiluo-msft Jul 17, 2018

Choose a reason for hiding this comment

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

You may define another RedisCommand::formatHMSET() function, and call it here. #Closed

Copy link
Contributor

Choose a reason for hiding this comment

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

I will provide a PR.


In reply to: 203206068 [](ancestors = 203206068)

{
if (vmap.empty())
return;

vector<FieldValueTuple> values;
for(const auto it:vmap)
{
values.emplace_back(it.first, it.second);
}

RedisCommand shmset;
shmset.formatHMSET(key, values);
RedisReply r(m_db, shmset, REDIS_REPLY_STATUS);
}

void RedisClient::set(const string &key, const string &value)
{
RedisCommand sset;
Expand All @@ -52,7 +86,22 @@ unordered_map<string, string> RedisClient::hgetall(const string &key)

unordered_map<string, string> map;
for (unsigned int i = 0; i < ctx->elements; i += 2)
map[string(ctx->element[i]->str)] = string(ctx->element[i+1]->str);
map.emplace(ctx->element[i]->str, ctx->element[i+1]->str);

return map;
}

std::map<std::string, std::string> RedisClient::hgetallordered(const std::string &key)
Copy link
Contributor

@qiluo-msft qiluo-msft Jul 17, 2018

Choose a reason for hiding this comment

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

hgetallordered [](start = 48, length = 14)

Redis hash fields have no order. What is the use case? #Closed

Copy link
Contributor Author

@jipanyang jipanyang Jul 17, 2018

Choose a reason for hiding this comment

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

We have case to sort the field in string alphabetical order, instead of getting data into unordered_map<string, string> then sort them again outside, it is more efficient putting data directly into map<string, string>. #Closed

Copy link
Contributor

@qiluo-msft qiluo-msft Jul 17, 2018

Choose a reason for hiding this comment

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

This is a valid use case, however, it makes no sense to implement for every possible type of output container. And redis hash fields has no order by design.

You may implement a general function with inserter. Sample: http://www.cplusplus.com/forum/general/3194/


In reply to: 203201493 [](ancestors = 203201493)

Copy link
Contributor

Choose a reason for hiding this comment

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

I will provide a PR based on your PR.


In reply to: 203208567 [](ancestors = 203208567,203201493)

{
RedisCommand sincr;
sincr.format("HGETALL %s", key.c_str());
Copy link
Contributor

Choose a reason for hiding this comment

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

use string concatenation instead of format?

RedisReply r(m_db, sincr, REDIS_REPLY_ARRAY);

auto ctx = r.getContext();

map<string, string> map;
Copy link
Contributor

@pavel-shirshov pavel-shirshov Jul 20, 2018

Choose a reason for hiding this comment

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

what it was changed to map? Do we need an order here? #Resolved

Copy link
Contributor

Choose a reason for hiding this comment

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

It is not used and should be removed. My mistake and I will fix it.


In reply to: 204127846 [](ancestors = 204127846)

for (unsigned int i = 0; i < ctx->elements; i += 2)
map.emplace(ctx->element[i]->str, ctx->element[i+1]->str);

return map;
}
Expand Down Expand Up @@ -94,7 +143,7 @@ shared_ptr<string> RedisClient::get(const string &key)
sget.format("GET %s", key.c_str());
RedisReply r(m_db, sget);
auto reply = r.getContext();

if (reply->type == REDIS_REPLY_NIL)
{
return shared_ptr<string>(NULL);
Expand Down
5 changes: 5 additions & 0 deletions common/redisclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ class RedisClient

int64_t del(const std::string &key);

bool exists(const std::string &key);

int64_t hdel(const std::string &key, const std::string &field);

std::unordered_map<std::string, std::string> hgetall(const std::string &key);
std::map<std::string, std::string> hgetallordered(const std::string &key);

std::vector<std::string> keys(const std::string &key);

Expand All @@ -41,6 +44,8 @@ class RedisClient
void mset(const std::unordered_map<std::string, std::string> &map);

void hmset(const std::string &key, const std::unordered_map<std::string, std::string> &map);
void hmset(const std::string &key, const std::vector<FieldValueTuple> &values);
void hmset(const std::string &key, const std::map<std::string, std::string> &vmap);

std::shared_ptr<std::string> get(const std::string &key);

Expand Down
4 changes: 3 additions & 1 deletion common/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ typedef std::map<std::string,TableMap> TableDump;
class TableBase {
public:
TableBase(int dbId, const std::string &tableName)
: m_tableName(tableName)
: m_tableName(tableName), m_dbId(dbId)
{
/* Look up table separator for the provided DB */
auto it = tableNameSeparatorMap.find(dbId);
Expand All @@ -52,6 +52,7 @@ class TableBase {
}

std::string getTableName() const { return m_tableName; }
int getDbId() const { return m_dbId; }

/* Return the actual key name as a combination of tableName<table_separator>key */
std::string getKeyName(const std::string &key)
Expand All @@ -74,6 +75,7 @@ class TableBase {

std::string m_tableName;
std::string m_tableSeparator;
int m_dbId;
};

class TableEntryWritable {
Expand Down