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

Commit e53a49c

Browse files
Added LayoutCatalog. Yet to modify TableCatalog
1 parent 93b44e1 commit e53a49c

File tree

8 files changed

+389
-4
lines changed

8 files changed

+389
-4
lines changed

src/catalog/layout_catalog.cpp

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Peloton
4+
//
5+
// column_catalog.h
6+
//
7+
// Identification: src/include/catalog/layout_catalog.cpp
8+
//
9+
// Copyright (c) 2015-18, Carnegie Mellon University Database Group
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "catalog/layout_catalog.h"
14+
15+
#include "catalog/column_catalog.h"
16+
#include "catalog/table_catalog.h"
17+
#include "concurrency/transaction_context.h"
18+
#include "storage/data_table.h"
19+
#include "storage/layout.h"
20+
21+
namespace peloton {
22+
namespace catalog {
23+
24+
LayoutCatalog *LayoutCatalog::GetInstance(storage::Database *pg_catalog,
25+
type::AbstractPool *pool,
26+
concurrency::TransactionContext *txn) {
27+
static LayoutCatalog layout_catalog{pg_catalog, pool, txn};
28+
return &layout_catalog;
29+
}
30+
31+
LayoutCatalog::LayoutCatalog(storage::Database *pg_catalog,
32+
type::AbstractPool *pool,
33+
concurrency::TransactionContext *txn)
34+
: AbstractCatalog(LAYOUT_CATALOG_OID, LAYOUT_CATALOG_NAME,
35+
InitializeSchema().release(), pg_catalog) {
36+
// Add indexes for pg_attribute
37+
AddIndex({ColumnId::TABLE_OID, ColumnId::LAYOUT_OID},
38+
LAYOUT_CATALOG_PKEY_OID, LAYOUT_CATALOG_NAME "_pkey",
39+
IndexConstraintType::PRIMARY_KEY);
40+
AddIndex({ColumnId::TABLE_OID}, LAYOUT_CATALOG_SKEY0_OID,
41+
LAYOUT_CATALOG_NAME "_skey0", IndexConstraintType::DEFAULT);
42+
43+
// Insert columns into pg_attribute
44+
ColumnCatalog *pg_attribute =
45+
ColumnCatalog::GetInstance(pg_catalog, pool, txn);
46+
47+
oid_t column_id = 0;
48+
for (auto column : catalog_table_->GetSchema()->GetColumns()) {
49+
pg_attribute->InsertColumn(LAYOUT_CATALOG_OID, column.GetName(), column_id,
50+
column.GetOffset(), column.GetType(),
51+
column.IsInlined(), column.GetConstraints(),
52+
pool, txn);
53+
column_id++;
54+
}
55+
}
56+
57+
LayoutCatalog::~LayoutCatalog() {}
58+
59+
std::unique_ptr<catalog::Schema> ColumnCatalog::InitializeSchema() {
60+
const std::string primary_key_constraint_name = "primary_key";
61+
const std::string not_null_constraint_name = "not_null";
62+
63+
auto table_id_column = catalog::Column(
64+
type::TypeId::INTEGER, type::Type::GetTypeSize(type::TypeId::INTEGER),
65+
"table_oid", true);
66+
table_id_column.AddConstraint(catalog::Constraint(
67+
ConstraintType::PRIMARY, primary_key_constraint_name));
68+
table_id_column.AddConstraint(
69+
catalog::Constraint(ConstraintType::NOTNULL, not_null_constraint_name));
70+
71+
auto layout_oid_column = catalog::Column(
72+
type::TypeId::INTEGER, type::Type::GetTypeSize(type::TypeId::INTEGER),
73+
"layout_oid", true);
74+
layout_oid_column.AddConstraint(catalog::Constraint(
75+
ConstraintType::PRIMARY, primary_key_constraint_name));
76+
layout_oid_column.AddConstraint(
77+
catalog::Constraint(ConstraintType::NOTNULL, not_null_constraint_name));
78+
79+
auto num_columns_column = catalog::Column(
80+
type::TypeId::INTEGER, type::Type::GetTypeSize(type::TypeId::INTEGER),
81+
"num_columns", true);
82+
num_columns_column.AddConstraint(
83+
catalog::Constraint(ConstraintType::NOTNULL, not_null_constraint_name));
84+
85+
auto column_map_column = catalog::Column(
86+
type::TypeId::VARCHAR, type::Type::GetTypeSize(type::TypeId::VARCHAR),
87+
"column_map", false);
88+
column_map_column.AddConstraint(
89+
catalog::Constraint(ConstraintType::NOTNULL, not_null_constraint_name));
90+
91+
std::unique_ptr<catalog::Schema> column_catalog_schema(new catalog::Schema(
92+
{table_id_column, layout_oid_column, num_columns_column, column_map_column}));
93+
94+
return column_catalog_schema;
95+
}
96+
97+
bool LayoutCatalog::InsertLayout(oid_t table_oid,
98+
std::shared_ptr<const storage::Layout> layout,
99+
type::AbstractPool *pool,
100+
concurrency::TransactionContext *txn) {
101+
// Create the tuple first
102+
std::unique_ptr<storage::Tuple> tuple(
103+
new storage::Tuple(catalog_table_->GetSchema(), true));
104+
105+
auto val0 = type::ValueFactory::GetIntegerValue(table_oid);
106+
auto val1 = type::ValueFactory::GetIntegerValue(layout->GetLayoutId());
107+
auto val2 = type::ValueFactory::GetIntegerValue(layout->GetColumnCount());
108+
auto val3 = type::ValueFactory::GetVarcharValue(layout->SerializeColumnMap(),
109+
nullptr);
110+
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);
115+
116+
// Insert the tuple
117+
return InsertTuple(std::move(tuple), txn);
118+
}
119+
120+
bool LayoutCatalog::DeleteLayout(oid_t table_oid, oid_t layout_id,
121+
concurrency::TransactionContext *txn) {
122+
oid_t index_offset =
123+
IndexId::PRIMARY_KEY; // Index of table_oid & layout_oid
124+
125+
std::vector<type::Value> values;
126+
values.push_back(type::ValueFactory::GetIntegerValue(table_oid).Copy());
127+
values.push_back(type::ValueFactory::GetIntegerValue(layout_id).Copy());
128+
129+
// delete column from cache
130+
auto table_object =
131+
TableCatalog::GetInstance()->GetTableObject(table_oid, txn);
132+
table_object->EvictLayout(layout_id);
133+
134+
return DeleteWithIndexScan(index_offset, values, txn);
135+
}
136+
137+
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) {
140+
// Try to find the layouts in the cache
141+
auto table_object =
142+
TableCatalog::GetInstance()->GetTableObject(table_oid, txn);
143+
PELOTON_ASSERT(table_object && table_object->GetTableOid() == table_oid);
144+
auto layout_objects = table_object->GetLayouts(true);
145+
if (layout_objects.size() != 0) {
146+
return layout_objects;
147+
}
148+
149+
// Cache miss, get from pg_catalog
150+
std::vector<oid_t> column_ids(all_column_ids);
151+
oid_t index_offset = IndexId::SKEY_TABLE_OID; // Index of table_oid
152+
std::vector<type::Value> values;
153+
values.push_back(type::ValueFactory::GetIntegerValue(table_oid).Copy());
154+
155+
auto result_tiles =
156+
GetResultWithIndexScan(column_ids, index_offset, values, txn);
157+
158+
for (auto &tile : (*result_tiles)) {
159+
for (auto tuple_id : *tile) {
160+
oid_t layout_oid = tile->GetValue(tuple_id, ColumnId::LAYOUT_OID)
161+
.GetAs<oid_t>();
162+
oid_t num_coulmns = tile->GetValue(tuple_id, ColumnId::NUM_COLUMNS)
163+
.GetAs<oid_t>();
164+
165+
std::string column_map_str = tile->GetValue(
166+
tuple_id, ColumnId::COLUMN_MAP).GetAs<std::string>();
167+
auto column_map = storage::Layout::DeserializeColumnMap(num_coulmns,
168+
column_map_str);
169+
auto layout_object =
170+
std::make_shared<const storage::Layout>(column_map, layout_oid);
171+
table_object->InsertLayout(layout_object);
172+
}
173+
}
174+
175+
return table_object->GetLayouts();
176+
}
177+
178+
179+
180+
181+
182+
} // namespace catalog
183+
} // namespace peloton

src/include/catalog/catalog_defaults.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ namespace catalog {
3333
#define TABLE_CATALOG_NAME "pg_table"
3434
#define INDEX_CATALOG_NAME "pg_index"
3535
#define COLUMN_CATALOG_NAME "pg_attribute"
36+
#define LAYOUT_CATALOG_NAME "pg_layout"
3637

3738
// Local oids from START_OID = 0 to START_OID + OID_OFFSET are reserved
3839
#define OID_OFFSET 100
@@ -53,6 +54,7 @@ namespace catalog {
5354
#define TABLE_CATALOG_OID (1 | TABLE_OID_MASK)
5455
#define INDEX_CATALOG_OID (2 | TABLE_OID_MASK)
5556
#define COLUMN_CATALOG_OID (3 | TABLE_OID_MASK)
57+
#define LAYOUT_CATALOG_OID (4 | TABLE_OID_MASK)
5658

5759
// Reserved pg_column index oid
5860
#define COLUMN_CATALOG_PKEY_OID (0 | INDEX_OID_MASK)
@@ -64,6 +66,10 @@ namespace catalog {
6466
#define INDEX_CATALOG_SKEY0_OID (4 | INDEX_OID_MASK)
6567
#define INDEX_CATALOG_SKEY1_OID (5 | INDEX_OID_MASK)
6668

69+
// Reserve pg_layout index oid
70+
#define LAYOUT_CATALOG_PKEY_OID (6 | INDEX_OID_MASK)
71+
#define LAYOUT_CATALOG_SKEY0_OID (7 | INDEX_OID_MASK)
72+
6773
// Use upper 8 bits indicating catalog type
6874
#define CATALOG_TYPE_OFFSET 24
6975

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Peloton
4+
//
5+
// layout_catalog.h
6+
//
7+
// Identification: src/include/catalog/layout_catalog.h
8+
//
9+
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
//===----------------------------------------------------------------------===//
14+
// pg_layout
15+
//
16+
// Schema: (column offset: column_name)
17+
// 0: table_oid (pkey)
18+
// 1: layout_oid (pkey)
19+
// 2: num_columns (number of columns in the layout)
20+
// 3: column_map (map column_oid to <tile_offset, tile_column_oid>)
21+
//
22+
// Indexes: (index offset: indexed columns)
23+
// 0: table_oid & layout_oid (unique & primary key)
24+
// 1: table_oid (non-unique)
25+
//
26+
//===----------------------------------------------------------------------===//
27+
28+
#pragma once
29+
30+
#include "catalog/abstract_catalog.h"
31+
32+
namespace peloton {
33+
34+
namespace storage {
35+
class Layout;
36+
} // namespace storage
37+
38+
namespace catalog {
39+
40+
class LayoutCatalog : public AbstractCatalog {
41+
friend class TableCatalogObject;
42+
friend class Catalog;
43+
44+
public:
45+
// Global Singleton, only the first call requires passing parameters.
46+
static LayoutCatalog *GetInstance(storage::Database *pg_catalog = nullptr,
47+
type::AbstractPool *pool = nullptr,
48+
concurrency::TransactionContext *txn = nullptr);
49+
50+
~LayoutCatalog();
51+
52+
//===--------------------------------------------------------------------===//
53+
// write Related API
54+
//===--------------------------------------------------------------------===//
55+
bool InsertLayout(oid_t table_oid,
56+
std::shared_ptr<const storage::Layout> layout,
57+
type::AbstractPool *pool,
58+
concurrency::TransactionContext *txn);
59+
60+
bool DeleteLayout(oid_t table_oid, oid_t layout_id,
61+
concurrency::TransactionContext *txn);
62+
//===--------------------------------------------------------------------===//
63+
// Read Related API
64+
//===--------------------------------------------------------------------===//
65+
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);
68+
69+
private:
70+
71+
LayoutCatalog(storage::Database *pg_catalog, type::AbstractPool *pool,
72+
concurrency::TransactionContext *txn);
73+
74+
std::unique_ptr<catalog::Schema> InitializeSchema();
75+
76+
enum ColumnId {
77+
TABLE_OID = 0,
78+
LAYOUT_OID = 1,
79+
NUM_COLUMNS = 2,
80+
COLUMN_MAP = 3,
81+
// Add new columns here in creation order
82+
};
83+
std::vector<oid_t> all_column_ids = {0, 1, 2, 3};
84+
85+
enum IndexId {
86+
PRIMARY_KEY = 0,
87+
SKEY_TABLE_OID = 1,
88+
// Add new indexes here in creation order
89+
};
90+
};
91+
92+
93+
} // namespace catalog
94+
} // namespace peloton

src/include/catalog/table_catalog.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#include "catalog/abstract_catalog.h"
3434
#include "executor/logical_tile.h"
35+
#include "storage/layout.h"
3536

3637
namespace peloton {
3738
namespace catalog {
@@ -43,6 +44,7 @@ class TableCatalogObject {
4344
friend class TableCatalog;
4445
friend class IndexCatalog;
4546
friend class ColumnCatalog;
47+
friend class LayoutCatalog;
4648

4749
public:
4850
TableCatalogObject(executor::LogicalTile *tile, concurrency::TransactionContext *txn,
@@ -71,6 +73,15 @@ class TableCatalogObject {
7173
std::shared_ptr<ColumnCatalogObject> GetColumnObject(
7274
const std::string &column_name, bool cached_only = false);
7375

76+
// Evict all layouts from the cache
77+
void EvictAllLayoutObjects();
78+
79+
// Get layouts
80+
std::unordered_map<oid_t, std::shared_ptr<const storage::Layout>>
81+
GetLayouts(bool cached_only = false);
82+
std::shared_ptr<const storage::Layout> GetLayout(
83+
oid_t layout_id, bool cached_entry = false);
84+
7485
inline oid_t GetTableOid() { return table_oid; }
7586
inline const std::string &GetTableName() { return table_name; }
7687
inline oid_t GetDatabaseOid() { return database_oid; }
@@ -91,6 +102,11 @@ class TableCatalogObject {
91102
bool EvictColumnObject(oid_t column_id);
92103
bool EvictColumnObject(const std::string &column_name);
93104

105+
// Insert layout into table object
106+
bool InsertLayout(std::shared_ptr<const storage::Layout> layout);
107+
// Evict layout_id from the table object
108+
bool EvictLayout(oid_t layout_id);
109+
94110
// cache for *all* index catalog objects in this table
95111
std::unordered_map<oid_t, std::shared_ptr<IndexCatalogObject>> index_objects;
96112
std::unordered_map<std::string, std::shared_ptr<IndexCatalogObject>>
@@ -104,6 +120,11 @@ class TableCatalogObject {
104120
column_names;
105121
bool valid_column_objects;
106122

123+
// cache for *all* layout objects in the table
124+
std::unordered_map<oid_t, std::shared_ptr<const storage::Layout>>
125+
layout_objects_;
126+
bool valid_layout_objects_;
127+
107128
// Pointer to its corresponding transaction
108129
concurrency::TransactionContext *txn;
109130
};
@@ -113,6 +134,7 @@ class TableCatalog : public AbstractCatalog {
113134
friend class DatabaseCatalogObject;
114135
friend class ColumnCatalog;
115136
friend class IndexCatalog;
137+
friend class LayoutCatalog;
116138
friend class Catalog;
117139

118140
public:

0 commit comments

Comments
 (0)