-
Notifications
You must be signed in to change notification settings - Fork 320
Add PublisherEventTable/SubscriberEventTable classes #1020
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
base: master
Are you sure you want to change the base?
Changes from 9 commits
172c072
031aba1
798b86e
e15f7f9
a3f474a
f8452f6
57eea2b
b44cde6
e306266
712dab7
0cdfcc5
f01201d
d94f3f2
fe9a204
9849e30
a5cd031
8e6c52d
1397c0f
6602971
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 |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| #include <nlohmann/json.hpp> | ||
| #include "publishereventtable.h" | ||
| #include "rediscommand.h" | ||
| #include "schema.h" | ||
| #include "table.h" | ||
|
|
||
| using namespace std; | ||
| using namespace swss; | ||
|
|
||
| string buildJsonWithKey(const FieldValueTuple &fvHead, const vector<FieldValueTuple> &fv) | ||
| { | ||
| nlohmann::json j = nlohmann::json::array(); | ||
| j.push_back(fvField(fvHead)); | ||
| j.push_back(fvValue(fvHead)); | ||
|
|
||
| // we use array to save order | ||
| for (const auto &i : fv) | ||
| { | ||
| j.push_back(fvField(i)); | ||
| j.push_back(fvValue(i)); | ||
| } | ||
|
|
||
| return j.dump(); | ||
| } | ||
|
|
||
| PublisherEventTable::PublisherEventTable(const DBConnector *db, const std::string &tableName) | ||
| : Table(db, tableName) | ||
| { | ||
| m_channel = getChannelName(); | ||
| } | ||
|
|
||
| PublisherEventTable::PublisherEventTable(RedisPipeline *pipeline, const std::string &tableName, bool buffered) | ||
| : Table(pipeline, tableName, buffered) | ||
| { | ||
| m_channel = getChannelName(); | ||
| } | ||
|
|
||
| PublisherEventTable::~PublisherEventTable() | ||
| { | ||
| } | ||
|
|
||
| void PublisherEventTable::set(const string &key, const vector<FieldValueTuple> &values, | ||
| const string &op, const string &prefix) | ||
| { | ||
| if (values.size() == 0) | ||
| return; | ||
|
|
||
| RedisCommand cmd; | ||
|
|
||
| cmd.formatHSET(getKeyName(key), values.begin(), values.end()); | ||
| m_pipe->push(cmd, REDIS_REPLY_INTEGER); | ||
|
|
||
|
|
||
| FieldValueTuple opdata(SET_COMMAND, key); | ||
| std::string msg = buildJsonWithKey(opdata, values); | ||
|
|
||
| SWSS_LOG_DEBUG("channel %s, publish: %s", m_channel.c_str(), msg.c_str()); | ||
|
|
||
| RedisCommand command; | ||
| command.format("PUBLISH %s %s", m_channel.c_str(), msg.c_str()); | ||
| m_pipe->push(command, REDIS_REPLY_INTEGER); | ||
|
|
||
| if (!m_buffered) | ||
| { | ||
| m_pipe->flush(); | ||
| } | ||
| } | ||
|
|
||
| void PublisherEventTable::del(const string &key, const string& op, const string& /*prefix*/) | ||
| { | ||
| RedisCommand del_key; | ||
| del_key.format("DEL %s", getKeyName(key).c_str()); | ||
| m_pipe->push(del_key, REDIS_REPLY_INTEGER); | ||
|
|
||
| FieldValueTuple opdata(DEL_COMMAND, key); | ||
| std::string msg = buildJsonWithKey(opdata, {}); | ||
|
|
||
| SWSS_LOG_DEBUG("channel %s, publish: %s", m_channel.c_str(), msg.c_str()); | ||
|
|
||
| RedisCommand command; | ||
| command.format("PUBLISH %s %s", m_channel.c_str(), msg.c_str()); | ||
| m_pipe->push(command, REDIS_REPLY_INTEGER); | ||
|
|
||
| if (!m_buffered) | ||
| { | ||
| m_pipe->flush(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| #pragma once | ||
|
|
||
| #include <string> | ||
| #include "dbconnector.h" | ||
| #include "table.h" | ||
|
|
||
| namespace swss { | ||
|
|
||
| class PublisherEventTable : public Table { // public TableBase, public TableEntryEnumerable { | ||
|
||
| public: | ||
| PublisherEventTable(const DBConnector *db, const std::string &tableName); | ||
| PublisherEventTable(RedisPipeline *pipeline, const std::string &tableName, bool buffered); | ||
| ~PublisherEventTable() override; | ||
|
|
||
| /* Set an entry in the DB directly (op not in use) */ | ||
| virtual void set(const std::string &key, | ||
| const std::vector<FieldValueTuple> &values, | ||
| const std::string &op = "", | ||
| const std::string &prefix = EMPTY_PREFIX); | ||
|
|
||
| /* Set an entry in the DB directly and configure ttl for it (op not in use) */ | ||
| // explicitly disable the virtual function | ||
| virtual void set(const std::string &key, | ||
| const std::vector<FieldValueTuple> &values, | ||
| const std::string &op, | ||
| const std::string &prefix, | ||
| const int64_t &ttl) | ||
| { | ||
| throw std::runtime_error("set with ttl is not supported in PublisherEventTable"); | ||
| } | ||
|
|
||
| /* Delete an entry in the table */ | ||
| virtual void del(const std::string &key, | ||
| const std::string &op = "", | ||
| const std::string &prefix = EMPTY_PREFIX); | ||
|
|
||
| // explicitly disable the virtual function | ||
| virtual void hdel(const std::string &key, | ||
| const std::string &field, | ||
| const std::string &op = "", | ||
| const std::string &prefix = EMPTY_PREFIX) | ||
| { | ||
| throw std::runtime_error("hdel is not supported in PublisherEventTable"); | ||
| } | ||
|
|
||
| virtual void hset(const std::string &key, | ||
| const std::string &field, | ||
| const std::string &value, | ||
| const std::string &op = "", | ||
| const std::string &prefix = EMPTY_PREFIX) | ||
| { | ||
| throw std::runtime_error("hset is not supported in PublisherEventTable"); | ||
| } | ||
|
|
||
| /* Read a value from the DB directly */ | ||
| /* Returns false if the key doesn't exists */ | ||
| // virtual bool get(const std::string &key, std::vector<FieldValueTuple> &ovalues); | ||
Check noticeCode scanning / CodeQL Commented-out code Note
This comment appears to contain commented-out code.
|
||
|
|
||
| // void getKeys(std::vector<std::string> &keys); | ||
Check noticeCode scanning / CodeQL Commented-out code Note
This comment appears to contain commented-out code.
|
||
|
|
||
| // void setBuffered(bool buffered); | ||
Check noticeCode scanning / CodeQL Commented-out code Note
This comment appears to contain commented-out code.
|
||
|
|
||
| // void flush(); | ||
Check noticeCode scanning / CodeQL Commented-out code Note
This comment appears to contain commented-out code.
|
||
|
|
||
| // void dump(TableDump &tableDump); | ||
Check noticeCode scanning / CodeQL Commented-out code Note
This comment appears to contain commented-out code.
|
||
|
|
||
| private: | ||
| std::string m_channel; | ||
| }; | ||
|
|
||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,175 @@ | ||||||
| #include <string> | ||||||
| #include <deque> | ||||||
| #include <limits> | ||||||
| #include <hiredis/hiredis.h> | ||||||
| #include "json.h" | ||||||
| #include "dbconnector.h" | ||||||
| #include "rediscommand.h" | ||||||
| #include "table.h" | ||||||
| #include "selectable.h" | ||||||
| #include "redisselect.h" | ||||||
| #include "redisapi.h" | ||||||
| #include "tokenize.h" | ||||||
| #include "subscriberstatetable.h" | ||||||
| #include "subscribereventtable.h" | ||||||
|
|
||||||
| using namespace std; | ||||||
|
|
||||||
| namespace swss { | ||||||
|
|
||||||
| // TODO: reuse | ||||||
| #define REDIS_PUBLISH_MESSAGE_ELEMNTS (3) | ||||||
|
||||||
| #define REDIS_PUBLISH_MESSAGE_ELEMNTS (3) | |
| #define REDIS_PUBLISH_MESSAGE_ELEMENTS (3) |
Check notice
Code scanning / CodeQL
Commented-out code Note
qiluo-msft marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| #pragma once | ||
|
|
||
| #include "subscriberstatetable.h" | ||
|
|
||
| namespace swss { | ||
|
|
||
| class SubscriberEventTable : public ConsumerTableBase | ||
| { | ||
| public: | ||
| SubscriberEventTable(DBConnector *db, const std::string &tableName, int popBatchSize = DEFAULT_POP_BATCH_SIZE, int pri = 0); | ||
|
|
||
| /* Get all available events from pub/sub channel */ | ||
| void pops(std::deque<KeyOpFieldsValuesTuple> &vkco, const std::string &prefix = EMPTY_PREFIX); | ||
|
|
||
| /* Read event from redis channel*/ | ||
| uint64_t readData() override; | ||
| bool hasData() override; | ||
| bool hasCachedData() override; | ||
| bool initializedWithData() override | ||
| { | ||
| return !m_buffer.empty(); | ||
| } | ||
|
|
||
| private: | ||
| /* Pop event from event buffer. Caller should free resources. */ | ||
| std::shared_ptr<RedisReply> popEventBuffer(); | ||
|
|
||
| std::string m_channel; | ||
|
|
||
| std::deque<std::shared_ptr<RedisReply>> m_event_buffer; | ||
| Table m_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.
[nitpick] Outdated comment
// public TableBase, public TableEntryEnumerable; update or remove it to reflect that this class now inherits directly fromTable.