Commit 8f9d50ce authored by Philipp Götze's avatar Philipp Götze
Browse files

Added some test cases for NVMTable based on RDBTable

parent d331641a
...@@ -51,7 +51,6 @@ ...@@ -51,7 +51,6 @@
#include "table/BaseTable.hpp" #include "table/BaseTable.hpp"
#include "table/TableInfo.hpp" #include "table/TableInfo.hpp"
namespace pfabric { namespace pfabric {
//TODO: Maybe the pmem device path prefix should be a CMake variable? //TODO: Maybe the pmem device path prefix should be a CMake variable?
...@@ -136,7 +135,8 @@ class NVMIterator { ...@@ -136,7 +135,8 @@ class NVMIterator {
public: public:
static_assert(detail::is_tuple<RecordType>::value, "Value type must be a pfabric::Tuple"); static_assert(detail::is_tuple<RecordType>::value, "Value type must be a pfabric::Tuple");
using TupleType = typename RecordType::Base; using TupleType = typename RecordType::Base;
using Predicate = std::function<bool(const PTuple<TupleType, KeyType> &)>; // using Predicate = std::function<bool(const PTuple<TupleType, KeyType> &)>;
using Predicate = std::function<bool(const RecordType &)>;
using PTableType = PTable<TupleType, KeyType>; using PTableType = PTable<TupleType, KeyType>;
explicit NVMIterator() { explicit NVMIterator() {
...@@ -145,13 +145,13 @@ class NVMIterator { ...@@ -145,13 +145,13 @@ class NVMIterator {
explicit NVMIterator(typename PTableType::iterator &&_iter, typename PTableType::iterator &&_end, Predicate _pred) : explicit NVMIterator(typename PTableType::iterator &&_iter, typename PTableType::iterator &&_end, Predicate _pred) :
iter(std::move(_iter)), end(std::move(_end)), pred(_pred) { iter(std::move(_iter)), end(std::move(_end)), pred(_pred) {
while (isValid() && !pred(*iter)) while (isValid() && !pred(*(*iter).createTuple()))
iter++; iter++;
} }
NVMIterator &operator++() { NVMIterator &operator++() {
iter++; iter++;
while (isValid() && !pred(*iter)) while (isValid() && !pred(*(*iter).createTuple()))
iter++; iter++;
return *this; return *this;
} }
...@@ -261,8 +261,8 @@ class NVMTable : public BaseTable { ...@@ -261,8 +261,8 @@ class NVMTable : public BaseTable {
* \param rec the actual tuple * \param rec the actual tuple
*****************************************************************************/ *****************************************************************************/
void insert(KeyType key, const RecordType &rec) noexcept(false) { void insert(KeyType key, const RecordType &rec) noexcept(false) {
pTable->insert(key, rec.data()); pTable->insert(key, rec.data());
notifyObservers(rec, TableParams::Insert, TableParams::Immediate); notifyObservers(rec, TableParams::Insert, TableParams::Immediate);
} }
/************************************************************************//** /************************************************************************//**
...@@ -354,9 +354,12 @@ class NVMTable : public BaseTable { ...@@ -354,9 +354,12 @@ class NVMTable : public BaseTable {
*****************************************************************************/ *****************************************************************************/
const SmartPtr<RecordType> getByKey(KeyType key) noexcept(false) { const SmartPtr<RecordType> getByKey(KeyType key) noexcept(false) {
//TODO: ugly, can we do better? //TODO: ugly, can we do better?
SmartPtr<RecordType> tptr(new RecordType(*pTable->getByKey(key).createTuple())); try {
return tptr; SmartPtr<RecordType> tptr(new RecordType(*pTable->getByKey(key).createTuple()));
return tptr;
} catch (ptable::PTableException &pex) {
throw TableException(pex.what());
}
} }
/************************************************************************//** /************************************************************************//**
...@@ -395,7 +398,8 @@ class NVMTable : public BaseTable { ...@@ -395,7 +398,8 @@ class NVMTable : public BaseTable {
* \return a pair of iterators * \return a pair of iterators
*****************************************************************************/ *****************************************************************************/
TableIterator select() { TableIterator select() {
auto alwaysTrue = [](const PTuple<TupleType, KeyType> &) { return true; }; auto alwaysTrue = [](const RecordType &) { return true; };
// auto alwaysTrue = [](const PTuple<TupleType, KeyType> &) { return true; };
return makeNVMIterator<RecordType, KeyType>(std::move(pTable->begin()), std::move(pTable->end()), alwaysTrue); return makeNVMIterator<RecordType, KeyType>(std::move(pTable->begin()), std::move(pTable->end()), alwaysTrue);
} }
...@@ -445,8 +449,7 @@ class NVMTable : public BaseTable { ...@@ -445,8 +449,7 @@ class NVMTable : public BaseTable {
} }
private: private:
void openOrCreateTable(const TableInfo &tableInfo) /*throw(TableException)*/ void openOrCreateTable(const TableInfo &tableInfo) noexcept(false) {
{
std::string path = pathPrefix + tableInfo.tableName() + ".db"; std::string path = pathPrefix + tableInfo.tableName() + ".db";
pool<root> pop; pool<root> pop;
......
...@@ -32,3 +32,70 @@ TEST_CASE("Creating a table with a given schema and inserting data", ...@@ -32,3 +32,70 @@ TEST_CASE("Creating a table with a given schema and inserting data",
} }
testTable->drop(); testTable->drop();
} }
TEST_CASE("Creating a table with a given schema and deleting data",
"[NVMTable]") {
auto testTable = std::make_shared<TableType>("MyTestTable2");
for (int i = 0; i < 10000; i++) {
auto tp = MyTuple((unsigned long)i, i + 100, fmt::format("String#{}", i),
i / 100.0);
testTable->insert(i, tp);
}
REQUIRE(testTable->size() == 10000);
for (int i = 0; i < 10000; i += 100) testTable->deleteByKey(i);
//TODO: REQUIRE(testTable->size() == 9900);
// check if the records were really deleted
for (int i = 0; i < 10000; i += 100) {
try {
auto tp = testTable->getByKey(i);
REQUIRE(false);
} catch (TableException& exc) {
// if the key wasn't found then an exception is raised
// which we can ignore here
}
}
testTable->drop();
}
TEST_CASE("scanning the whole table", "[NVMTable]") {
auto testTable = std::make_shared<TableType>("MyTestTable7");
for (int i = 0; i < 10000; i++) {
auto tp = MyTuple((unsigned long)i, i + 100, fmt::format("String#{}", i),
i / 100.0);
testTable->insert(i, tp);
}
REQUIRE(testTable->size() == 10000);
unsigned int num = 0;
for (auto iter = testTable->select(); iter.isValid(); ++iter) num++;
REQUIRE(num == testTable->size());
testTable->drop();
}
TEST_CASE("scanning the table with a predicate", "[NVMTable]") {
auto testTable = std::make_shared<TableType>("MyTestTable8");
for (int i = 0; i < 10000; i++) {
auto tp = MyTuple((unsigned long)i, i + 100, fmt::format("String#{}", i),
i / 100.0);
testTable->insert(i, tp);
}
REQUIRE(testTable->size() == 10000);
unsigned int num = 0;
{
auto iter = testTable->select(
[](const MyTuple& tp) { return get<0>(tp) % 2 == 0; });
for (; iter.isValid(); iter++) {
REQUIRE(get<0>(*iter) % 2 == 0);
num++;
}
REQUIRE(num == testTable->size() / 2);
}
testTable->drop();
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment