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

Commit a0c67b0

Browse files
committed
Support to insert all layout info into catalog
1 parent c92b056 commit a0c67b0

File tree

9 files changed

+115
-42
lines changed

9 files changed

+115
-42
lines changed

src/catalog/catalog.cpp

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,15 @@ ResultType Catalog::CreateTable(const std::string &database_name,
389389
}
390390
CreatePrimaryIndex(database_object->GetDatabaseOid(), table_oid, schema_name,
391391
txn);
392+
393+
// Create layout as default layout
394+
auto pg_layout =
395+
catalog_map_[database_object->GetDatabaseOid()]->GetLayoutCatalog();
396+
auto default_layout = table->GetDefaultLayout();
397+
if (!pg_layout->InsertLayout(table_oid, default_layout, pool_.get(), txn))
398+
throw CatalogException("Failed to create a new layout for table "
399+
+ table_oid);
400+
392401
return ResultType::SUCCESS;
393402
}
394403

@@ -579,15 +588,15 @@ std::shared_ptr<const storage::Layout> Catalog::CreateLayout(
579588
// Ensure that the new layout
580589
PELOTON_ASSERT(layout_oid < INVALID_OID);
581590
auto new_layout = std::shared_ptr<const storage::Layout>(
582-
new const storage::Layout(column_map, layout_oid));
591+
new const storage::Layout(column_map, column_map.size(), layout_oid));
583592

584593
// Add the layout the pg_layout table
585594
auto pg_layout = catalog_map_[database_oid]->GetLayoutCatalog();
586-
bool result =
587-
pg_layout->InsertLayout(table_oid, new_layout, pool_.get(), txn);
588-
if (!result) {
589-
LOG_ERROR("Failed to create a new layout for table %u", table_oid);
590-
return nullptr;
595+
if (pg_layout->GetLayoutWithOid(table_oid, new_layout->GetOid(), txn)
596+
== nullptr &&
597+
!pg_layout->InsertLayout(table_oid, new_layout, pool_.get(), txn)) {
598+
LOG_ERROR("Failed to create a new layout for table %u", table_oid);
599+
return nullptr;
591600
}
592601
return new_layout;
593602
}
@@ -828,17 +837,25 @@ ResultType Catalog::DropLayout(oid_t database_oid, oid_t table_oid,
828837
auto table = database->GetTableWithOid(table_oid);
829838
auto default_layout = table->GetDefaultLayout();
830839

831-
if (default_layout.GetOid() == layout_oid) {
832-
table->ResetDefaultLayout();
833-
}
834-
835840
auto pg_layout = catalog_map_[database_oid]->GetLayoutCatalog();
836841
if (!pg_layout->DeleteLayout(table_oid, layout_oid, txn)) {
837842
auto layout = table->GetDefaultLayout();
838-
LOG_DEBUG("Layout delete failed. Default layout id: %u", layout.GetOid());
843+
LOG_DEBUG("Layout delete failed. Default layout id: %u", layout->GetOid());
839844
return ResultType::FAILURE;
840845
}
841846

847+
if (default_layout->GetOid() == layout_oid) {
848+
table->ResetDefaultLayout();
849+
auto new_default_layout = table->GetDefaultLayout();
850+
if (pg_layout->GetLayoutWithOid(table_oid, new_default_layout->GetOid(),
851+
txn) == nullptr &&
852+
!pg_layout->InsertLayout(table_oid, new_default_layout,
853+
pool_.get(), txn)) {
854+
LOG_DEBUG("Failed to create a new layout for table %d", table_oid);
855+
return ResultType::FAILURE;
856+
}
857+
}
858+
842859
return ResultType::SUCCESS;
843860
}
844861

src/catalog/layout_catalog.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,15 @@ LayoutCatalog::GetLayouts(oid_t table_oid,
197197
std::string column_map_str =
198198
tile->GetValue(tuple_id, LayoutCatalog::ColumnId::COLUMN_MAP)
199199
.ToString();
200-
auto column_map =
201-
storage::Layout::DeserializeColumnMap(num_columns, column_map_str);
200+
201+
column_map_type column_map;
202+
if (column_map_str.length() != 0) {
203+
column_map =
204+
storage::Layout::DeserializeColumnMap(num_columns, column_map_str);
205+
}
202206
auto layout_object =
203-
std::make_shared<const storage::Layout>(column_map, layout_oid);
207+
std::make_shared<const storage::Layout>(column_map, num_columns,
208+
layout_oid);
204209
table_object->InsertLayout(layout_object);
205210
}
206211
}
@@ -227,4 +232,4 @@ std::shared_ptr<const storage::Layout> LayoutCatalog::GetLayoutWithOid(
227232
}
228233

229234
} // namespace catalog
230-
} // namespace peloton
235+
} // namespace peloton

src/include/storage/data_table.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,10 @@ class DataTable : public AbstractTable {
265265
default_layout_ = std::shared_ptr<const Layout>(
266266
new const Layout(schema->GetColumnCount(), type));
267267
}
268-
const Layout &GetDefaultLayout() const;
268+
269+
const std::shared_ptr<const Layout> GetDefaultLayout() const {
270+
return default_layout_;
271+
}
269272

270273
//===--------------------------------------------------------------------===//
271274
// INDEX TUNER

src/include/storage/layout.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,13 @@ class Layout : public Printable {
6060
Layout(const column_map_type &column_map);
6161

6262
/**
63-
* @brief Constructor for arbitrary column_maps.
63+
* @brief Constructor for arbitrary column_maps with layout oid.
6464
* @param column_map Column map of the layout to be constructed.
65+
* @param num_columns Number of column.
6566
* @param layout_oid Per-table unique OID. Generted by DataTable.
6667
*/
67-
Layout(const column_map_type &column_map, oid_t layout_oid);
68+
Layout(const column_map_type &column_map, const oid_t num_columns,
69+
const oid_t layout_oid);
6870

6971
/** @brief Check whether this layout is a row store. */
7072
bool IsRowStore() const { return (layout_type_ == LayoutType::ROW); }

src/storage/data_table.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,8 +1366,6 @@ void DataTable::ClearIndexSamples() {
13661366
}
13671367
}
13681368

1369-
const Layout &DataTable::GetDefaultLayout() const { return *default_layout_; }
1370-
13711369
void DataTable::AddTrigger(trigger::Trigger new_trigger) {
13721370
trigger_list_->AddTrigger(new_trigger);
13731371
}

src/storage/layout.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,16 @@ namespace storage {
2424
// Constructor for the layout class with column_count
2525
// The default layout is always a ROW_STORE
2626
Layout::Layout(const oid_t num_columns, LayoutType layout_type)
27-
: num_columns_(num_columns), layout_type_(layout_type) {}
27+
: num_columns_(num_columns), layout_type_(layout_type) {
28+
// Assign the oid
29+
if (layout_type == LayoutType::ROW) {
30+
layout_oid_ = ROW_STORE_OID;
31+
} else if (layout_type == LayoutType::COLUMN) {
32+
layout_oid_ = COLUMN_STORE_OID;
33+
} else {
34+
layout_oid_ = INVALID_OID;
35+
}
36+
}
2837

2938
// Constructor for the Layout class with column_map
3039
Layout::Layout(const column_map_type &column_map)
@@ -63,9 +72,10 @@ Layout::Layout(const column_map_type &column_map)
6372
}
6473

6574
// Constructor for Layout class with predefined layout_oid
66-
Layout::Layout(const column_map_type &column_map, oid_t layout_id)
75+
Layout::Layout(const column_map_type &column_map, const oid_t num_columns,
76+
const oid_t layout_id)
6777
: layout_oid_(layout_id),
68-
num_columns_(column_map.size()),
78+
num_columns_(num_columns),
6979
column_layout_(column_map) {
7080
if (layout_oid_ == ROW_STORE_OID) {
7181
layout_type_ = LayoutType::ROW;
@@ -349,4 +359,4 @@ bool operator==(const Layout &lhs, const Layout &rhs) {
349359
bool operator!=(const Layout &lhs, const Layout &rhs) { return !(lhs == rhs); }
350360

351361
} // namespace storage
352-
} // namespace peloton
362+
} // namespace peloton

src/tuning/layout_tuner.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ bool LayoutTuner::UpdateDefaultPartition(storage::DataTable *table) {
127127
txn_manager.CommitTransaction(txn);
128128

129129
UNUSED_ATTRIBUTE auto layout = table->GetDefaultLayout();
130-
LOG_TRACE("Updated Layout: %s", layout.GetInfo().c_str());
130+
LOG_TRACE("Updated Layout: %s", layout.GetInfo()->c_str());
131131
return true;
132132
}
133133

test/catalog/catalog_test.cpp

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ TEST_F(CatalogTests, LayoutCatalogTest) {
343343
auto db_name = "temp_db";
344344
auto table_name = "temp_table";
345345
auto catalog = catalog::Catalog::GetInstance();
346+
346347
// Create database.
347348
auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
348349
auto txn = txn_manager.BeginTransaction();
@@ -376,10 +377,23 @@ TEST_F(CatalogTests, LayoutCatalogTest) {
376377
auto table_oid = table_object->GetTableOid();
377378
auto table =
378379
catalog->GetTableWithName(db_name, DEFAULT_SCHEMA_NAME, table_name, txn);
380+
auto pg_layout = catalog->GetSystemCatalogs(database_oid)->GetLayoutCatalog();
379381
txn_manager.CommitTransaction(txn);
380382

381-
// Change default layout.
383+
// Check the first default layout
384+
auto first_default_layout = table->GetDefaultLayout();
385+
EXPECT_EQ(0, first_default_layout->GetOid());
386+
EXPECT_TRUE(first_default_layout->IsRowStore());
387+
388+
// Check the first default layout in pg_layout
389+
txn = txn_manager.BeginTransaction();
390+
auto first_layout_oid = first_default_layout->GetOid();
391+
EXPECT_EQ(
392+
*(first_default_layout.get()),
393+
*(pg_layout->GetLayoutWithOid(table_oid, first_layout_oid, txn).get()));
394+
txn_manager.CommitTransaction(txn);
382395

396+
// Change default layout.
383397
std::map<oid_t, std::pair<oid_t, oid_t>> default_map;
384398
default_map[0] = std::make_pair(0, 0);
385399
default_map[1] = std::make_pair(0, 1);
@@ -389,8 +403,21 @@ TEST_F(CatalogTests, LayoutCatalogTest) {
389403
txn = txn_manager.BeginTransaction();
390404
auto default_layout =
391405
catalog->CreateDefaultLayout(database_oid, table_oid, default_map, txn);
392-
txn_manager.CommitTransaction(txn);
393406
EXPECT_NE(nullptr, default_layout);
407+
txn_manager.CommitTransaction(txn);
408+
409+
// Check the changed default layout
410+
auto default_layout_oid = default_layout->GetOid();
411+
EXPECT_EQ(default_layout_oid, table->GetDefaultLayout()->GetOid());
412+
EXPECT_FALSE(default_layout->IsColumnStore());
413+
EXPECT_FALSE(default_layout->IsRowStore());
414+
415+
// Check the changed default layout in pg_layout
416+
txn = txn_manager.BeginTransaction();
417+
EXPECT_EQ(
418+
*(default_layout.get()),
419+
*(pg_layout->GetLayoutWithOid(table_oid, default_layout_oid, txn).get()));
420+
txn_manager.CommitTransaction(txn);
394421

395422
// Create additional layout.
396423
std::map<oid_t, std::pair<oid_t, oid_t>> non_default_map;
@@ -402,30 +429,41 @@ TEST_F(CatalogTests, LayoutCatalogTest) {
402429
txn = txn_manager.BeginTransaction();
403430
auto other_layout =
404431
catalog->CreateLayout(database_oid, table_oid, non_default_map, txn);
432+
EXPECT_NE(nullptr, other_layout);
433+
txn_manager.CommitTransaction(txn);
434+
435+
// Check the created layout
436+
EXPECT_FALSE(other_layout->IsColumnStore());
437+
EXPECT_FALSE(other_layout->IsRowStore());
438+
439+
// Check the created layout in pg_layout
440+
txn = txn_manager.BeginTransaction();
441+
auto other_layout_oid = other_layout->GetOid();
442+
EXPECT_EQ(
443+
*(other_layout.get()),
444+
*(pg_layout->GetLayoutWithOid(table_oid, other_layout_oid, txn).get()));
405445
txn_manager.CommitTransaction(txn);
406446

407447
// Check that the default layout is still the same.
408-
EXPECT_NE(*other_layout.get(), table->GetDefaultLayout());
448+
EXPECT_NE(other_layout, table->GetDefaultLayout());
409449

410450
// Drop the default layout.
411-
auto default_layout_oid = default_layout->GetOid();
412451
txn = txn_manager.BeginTransaction();
413452
EXPECT_EQ(ResultType::SUCCESS, catalog->DropLayout(database_oid, table_oid,
414453
default_layout_oid, txn));
415454
txn_manager.CommitTransaction(txn);
416455

417456
// Check that default layout is reset and set to row_store.
418-
EXPECT_NE(*default_layout.get(), table->GetDefaultLayout());
419-
EXPECT_TRUE(table->GetDefaultLayout().IsRowStore());
457+
EXPECT_NE(default_layout, table->GetDefaultLayout());
458+
EXPECT_TRUE(table->GetDefaultLayout()->IsRowStore());
459+
EXPECT_EQ(0, table->GetDefaultLayout()->GetOid());
420460

421461
// Query pg_layout to ensure that the entry is dropped
422462
txn = txn_manager.BeginTransaction();
423-
auto pg_layout = catalog->GetSystemCatalogs(database_oid)->GetLayoutCatalog();
424463
EXPECT_EQ(nullptr,
425464
pg_layout->GetLayoutWithOid(table_oid, default_layout_oid, txn));
426465

427466
// The additional layout must be present in pg_layout
428-
auto other_layout_oid = other_layout->GetOid();
429467
EXPECT_EQ(
430468
*(other_layout.get()),
431469
*(pg_layout->GetLayoutWithOid(table_oid, other_layout_oid, txn).get()));

test/tuning/layout_tuner_test.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ TEST_F(LayoutTunerTests, BasicTest) {
6363

6464
// Check old default tile group layout
6565
auto old_default_layout = data_table->GetDefaultLayout();
66-
LOG_INFO("Layout: %s", old_default_layout.GetColumnMapInfo().c_str());
66+
LOG_INFO("Layout: %s", old_default_layout->GetColumnMapInfo().c_str());
6767

6868
// Start layout tuner
6969
layout_tuner.Start();
@@ -109,26 +109,26 @@ TEST_F(LayoutTunerTests, BasicTest) {
109109

110110
// Check new default tile group layout
111111
auto new_default_layout = data_table->GetDefaultLayout();
112-
LOG_INFO("Layout: %s", new_default_layout.GetColumnMapInfo().c_str());
112+
LOG_INFO("Layout: %s", new_default_layout->GetColumnMapInfo().c_str());
113113

114114
// Ensure that the layout has been changed
115-
EXPECT_NE(new_default_layout, old_default_layout);
115+
EXPECT_NE(*new_default_layout, *old_default_layout);
116116

117117
// Check the new default table layout
118-
column_count = new_default_layout.GetColumnCount();
118+
column_count = new_default_layout->GetColumnCount();
119119
EXPECT_EQ(column_count, 4);
120120

121121
// Check the tile corresponding to each column.
122-
EXPECT_EQ(new_default_layout.GetTileIdFromColumnId(0), 0);
123-
EXPECT_EQ(new_default_layout.GetTileIdFromColumnId(1), 0);
124-
EXPECT_EQ(new_default_layout.GetTileIdFromColumnId(2), 0);
125-
EXPECT_EQ(new_default_layout.GetTileIdFromColumnId(3), 1);
122+
EXPECT_EQ(new_default_layout->GetTileIdFromColumnId(0), 0);
123+
EXPECT_EQ(new_default_layout->GetTileIdFromColumnId(1), 0);
124+
EXPECT_EQ(new_default_layout->GetTileIdFromColumnId(2), 0);
125+
EXPECT_EQ(new_default_layout->GetTileIdFromColumnId(3), 1);
126126

127127
// Check the per tile stats of the new layout
128128
// The layout must contain 2 tiles with the following stats
129129
// 0 -> 3
130130
// 1 -> 1
131-
auto layout_stats = new_default_layout.GetLayoutStats();
131+
auto layout_stats = new_default_layout->GetLayoutStats();
132132
EXPECT_EQ(layout_stats[0], 3);
133133
EXPECT_EQ(layout_stats[1], 1);
134134

0 commit comments

Comments
 (0)