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

Commit 48b73fc

Browse files
Access LayoutCatalog via the global Catalog object
1 parent 403f39b commit 48b73fc

File tree

11 files changed

+156
-48
lines changed

11 files changed

+156
-48
lines changed

src/catalog/catalog.cpp

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,76 @@ ResultType Catalog::CreateIndex(
557557
return ResultType::SUCCESS;
558558
}
559559

560+
/*
561+
*
562+
// Create a new layout
563+
ResultType CreateLayout(oid_t database_oid, oid_t table_oid,
564+
const column_map_type &column_map,
565+
concurrency::TransactionContext *txn);
566+
// Create a new layout and set it as the default for the table
567+
ResultType CreateDefaultLayout(oid_t database_oid, oid_t table_oid,
568+
const column_map_type &column_map,
569+
concurrency::TransactionContext *txn);
570+
*/
571+
572+
/*
573+
* @brief create a new layout for a table
574+
* @param database_oid database to which the table belongs to
575+
* @param table_oid table to which the layout has to be added
576+
* @param column_map column_map of the new layout to be created
577+
* @param txn TransactionContext
578+
* @return shared_ptr shared_ptr to the newly created layout in case of
579+
* success. nullptr in case of failure.
580+
*/
581+
std::shared_ptr<const storage::Layout>
582+
Catalog::CreateLayout(oid_t database_oid, oid_t table_oid,
583+
const column_map_type &column_map,
584+
concurrency::TransactionContext *txn) {
585+
586+
auto storage_manager = storage::StorageManager::GetInstance();
587+
auto database = storage_manager->GetDatabaseWithOid(database_oid);
588+
auto table = database->GetTableWithOid(table_oid);
589+
590+
oid_t layout_oid = table->GetNextLayoutOid();
591+
// Ensure that the new layout
592+
PELOTON_ASSERT(layout_oid < INVALID_OID);
593+
auto new_layout = std::shared_ptr<const storage::Layout>(
594+
new const storage::Layout(column_map, layout_oid));
595+
auto layout_catalog = catalog::LayoutCatalog::GetInstance();
596+
bool result = layout_catalog->InsertLayout(table_oid, new_layout,
597+
pool_.get(), txn);
598+
if (!result) {
599+
LOG_DEBUG("Failed to create a new layout for table %u", table_oid);
600+
return nullptr;
601+
}
602+
return new_layout;
603+
}
604+
605+
/*
606+
* @brief create a new layout for a table and make it the deafult if
607+
* if the creating is successsful.
608+
* @param database_oid database to which the table belongs to
609+
* @param table_oid table to which the layout has to be added
610+
* @param column_map column_map of the new layout to be created
611+
* @param txn TransactionContext
612+
* @return shared_ptr shared_ptr to the newly created layout in case of
613+
* success. nullptr in case of failure.
614+
*/
615+
std::shared_ptr<const storage::Layout>
616+
Catalog::CreateDefaultLayout(oid_t database_oid, oid_t table_oid,
617+
const column_map_type &column_map,
618+
concurrency::TransactionContext *txn) {
619+
auto new_layout = CreateLayout(database_oid, table_oid, column_map, txn);
620+
// If the layout creation was successful, set it as the default
621+
if (new_layout != nullptr) {
622+
auto storage_manager = storage::StorageManager::GetInstance();
623+
auto database = storage_manager->GetDatabaseWithOid(database_oid);
624+
auto table = database->GetTableWithOid(table_oid);
625+
table->SetDefaultLayout(new_layout);
626+
}
627+
return new_layout;
628+
}
629+
560630
//===----------------------------------------------------------------------===//
561631
// DROP FUNCTIONS
562632
//===----------------------------------------------------------------------===//
@@ -764,6 +834,40 @@ ResultType Catalog::DropIndex(oid_t database_oid, oid_t index_oid,
764834
return ResultType::SUCCESS;
765835
}
766836

837+
838+
/*@brief Drop layout
839+
* tile_groups
840+
* @param database_oid the database to which the table belongs
841+
* @param table_oid the table to which the layout belongs
842+
* @param layout_oid the layout to be dropped
843+
* @param txn TransactionContext
844+
* @return TransactionContext ResultType(SUCCESS or FAILURE)
845+
*/
846+
ResultType Catalog::DropLayout(oid_t database_oid, oid_t table_oid,
847+
oid_t layout_oid,
848+
concurrency::TransactionContext *txn) {
849+
// Check if the default_layout of the table is the same.
850+
// If true reset it to a row store.
851+
auto storage_manager = storage::StorageManager::GetInstance();
852+
auto database = storage_manager->GetDatabaseWithOid(database_oid);
853+
auto table = database->GetTableWithOid(table_oid);
854+
auto default_layout = table->GetDefaultLayout();
855+
856+
if (default_layout.GetOid() == layout_oid) {
857+
table->ResetDefaultLayout();
858+
}
859+
860+
auto layout_catalog = LayoutCatalog::GetInstance();
861+
if (!layout_catalog->DeleteLayout(table_oid, layout_oid, txn)) {
862+
auto layout = table->GetDefaultLayout();
863+
LOG_DEBUG("Layout delete failed. Default layout id: %u",
864+
layout.GetOid());
865+
return ResultType::FAILURE;
866+
}
867+
868+
return ResultType::SUCCESS;
869+
}
870+
767871
//===--------------------------------------------------------------------===//
768872
// GET WITH NAME - CHECK FROM CATALOG TABLES, USING TRANSACTION
769873
//===--------------------------------------------------------------------===//

src/catalog/layout_catalog.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ bool LayoutCatalog::InsertLayout(oid_t table_oid,
103103
new storage::Tuple(catalog_table_->GetSchema(), true));
104104

105105
auto val0 = type::ValueFactory::GetIntegerValue(table_oid);
106-
auto val1 = type::ValueFactory::GetIntegerValue(layout->GetLayoutId());
106+
auto val1 = type::ValueFactory::GetIntegerValue(layout->GetOid());
107107
auto val2 = type::ValueFactory::GetIntegerValue(layout->GetColumnCount());
108108
auto val3 = type::ValueFactory::GetVarcharValue(layout->SerializeColumnMap(),
109109
nullptr);

src/catalog/table_catalog.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ bool TableCatalogObject::InsertLayout(std::shared_ptr<const storage::Layout> lay
342342
return false;
343343
}
344344

345-
oid_t layout_id = layout->GetLayoutId();
345+
oid_t layout_id = layout->GetOid();
346346
// layout is already present in the cache.
347347
if (layout_objects_.find(layout_id) != layout_objects_.end()) {
348348
LOG_DEBUG("Layout %u already exists in cache!", layout_id);

src/include/catalog/catalog.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class Index;
3838
namespace storage {
3939
class Database;
4040
class DataTable;
41+
class Layout;
4142
class TableFactory;
4243
class Tuple;
4344
} // namespace storage
@@ -119,6 +120,18 @@ class Catalog {
119120
concurrency::TransactionContext *txn,
120121
bool is_catalog = false);
121122

123+
// Create a new layout
124+
std::shared_ptr<const storage::Layout>
125+
CreateLayout(oid_t database_oid, oid_t table_oid,
126+
const column_map_type &column_map,
127+
concurrency::TransactionContext *txn);
128+
129+
// Create a new layout and set it as the default for the table
130+
std::shared_ptr<const storage::Layout>
131+
CreateDefaultLayout(oid_t database_oid, oid_t table_oid,
132+
const column_map_type &column_map,
133+
concurrency::TransactionContext *txn);
134+
122135
//===--------------------------------------------------------------------===//
123136
// DROP FUNCTIONS
124137
//===--------------------------------------------------------------------===//
@@ -146,6 +159,10 @@ class Catalog {
146159
// Drop an index, using its index_oid
147160
ResultType DropIndex(oid_t database_oid, oid_t index_oid,
148161
concurrency::TransactionContext *txn);
162+
163+
// Delete a layout using its database_oid, table_oid and layout_oid
164+
ResultType DropLayout(oid_t database_oid, oid_t table_oid, oid_t layout_oid,
165+
concurrency::TransactionContext *txn);
149166
//===--------------------------------------------------------------------===//
150167
// GET WITH NAME - CHECK FROM CATALOG TABLES, USING TRANSACTION
151168
//===--------------------------------------------------------------------===//

src/include/catalog/layout_catalog.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,15 @@ class LayoutCatalog : public AbstractCatalog {
6161
concurrency::TransactionContext *txn);
6262

6363
bool DeleteLayouts(oid_t table_oid, concurrency::TransactionContext *txn);
64+
65+
private:
66+
6467
//===--------------------------------------------------------------------===//
6568
// Read Related API
6669
//===--------------------------------------------------------------------===//
6770
const std::unordered_map<oid_t, std::shared_ptr<const storage::Layout>>
6871
GetLayouts(oid_t table_oid, concurrency::TransactionContext *txn);
6972

70-
private:
7173

7274
LayoutCatalog(storage::Database *pg_catalog, type::AbstractPool *pool,
7375
concurrency::TransactionContext *txn);

src/include/storage/data_table.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class Sample;
4040

4141
namespace catalog {
4242
class ForeignKey;
43+
class Catalog;
4344
} // namespace catalog
4445

4546
namespace index {
@@ -77,6 +78,7 @@ class DataTable : public AbstractTable {
7778
friend class TileGroup;
7879
friend class TileGroupFactory;
7980
friend class TableFactory;
81+
friend class catalog::Catalog;
8082
friend class logging::LogManager;
8183

8284
DataTable() = delete;
@@ -190,7 +192,6 @@ class DataTable : public AbstractTable {
190192
const std::vector<std::set<oid_t>> &GetIndexColumns() const {
191193
return indexes_columns_;
192194
}
193-
194195
//===--------------------------------------------------------------------===//
195196
// FOREIGN KEYS
196197
//===--------------------------------------------------------------------===//
@@ -248,10 +249,14 @@ class DataTable : public AbstractTable {
248249

249250
void ClearLayoutSamples();
250251

251-
bool SetDefaultLayout(const column_map_type &column_map,
252-
type::AbstractPool *pool,
253-
concurrency::TransactionContext *txn);
252+
inline void SetDefaultLayout(std::shared_ptr<const Layout> new_layout) {
253+
default_layout_ = new_layout;
254+
}
254255

256+
inline void ResetDefaultLayout() {
257+
default_layout_ = std::shared_ptr<const Layout>(
258+
new const Layout(schema->GetColumnCount()));
259+
}
255260
const Layout& GetDefaultLayout() const;
256261

257262
//===--------------------------------------------------------------------===//

src/include/storage/layout.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class Layout : public Printable {
4343
// Check whether the Layout is ColumnStore.
4444
inline bool IsColumnStore() const { return (layout_type_ == LayoutType::COLUMN); }
4545

46-
oid_t GetLayoutId() const { return layout_id_; }
46+
oid_t GetOid() const { return layout_oid_; }
4747

4848
// Sets the tile id and column id w.r.t that tile corresponding to
4949
// the specified tile group column id.
@@ -78,7 +78,7 @@ class Layout : public Printable {
7878
private:
7979

8080
// Layout Id of the tile
81-
oid_t layout_id_;
81+
oid_t layout_oid_;
8282

8383
// Number of columns in the layout
8484
oid_t num_columns_;

src/include/tuning/layout_tuner.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ namespace storage {
2727
class DataTable;
2828
}
2929

30-
namespace type {
31-
class AbstractPool;
32-
}
33-
3430
namespace tuning {
3531

3632
//===--------------------------------------------------------------------===//
@@ -138,9 +134,6 @@ class LayoutTuner {
138134
/** Desired layout tile count */
139135
oid_t tile_count = 2;
140136

141-
/** Pool to add new layouts in the pg_layout table */
142-
std::unique_ptr<type::AbstractPool> pool_;
143-
144137
};
145138

146139
} // namespace indextuner

src/storage/data_table.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,20 +1367,6 @@ void DataTable::ClearIndexSamples() {
13671367
}
13681368
}
13691369

1370-
bool DataTable::SetDefaultLayout(const column_map_type &column_map,
1371-
type::AbstractPool *pool,
1372-
concurrency::TransactionContext *txn) {
1373-
oid_t layout_id = GetNextLayoutOid();
1374-
auto new_layout = std::shared_ptr<const Layout>(
1375-
new const Layout(column_map, layout_id));
1376-
auto layout_catalog = catalog::LayoutCatalog::GetInstance();
1377-
bool result = layout_catalog->InsertLayout(table_oid, new_layout, pool, txn);
1378-
if (result) {
1379-
default_layout_ = new_layout;
1380-
}
1381-
return result;
1382-
}
1383-
13841370
const Layout& DataTable::GetDefaultLayout() const {
13851371
return *default_layout_;
13861372
}

src/storage/layout.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,16 @@ Layout::Layout(const column_map_type& column_map)
4646
// If a table has one column, it would be LayoutType::Row
4747
if (row_layout) {
4848
layout_type_ = LayoutType::ROW;
49-
layout_id_ = ROW_STORE_OID;
49+
layout_oid_ = ROW_STORE_OID;
5050
} else if (column_layout) {
5151
layout_type_ = LayoutType::COLUMN;
52-
layout_id_ = COLUMN_STORE_OID;
52+
layout_oid_ = COLUMN_STORE_OID;
5353
} else {
54-
// layout_id_ is set to INVALID_OID, indicating that this
54+
// layout_oid_ is set to INVALID_OID, indicating that this
5555
// layout is not stored in the catalog and thus not persistent.
5656
// To be used only in TempTable or Tests.
5757
layout_type_ = LayoutType::HYBRID;
58-
layout_id_ = INVALID_OID;
58+
layout_oid_ = INVALID_OID;
5959
}
6060

6161
if (layout_type_ != LayoutType::HYBRID) {
@@ -64,15 +64,15 @@ Layout::Layout(const column_map_type& column_map)
6464
}
6565
}
6666

67-
// Constructor for Layout class with predefined layout_id
67+
// Constructor for Layout class with predefined layout_oid
6868
Layout::Layout(const column_map_type &column_map, oid_t layout_id)
69-
: layout_id_(layout_id),
69+
: layout_oid_(layout_id),
7070
num_columns_(column_map.size()),
7171
column_layout_(column_map) {
7272

73-
if (layout_id_ == ROW_STORE_OID) {
73+
if (layout_oid_ == ROW_STORE_OID) {
7474
layout_type_ = LayoutType::ROW;
75-
} else if (layout_id == COLUMN_STORE_OID) {
75+
} else if (layout_oid_ == COLUMN_STORE_OID) {
7676
layout_type_ = LayoutType::COLUMN;
7777
} else {
7878
layout_type_ = LayoutType::HYBRID;
@@ -124,7 +124,7 @@ double Layout::GetLayoutDifference(const storage::Layout &other) const {
124124
// Check the schema before invoking this function.
125125
PELOTON_ASSERT(this->num_columns_ == other.num_columns_);
126126

127-
if ((this->layout_id_ != other.layout_id_)) {
127+
if ((this->layout_oid_ != other.layout_oid_)) {
128128

129129
for (oid_t col_itr = 0; col_itr < num_columns_; col_itr++) {
130130

@@ -289,7 +289,7 @@ std::string Layout::GetColumnMapInfo() const {
289289
const std::string Layout::GetInfo() const {
290290
std::ostringstream os;
291291

292-
os << peloton::GETINFO_DOUBLE_STAR << " Layout[#" << layout_id_ << "] "
292+
os << peloton::GETINFO_DOUBLE_STAR << " Layout[#" << layout_oid_ << "] "
293293
<< peloton::GETINFO_DOUBLE_STAR << std::endl;
294294
os << "Number of columns[" << num_columns_ << "] " << std::endl;
295295
os << "LayoutType[" << LayoutTypeToString(layout_type_) << std::endl;
@@ -307,7 +307,7 @@ bool operator==(const Layout& lhs, const Layout& rhs) {
307307
}
308308

309309
// Check the equality of layout_oid_
310-
if (lhs.GetLayoutId() != rhs.GetLayoutId()) {
310+
if (lhs.GetOid() != rhs.GetOid()) {
311311
return false;
312312
}
313313
// Check the equality of column_count_

0 commit comments

Comments
 (0)