Skip to content

Commit

Permalink
table: Adding getTableContent function
Browse files Browse the repository at this point in the history
This function is used to retrieve the whole content of a table.
  • Loading branch information
Shuotian Cheng committed May 7, 2016
1 parent b068a60 commit 2b1b314
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 1 deletion.
37 changes: 37 additions & 0 deletions common/table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,43 @@ 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));
}
}

bool 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();

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

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

/* Start a transaction */
void multi();
Expand Down
52 changes: 52 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,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<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 << "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<KeyOpFieldsValuesTuple> 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++;
}
}

0 comments on commit 2b1b314

Please sign in to comment.