Skip to content

Commit

Permalink
add python binding for swss::Table (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
lguohan committed Nov 11, 2017
1 parent adf7d4c commit be508cf
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 28 deletions.
2 changes: 1 addition & 1 deletion common/subscriberstatetable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ SubscriberStateTable::SubscriberStateTable(DBConnector *db, string tableName)
psubscribe(m_db, m_keyspace);

vector<string> keys;
m_table.getTableKeys(keys);
m_table.getKeys(keys);

for (auto key: keys)
{
Expand Down
6 changes: 3 additions & 3 deletions common/table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ void Table::del(string key, string /* op */, string /*prefix*/)
RedisReply r(m_db, string("DEL ") + getKeyName(key), REDIS_REPLY_INTEGER);
}

void TableEntryEnumerable::getTableContent(vector<KeyOpFieldsValuesTuple> &tuples)
void TableEntryEnumerable::getContent(vector<KeyOpFieldsValuesTuple> &tuples)
{
vector<string> keys;
getTableKeys(keys);
getKeys(keys);

tuples.clear();

Expand All @@ -78,7 +78,7 @@ void TableEntryEnumerable::getTableContent(vector<KeyOpFieldsValuesTuple> &tuple
}
}

void Table::getTableKeys(vector<string> &keys)
void Table::getKeys(vector<string> &keys)
{
string keys_cmd("KEYS " + getTableName() + getTableNameSeparator() + "*");
RedisReply r(m_db, keys_cmd, REDIS_REPLY_ARRAY);
Expand Down
8 changes: 4 additions & 4 deletions common/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ class TableEntryEnumerable {
virtual bool get(std::string key, std::vector<FieldValueTuple> &values) = 0;

/* get all the keys in the table */
virtual void getTableKeys(std::vector<std::string> &keys) = 0;
virtual void getKeys(std::vector<std::string> &keys) = 0;

/* Read the whole table content from the DB directly */
/* NOTE: Not an atomic function */
void getTableContent(std::vector<KeyOpFieldsValuesTuple> &tuples);
void getContent(std::vector<KeyOpFieldsValuesTuple> &tuples);
};

class Table : public RedisTransactioner, public TableBase, public TableEntryEnumerable {
Expand All @@ -129,9 +129,9 @@ class Table : public RedisTransactioner, public TableBase, public TableEntryEnum

/* Read a value from the DB directly */
/* Returns false if the key doesn't exists */
virtual bool get(std::string key, std::vector<FieldValueTuple> &values);
virtual bool get(std::string key, std::vector<FieldValueTuple> &ovalues);

void getTableKeys(std::vector<std::string> &keys);
void getKeys(std::vector<std::string> &keys);

void dump(TableDump &tableDump);

Expand Down
33 changes: 23 additions & 10 deletions pyext/swsscommon.i
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
%module swsscommon

%include <std_string.i>
%include <std_vector.i>
%include <std_pair.i>
%include <typemaps.i>

%template(FieldValuePair) std::pair<std::string, std::string>;
%template(FieldValuePairs) std::vector<std::pair<std::string, std::string>>;
%apply std::string& OUTPUT {std::string &key};
%apply std::string& OUTPUT {std::string &op};

%{
#include "schema.h"
#include "dbconnector.h"
Expand All @@ -27,6 +17,15 @@
#include "notificationconsumer.h"
%}

%include <std_string.i>
%include <std_vector.i>
%include <std_pair.i>
%include <typemaps.i>

%template(FieldValuePair) std::pair<std::string, std::string>;
%template(FieldValuePairs) std::vector<std::pair<std::string, std::string>>;
%template(VectorString) std::vector<std::string>;

%apply int *OUTPUT {int *fd};
%typemap(in, numinputs=0) swss::Selectable ** (swss::Selectable *temp) {
$1 = &temp;
Expand All @@ -49,9 +48,23 @@
%include "redispipeline.h"
%include "redisselect.h"
%include "redistran.h"

%apply std::vector<std::string>& OUTPUT {std::vector<std::string> &keys};
%apply std::vector<std::pair<std::string, std::string>>& OUTPUT {std::vector<std::pair<std::string, std::string>> &ovalues};
%include "table.h"
%clear std::vector<std::string> &keys;
%clear std::vector<std::pair<std::string, std::string>> &values;

%include "producerstatetable.h"

%apply std::string& OUTPUT {std::string &key};
%apply std::string& OUTPUT {std::string &op};
%apply std::vector<std::pair<std::string, std::string>>& OUTPUT {std::vector<std::pair<std::string, std::string>> &fvs};
%include "consumertablebase.h"
%clear std::string &key;
%clear std::string &op;
%clear std::vector<std::pair<std::string, std::string>> &fvs;

%include "consumerstatetable.h"
%include "subscriberstatetable.h"
%include "notificationconsumer.h"
4 changes: 2 additions & 2 deletions tests/redis_piped_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ TEST(Table, piped_test)

cout << "- Step 2. GET_TABLE_CONTENT" << endl;
vector<KeyOpFieldsValuesTuple> tuples;
t.getTableContent(tuples);
t.getContent(tuples);

cout << "Get total " << tuples.size() << " number of entries" << endl;
EXPECT_EQ(tuples.size(), (size_t)2);
Expand Down Expand Up @@ -393,7 +393,7 @@ TEST(Table, piped_test)
cout << "- Step 5. DEL and GET_TABLE_CONTENT" << endl;
cout << "Delete key [b]" << endl;
t.del(key_2);
t.getTableContent(tuples);
t.getContent(tuples);

EXPECT_EQ(tuples.size(), unsigned(0));

Expand Down
8 changes: 4 additions & 4 deletions tests/redis_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ TEST(Table, test)

cout << "- Step 2. GET_TABLE_CONTENT" << endl;
vector<KeyOpFieldsValuesTuple> tuples;
t.getTableContent(tuples);
t.getContent(tuples);

cout << "Get total " << tuples.size() << " number of entries" << endl;
EXPECT_EQ(tuples.size(), (size_t)2);
Expand Down Expand Up @@ -392,7 +392,7 @@ TEST(Table, test)
cout << "- Step 5. DEL and GET_TABLE_CONTENT" << endl;
cout << "Delete key [b]" << endl;
t.del(key_2);
t.getTableContent(tuples);
t.getContent(tuples);

EXPECT_EQ(tuples.size(), unsigned(0));

Expand Down Expand Up @@ -431,7 +431,7 @@ TEST(Table, table_separator_test)

cout << "- Step 2. GET_TABLE_CONTENT" << endl;
vector<KeyOpFieldsValuesTuple> tuples;
t.getTableContent(tuples);
t.getContent(tuples);

cout << "Get total " << tuples.size() << " number of entries" << endl;
EXPECT_EQ(tuples.size(), (size_t)2);
Expand Down Expand Up @@ -473,7 +473,7 @@ TEST(Table, table_separator_test)
cout << "- Step 5. DEL and GET_TABLE_CONTENT" << endl;
cout << "Delete key [b]" << endl;
t.del(key_2);
t.getTableContent(tuples);
t.getContent(tuples);

EXPECT_EQ(tuples.size(), unsigned(0));

Expand Down
20 changes: 16 additions & 4 deletions tests/test_redis_ut.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,26 @@ def test_ProducerStateTable():
cs = swsscommon.ConsumerStateTable(db, "abc")
fvs = swsscommon.FieldValuePairs([('a','b')])
ps.set("aaa", fvs)
cfvs = swsscommon.FieldValuePairs([])
(key, op) = cs.pop(cfvs)
(key, op, cfvs) = cs.pop()
assert key == "aaa"
assert op == "SET"
assert len(cfvs) == 1
assert cfvs[0] == ('a', 'b')

def test_Table():
db = swsscommon.DBConnector(0, "localhost", 6379, 0)
tbl = swsscommon.Table(db, "test_TABLE")
fvs = swsscommon.FieldValuePairs([('a','b'), ('c', 'd')])
tbl.set("aaa", fvs)
keys = tbl.getKeys()
assert len(keys) == 1
assert keys[0] == "aaa"
(status, fvs) = tbl.get("aaa")
assert status == True
assert len(fvs) == 2
assert fvs[0] == ('a', 'b')
assert fvs[1] == ('c', 'd')

def test_SubscriberStateTable():
db = swsscommon.DBConnector(0, "localhost", 6379, 0)
t = swsscommon.Table(db, "testsst", '|')
Expand All @@ -23,8 +36,7 @@ def test_SubscriberStateTable():
t.set("aaa", fvs)
(state, c, fd) = sel.select()
assert state == swsscommon.Select.OBJECT
cfvs = swsscommon.FieldValuePairs([])
(key, op) = cst.pop(cfvs)
(key, op, cfvs) = cst.pop()
assert key == "aaa"
assert op == "SET"
assert len(cfvs) == 1
Expand Down

0 comments on commit be508cf

Please sign in to comment.