redisclient exists, hmset, hgetallordered function, warm restart table names and table getDbId() function#208
Conversation
Signed-off-by: Jipan Yang <[email protected]>
Signed-off-by: Jipan Yang <[email protected]>
Signed-off-by: Jipan Yang <[email protected]>
common/redisclient.cpp
Outdated
| return r.getContext()->integer; | ||
| } | ||
|
|
||
| int64_t RedisClient::exists(const string &key) |
There was a problem hiding this comment.
I think it's better to use bool as return value
There was a problem hiding this comment.
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.
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.
Ok, will change to bool return.
There was a problem hiding this comment.
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.
I think It would be faster to concatenate to strings than use .format her.
There was a problem hiding this comment.
.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.
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.
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.
,emplace_back(it->first, it->second)
common/redisclient.cpp
Outdated
|
|
||
| vector<FieldValueTuple> values; | ||
| auto it = vmap.begin(); | ||
| while (it != vmap.end()) |
There was a problem hiding this comment.
is probably better to use
for(const auto it: vmap) ?
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.
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.
map.emplace(ctx->element[i]->str, ctx->element[i+1]->str) ?
common/redisclient.cpp
Outdated
|
|
||
| void RedisClient::hmset(const string &key, const vector<FieldValueTuple> &values) | ||
| { | ||
| if (values.size() == 0) |
There was a problem hiding this comment.
if (values.empty()) ?
common/redisclient.cpp
Outdated
|
|
||
| void RedisClient::hmset(const string &key, const std::map<std::string, std::string> &vmap) | ||
| { | ||
| if (vmap.size() == 0) |
common/schema.h
Outdated
| #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.
RESTORE_DB [](start = 8, length = 10)
Let's postpone schema changes into future PR. #Closed
There was a problem hiding this comment.
Sorry, wrong push. Should be gone now.
common/redisclient.cpp
Outdated
|
|
||
| int64_t RedisClient::exists(const string &key) | ||
| { | ||
| RedisCommand sexists; |
There was a problem hiding this comment.
sexists [](start = 17, length = 7)
Let's use a different var name. #Closed
common/redisclient.cpp
Outdated
| return r.getContext()->integer; | ||
| } | ||
|
|
||
| int64_t RedisClient::exists(const string &key) |
There was a problem hiding this comment.
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 <[email protected]>
common/redisclient.cpp
Outdated
| { | ||
| if (values.size() == 0) | ||
| return; | ||
|
|
There was a problem hiding this comment.
You don't need this check. Already checked in RedisCommand::formatHMSET() #Closed
There was a problem hiding this comment.
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.
RedisClient::hmset [](start = 5, length = 18)
You can overload formatHMSET(), and call it. #Closed
There was a problem hiding this comment.
Not sure what change you mean here?
There was a problem hiding this comment.
You may define another RedisCommand::formatHMSET() function, and call it here. #Closed
There was a problem hiding this comment.
common/redisclient.cpp
Outdated
| return map; | ||
| } | ||
|
|
||
| std::map<std::string, std::string> RedisClient::hgetallordered(const std::string &key) |
There was a problem hiding this comment.
hgetallordered [](start = 48, length = 14)
Redis hash fields have no order. What is the use case? #Closed
There was a problem hiding this comment.
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.
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.
Signed-off-by: Jipan Yang <[email protected]>
Signed-off-by: Jipan Yang <[email protected]>
9ff098a to
b1f270e
Compare
Signed-off-by: Qi Luo <[email protected]>
Signed-off-by: Qi Luo <[email protected]>
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.
Probably it's better to trim spaces and tabs from the key? #Resolved
There was a problem hiding this comment.
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.
I'm good. Thank you for the explanations. I'd a comment explaining why do we need this validation here.
common/redisclient.cpp
Outdated
| 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.
implement both functions as a template? The code in them are identical #Resolved
There was a problem hiding this comment.
common/redisclient.cpp
Outdated
| auto ctx = r.getContext(); | ||
|
|
||
| unordered_map<string, string> map; | ||
| map<string, string> map; |
There was a problem hiding this comment.
what it was changed to map? Do we need an order here? #Resolved
There was a problem hiding this comment.
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 <[email protected]> * Fix: implement template function in header filese Signed-off-by: Qi Luo <[email protected]> * Remove unused variable Signed-off-by: Qi Luo <[email protected]> * Implement template hmset Signed-off-by: Qi Luo <[email protected]>
…erits from YCableBase required for Y-Cable API's in sonic-platform-daemons (sonic-net#208) This PR adds support for YCable class required for platform-daemons to use the YCable API's for Broadcom. Description Basically a vendor specific implementation of abstract YCableBase class . detailed design discussion can be found https://github.com/Azure/SONiC/pull/757/files Motivation and Context Required for transitioning to vendor agnostic API's to be called by xcvrd, so that all types of cables can be called. How Has This Been Tested? Ran the changes on Arista7050cx3 switch, making changes inside the container. Signed-off-by: vaibhav-dahiya <[email protected]>
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