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

Commit 6d3a72b

Browse files
Added documentation
1 parent 0d668ad commit 6d3a72b

File tree

10 files changed

+140
-72
lines changed

10 files changed

+140
-72
lines changed

src/catalog/catalog.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,8 @@ ResultType Catalog::CreateTable(const std::string &database_name,
353353
bool adapt_table = false;
354354
auto table = storage::TableFactory::GetDataTable(
355355
database_object->GetDatabaseOid(), table_oid, schema.release(),
356-
table_name, tuples_per_tilegroup, own_schema, adapt_table, is_catalog, layout_type);
356+
table_name, tuples_per_tilegroup, own_schema, adapt_table, is_catalog,
357+
layout_type);
357358
database->AddTable(table, is_catalog);
358359
// put data table object into rw_object_set
359360
txn->RecordCreate(database_object->GetDatabaseOid(), table_oid, INVALID_OID);

src/catalog/layout_catalog.cpp

Lines changed: 57 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,9 @@
2222
namespace peloton {
2323
namespace 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+
*/
3228
LayoutCatalog::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. */
4642
LayoutCatalog::~LayoutCatalog() {}
4743

44+
/** @brief Initilailizes the schema for the pg_layout table.
45+
* @return unique_ptr of the schema for pg_layout.
46+
*/
4847
std::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+
*/
8692
bool 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+
*/
129146
bool 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+
*/
145167
const std::unordered_map<oid_t, std::shared_ptr<const storage::Layout>>
146168
LayoutCatalog::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+
*/
199219
std::shared_ptr<const storage::Layout>
200220
LayoutCatalog::GetLayoutWithOid(oid_t table_oid, oid_t layout_oid,
201221
concurrency::TransactionContext *txn) {

src/catalog/table_catalog.cpp

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,9 @@ TableCatalog::TableCatalog(
331331
IndexConstraintType::DEFAULT);
332332
}
333333

334-
/* @brief Insert layout object into the cache.
335-
* @param layout Layout object to be inserted
336-
* @return false if layout already exists in cache
334+
/** @brief Insert layout object into the cache.
335+
* @param layout Layout object to be inserted
336+
* @return false if layout already exists in cache
337337
*/
338338
bool TableCatalogObject::InsertLayout(std::shared_ptr<const storage::Layout> layout) {
339339

@@ -354,33 +354,34 @@ bool TableCatalogObject::InsertLayout(std::shared_ptr<const storage::Layout> lay
354354
return true;
355355
}
356356

357-
/*
358-
* @brief evict all layout objects from cache
359-
*/
357+
/** @brief evict all layout objects from cache. */
360358
void TableCatalogObject::EvictAllLayouts() {
361359
layout_objects_.clear();
362360
valid_layout_objects_ = false;
363361
}
364362

365-
/* @brief Get all layout objects of this table.
366-
* Add it to the cache if necessary.
367-
* @param cached_only If set to true, don't fetch the layout objects.
368-
* @return Map from layout_oid to cached layout object.
363+
/** @brief Get all layout objects of this table.
364+
* Add it to the cache if necessary.
365+
* @param cached_only If set to true, don't fetch the layout objects.
366+
* @return Map from layout_oid to cached layout object.
369367
*/
370368
std::unordered_map<oid_t, std::shared_ptr<const storage::Layout>>
371369
TableCatalogObject::GetLayouts(bool cached_only) {
372370
if (!valid_layout_objects_ && !cached_only) {
373-
// get column catalog objects from pg_column
374-
LayoutCatalog::GetInstance()->GetLayouts(table_oid, txn);
371+
// get layout catalog objects from pg_layout
372+
auto pg_layout = Catalog::GetInstance()
373+
->GetSystemCatalogs(database_oid)
374+
->GetLayoutCatalog();
375+
pg_layout->GetLayouts(table_oid, txn);
375376
valid_column_objects = true;
376377
}
377378
return layout_objects_;
378379
}
379380

380-
/* @brief get the layout object of the given layout_id.
381-
* @param layout_id The id of the layout to be fetched.
382-
* @param cached_only If set to true, don't fetch the layout objects.
383-
* @return Layout object of corresponding to the layout_id if present.
381+
/** @brief Get the layout object of the given layout_id.
382+
* @param layout_id The id of the layout to be fetched.
383+
* @param cached_only If set to true, don't fetch the layout objects.
384+
* @return Layout object of corresponding to the layout_id if present.
384385
*/
385386
std::shared_ptr<const storage::Layout>
386387
TableCatalogObject::GetLayout(oid_t layout_id, bool cached_entry) {
@@ -393,9 +394,9 @@ TableCatalogObject::GetLayout(oid_t layout_id, bool cached_entry) {
393394
return nullptr;
394395
}
395396

396-
/* @brief Evict layout from the cache.
397-
* @param layout_id Id of the layout to be deleted.
398-
* @return true if layout_id is found and evicted; false if not found.
397+
/** @brief Evict layout from the cache.
398+
* @param layout_id Id of the layout to be deleted.
399+
* @return true if layout_id is found and evicted; false if not found.
399400
*/
400401
bool TableCatalogObject::EvictLayout(oid_t layout_id) {
401402
if (!valid_layout_objects_) return false;

src/include/catalog/layout_catalog.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class LayoutCatalog : public AbstractCatalog {
5858
type::AbstractPool *pool,
5959
concurrency::TransactionContext *txn);
6060

61-
bool DeleteLayout(oid_t table_oid, oid_t layout_id,
61+
bool DeleteLayout(oid_t table_oid, oid_t layout_oid,
6262
concurrency::TransactionContext *txn);
6363

6464
bool DeleteLayouts(oid_t table_oid, concurrency::TransactionContext *txn);

src/include/storage/layout.h

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
#include "common/internal_types.h"
1818
#include "common/printable.h"
1919

20+
//@{
21+
/** @brief Pre-defined OIDs for ROW and COLUMN store. */
2022
#define ROW_STORE_OID 0
2123
#define COLUMN_STORE_OID 1
24+
//@}
2225

2326
namespace peloton {
2427

@@ -28,64 +31,105 @@ class Schema;
2831

2932
namespace storage {
3033

34+
/**
35+
* @brief Class to store the physical layout of a TileGroup.
36+
*/
3137
class Layout : public Printable {
3238
public:
3339

40+
/**
41+
* @brief Constructor for predefined layout types.
42+
* @param num_columns Number of columns in the layouts.
43+
* @param layout_type Has to be LayoutType::ROW or LayoutType::COLUMN.
44+
*/
3445
Layout (const oid_t num_columns, LayoutType layout_type = LayoutType::ROW);
3546

47+
/**
48+
* @brief Constructor for arbitrary column_maps.
49+
* If the column_map is of type LayoutType::HYBRID,
50+
* the layout_oid_ is set to INVALID_OID. For all other
51+
* layouts, the pre-defined OIDs are used. Should be used
52+
* only for testing & TempTables, the layout isn't persistent.
53+
* @param column_map Column map of the layout to be constructed
54+
*
55+
*/
3656
Layout(const column_map_type& column_map);
3757

38-
Layout(const column_map_type& column_map, oid_t layout_id);
58+
/**
59+
* @brief Constructor for arbitrary column_maps.
60+
* @param column_map Column map of the layout to be constructed.
61+
* @param layout_oid Per-table unique OID. Generted by DataTable.
62+
*/
63+
Layout(const column_map_type& column_map, oid_t layout_oid);
3964

40-
// Check whether the Layout is a RowStore.
65+
/** @brief Check whether this layout is a row store. */
4166
inline bool IsRowStore() const { return (layout_type_ == LayoutType::ROW); }
4267

43-
// Check whether the Layout is ColumnStore.
68+
/** @brief Check whether this layout is a column store. */
4469
inline bool IsColumnStore() const { return (layout_type_ == LayoutType::COLUMN); }
4570

71+
/** @brief Return the layout_oid_ of this object. */
4672
oid_t GetOid() const { return layout_oid_; }
4773

48-
// Sets the tile id and column id w.r.t that tile corresponding to
49-
// the specified tile group column id.
74+
/** @brief Sets the tile id and column id w.r.t. of the tile corresponding
75+
* to the specified tile group column id.
76+
*/
5077
void LocateTileAndColumn(oid_t column_offset, oid_t &tile_offset,
5178
oid_t &tile_column_offset) const;
5279

80+
/** @brief Returns the layout difference w.r.t. the other Layout.
81+
* @double The delta between the layouts. Used by LayoutTuner class.
82+
*/
5383
double GetLayoutDifference(const storage::Layout &other) const;
5484

85+
/** @brief Returns the tile_id of the given column_id. */
5586
oid_t GetTileIdFromColumnId(oid_t column_id) const;
5687

57-
oid_t GetTileColumnId(oid_t column_id) const;
88+
/** @brief Returns the column offset of the column_id. */
89+
oid_t GetTileColumnOffset(oid_t column_id) const;
5890

91+
/** @brief Returns the number of columns in the layout. */
5992
oid_t GetColumnCount() const { return num_columns_;}
6093

94+
/** @brief Constructs the schema for the given layout. Thid function
95+
* is used only in TempTables and LogicalTiles.
96+
*/
6197
std::vector<catalog::Schema> GetLayoutSchemas(catalog::Schema* const schema) const;
6298

99+
/** @brief Returns the layout statistics used by the LayoutTuner. */
63100
std::map<oid_t, oid_t> GetLayoutStats() const;
64101

102+
/** @brief Serialzies the column_map to be added to pg_layout. */
65103
std::string SerializeColumnMap() const;
66104

105+
/** @brief Deserializes the string that has been read from pg_layout. */
67106
static column_map_type DeserializeColumnMap(oid_t num_columns,
68107
std::string column_map_str);
69108

109+
/** @brief Returns a string containing the column_map of this layout. */
70110
std::string GetColumnMapInfo() const;
71111

72-
// Get a string representation for debugging
112+
/** @brief Returns a string representation for debugging function. */
73113
const std::string GetInfo() const;
74114

115+
//@{
116+
/** @brief Operators for checking equality of two layouts. */
75117
friend bool operator==(const Layout& lhs, const Layout& rhs);
76118
friend bool operator!=(const Layout& lhs, const Layout& rhs);
119+
//@}
77120

78121
private:
79122

80-
// Layout Id of the tile
123+
/** @brief Layout Oid of the layout object. */
81124
oid_t layout_oid_;
82125

83-
// Number of columns in the layout
126+
/** @brief Number of columns in the layout. */
84127
oid_t num_columns_;
85128

86-
// Layout of the columns.
129+
/** @brief column_map of the layout. */
87130
column_map_type column_layout_;
88131

132+
/** @brief Layout type is always (ROW, COLUMN or HYBRID). */
89133
LayoutType layout_type_;
90134

91135
};

src/include/storage/tile_group.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ class TileGroup : public Printable {
161161
// Sync the contents
162162
void Sync();
163163

164+
// Get the layout of the TileGroup. Used to locate columns.
164165
const storage::Layout& GetLayout() const { return *tile_group_layout_; }
165166

166167
protected:

src/storage/layout.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ oid_t Layout::GetTileIdFromColumnId(oid_t column_id) const {
146146
return tile_offset;
147147
}
148148

149-
oid_t Layout::GetTileColumnId(oid_t column_id) const {
149+
oid_t Layout::GetTileColumnOffset(oid_t column_id) const {
150150
oid_t tile_column_id, tile_offset;
151151
LocateTileAndColumn(column_id, tile_offset, tile_column_id);
152152
return tile_column_id;

0 commit comments

Comments
 (0)