-
Notifications
You must be signed in to change notification settings - Fork 261
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
Changes from 5 commits
151be00
77352f6
2939e6a
5fe4e57
c1414ed
bbff96a
b1f270e
dc7f42d
58dacdd
876a991
0d24fd3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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) | ||
{ | ||
if (values.empty()) | ||
return; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need this check. Already checked in RedisCommand::formatHMSET() #Closed There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
You can overload formatHMSET(), and call it. #Closed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure what change you mean here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You may define another RedisCommand::formatHMSET() function, and call it here. #Closed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
{ | ||
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; | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Redis hash fields have no order. What is the use case? #Closed There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
{ | ||
RedisCommand sincr; | ||
sincr.format("HGETALL %s", key.c_str()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
|
@@ -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); | ||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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)