PTableTest.cpp 4.8 KB
Newer Older
Philipp Götze's avatar
Philipp Götze committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (C) 2017 DBIS Group - TU Ilmenau, All Rights Reserved.
 *
 * This file is part of PTable.
 *
 * PTable is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * PTable is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PTable.  If not, see <http://www.gnu.org/licenses/>.
 */

Philipp Götze's avatar
Philipp Götze committed
20
#include <unistd.h>
21
#include <libpmemobj++/make_persistent_atomic.hpp>
Philipp Götze's avatar
Philipp Götze committed
22 23 24 25 26 27

#include "catch.hpp"
#include "PTable.hpp"

using namespace ptable;

28
using nvml::obj::delete_persistent_atomic;
Philipp Götze's avatar
Philipp Götze committed
29 30
using nvml::obj::pool;

31 32
using MyTuple = std::tuple<int, int, std::string, double>;
using PTableType = PTable<MyTuple, int>;
Philipp Götze's avatar
Philipp Götze committed
33

34
TEST_CASE("Storing tuples in PTable", "[PTable]") {
Philipp Götze's avatar
Philipp Götze committed
35 36 37 38 39 40
  struct root {
    persistent_ptr<PTableType> pTable;
  };

  pool<root> pop;

41
  const std::string path = gPmemPath + "testdb.db";
Philipp Götze's avatar
Philipp Götze committed
42 43 44 45

  if (access(path.c_str(), F_OK) != 0) {
    pop = pool<root>::create(path, LAYOUT, 16 * 1024 * 1024);
    transaction::exec_tx(pop, [&] {
46 47 48 49 50 51
      auto tInfo = VTableInfo("MyTable", {
        {"a", Int_Type},
        {"b", Int_Type},
        {"c", String_Type},
        {"d", Double_Type}
      }, Int_Type);
52
      auto dims = Dimensions({{0, 4, 4}, {3, 6, 6}});
Philipp Götze's avatar
Philipp Götze committed
53 54 55 56 57 58
      pop.get_root()->pTable = make_persistent<PTableType>(tInfo, dims);
    });
  } else {
    std::cerr << "WARNING: Table already exists" << std::endl;
    pop = pool<root>::open(path, LAYOUT);
  }
59 60
  auto q = pop.get_root();
  auto pTable = q->pTable;
Philipp Götze's avatar
Philipp Götze committed
61

62 63 64
  //SECTION("insert and count")
  auto c = 0u;
  for (auto i = 0u; i < 10; i++) {
Philipp Götze's avatar
Philipp Götze committed
65 66 67 68
    auto tup = MyTuple(i + 1,
                       (i + 1) * 100,
                       fmt::format("String #{0}", i),
                       (i + 1) * 12.345);
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    c += pTable->insert(i + 1, tup);
  }
  REQUIRE(c == 10);
  REQUIRE(pTable->count() == 10);

  SECTION("delete and get by key, update by using delete/insert") {
    for (auto i = 0u; i < 10; i++) {
      REQUIRE(get<0>(pTable->getByKey(i + 1)) == i + 1);
      REQUIRE(get<1>(pTable->getByKey(i + 1)) == (i + 1) * 100);
      REQUIRE(get<2>(pTable->getByKey(i + 1)) == fmt::format("String #{0}", i));
      REQUIRE(get<3>(pTable->getByKey(i + 1)) == (i + 1) * 12.345);
    }

    auto c = 0u;
    for (auto i = 0u; i < 5; i++) {
      c += pTable->deleteByKey(i + 1);
    }
    REQUIRE(c == 5);
87
    REQUIRE(pTable->count() == 5);
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130

    for (auto i = 5u; i < 10; i++) {
      REQUIRE(get<0>(pTable->getByKey(i + 1)) == i + 1);
      REQUIRE(get<1>(pTable->getByKey(i + 1)) == (i + 1) * 100);
      REQUIRE(get<2>(pTable->getByKey(i + 1)) == fmt::format("String #{0}", i));
      REQUIRE(get<3>(pTable->getByKey(i + 1)) == (i + 1) * 12.345);
    }

    for (auto i = 0u; i < 5; i++) {
      try {
        pTable->getByKey(i + 1);
        REQUIRE(false);
      } catch (PTableException &exc) {
        // do nothing, correct behaviour
      }
    }

    for (auto i = 5u; i < 10; i++) {
      auto tup = MyTuple(i + 1,
                         (i + 1) * 200,
                         fmt::format("String #{0}", i),
                         (i + 1) * 1.0);
      pTable->updateComplete(i + 1, tup);
    }
    for (auto i = 5u; i < 10; i++) {
      REQUIRE(get<0>(pTable->getByKey(i + 1)) == i + 1);
      REQUIRE(get<1>(pTable->getByKey(i + 1)) == (i + 1) * 200);
      REQUIRE(get<2>(pTable->getByKey(i + 1)) == fmt::format("String #{0}", i));
      REQUIRE(get<3>(pTable->getByKey(i + 1)) == (i + 1) * 1.0);
    }

    for (auto i = 0u; i < 5; i++) {
      auto tup = MyTuple(i + 1,
                         (i + 1) * 100,
                         fmt::format("String #{0}", i),
                         (i + 1) * 12.345);
      try {
        pTable->updateComplete(i + 1, tup);
        REQUIRE(false);
      } catch (PTableException &exc) {
        // do nothing, correct behaviour
      }
    }
Philipp Götze's avatar
Philipp Götze committed
131 132
  }

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
  SECTION("iterate and select") {
    auto eIter = pTable->end();
    auto iter = pTable->select([](const PTuple<MyTuple, int> &tp) { return (tp.get<0>() % 2 == 0); });

    for (; iter != eIter; iter++) {
      REQUIRE((*iter).get<0>() % 2 == 0);
    }
  }

  SECTION("range scan") {
    auto iter = pTable->rangeScan(ColumnRangeMap({{0, {5, 10}}, {3, {25.0, 90.0}}}));
    auto c = 0u;
    auto d = 0.0;
    for (const auto &ptp: iter) {
      c++;
      d += get<3>(ptp);
    }
    REQUIRE(c == 3); // 5,6,7
    REQUIRE(d == 5*12.345 + 6*12.345 + 7*12.345);
  }
Philipp Götze's avatar
Philipp Götze committed
153 154

  /* Clean up */
155 156
  delete_persistent_atomic<PTableType>(q->pTable);
  delete_persistent_atomic<root>(q);
Philipp Götze's avatar
Philipp Götze committed
157
  pop.close();
158
  std::remove(path.c_str());
Philipp Götze's avatar
Philipp Götze committed
159
}