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

Commit b618603

Browse files
Added catalog functions
1 parent e53a49c commit b618603

File tree

4 files changed

+96
-15
lines changed

4 files changed

+96
-15
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: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "catalog/database_catalog.h"
1818
#include "catalog/index_catalog.h"
1919
#include "catalog/column_catalog.h"
20+
#include "catalog/layout_catalog.h"
2021
#include "concurrency/transaction_context.h"
2122
#include "storage/data_table.h"
2223
#include "type/value_factory.h"
@@ -303,6 +304,87 @@ std::shared_ptr<ColumnCatalogObject> TableCatalogObject::GetColumnObject(
303304
return nullptr;
304305
}
305306

307+
/* @brief Insert layout object into the cache.
308+
* @param layout Layout object to be inserted
309+
* @return false if layout already exists in cache
310+
*/
311+
bool TableCatalogObject::InsertLayout(std::shared_ptr<const storage::Layout> layout) {
312+
313+
// Invalid object
314+
if (layout == nullptr) {
315+
return false;
316+
}
317+
318+
oid_t layout_id = layout->GetLayoutId();
319+
// layout is already present in the cache.
320+
if (layout_objects_.find(layout_id) != layout_objects_.end()) {
321+
LOG_DEBUG("Layout %u already exists in cache!", layout_id);
322+
return false;
323+
}
324+
325+
valid_layout_objects_ = true;
326+
layout_objects_.insert(std::make_pair(layout_id, layout));
327+
return true;
328+
}
329+
330+
/*
331+
* @brief evict all layout objects from cache
332+
*/
333+
void TableCatalogObject::EvictAllLayouts() {
334+
layout_objects_.clear();
335+
valid_layout_objects_ = false;
336+
}
337+
338+
/* @brief Get all layout objects of this table.
339+
* Add it to the cache if necessary.
340+
* @param cached_only If set to true, don't fetch the layout objects.
341+
* @return Map from layout_oid to cached layout object.
342+
*/
343+
std::unordered_map<oid_t, std::shared_ptr<const storage::Layout>>
344+
TableCatalogObject::GetLayouts(bool cached_only) {
345+
if (!valid_layout_objects_ && !cached_only) {
346+
// get column catalog objects from pg_column
347+
LayoutCatalog::GetInstance()->GetLayouts(table_oid, txn);
348+
valid_column_objects = true;
349+
}
350+
return layout_objects_;
351+
}
352+
353+
/* @brief get the layout object of the given layout_id.
354+
* @param layout_id The id of the layout to be fetched.
355+
* @param cached_only If set to true, don't fetch the layout objects.
356+
* @return Layout object of corresponding to the layout_id if present.
357+
*/
358+
std::shared_ptr<const storage::Layout>
359+
TableCatalogObject::GetLayout(oid_t layout_id, bool cached_entry) {
360+
// fetch layout objects in case we have not
361+
GetLayouts(cached_entry);
362+
auto it = layout_objects_.find(layout_id);
363+
if (it != layout_objects_.end()) {
364+
return it->second;
365+
}
366+
return nullptr;
367+
}
368+
369+
/* @brief Evict layout from the cache.
370+
* @param layout_id Id of the layout to be deleted.
371+
* @return true if layout_id is found and evicted; false if not found.
372+
*/
373+
bool TableCatalogObject::EvictLayout(oid_t layout_id) {
374+
if (!valid_layout_objects_) return false;
375+
376+
// find layout from the cache
377+
auto it = layout_objects_.find(layout_id);
378+
if (it == layout_objects_.end()) {
379+
return false; // layout_id not found in cache
380+
}
381+
382+
auto layout = it->second;
383+
PELOTON_ASSERT(layout);
384+
layout_objects_.erase(it);
385+
return true;
386+
}
387+
306388
TableCatalog *TableCatalog::GetInstance(storage::Database *pg_catalog,
307389
type::AbstractPool *pool,
308390
concurrency::TransactionContext *txn) {

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
@@ -32,9 +32,13 @@
3232

3333
#include "catalog/abstract_catalog.h"
3434
#include "executor/logical_tile.h"
35-
#include "storage/layout.h"
3635

3736
namespace peloton {
37+
38+
namespace storage {
39+
class Layout;
40+
}
41+
3842
namespace catalog {
3943

4044
class IndexCatalogObject;
@@ -74,7 +78,7 @@ class TableCatalogObject {
7478
const std::string &column_name, bool cached_only = false);
7579

7680
// Evict all layouts from the cache
77-
void EvictAllLayoutObjects();
81+
void EvictAllLayouts();
7882

7983
// Get layouts
8084
std::unordered_map<oid_t, std::shared_ptr<const storage::Layout>>

0 commit comments

Comments
 (0)