Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions common/table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,38 @@ void Table::delField(std::string key, std::string field)
"DEL operation failed");
}

void Table::getTableContent(std::vector<KeyOpFieldsValuesTuple> &tuples)
{
std::vector<std::string> keys;
getTableKeys(keys);

tuples.clear();

for (auto key: keys)
{
std::vector<FieldValueTuple> values;
std::string op = "";

get(key, values);
tuples.push_back(make_tuple(key, op, values));
}
}

void Table::getTableKeys(std::vector<std::string> &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()
{
}
Expand Down
7 changes: 6 additions & 1 deletion common/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<KeyOpFieldsValuesTuple> &tuples);

protected:
/* Return the actual key name as a comibation of tableName:key */
Expand All @@ -47,6 +51,7 @@ class Table {
std::string getValueQueueTableName();
std::string getOpQueueTableName();
std::string getChannelTableName();
void getTableKeys(std::vector<std::string> &keys);

/* Start a transaction */
void multi();
Expand Down
79 changes: 79 additions & 0 deletions tests/redis_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "common/notificationproducer.h"
#include "common/select.h"
#include "common/selectableevent.h"
#include "common/table.h"
#include <iostream>
#include <memory>
#include <thread>
Expand Down Expand Up @@ -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<FieldValueTuple> 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<KeyOpFieldsValuesTuple> 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;
}