From 9ca8439b47aa53b51a83d6a34e914ff290593aaa Mon Sep 17 00:00:00 2001 From: Shuotian Cheng Date: Sat, 7 May 2016 16:24:01 -0700 Subject: [PATCH] table: Adding getTableContent function This function is used to retrieve the whole content of a table. --- common/table.cpp | 32 +++++++++++++++++++ common/table.h | 7 +++- tests/redis_ut.cpp | 79 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+), 1 deletion(-) diff --git a/common/table.cpp b/common/table.cpp index fde86fb41..a56588894 100644 --- a/common/table.cpp +++ b/common/table.cpp @@ -126,6 +126,38 @@ 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)); + } +} + +void 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(); + + 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)); + } +} + Table::~Table() { } diff --git a/common/table.h b/common/table.h index 0c25b9474..11f880f13 100644 --- a/common/table.h +++ b/common/table.h @@ -38,7 +38,11 @@ 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 */ + /* NOTE: Not an atomic function */ + void getTableContent(std::vector &tuples); protected: /* Return the actual key name as a comibation of tableName:key */ @@ -47,6 +51,7 @@ class Table { std::string getValueQueueTableName(); std::string getOpQueueTableName(); std::string getChannelTableName(); + void getTableKeys(std::vector &keys); /* Start a transaction */ void multi(); diff --git a/tests/redis_ut.cpp b/tests/redis_ut.cpp index a7ab23cf3..048d620e7 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,81 @@ 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 << "- Step 1. SET" << endl; + 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); + + cout << "- Step 2. GET_TABLE_CONTENT" << endl; + vector tuples; + t.getTableContent(tuples); + + unsigned int size_t = 2; + cout << "Get total " << tuples.size() << " number of entries" << endl; + EXPECT_EQ(tuples.size(), size_t); + + for (auto tuple: tuples) + { + cout << "Get key [" << kfvKey(tuple) << "]" << flush; + unsigned int size_v = 3; + EXPECT_EQ(kfvFieldsValues(tuple).size(), size_v); + for (auto fv: kfvFieldsValues(tuple)) + { + string value_1 = "1", value_2 = "2"; + cout << " " << fvField(fv) << ":" << fvValue(fv) << flush; + if (fvField(fv) == "field_1") EXPECT_EQ(fvValue(fv), value_1); + if (fvField(fv) == "field_2") EXPECT_EQ(fvValue(fv), value_2); + } + cout << endl; + } + + cout << "- Step 3. DEL" << endl; + cout << "Delete key [a]" << endl; + t.del(key_1); + + cout << "- Step 4. GET" << endl; + cout << "Get key [a] and key [b]" << endl; + EXPECT_EQ(t.get(key_1, values), false); + t.get(key_2, values); + + cout << "Get key [b]" << flush; + for (auto fv: values) + { + string value_1 = "1", value_2 = "2"; + cout << " " << fvField(fv) << ":" << fvValue(fv) << flush; + if (fvField(fv) == "field_1") EXPECT_EQ(fvValue(fv), value_1); + if (fvField(fv) == "field_2") EXPECT_EQ(fvValue(fv), value_2); + } + cout << endl; + + cout << "- Step 5. DEL and GET_TABLE_CONTENT" << endl; + cout << "Delete key [b]" << endl; + t.del(key_2); + t.getTableContent(tuples); + + EXPECT_EQ(tuples.size(), unsigned(0)); + + cout << "Done." << endl; +} \ No newline at end of file