-
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
Conversation
Signed-off-by: Jipan Yang <jipan.yang@alibaba-inc.com>
Signed-off-by: Jipan Yang <jipan.yang@alibaba-inc.com>
Signed-off-by: Jipan Yang <jipan.yang@alibaba-inc.com>
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.
as comments
common/redisclient.cpp
Outdated
@@ -20,6 +20,14 @@ int64_t RedisClient::del(const string &key) | |||
return r.getContext()->integer; | |||
} | |||
|
|||
int64_t RedisClient::exists(const string &key) |
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.
I think it's better to use bool as return value
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.
int64_t is the return value type from libhiredis for exists call.
We are giving one key as parameter here, using bool as return value seems reasonable. But is it a concern if we deviate from libhiredis as to the return value type?
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.
Hi Jipan,
I got your point, but I think in case of this library we have abstracted hiredis library and use swss-common instead. EXISTS returns 0 or 1 with Boolean semantics. So I think it's better to make it explicitly Boolean.
When I initially saw that your method return int64_t I checked redis documentation, because it was not clear for me why we need int64_t to return exists result. #Closed
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.
Ok, will change to bool return.
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.
https://redis.io/commands/exists
Because redis command return integer. However, we don't need it.
In reply to: 203180025 [](ancestors = 203180025)
common/redisclient.cpp
Outdated
int64_t RedisClient::exists(const string &key) | ||
{ | ||
RedisCommand sexists; | ||
sexists.format("EXISTS %s", key.c_str()); |
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.
I think It would be faster to concatenate to strings than use .format her.
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.
.format is the redis library function, it is being used in almost all the exist swss-common redis related functions. I assume it should be fast and safe enough?
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 with you. But I think concatenating two strings are faster than parse a format string.
So it's more a speed optimization.
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.
After checking it again, RedisCommand is swss-common class, format is the member function of it which in turn calls redisvFormatCommand() to populate the command data.
I saw comments like "Build the command string accordingly to protocol" in redisvFormatCommand(), probably it is safer to use format() here to avoid any potential issue.
common/redisclient.cpp
Outdated
auto it = vmap.begin(); | ||
while (it != vmap.end()) | ||
{ | ||
values.push_back(FieldValueTuple(it->first, it->second)); |
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.
,emplace_back(it->first, it->second)
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.
ok
common/redisclient.cpp
Outdated
|
||
vector<FieldValueTuple> values; | ||
auto it = vmap.begin(); | ||
while (it != vmap.end()) |
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.
is probably better to use
for(const auto it: vmap) ?
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.
ok.
common/redisclient.cpp
Outdated
std::map<std::string, std::string> RedisClient::hgetallordered(const std::string &key) | ||
{ | ||
RedisCommand sincr; | ||
sincr.format("HGETALL %s", key.c_str()); |
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.
use string concatenation instead of format?
common/redisclient.cpp
Outdated
|
||
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); |
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.
map.emplace(ctx->element[i]->str, ctx->element[i+1]->str) ?
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.
ok
common/redisclient.cpp
Outdated
@@ -35,6 +43,34 @@ 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.size() == 0) |
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.
if (values.empty()) ?
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.
ok
common/redisclient.cpp
Outdated
|
||
void RedisClient::hmset(const string &key, const std::map<std::string, std::string> &vmap) | ||
{ | ||
if (vmap.size() == 0) |
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.
if (vmap.empty())?
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.
ok
common/schema.h
Outdated
@@ -13,6 +13,7 @@ namespace swss { | |||
#define PFC_WD_DB 5 | |||
#define FLEX_COUNTER_DB 5 | |||
#define STATE_DB 6 | |||
#define RESTORE_DB 7 |
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.
RESTORE_DB [](start = 8, length = 10)
Let's postpone schema changes into future PR. #Closed
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.
Sorry, wrong push. Should be gone now.
common/redisclient.cpp
Outdated
@@ -20,6 +20,14 @@ int64_t RedisClient::del(const string &key) | |||
return r.getContext()->integer; | |||
} | |||
|
|||
int64_t RedisClient::exists(const string &key) | |||
{ | |||
RedisCommand sexists; |
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.
sexists [](start = 17, length = 7)
Let's use a different var name. #Closed
common/redisclient.cpp
Outdated
@@ -20,6 +20,14 @@ int64_t RedisClient::del(const string &key) | |||
return r.getContext()->integer; | |||
} | |||
|
|||
int64_t RedisClient::exists(const string &key) |
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.
key [](start = 42, length = 3)
If the key contains any blank characters (including \t), there will be problem ignored. #Closed
Signed-off-by: Jipan Yang <jipan.yang@alibaba-inc.com>
common/redisclient.cpp
Outdated
{ | ||
if (values.size() == 0) | ||
return; | ||
|
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, will remote the check in hmset()
common/redisclient.cpp
Outdated
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 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
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.
Not sure what change you mean here?
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
common/redisclient.cpp
Outdated
@@ -57,6 +93,21 @@ unordered_map<string, string> RedisClient::hgetall(const string &key) | |||
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 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
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.
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 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)
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.
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.
As comments.
Signed-off-by: Jipan Yang <jipan.yang@alibaba-inc.com>
Signed-off-by: Jipan Yang <jipan.yang@alibaba-inc.com>
9ff098a
to
b1f270e
Compare
Signed-off-by: Qi Luo <qiluo-msft@users.noreply.github.com>
Signed-off-by: Qi Luo <qiluo-msft@users.noreply.github.com>
Help resolve review comments
bool RedisClient::exists(const string &key) | ||
{ | ||
RedisCommand rexists; | ||
if (key.find_first_of(" \t") != string::npos) |
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.
Probably it's better to trim spaces and tabs from the key? #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.
Actually this input validation is to prevent any input like "key1 key2". Not for conveniently trimming.
In reply to: 204127076 [](ancestors = 204127076)
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.
I'm good. Thank you for the explanations. I'd a comment explaining why do we need this validation here.
common/redisclient.cpp
Outdated
@@ -35,6 +48,20 @@ 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) |
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.
common/redisclient.cpp
Outdated
{ | ||
RedisCommand sincr; | ||
sincr.format("HGETALL %s", key.c_str()); | ||
RedisReply r(m_db, sincr, REDIS_REPLY_ARRAY); | ||
|
||
auto ctx = r.getContext(); | ||
|
||
unordered_map<string, string> map; | ||
map<string, string> map; |
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.
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 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)
* Fix swig compile Signed-off-by: Qi Luo <qiluo-msft@users.noreply.github.com> * Fix: implement template function in header filese Signed-off-by: Qi Luo <qiluo-msft@users.noreply.github.com> * Remove unused variable Signed-off-by: Qi Luo <qiluo-msft@users.noreply.github.com> * Implement template hmset Signed-off-by: Qi Luo <qiluo-msft@users.noreply.github.com>
This is to add some swss common function support for warm reboot development:
<1> redisclient exists(), hmset(), hgetallordered() functions.
<2> warm restart table names in appDB and configDB
<3> getDbId() function for Table class