Skip to content
This repository was archived by the owner on Sep 27, 2019. It is now read-only.

Commit 0222d8a

Browse files
Added catalog functions
1 parent 2746f00 commit 0222d8a

File tree

4 files changed

+106
-17
lines changed

4 files changed

+106
-17
lines changed

src/catalog/layout_catalog.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ LayoutCatalog::LayoutCatalog(storage::Database *pg_catalog,
5656

5757
LayoutCatalog::~LayoutCatalog() {}
5858

59-
std::unique_ptr<catalog::Schema> ColumnCatalog::InitializeSchema() {
59+
std::unique_ptr<catalog::Schema> LayoutCatalog::InitializeSchema() {
6060
const std::string primary_key_constraint_name = "primary_key";
6161
const std::string not_null_constraint_name = "not_null";
6262

@@ -108,10 +108,10 @@ bool LayoutCatalog::InsertLayout(oid_t table_oid,
108108
auto val3 = type::ValueFactory::GetVarcharValue(layout->SerializeColumnMap(),
109109
nullptr);
110110

111-
tuple->SetValue(ColumnId::TABLE_OID, val0);
112-
tuple->SetValue(ColumnId::LAYOUT_OID, val1);
113-
tuple->SetValue(ColumnId::NUM_COLUMNS, val2);
114-
tuple->SetValue(ColumnId::COLUMN_MAP, val3);
111+
tuple->SetValue(ColumnId::TABLE_OID, val0, pool);
112+
tuple->SetValue(ColumnId::LAYOUT_OID, val1, pool);
113+
tuple->SetValue(ColumnId::NUM_COLUMNS, val2, pool);
114+
tuple->SetValue(ColumnId::COLUMN_MAP, val3, pool);
115115

116116
// Insert the tuple
117117
return InsertTuple(std::move(tuple), txn);
@@ -135,8 +135,8 @@ bool LayoutCatalog::DeleteLayout(oid_t table_oid, oid_t layout_id,
135135
}
136136

137137
const std::unordered_map<oid_t, std::shared_ptr<const storage::Layout>>
138-
LayoutCatalog::GetLayouts(oid_t table_oid, oid_t layout_id,
139-
concurrency::TransactionContext *txn) {
138+
LayoutCatalog::GetLayouts(oid_t table_oid,
139+
concurrency::TransactionContext *txn) {
140140
// Try to find the layouts in the cache
141141
auto table_object =
142142
TableCatalog::GetInstance()->GetTableObject(table_oid, txn);
@@ -175,9 +175,5 @@ LayoutCatalog::GetLayouts(oid_t table_oid, oid_t layout_id,
175175
return table_object->GetLayouts();
176176
}
177177

178-
179-
180-
181-
182178
} // namespace catalog
183179
} // namespace peloton

src/catalog/table_catalog.cpp

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "catalog/column_catalog.h"
1919
#include "catalog/database_catalog.h"
2020
#include "catalog/index_catalog.h"
21+
#include "catalog/layout_catalog.h"
2122
#include "catalog/system_catalogs.h"
2223
#include "concurrency/transaction_context.h"
2324
#include "storage/data_table.h"
@@ -319,8 +320,8 @@ std::shared_ptr<ColumnCatalogObject> TableCatalogObject::GetColumnObject(
319320
TableCatalog::TableCatalog(
320321
storage::Database *database, UNUSED_ATTRIBUTE type::AbstractPool *pool,
321322
UNUSED_ATTRIBUTE concurrency::TransactionContext *txn)
322-
: AbstractCatalog(TABLE_CATALOG_OID, TABLE_CATALOG_NAME,
323-
InitializeSchema().release(), database) {
323+
: AbstractCatalog(TABLE_CATALOG_OID, TABLE_CATALOG_NAME,
324+
InitializeSchema().release(), database) {
324325
// Add indexes for pg_namespace
325326
AddIndex({0}, TABLE_CATALOG_PKEY_OID, TABLE_CATALOG_NAME "_pkey",
326327
IndexConstraintType::PRIMARY_KEY);
@@ -330,6 +331,95 @@ TableCatalog::TableCatalog(
330331
IndexConstraintType::DEFAULT);
331332
}
332333

334+
/* @brief Insert layout object into the cache.
335+
* @param layout Layout object to be inserted
336+
* @return false if layout already exists in cache
337+
*/
338+
bool TableCatalogObject::InsertLayout(std::shared_ptr<const storage::Layout> layout) {
339+
340+
// Invalid object
341+
if (layout == nullptr) {
342+
return false;
343+
}
344+
345+
oid_t layout_id = layout->GetLayoutId();
346+
// layout is already present in the cache.
347+
if (layout_objects_.find(layout_id) != layout_objects_.end()) {
348+
LOG_DEBUG("Layout %u already exists in cache!", layout_id);
349+
return false;
350+
}
351+
352+
valid_layout_objects_ = true;
353+
layout_objects_.insert(std::make_pair(layout_id, layout));
354+
return true;
355+
}
356+
357+
/*
358+
* @brief evict all layout objects from cache
359+
*/
360+
void TableCatalogObject::EvictAllLayouts() {
361+
layout_objects_.clear();
362+
valid_layout_objects_ = false;
363+
}
364+
365+
/* @brief Get all layout objects of this table.
366+
* Add it to the cache if necessary.
367+
* @param cached_only If set to true, don't fetch the layout objects.
368+
* @return Map from layout_oid to cached layout object.
369+
*/
370+
std::unordered_map<oid_t, std::shared_ptr<const storage::Layout>>
371+
TableCatalogObject::GetLayouts(bool cached_only) {
372+
if (!valid_layout_objects_ && !cached_only) {
373+
// get column catalog objects from pg_column
374+
LayoutCatalog::GetInstance()->GetLayouts(table_oid, txn);
375+
valid_column_objects = true;
376+
}
377+
return layout_objects_;
378+
}
379+
380+
/* @brief get the layout object of the given layout_id.
381+
* @param layout_id The id of the layout to be fetched.
382+
* @param cached_only If set to true, don't fetch the layout objects.
383+
* @return Layout object of corresponding to the layout_id if present.
384+
*/
385+
std::shared_ptr<const storage::Layout>
386+
TableCatalogObject::GetLayout(oid_t layout_id, bool cached_entry) {
387+
// fetch layout objects in case we have not
388+
GetLayouts(cached_entry);
389+
auto it = layout_objects_.find(layout_id);
390+
if (it != layout_objects_.end()) {
391+
return it->second;
392+
}
393+
return nullptr;
394+
}
395+
396+
/* @brief Evict layout from the cache.
397+
* @param layout_id Id of the layout to be deleted.
398+
* @return true if layout_id is found and evicted; false if not found.
399+
*/
400+
bool TableCatalogObject::EvictLayout(oid_t layout_id) {
401+
if (!valid_layout_objects_) return false;
402+
403+
// find layout from the cache
404+
auto it = layout_objects_.find(layout_id);
405+
if (it == layout_objects_.end()) {
406+
return false; // layout_id not found in cache
407+
}
408+
409+
auto layout = it->second;
410+
PELOTON_ASSERT(layout);
411+
layout_objects_.erase(it);
412+
return true;
413+
}
414+
415+
TableCatalog *TableCatalog::GetInstance(storage::Database *pg_catalog,
416+
type::AbstractPool *pool,
417+
concurrency::TransactionContext *txn) {
418+
static TableCatalog table_catalog{pg_catalog, pool, txn};
419+
return &table_catalog;
420+
}
421+
422+
333423
TableCatalog::~TableCatalog() {}
334424

335425
/*@brief private function for initialize schema of pg_table

src/include/catalog/layout_catalog.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ class LayoutCatalog : public AbstractCatalog {
6363
// Read Related API
6464
//===--------------------------------------------------------------------===//
6565
const std::unordered_map<oid_t, std::shared_ptr<const storage::Layout>>
66-
GetLayouts(oid_t table_oid, oid_t layout_id,
67-
concurrency::TransactionContext *txn);
66+
GetLayouts(oid_t table_oid, concurrency::TransactionContext *txn);
6867

6968
private:
7069

src/include/catalog/table_catalog.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,13 @@
3434

3535
#include "catalog/abstract_catalog.h"
3636
#include "executor/logical_tile.h"
37-
#include "storage/layout.h"
3837

3938
namespace peloton {
39+
40+
namespace storage {
41+
class Layout;
42+
}
43+
4044
namespace catalog {
4145

4246
class IndexCatalogObject;
@@ -76,7 +80,7 @@ class TableCatalogObject {
7680
const std::string &column_name, bool cached_only = false);
7781

7882
// Evict all layouts from the cache
79-
void EvictAllLayoutObjects();
83+
void EvictAllLayouts();
8084

8185
// Get layouts
8286
std::unordered_map<oid_t, std::shared_ptr<const storage::Layout>>

0 commit comments

Comments
 (0)