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
0 commit comments