<commit_before>//===----------------------------------------------------------------------===//
//
//                         Peloton
//
// testing_index_util.cpp
//
// Identification: test/index/testing_index_util.cpp
//
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//

#include "index/testing_index_util.h"

#include "gtest/gtest.h"

#include "common/harness.h"
#include "common/internal_types.h"
#include "common/item_pointer.h"
#include "common/logger.h"
#include "catalog/catalog.h"
#include "index/index.h"
#include "index/index_util.h"
#include "storage/tuple.h"
#include "type/value_factory.h"

namespace peloton {
namespace test {

std::shared_ptr<ItemPointer> TestingIndexUtil::item0(new ItemPointer(120, 5));
std::shared_ptr<ItemPointer> TestingIndexUtil::item1(new ItemPointer(120, 7));
std::shared_ptr<ItemPointer> TestingIndexUtil::item2(new ItemPointer(123, 19));

void TestingIndexUtil::BasicTest(const IndexType index_type) {
  auto pool = TestingHarness::GetInstance().GetTestingPool();
  std::vector<ItemPointer *> location_ptrs;

  // INDEX
  std::unique_ptr<index::Index, void (*)(index::Index *)> index(
      TestingIndexUtil::BuildIndex(index_type, false), DestroyIndex);
  const catalog::Schema *key_schema = index->GetKeySchema();

  std::unique_ptr<storage::Tuple> key0(new storage::Tuple(key_schema, true));
  key0->SetValue(0, type::ValueFactory::GetIntegerValue(100), pool);
  key0->SetValue(1, type::ValueFactory::GetVarcharValue("a"), pool);

  // INSERT
  index->InsertEntry(key0.get(), TestingIndexUtil::item0.get());

  // SCAN
  index->ScanKey(key0.get(), location_ptrs);
  EXPECT_EQ(1, location_ptrs.size());
  EXPECT_EQ(TestingIndexUtil::item0->block, location_ptrs[0]->block);
  location_ptrs.clear();

  // DELETE
  index->DeleteEntry(key0.get(), TestingIndexUtil::item0.get());

  index->ScanKey(key0.get(), location_ptrs);
  EXPECT_EQ(0, location_ptrs.size());
  location_ptrs.clear();
}

void TestingIndexUtil::MultiMapInsertTest(const IndexType index_type) {
  auto pool = TestingHarness::GetInstance().GetTestingPool();
  std::vector<ItemPointer *> location_ptrs;

  // INDEX
  std::unique_ptr<index::Index, void (*)(index::Index *)> index(
      TestingIndexUtil::BuildIndex(index_type, false), DestroyIndex);
  const catalog::Schema *key_schema = index->GetKeySchema();

  // Single threaded test
  size_t scale_factor = 1;
  LaunchParallelTest(1, TestingIndexUtil::InsertHelper, index.get(), pool,
                     scale_factor);

  // Checks
  index->ScanAllKeys(location_ptrs);
  EXPECT_EQ(7, location_ptrs.size());
  location_ptrs.clear();

  std::unique_ptr<storage::Tuple> key0(new storage::Tuple(key_schema, true));
  std::unique_ptr<storage::Tuple> keynonce(
      new storage::Tuple(key_schema, true));
  key0->SetValue(0, type::ValueFactory::GetIntegerValue(100), pool);
  key0->SetValue(1, type::ValueFactory::GetVarcharValue("a"), pool);
  keynonce->SetValue(0, type::ValueFactory::GetIntegerValue(1000), pool);
  keynonce->SetValue(1, type::ValueFactory::GetVarcharValue("f"), pool);

  index->ScanKey(keynonce.get(), location_ptrs);
  EXPECT_EQ(0, location_ptrs.size());
  location_ptrs.clear();

  index->ScanKey(key0.get(), location_ptrs);
  EXPECT_EQ(1, location_ptrs.size());
  EXPECT_EQ(TestingIndexUtil::item0->block, location_ptrs[0]->block);
  location_ptrs.clear();
}

void TestingIndexUtil::UniqueKeyInsertTest(const IndexType index_type) {
  auto pool = TestingHarness::GetInstance().GetTestingPool();
  std::vector<ItemPointer *> location_ptrs;

  // INDEX
  std::unique_ptr<index::Index, void (*)(index::Index *)> index(
      TestingIndexUtil::BuildIndex(index_type, true), DestroyIndex);
  const catalog::Schema *key_schema = index->GetKeySchema();

  // Single threaded test
  size_t scale_factor = 1;
  LaunchParallelTest(1, TestingIndexUtil::InsertHelper, index.get(), pool,
                     scale_factor);

  // Check basic insert (key0 only has 1 value, so it is not sufficient)
  std::unique_ptr<storage::Tuple> key0(new storage::Tuple(key_schema, true));
  key0->SetValue(0, type::ValueFactory::GetIntegerValue(100), pool);
  key0->SetValue(1, type::ValueFactory::GetVarcharValue("a"), pool);

  index->ScanKey(key0.get(), location_ptrs);
  EXPECT_EQ(1, location_ptrs.size());
  location_ptrs.clear();

  // This tests non-unqie key. If scan returns more than 1 then it should fail
  std::unique_ptr<storage::Tuple> key1(new storage::Tuple(key_schema, true));
  key1->SetValue(0, type::ValueFactory::GetIntegerValue(100), pool);
  key1->SetValue(1, type::ValueFactory::GetVarcharValue("b"), pool);

  index->ScanKey(key1.get(), location_ptrs);
  EXPECT_EQ(1, location_ptrs.size());
  location_ptrs.clear();

  return;
}

void TestingIndexUtil::UniqueKeyDeleteTest(const IndexType index_type) {
  auto pool = TestingHarness::GetInstance().GetTestingPool();
  std::vector<ItemPointer *> location_ptrs;

  // INDEX
  std::unique_ptr<index::Index, void(*)(index::Index *)> index(
      TestingIndexUtil::BuildIndex(index_type, true), DestroyIndex);

  const catalog::Schema *key_schema = index->GetKeySchema();

  // Single threaded test
  size_t scale_factor = 1;
  LaunchParallelTest(1, TestingIndexUtil::InsertHelper, index.get(), pool,
                     scale_factor);
  LOG_DEBUG("INDEX VALUE CONTENTS BEFORE DELETE:\n%s",<commit_msg>Adding printing<commit_after>//===----------------------------------------------------------------------===//
//
//                         Peloton
//
// testing_index_util.cpp
//
// Identification: test/index/testing_index_util.cpp
//
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//

#include "index/testing_index_util.h"

#include "gtest/gtest.h"

#include "common/harness.h"
#include "common/internal_types.h"
#include "common/item_pointer.h"
#include "common/logger.h"
#include "catalog/catalog.h"
#include "index/index.h"
#include "index/index_util.h"
#include "storage/tuple.h"
#include "type/value_factory.h"

namespace peloton {
namespace test {

std::shared_ptr<ItemPointer> TestingIndexUtil::item0(new ItemPointer(120, 5));
std::shared_ptr<ItemPointer> TestingIndexUtil::item1(new ItemPointer(120, 7));
std::shared_ptr<ItemPointer> TestingIndexUtil::item2(new ItemPointer(123, 19));

void TestingIndexUtil::BasicTest(const IndexType index_type) {
  auto pool = TestingHarness::GetInstance().GetTestingPool();
  std::vector<ItemPointer *> location_ptrs;

  // INDEX
  std::unique_ptr<index::Index, void (*)(index::Index *)> index(
      TestingIndexUtil::BuildIndex(index_type, false), DestroyIndex);
  const catalog::Schema *key_schema = index->GetKeySchema();

  std::unique_ptr<storage::Tuple> key0(new storage::Tuple(key_schema, true));
  key0->SetValue(0, type::ValueFactory::GetIntegerValue(100), pool);
  key0->SetValue(1, type::ValueFactory::GetVarcharValue("a"), pool);

  // INSERT
  index->InsertEntry(key0.get(), TestingIndexUtil::item0.get());

  // SCAN
  index->ScanKey(key0.get(), location_ptrs);
  EXPECT_EQ(1, location_ptrs.size());
  EXPECT_EQ(TestingIndexUtil::item0->block, location_ptrs[0]->block);
  location_ptrs.clear();

  // DELETE
  index->DeleteEntry(key0.get(), TestingIndexUtil::item0.get());

  index->ScanKey(key0.get(), location_ptrs);
  EXPECT_EQ(0, location_ptrs.size());
  location_ptrs.clear();
}

void TestingIndexUtil::MultiMapInsertTest(const IndexType index_type) {
  auto pool = TestingHarness::GetInstance().GetTestingPool();
  std::vector<ItemPointer *> location_ptrs;

  // INDEX
  std::unique_ptr<index::Index, void (*)(index::Index *)> index(
      TestingIndexUtil::BuildIndex(index_type, false), DestroyIndex);
  const catalog::Schema *key_schema = index->GetKeySchema();

  // Single threaded test
  size_t scale_factor = 1;
  LaunchParallelTest(1, TestingIndexUtil::InsertHelper, index.get(), pool,
                     scale_factor);

  // Checks
  index->ScanAllKeys(location_ptrs);
  EXPECT_EQ(7, location_ptrs.size());
  location_ptrs.clear();

  std::unique_ptr<storage::Tuple> key0(new storage::Tuple(key_schema, true));
  std::unique_ptr<storage::Tuple> keynonce(
      new storage::Tuple(key_schema, true));
  key0->SetValue(0, type::ValueFactory::GetIntegerValue(100), pool);
  key0->SetValue(1, type::ValueFactory::GetVarcharValue("a"), pool);
  keynonce->SetValue(0, type::ValueFactory::GetIntegerValue(1000), pool);
  keynonce->SetValue(1, type::ValueFactory::GetVarcharValue("f"), pool);

  index->ScanKey(keynonce.get(), location_ptrs);
  EXPECT_EQ(0, location_ptrs.size());
  location_ptrs.clear();

  index->ScanKey(key0.get(), location_ptrs);
  EXPECT_EQ(1, location_ptrs.size());
  EXPECT_EQ(TestingIndexUtil::item0->block, location_ptrs[0]->block);
  location_ptrs.clear();
}

void TestingIndexUtil::UniqueKeyInsertTest(const IndexType index_type) {
  auto pool = TestingHarness::GetInstance().GetTestingPool();
  std::vector<ItemPointer *> location_ptrs;

  // INDEX
  std::unique_ptr<index::Index, void (*)(index::Index *)> index(
      TestingIndexUtil::BuildIndex(index_type, true), DestroyIndex);
  const catalog::Schema *key_schema = index->GetKeySchema();

  // Single threaded test
  size_t scale_factor = 1;
  LaunchParallelTest(1, TestingIndexUtil::InsertHelper, index.get(), pool,
                     scale_factor);

  // Check basic insert (key0 only has 1 value, so it is not sufficient)
  std::unique_ptr<storage::Tuple> key0(new storage::Tuple(key_schema, true));
  key0->SetValue(0, type::ValueFactory::GetIntegerValue(100), pool);
  key0->SetValue(1, type::ValueFactory::GetVarcharValue("a"), pool);

  index->ScanKey(key0.get(), location_ptrs);
  LOG_DEBUG("Checking key0's value: %lu", location_ptrs.size());
  EXPECT_EQ(1, location_ptrs.size());
  location_ptrs.clear();

  // This tests non-unqie key. If scan returns more than 1 then it should fail
  std::unique_ptr<storage::Tuple> key1(new storage::Tuple(key_schema, true));
  key1->SetValue(0, type::ValueFactory::GetIntegerValue(100), pool);
  key1->SetValue(1, type::ValueFactory::GetVarcharValue("b"), pool);

  index->ScanKey(key1.get(), location_ptrs);
  LOG_DEBUG("Checking key1's value: %lu", location_ptrs.size());
  EXPECT_EQ(1, location_ptrs.size());
  location_ptrs.clear();

  return;
}

void TestingIndexUtil::UniqueKeyDeleteTest(const IndexType index_type) {
  auto pool = TestingHarness::GetInstance().GetTestingPool();
  std::vector<ItemPointer *> location_ptrs;

  // INDEX
  std::unique_ptr<index::Index, void(*)(index::Index *)> index(
      TestingIndexUtil::BuildIndex(index_type, true), DestroyIndex);

  const catalog::Schema *key_schema = index->GetKeySchema();

  // Single threaded test
  size_t scale_factor = 1;
  LaunchParallelTest(1, TestingIndexUtil::InsertHelper, index.get(), pool,
                     scale_factor);
  LOG_DEBUG("INDEX VALUE CONTENTS BEFORE DELETE:\n%s",
{'fraction_non_alphanumeric': 0.17978549354662787, 'fraction_numerical': 0.01672423195782585, 'mean_word_length': 4.865138592750533, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 89, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5543947', 'n_tokens_mistral': 3667, 'n_tokens_neox': 3499, 'n_words': 604}