diff --git a/common/table.cpp b/common/table.cpp index fde86fb41..2fdff9f26 100644 --- a/common/table.cpp +++ b/common/table.cpp @@ -126,6 +126,43 @@ void Table::delField(std::string key, std::string field) "DEL operation failed"); } +void Table::getTableContent(std::vector &tuples) +{ + std::vector keys; + getTableKeys(keys); + + tuples.clear(); + + for (auto key: keys) + { + std::vector values; + std::string op = ""; + + get(key, values); + tuples.push_back(make_tuple(key, op, values)); + } +} + +bool Table::getTableKeys(std::vector &keys) +{ + string keys_cmd("KEYS " + getTableName() + ":*"); + RedisReply r(m_db, keys_cmd, REDIS_REPLY_ARRAY); + redisReply *reply = r.getContext(); + keys.clear(); + + if (!reply->elements) + return false; + + for (unsigned int i = 0; i < reply->elements; i++) + { + string key = reply->element[i]->str; + auto pos = key.find(':'); + keys.push_back(key.substr(pos+1)); + } + + return true; +} + Table::~Table() { } diff --git a/common/table.h b/common/table.h index 0c25b9474..ef15dfddc 100644 --- a/common/table.h +++ b/common/table.h @@ -38,7 +38,10 @@ class Table { void delField(std::string key, std::string field); virtual ~Table(); - std::string getTableName()const; + std::string getTableName() const; + + /* Read the whole table content from the DB directly */ + void getTableContent(std::vector &tuples); protected: /* Return the actual key name as a comibation of tableName:key */ @@ -47,6 +50,7 @@ class Table { std::string getValueQueueTableName(); std::string getOpQueueTableName(); std::string getChannelTableName(); + bool getTableKeys(std::vector &keys); /* Start a transaction */ void multi(); diff --git a/tests/redis_ut.cpp b/tests/redis_ut.cpp index a7ab23cf3..b3bb795cd 100644 --- a/tests/redis_ut.cpp +++ b/tests/redis_ut.cpp @@ -5,6 +5,7 @@ #include "common/notificationproducer.h" #include "common/select.h" #include "common/selectableevent.h" +#include "common/table.h" #include #include #include @@ -316,3 +317,54 @@ TEST(DBConnector, selectableevent) EXPECT_EQ(value, 2); } +TEST(Table, test) +{ + string tableName = "TABLE_UT_TEST"; + DBConnector db(TEST_VIEW, "localhost", 6379, 0); + Table t(&db, tableName); + + clearDB(); + cout << "Starting table manipulations" << endl; + + string key_1 = "a"; + string key_2 = "b"; + vector values; + + for (int i = 1; i < 4; i++) + { + string field = "field_" + to_string(i); + string value = to_string(i); + values.push_back(make_pair(field, value)); + } + + cout << "Set key [a] field_1:1 field_2:2 field_3:3" << endl; + cout << "Set key [b] field_1:1 field_2:2 field_3:3" << endl; + + t.set(key_1, values); + t.set(key_2, values); + + vector tuples; + t.getTableContent(tuples); + + unsigned int size = 2; + cout << "Get total " << tuples.size() << " number of entries" << endl; + EXPECT_EQ(tuples.size(), size); + + unsigned int i = 1; + for (auto t: tuples) + { + cout << "Get key [" << kfvKey(t) << "]" << flush; + unsigned int j = 1; + for (auto fv: kfvFieldsValues(t)) + { + string field = "field_" + to_string(j); + string value = to_string(j); + cout << " " << fvField(fv) << ":" << fvValue(fv) << flush; + EXPECT_EQ(fvField(fv), field); + EXPECT_EQ(fvValue(fv), value); + j++; + } + cout << endl; + i++; + } +} \ No newline at end of file