Skip to content

Commit

Permalink
Catalog (cmu-db#260)
Browse files Browse the repository at this point in the history
Initial version of catalog support.
  • Loading branch information
Yangjun Sheng authored and pervazea committed May 13, 2019
1 parent 7ab277f commit de75835
Show file tree
Hide file tree
Showing 53 changed files with 4,755 additions and 55 deletions.
3 changes: 2 additions & 1 deletion benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ list(APPEND BENCHMARK_UTIL_SRCS ${BENCHMARK_UTIL_HDRS})
###############################################
if (TERRIER_BUILD_BENCHMARKS)
add_library(benchmark_util STATIC ${BENCHMARK_UTIL_SRCS})
target_link_libraries(benchmark_util benchmark)
target_link_libraries(benchmark_util benchmark pthread)
add_dependencies(benchmark_util gtest)

add_subdirectory(catalog)
add_subdirectory(storage)
add_subdirectory(transaction)
endif()
1 change: 1 addition & 0 deletions benchmark/catalog/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ADD_TERRIER_BENCHMARKS()
63 changes: 63 additions & 0 deletions benchmark/catalog/catalog_benchmark.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <utility>
#include <vector>

#include "benchmark/benchmark.h"
#include "catalog/catalog.h"
#include "catalog/catalog_defs.h"
#include "common/scoped_timer.h"
#include "transaction/transaction_manager.h"
#include "util/transaction_benchmark_util.h"

namespace terrier {

class CatalogBenchmark : public benchmark::Fixture {
public:
void SetUp(const benchmark::State &state) final {
txn_manager_ = new transaction::TransactionManager(&buffer_pool_, true, LOGGING_DISABLED);

txn_ = txn_manager_->BeginTransaction();
catalog_ = new catalog::Catalog(txn_manager_, txn_);
}

void TearDown(const benchmark::State &state) final {
txn_manager_->Commit(txn_, TestCallbacks::EmptyCallback, nullptr);

delete catalog_; // need to delete catalog_first
delete txn_manager_;
delete txn_;
}

// transaction manager
transaction::TransactionManager *txn_manager_;
storage::RecordBufferSegmentPool buffer_pool_{100, 100};

transaction::TransactionContext *txn_;

catalog::Catalog *catalog_;

const int num_lookups = 100000;
};

// NOLINTNEXTLINE
BENCHMARK_DEFINE_F(CatalogBenchmark, DatabaseLookupTime)(benchmark::State &state) {
std::vector<type::TransientValue> search_vec, ret_row;
// setup search vector, lookup the default database
search_vec.push_back(type::TransientValueFactory::GetNull(type::TypeId::INTEGER));
search_vec.push_back(type::TransientValueFactory::GetVarChar("terrier"));

catalog::DatabaseCatalogTable db_handle = catalog_->GetDatabaseHandle();

// NOLINTNEXTLINE
for (auto _ : state) {
for (int32_t iter = 0; iter < num_lookups; iter++) {
// TODO(pakhtar): replace with GetDatabaseEntry(from name);
// ret_row = db_handle.pg_database_rw_->FindRow(txn_, search_vec);
auto entry = db_handle.GetDatabaseEntry(txn_, "terrier");
}
}
state.SetItemsProcessed(state.iterations() * num_lookups);
}

BENCHMARK_REGISTER_F(CatalogBenchmark, DatabaseLookupTime)->Unit(benchmark::kMillisecond)->MinTime(2);

} // namespace terrier
2 changes: 2 additions & 0 deletions benchmark/util/benchmark_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
// Modified from the Apache Arrow project for the Terrier project.

#include "benchmark/benchmark.h"
#include "loggers/catalog_logger.h"
#include "loggers/index_logger.h"
#include "loggers/main_logger.h"
#include "loggers/storage_logger.h"
Expand All @@ -29,6 +30,7 @@ int main(int argc, char **argv) {
terrier::storage::init_index_logger();
terrier::storage::init_storage_logger();
terrier::transaction::init_transaction_logger();
terrier::catalog::init_catalog_logger();

benchmark::Initialize(&argc, argv);
benchmark::RunSpecifiedBenchmarks();
Expand Down
74 changes: 74 additions & 0 deletions src/catalog/attr_def_handle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "catalog/attr_def_handle.h"
#include "catalog/catalog.h"
#include "catalog/catalog_defs.h"
#include "storage/sql_table.h"
#include "transaction/transaction_context.h"

namespace terrier::catalog {

const std::vector<SchemaCol> AttrDefCatalogTable::schema_cols_ = {{0, true, "oid", type::TypeId::INTEGER},
{1, true, "adrelid", type::TypeId::INTEGER},
{2, true, "adnum", type::TypeId::INTEGER},
{3, true, "adbin", type::TypeId::VARCHAR},
{4, false, "adsrc", type::TypeId::VARCHAR}};

// Find entry with (row) oid and return it
std::shared_ptr<AttrDefCatalogEntry> AttrDefCatalogTable::GetAttrDefEntry(transaction::TransactionContext *txn,
col_oid_t oid) {
std::vector<type::TransientValue> search_vec, ret_row;
search_vec.push_back(type::TransientValueFactory::GetInteger(!oid));
ret_row = pg_attrdef_rw_->FindRow(txn, search_vec);
if (ret_row.empty()) {
return nullptr;
}
return std::make_shared<AttrDefCatalogEntry>(oid, pg_attrdef_rw_, std::move(ret_row));
}

void AttrDefCatalogTable::DeleteEntries(transaction::TransactionContext *txn, table_oid_t table_oid) {
// auto layout = pg_attrdef_rw_->GetLayout();
int32_t col_index = pg_attrdef_rw_->ColNameToIndex("adrelid");

auto it = pg_attrdef_rw_->begin(txn);
while (it != pg_attrdef_rw_->end(txn)) {
// storage::ProjectedColumns::RowView row_view = it->InterpretAsRow(layout, 0);
storage::ProjectedColumns::RowView row_view = it->InterpretAsRow(0);
// check if a matching row, delete if it is
byte *col_p = row_view.AccessWithNullCheck(pg_attrdef_rw_->ColNumToOffset(col_index));
if (col_p == nullptr) {
continue;
}
auto col_int_value = *(reinterpret_cast<int32_t *>(col_p));
if (static_cast<uint32_t>(col_int_value) == !table_oid) {
// delete the entry
pg_attrdef_rw_->GetSqlTable()->Delete(txn, *(it->TupleSlots()));
}
++it;
}
}

SqlTableHelper *AttrDefCatalogTable::Create(transaction::TransactionContext *txn, Catalog *catalog, db_oid_t db_oid,
const std::string &name) {
catalog::SqlTableHelper *pg_attrdef;

// get an oid
table_oid_t pg_attrdef_oid(catalog->GetNextOid());

// uninitialized storage
pg_attrdef = new catalog::SqlTableHelper(pg_attrdef_oid);

for (auto col : AttrDefCatalogTable::schema_cols_) {
pg_attrdef->DefineColumn(col.col_name, col.type_id, false, col_oid_t(catalog->GetNextOid()));
}

// now actually create, with the provided schema
pg_attrdef->Create();
catalog->AddToMap(db_oid, CatalogTableType::ATTRDEF, pg_attrdef);
return pg_attrdef;
}

} // namespace terrier::catalog
99 changes: 99 additions & 0 deletions src/catalog/attribute_handle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#include "catalog/attribute_handle.h"
#include <iostream>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "catalog/catalog.h"
#include "catalog/schema.h"
#include "common/exception.h"
#include "loggers/catalog_logger.h"
#include "storage/block_layout.h"
#include "storage/sql_table.h"
#include "storage/storage_defs.h"
#include "type/type_id.h"

namespace terrier::catalog {

// note that this is not identical to Postgres's column sequence

const std::vector<SchemaCol> AttributeCatalogTable::schema_cols_ = {
{0, true, "oid", type::TypeId::INTEGER}, {1, true, "attrelid", type::TypeId::INTEGER},
{2, true, "attname", type::TypeId::VARCHAR}, {3, true, "atttypid", type::TypeId::INTEGER},
{4, true, "attlen", type::TypeId::INTEGER}, {5, true, "attnum", type::TypeId::INTEGER}};

// TODO(pakhtar): add unused columns

std::shared_ptr<AttributeCatalogEntry> AttributeCatalogTable::GetAttributeEntry(transaction::TransactionContext *txn,
table_oid_t table_oid,
col_oid_t col_oid) {
std::vector<type::TransientValue> search_vec, ret_row;
search_vec.push_back(type::TransientValueFactory::GetInteger(!col_oid));
search_vec.push_back(type::TransientValueFactory::GetInteger(!table_oid));
ret_row = pg_attribute_hrw_->FindRow(txn, search_vec);
if (ret_row.empty()) {
return nullptr;
}
col_oid_t oid(type::TransientValuePeeker::PeekInteger(ret_row[0]));
return std::make_shared<AttributeCatalogEntry>(oid, pg_attribute_hrw_, std::move(ret_row));
}

std::shared_ptr<AttributeCatalogEntry> AttributeCatalogTable::GetAttributeEntry(transaction::TransactionContext *txn,
table_oid_t table_oid,
const std::string &name) {
std::vector<type::TransientValue> search_vec, ret_row;
search_vec.push_back(type::TransientValueFactory::GetNull(type::TypeId::INTEGER));
search_vec.push_back(type::TransientValueFactory::GetInteger(!table_oid));
search_vec.push_back(type::TransientValueFactory::GetVarChar(name));
ret_row = pg_attribute_hrw_->FindRow(txn, search_vec);
if (ret_row.empty()) {
return nullptr;
// throw CATALOG_EXCEPTION("attribute doesn't exist");
}
col_oid_t oid(type::TransientValuePeeker::PeekInteger(ret_row[0]));
return std::make_shared<AttributeCatalogEntry>(oid, pg_attribute_hrw_, std::move(ret_row));
}

void AttributeCatalogTable::DeleteEntries(transaction::TransactionContext *txn, table_oid_t table_oid) {
// auto layout = pg_attribute_hrw_->GetLayout();
int32_t col_index = pg_attribute_hrw_->ColNameToIndex("attrelid");

auto it = pg_attribute_hrw_->begin(txn);
while (it != pg_attribute_hrw_->end(txn)) {
// storage::ProjectedColumns::RowView row_view = it->InterpretAsRow(layout, 0);
storage::ProjectedColumns::RowView row_view = it->InterpretAsRow(0);
// check if a matching row, delete if it is
byte *col_p = row_view.AccessWithNullCheck(pg_attribute_hrw_->ColNumToOffset(col_index));
if (col_p == nullptr) {
continue;
}
auto col_int_value = *(reinterpret_cast<int32_t *>(col_p));
if (static_cast<uint32_t>(col_int_value) == !table_oid) {
// delete the entry
pg_attribute_hrw_->GetSqlTable()->Delete(txn, *(it->TupleSlots()));
}
++it;
}
}

SqlTableHelper *AttributeCatalogTable::Create(transaction::TransactionContext *txn, Catalog *catalog, db_oid_t db_oid,
const std::string &name) {
catalog::SqlTableHelper *pg_attr;

// get an oid
table_oid_t pg_attr_oid(catalog->GetNextOid());

// uninitialized storage
pg_attr = new catalog::SqlTableHelper(pg_attr_oid);

for (auto col : AttributeCatalogTable::schema_cols_) {
pg_attr->DefineColumn(col.col_name, col.type_id, false, col_oid_t(catalog->GetNextOid()));
}

// now actually create, with the provided schema
pg_attr->Create();
catalog->AddToMap(db_oid, CatalogTableType::ATTRIBUTE, pg_attr);
return pg_attr;
}

} // namespace terrier::catalog
Loading

0 comments on commit de75835

Please sign in to comment.