<commit_before>/*-------------------------------------------------------------------------
 *
 * database.cpp
 * file description
 *
 * Copyright(c) 2015, CMU
 *
 * /n-store/src/storage/table.cpp
 *
 *-------------------------------------------------------------------------
 */

#include "backend/storage/database.h"
#include "backend/storage/table_factory.h"
#include "backend/common/logger.h"

#include <sstream>

namespace peloton {
namespace storage {

Database::~Database() {
  // Clean up all the tables
  for (auto table : tables) delete table;
}

//===--------------------------------------------------------------------===//
// TABLE
//===--------------------------------------------------------------------===//

void Database::AddTable(storage::DataTable* table) {
  {
    std::lock_guard<std::mutex> lock(database_mutex);
    tables.push_back(table);
  }
}

storage::DataTable* Database::GetTableWithOid(const oid_t table_oid) const {
  for (auto table : tables)
    if (table->GetOid() == table_oid) return table;

  return nullptr;
}

storage::DataTable* Database::GetTableWithName(
    const std::string table_name) const {
  for (auto table : tables)
    if (table->GetName() == table_name) return table;

  return nullptr;
}

void Database::DropTableWithOid(const oid_t table_oid) {
  {
    std::lock_guard<std::mutex> lock(database_mutex);

    oid_t table_offset = 0;
    for (auto table : tables) {
      if (table->GetOid() == table_oid) break;
      table_offset++;
    }
    assert(table_offset < tables.size());

    // Drop the database
    tables.erase(tables.begin() + table_offset);
  }
}

storage::DataTable* Database::GetTable(const oid_t table_offset) const {
  assert(table_offset < tables.size());
  auto table = tables.at(table_offset);
  return table;
}

oid_t Database::GetTableCount() const { return tables.size(); }

//===--------------------------------------------------------------------===//
// STATS
//===--------------------------------------------------------------------===//

void Database::UpdateStats(){
  LOG_INFO("Update All Stats in Database(%u)", database_oid);
  for( int table_offset=0; table_offset<GetTableCount(); table_offset++){
    auto table = GetTable(table_offset);
    bridge::Bridge::SetNumberOfTuples(table->GetOid(),
                                      table->GetNumberOfTuples());

    for (int index_offset = 0; index_offset < table->GetIndexCount();
         index_offset++) {
      auto index = table->GetIndex(index_offset);
      bridge::Bridge::SetNumberOfTuples(index->GetOid(),
                                        index->GetNumberOfTuples());
    }
  }
}

void Database::UpdateStatsWithOid(const oid_t table_oid){
  LOG_INFO("Update table(%u)'s stats in Database(%u)", table_oid, database_oid);
  auto table = GetTableWithOid(table_oid);
  bridge::Bridge::SetNumberOfTuples(table_oid, table->GetNumberOfTuples());

  for (int index_offset = 0; index_offset < table->GetIndexCount();
       index_offset++) {
    auto index = table->GetIndex(index_offset);
    bridge::Bridge::SetNumberOfTuples(index->GetOid(),
                                      index->GetNumberOfTuples());
  }
}

//===--------------------------------------------------------------------===//
// UTILITIES
//===--------------------------------------------------------------------===//

std::ostream& operator<<(std::ostream& os, const Database& database) {
  os << "=====================================================\n";
  os << "DATABASE(" << database.GetOid() << ") : \n";

  oid_t table_count = database.GetTableCount();
  std::cout << "Table Count : " << table_count << "\n";

  oid_t table_itr = 0;
  for (auto table : database.tables) {
    if (table != nullptr) {
      std::cout << "(" << ++table_itr << "/" << table_count << ") "
                << "Table Name : " << table->GetName() << "\n"
                << *(table->GetSchema()) << std::endl;

      oid_t index_count = table->GetIndexCount();
      std::cout << "Index Count : " << index_count << std::endl;

      if (index_count > 0) {
        for (int index_itr = 0; index_itr < index_count; index_itr++) {
          index::Index* index = table->GetIndex(index_itr);<commit_msg>Print index count only if we have index<commit_after>/*-------------------------------------------------------------------------
 *
 * database.cpp
 * file description
 *
 * Copyright(c) 2015, CMU
 *
 * /n-store/src/storage/table.cpp
 *
 *-------------------------------------------------------------------------
 */

#include "backend/storage/database.h"
#include "backend/storage/table_factory.h"
#include "backend/common/logger.h"

#include <sstream>

namespace peloton {
namespace storage {

Database::~Database() {
  // Clean up all the tables
  for (auto table : tables) delete table;
}

//===--------------------------------------------------------------------===//
// TABLE
//===--------------------------------------------------------------------===//

void Database::AddTable(storage::DataTable* table) {
  {
    std::lock_guard<std::mutex> lock(database_mutex);
    tables.push_back(table);
  }
}

storage::DataTable* Database::GetTableWithOid(const oid_t table_oid) const {
  for (auto table : tables)
    if (table->GetOid() == table_oid) return table;

  return nullptr;
}

storage::DataTable* Database::GetTableWithName(
    const std::string table_name) const {
  for (auto table : tables)
    if (table->GetName() == table_name) return table;

  return nullptr;
}

void Database::DropTableWithOid(const oid_t table_oid) {
  {
    std::lock_guard<std::mutex> lock(database_mutex);

    oid_t table_offset = 0;
    for (auto table : tables) {
      if (table->GetOid() == table_oid) break;
      table_offset++;
    }
    assert(table_offset < tables.size());

    // Drop the database
    tables.erase(tables.begin() + table_offset);
  }
}

storage::DataTable* Database::GetTable(const oid_t table_offset) const {
  assert(table_offset < tables.size());
  auto table = tables.at(table_offset);
  return table;
}

oid_t Database::GetTableCount() const { return tables.size(); }

//===--------------------------------------------------------------------===//
// STATS
//===--------------------------------------------------------------------===//

void Database::UpdateStats(){
  LOG_INFO("Update All Stats in Database(%u)", database_oid);
  for( int table_offset=0; table_offset<GetTableCount(); table_offset++){
    auto table = GetTable(table_offset);
    bridge::Bridge::SetNumberOfTuples(table->GetOid(),
                                      table->GetNumberOfTuples());

    for (int index_offset = 0; index_offset < table->GetIndexCount();
         index_offset++) {
      auto index = table->GetIndex(index_offset);
      bridge::Bridge::SetNumberOfTuples(index->GetOid(),
                                        index->GetNumberOfTuples());
    }
  }
}

void Database::UpdateStatsWithOid(const oid_t table_oid){
  LOG_INFO("Update table(%u)'s stats in Database(%u)", table_oid, database_oid);
  auto table = GetTableWithOid(table_oid);
  bridge::Bridge::SetNumberOfTuples(table_oid, table->GetNumberOfTuples());

  for (int index_offset = 0; index_offset < table->GetIndexCount();
       index_offset++) {
    auto index = table->GetIndex(index_offset);
    bridge::Bridge::SetNumberOfTuples(index->GetOid(),
                                      index->GetNumberOfTuples());
  }
}

//===--------------------------------------------------------------------===//
// UTILITIES
//===--------------------------------------------------------------------===//

std::ostream& operator<<(std::ostream& os, const Database& database) {
  os << "=====================================================\n";
  os << "DATABASE(" << database.GetOid() << ") : \n";

  oid_t table_count = database.GetTableCount();
  std::cout << "Table Count : " << table_count << "\n";

  oid_t table_itr = 0;
  for (auto table : database.tables) {
    if (table != nullptr) {
      std::cout << "(" << ++table_itr << "/" << table_count << ") "
                << "Table Name(" << table->GetOid() << ") : " << table->GetName() << "\n"
                << *(table->GetSchema()) << std::endl;

      oid_t index_count = table->GetIndexCount();

      if (index_count > 0) {
        std::cout << "Index Count : " << index_count << std::endl;
        for (int index_itr = 0; index_itr < index_count; index_itr++) {
          index::Index* index = table->GetIndex(index_itr);
{'fraction_non_alphanumeric': 0.28713222261609356, 'fraction_numerical': 0.002599550986647761, 'mean_word_length': 2.041322314049587, 'pattern_counts': {'":': 0, '<': 109, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1189209', 'n_tokens_mistral': 2416, 'n_tokens_neox': 2343, 'n_words': 475}