2222namespace peloton {
2323namespace catalog {
2424
25- LayoutCatalog *LayoutCatalog::GetInstance (storage::Database *pg_catalog,
26- type::AbstractPool *pool,
27- concurrency::TransactionContext *txn) {
28- static LayoutCatalog layout_catalog{pg_catalog, pool, txn};
29- return &layout_catalog;
30- }
31-
25+ /* * @brief Constructor invoked by the SystemsCatalog constructor.
26+ * @param pg_catalog The database to which this pg_layout belongs.
27+ */
3228LayoutCatalog::LayoutCatalog (
3329 storage::Database *pg_catalog,
3430 UNUSED_ATTRIBUTE type::AbstractPool *pool,
@@ -42,9 +38,12 @@ LayoutCatalog::LayoutCatalog(
4238 AddIndex ({ColumnId::TABLE_OID}, LAYOUT_CATALOG_SKEY0_OID,
4339 LAYOUT_CATALOG_NAME " _skey0" , IndexConstraintType::DEFAULT);
4440}
45-
41+ /* * @brief Destructor. Do nothing. Layouts will be dropped by DropTable. */
4642LayoutCatalog::~LayoutCatalog () {}
4743
44+ /* * @brief Initilailizes the schema for the pg_layout table.
45+ * @return unique_ptr of the schema for pg_layout.
46+ */
4847std::unique_ptr<catalog::Schema> LayoutCatalog::InitializeSchema () {
4948 const std::string primary_key_constraint_name = " primary_key" ;
5049 const std::string not_null_constraint_name = " not_null" ;
@@ -83,6 +82,13 @@ std::unique_ptr<catalog::Schema> LayoutCatalog::InitializeSchema() {
8382 return column_catalog_schema;
8483}
8584
85+ /* * @brief Insert a layout into the pg_layout table.
86+ * @param table_oid oid of the table to which the new layout belongs.
87+ * @param layout layout to be added to the pg_layout table.
88+ * @param pool to allocate memory for the column_map column.
89+ * @param txn TransactionContext for adding the layout.
90+ * @return true on success.
91+ */
8692bool LayoutCatalog::InsertLayout (oid_t table_oid,
8793 std::shared_ptr<const storage::Layout> layout,
8894 type::AbstractPool *pool,
@@ -106,26 +112,37 @@ bool LayoutCatalog::InsertLayout(oid_t table_oid,
106112 return InsertTuple (std::move (tuple), txn);
107113}
108114
109- bool LayoutCatalog::DeleteLayout (oid_t table_oid, oid_t layout_id,
115+ /* * @brief Delete a layout from the pg_layout table.
116+ * @param table_oid oid of the table to which the old layout belongs.
117+ * @param layout_oid oid of the layout to be deleted.
118+ * @param txn TransactionContext for deleting the layout.
119+ * @return true on success.
120+ */
121+ bool LayoutCatalog::DeleteLayout (oid_t table_oid, oid_t layout_oid,
110122 concurrency::TransactionContext *txn) {
111123 oid_t index_offset =
112124 IndexId::PRIMARY_KEY; // Index of table_oid & layout_oid
113125
114126 std::vector<type::Value> values;
115127 values.push_back (type::ValueFactory::GetIntegerValue (table_oid).Copy ());
116- values.push_back (type::ValueFactory::GetIntegerValue (layout_id ).Copy ());
128+ values.push_back (type::ValueFactory::GetIntegerValue (layout_oid ).Copy ());
117129
118130 auto pg_table = Catalog::GetInstance ()
119131 ->GetSystemCatalogs (database_oid)
120132 ->GetTableCatalog ();
121133
122134 // delete column from cache
123135 auto table_object = pg_table->GetTableObject (table_oid, txn);
124- table_object->EvictLayout (layout_id );
136+ table_object->EvictLayout (layout_oid );
125137
126138 return DeleteWithIndexScan (index_offset, values, txn);
127139}
128140
141+ /* * @brief Delete all layouts correponding to a table from the pg_layout.
142+ * @param table_oid oid of the table to delete all layouts.
143+ * @param txn TransactionContext for deleting the layouts.
144+ * @return true on success.
145+ */
129146bool LayoutCatalog::DeleteLayouts (oid_t table_oid,
130147 concurrency::TransactionContext *txn) {
131148 oid_t index_offset = IndexId::SKEY_TABLE_OID; // Index of table_oid
@@ -142,6 +159,11 @@ bool LayoutCatalog::DeleteLayouts(oid_t table_oid,
142159 return DeleteWithIndexScan (index_offset, values, txn);
143160}
144161
162+ /* * @brief Get all layouts correponding to a table from the pg_layout.
163+ * @param table_oid oid of the table to fetch all layouts.
164+ * @param txn TransactionContext for getting the layouts.
165+ * @return unordered_map containing a layout_oid -> layout mapping.
166+ */
145167const std::unordered_map<oid_t , std::shared_ptr<const storage::Layout>>
146168LayoutCatalog::GetLayouts (oid_t table_oid,
147169 concurrency::TransactionContext *txn) {
@@ -165,37 +187,35 @@ LayoutCatalog::GetLayouts(oid_t table_oid,
165187 auto result_tiles =
166188 GetResultWithIndexScan (column_ids, index_offset, values, txn);
167189
168- // result_tiles should contain only 1 LogicalTile per table
169- auto result_size = (*result_tiles).size ();
170- PELOTON_ASSERT (result_size <= 1 );
171- if (result_size == 0 ) {
172- LOG_DEBUG (" No entry for table %u in pg_layout" , table_oid);
173- return table_object->GetLayouts ();
174- }
175-
176- // Get the LogicalTile
177- auto tile = (*result_tiles)[0 ].get ();
178- auto tuple_count = tile->GetTupleCount ();
179- for (oid_t tuple_id = 0 ; tuple_id < tuple_count; tuple_id++) {
180- oid_t layout_oid =
181- tile->GetValue (tuple_id, LayoutCatalog::ColumnId::LAYOUT_OID)
182- .GetAs <oid_t >();
183- oid_t num_columns =
184- tile->GetValue (tuple_id, LayoutCatalog::ColumnId::NUM_COLUMNS)
185- .GetAs <oid_t >();
186- std::string column_map_str =
187- tile->GetValue (tuple_id, LayoutCatalog::ColumnId::COLUMN_MAP)
188- .ToString ();
189- auto column_map = storage::Layout::DeserializeColumnMap (num_columns,
190- column_map_str);
191- auto layout_object =
192- std::make_shared<const storage::Layout>(column_map, layout_oid);
193- table_object->InsertLayout (layout_object);
190+ for (auto &tile : (*result_tiles)) { // Iterate through the result_tiles
191+ for (auto tuple_id : *tile) {
192+ oid_t layout_oid =
193+ tile->GetValue (tuple_id, LayoutCatalog::ColumnId::LAYOUT_OID)
194+ .GetAs <oid_t >();
195+ oid_t num_columns =
196+ tile->GetValue (tuple_id, LayoutCatalog::ColumnId::NUM_COLUMNS)
197+ .GetAs <oid_t >();
198+ std::string column_map_str =
199+ tile->GetValue (tuple_id, LayoutCatalog::ColumnId::COLUMN_MAP)
200+ .ToString ();
201+ auto column_map = storage::Layout::DeserializeColumnMap (num_columns,
202+ column_map_str);
203+ auto layout_object =
204+ std::make_shared<const storage::Layout>(column_map, layout_oid);
205+ table_object->InsertLayout (layout_object);
206+ }
194207 }
195208
196209 return table_object->GetLayouts ();
197210}
198211
212+ /* * @brief Get the layout by layout_oid from the pg_layout.
213+ * @param table_oid oid of the table to fetch the layout.
214+ * @param layout_oid oid of the layout being queried.
215+ * @param txn TransactionContext for getting the layout.
216+ * @return shared_ptr corresponding to the layout_oid if found.
217+ * nullptr otherwise.
218+ */
199219std::shared_ptr<const storage::Layout>
200220LayoutCatalog::GetLayoutWithOid (oid_t table_oid, oid_t layout_oid,
201221 concurrency::TransactionContext *txn) {
0 commit comments