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

Commit 48156e3

Browse files
Access LayoutCatalog via the global Catalog object
1 parent 4ed304d commit 48156e3

File tree

11 files changed

+155
-48
lines changed

11 files changed

+155
-48
lines changed

src/catalog/catalog.cpp

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,76 @@ ResultType Catalog::CreateIndex(
463463
return ResultType::SUCCESS;
464464
}
465465

466+
/*
467+
*
468+
// Create a new layout
469+
ResultType CreateLayout(oid_t database_oid, oid_t table_oid,
470+
const column_map_type &column_map,
471+
concurrency::TransactionContext *txn);
472+
// Create a new layout and set it as the default for the table
473+
ResultType CreateDefaultLayout(oid_t database_oid, oid_t table_oid,
474+
const column_map_type &column_map,
475+
concurrency::TransactionContext *txn);
476+
*/
477+
478+
/*
479+
* @brief create a new layout for a table
480+
* @param database_oid database to which the table belongs to
481+
* @param table_oid table to which the layout has to be added
482+
* @param column_map column_map of the new layout to be created
483+
* @param txn TransactionContext
484+
* @return shared_ptr shared_ptr to the newly created layout in case of
485+
* success. nullptr in case of failure.
486+
*/
487+
std::shared_ptr<const storage::Layout>
488+
Catalog::CreateLayout(oid_t database_oid, oid_t table_oid,
489+
const column_map_type &column_map,
490+
concurrency::TransactionContext *txn) {
491+
492+
auto storage_manager = storage::StorageManager::GetInstance();
493+
auto database = storage_manager->GetDatabaseWithOid(database_oid);
494+
auto table = database->GetTableWithOid(table_oid);
495+
496+
oid_t layout_oid = table->GetNextLayoutOid();
497+
// Ensure that the new layout
498+
PELOTON_ASSERT(layout_oid < INVALID_OID);
499+
auto new_layout = std::shared_ptr<const storage::Layout>(
500+
new const storage::Layout(column_map, layout_oid));
501+
auto layout_catalog = catalog::LayoutCatalog::GetInstance();
502+
bool result = layout_catalog->InsertLayout(table_oid, new_layout,
503+
pool_.get(), txn);
504+
if (!result) {
505+
LOG_DEBUG("Failed to create a new layout for table %u", table_oid);
506+
return nullptr;
507+
}
508+
return new_layout;
509+
}
510+
511+
/*
512+
* @brief create a new layout for a table and make it the deafult if
513+
* if the creating is successsful.
514+
* @param database_oid database to which the table belongs to
515+
* @param table_oid table to which the layout has to be added
516+
* @param column_map column_map of the new layout to be created
517+
* @param txn TransactionContext
518+
* @return shared_ptr shared_ptr to the newly created layout in case of
519+
* success. nullptr in case of failure.
520+
*/
521+
std::shared_ptr<const storage::Layout>
522+
Catalog::CreateDefaultLayout(oid_t database_oid, oid_t table_oid,
523+
const column_map_type &column_map,
524+
concurrency::TransactionContext *txn) {
525+
auto new_layout = CreateLayout(database_oid, table_oid, column_map, txn);
526+
// If the layout creation was successful, set it as the default
527+
if (new_layout != nullptr) {
528+
auto storage_manager = storage::StorageManager::GetInstance();
529+
auto database = storage_manager->GetDatabaseWithOid(database_oid);
530+
auto table = database->GetTableWithOid(table_oid);
531+
table->SetDefaultLayout(new_layout);
532+
}
533+
return new_layout;
534+
}
535+
466536
//===----------------------------------------------------------------------===//
467537
// DROP FUNCTIONS
468538
//===----------------------------------------------------------------------===//
@@ -634,6 +704,39 @@ ResultType Catalog::DropIndex(const std::string &index_name,
634704
return result;
635705
}
636706

707+
/*@brief Drop layout
708+
* tile_groups
709+
* @param database_oid the database to which the table belongs
710+
* @param table_oid the table to which the layout belongs
711+
* @param layout_oid the layout to be dropped
712+
* @param txn TransactionContext
713+
* @return TransactionContext ResultType(SUCCESS or FAILURE)
714+
*/
715+
ResultType Catalog::DropLayout(oid_t database_oid, oid_t table_oid,
716+
oid_t layout_oid,
717+
concurrency::TransactionContext *txn) {
718+
// Check if the default_layout of the table is the same.
719+
// If true reset it to a row store.
720+
auto storage_manager = storage::StorageManager::GetInstance();
721+
auto database = storage_manager->GetDatabaseWithOid(database_oid);
722+
auto table = database->GetTableWithOid(table_oid);
723+
auto default_layout = table->GetDefaultLayout();
724+
725+
if (default_layout.GetOid() == layout_oid) {
726+
table->ResetDefaultLayout();
727+
}
728+
729+
auto layout_catalog = LayoutCatalog::GetInstance();
730+
if (!layout_catalog->DeleteLayout(table_oid, layout_oid, txn)) {
731+
auto layout = table->GetDefaultLayout();
732+
LOG_DEBUG("Layout delete failed. Default layout id: %u",
733+
layout.GetOid());
734+
return ResultType::FAILURE;
735+
}
736+
737+
return ResultType::SUCCESS;
738+
}
739+
637740
//===--------------------------------------------------------------------===//
638741
// GET WITH NAME - CHECK FROM CATALOG TABLES, USING TRANSACTION
639742
//===--------------------------------------------------------------------===//

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
@@ -315,7 +315,7 @@ bool TableCatalogObject::InsertLayout(std::shared_ptr<const storage::Layout> lay
315315
return false;
316316
}
317317

318-
oid_t layout_id = layout->GetLayoutId();
318+
oid_t layout_id = layout->GetOid();
319319
// layout is already present in the cache.
320320
if (layout_objects_.find(layout_id) != layout_objects_.end()) {
321321
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
@@ -37,6 +37,7 @@ class Index;
3737
namespace storage {
3838
class Database;
3939
class DataTable;
40+
class Layout;
4041
class TableFactory;
4142
class Tuple;
4243
} // namespace storage
@@ -115,6 +116,18 @@ class Catalog {
115116
concurrency::TransactionContext *txn,
116117
bool is_catalog = false);
117118

119+
// Create a new layout
120+
std::shared_ptr<const storage::Layout>
121+
CreateLayout(oid_t database_oid, oid_t table_oid,
122+
const column_map_type &column_map,
123+
concurrency::TransactionContext *txn);
124+
125+
// Create a new layout and set it as the default for the table
126+
std::shared_ptr<const storage::Layout>
127+
CreateDefaultLayout(oid_t database_oid, oid_t table_oid,
128+
const column_map_type &column_map,
129+
concurrency::TransactionContext *txn);
130+
118131
//===--------------------------------------------------------------------===//
119132
// DROP FUNCTIONS
120133
//===--------------------------------------------------------------------===//
@@ -139,6 +152,10 @@ class Catalog {
139152
// Drop an index, using its index name
140153
ResultType DropIndex(const std::string &index_name,
141154
concurrency::TransactionContext *txn);
155+
156+
// Delete a layout using its database_oid, table_oid and layout_oid
157+
ResultType DropLayout(oid_t database_oid, oid_t table_oid, oid_t layout_oid,
158+
concurrency::TransactionContext *txn);
142159
//===--------------------------------------------------------------------===//
143160
// GET WITH NAME - CHECK FROM CATALOG TABLES, USING TRANSACTION
144161
//===--------------------------------------------------------------------===//

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
@@ -1368,20 +1368,6 @@ void DataTable::ClearIndexSamples() {
13681368
}
13691369
}
13701370

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

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)