-
Notifications
You must be signed in to change notification settings - Fork 342
redisclient exists, hmset, hgetallordered function, warm restart table names and table getDbId() function #208
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
Changes from 4 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; | ||
| } | ||
|
|
||
| int64_t RedisClient::exists(const string &key) | ||
|
||
| { | ||
| RedisCommand sexists; | ||
|
||
| sexists.format("EXISTS %s", key.c_str()); | ||
|
||
| RedisReply r(m_db, sexists, REDIS_REPLY_INTEGER); | ||
| return r.getContext()->integer; | ||
| } | ||
|
|
||
| int64_t RedisClient::hdel(const string &key, const string &field) | ||
| { | ||
| RedisCommand shdel; | ||
|
|
@@ -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) | ||
|
||
| return; | ||
|
|
||
|
||
| 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) | ||
|
||
| { | ||
| if (vmap.size() == 0) | ||
|
||
| return; | ||
|
|
||
| vector<FieldValueTuple> values; | ||
| auto it = vmap.begin(); | ||
| while (it != vmap.end()) | ||
|
||
| { | ||
| values.push_back(FieldValueTuple(it->first, it->second)); | ||
|
||
| it++; | ||
| } | ||
|
|
||
| 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; | ||
|
|
@@ -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) | ||
|
||
| { | ||
| RedisCommand sincr; | ||
| sincr.format("HGETALL %s", key.c_str()); | ||
|
||
| RedisReply r(m_db, sincr, REDIS_REPLY_ARRAY); | ||
|
|
||
| auto ctx = r.getContext(); | ||
|
|
||
| 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); | ||
|
||
|
|
||
| return map; | ||
| } | ||
|
|
||
| vector<string> RedisClient::keys(const string &key) | ||
| { | ||
| RedisCommand skeys; | ||
|
|
@@ -94,7 +145,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); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ namespace swss { | |
| #define PFC_WD_DB 5 | ||
| #define FLEX_COUNTER_DB 5 | ||
| #define STATE_DB 6 | ||
| #define RESTORE_DB 7 | ||
|
||
|
|
||
| /***** APPLICATION DATABASE *****/ | ||
|
|
||
|
|
@@ -32,6 +33,8 @@ namespace swss { | |
|
|
||
| #define APP_COPP_TABLE_NAME "COPP_TABLE" | ||
|
|
||
| #define APP_WARM_RESTART_TABLE_NAME "WARM_RESTART_TABLE" | ||
|
|
||
| /***** TO BE REMOVED *****/ | ||
|
|
||
| #define APP_TC_TO_QUEUE_MAP_TABLE_NAME "TC_TO_QUEUE_MAP_TABLE" | ||
|
|
@@ -77,7 +80,7 @@ namespace swss { | |
| #define PFC_WD_STATE_TABLE "PFC_WD_STATE_TABLE" | ||
| #define PFC_WD_PORT_COUNTER_ID_LIST "PORT_COUNTER_ID_LIST" | ||
| #define PFC_WD_QUEUE_COUNTER_ID_LIST "QUEUE_COUNTER_ID_LIST" | ||
| #define PFC_WD_QUEUE_ATTR_ID_LIST "QUEUE_ATTR_ID_LIST" | ||
| #define PFC_WD_QUEUE_ATTR_ID_LIST "QUEUE_ATTR_ID_LIST" | ||
| #define PLUGIN_TABLE "PLUGIN_TABLE" | ||
| #define LUA_PLUGIN_TYPE "LUA_PLUGIN_TYPE" | ||
| #define SAI_OBJECT_TYPE "SAI_OBJECT_TYPE" | ||
|
|
@@ -141,6 +144,8 @@ namespace swss { | |
| #define CFG_BUFFER_PORT_INGRESS_PROFILE_LIST_NAME "BUFFER_PORT_INGRESS_PROFILE_LIST" | ||
| #define CFG_BUFFER_PORT_EGRESS_PROFILE_LIST_NAME "BUFFER_PORT_EGRESS_PROFILE_LIST" | ||
|
|
||
|
|
||
| #define CFG_WARM_RESTART_TABLE_NAME "WARM_RESTART" | ||
| /***** STATE DATABASE *****/ | ||
|
|
||
| #define STATE_PORT_TABLE_NAME "PORT_TABLE" | ||
|
|
||
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?
Uh oh!
There was an error while loading. Please reload this page.
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)