|
17 | 17 | #include "catalog/database_catalog.h" |
18 | 18 | #include "catalog/index_catalog.h" |
19 | 19 | #include "catalog/column_catalog.h" |
| 20 | +#include "catalog/layout_catalog.h" |
20 | 21 | #include "concurrency/transaction_context.h" |
21 | 22 | #include "storage/data_table.h" |
22 | 23 | #include "type/value_factory.h" |
@@ -303,6 +304,87 @@ std::shared_ptr<ColumnCatalogObject> TableCatalogObject::GetColumnObject( |
303 | 304 | return nullptr; |
304 | 305 | } |
305 | 306 |
|
| 307 | +/* @brief Insert layout object into the cache. |
| 308 | + * @param layout Layout object to be inserted |
| 309 | + * @return false if layout already exists in cache |
| 310 | + */ |
| 311 | +bool TableCatalogObject::InsertLayout(std::shared_ptr<const storage::Layout> layout) { |
| 312 | + |
| 313 | + // Invalid object |
| 314 | + if (layout == nullptr) { |
| 315 | + return false; |
| 316 | + } |
| 317 | + |
| 318 | + oid_t layout_id = layout->GetLayoutId(); |
| 319 | + // layout is already present in the cache. |
| 320 | + if (layout_objects_.find(layout_id) != layout_objects_.end()) { |
| 321 | + LOG_DEBUG("Layout %u already exists in cache!", layout_id); |
| 322 | + return false; |
| 323 | + } |
| 324 | + |
| 325 | + valid_layout_objects_ = true; |
| 326 | + layout_objects_.insert(std::make_pair(layout_id, layout)); |
| 327 | + return true; |
| 328 | +} |
| 329 | + |
| 330 | +/* |
| 331 | + * @brief evict all layout objects from cache |
| 332 | + */ |
| 333 | +void TableCatalogObject::EvictAllLayouts() { |
| 334 | + layout_objects_.clear(); |
| 335 | + valid_layout_objects_ = false; |
| 336 | +} |
| 337 | + |
| 338 | +/* @brief Get all layout objects of this table. |
| 339 | + * Add it to the cache if necessary. |
| 340 | + * @param cached_only If set to true, don't fetch the layout objects. |
| 341 | + * @return Map from layout_oid to cached layout object. |
| 342 | + */ |
| 343 | +std::unordered_map<oid_t, std::shared_ptr<const storage::Layout>> |
| 344 | +TableCatalogObject::GetLayouts(bool cached_only) { |
| 345 | + if (!valid_layout_objects_ && !cached_only) { |
| 346 | + // get column catalog objects from pg_column |
| 347 | + LayoutCatalog::GetInstance()->GetLayouts(table_oid, txn); |
| 348 | + valid_column_objects = true; |
| 349 | + } |
| 350 | + return layout_objects_; |
| 351 | +} |
| 352 | + |
| 353 | +/* @brief get the layout object of the given layout_id. |
| 354 | + * @param layout_id The id of the layout to be fetched. |
| 355 | + * @param cached_only If set to true, don't fetch the layout objects. |
| 356 | + * @return Layout object of corresponding to the layout_id if present. |
| 357 | + */ |
| 358 | +std::shared_ptr<const storage::Layout> |
| 359 | +TableCatalogObject::GetLayout(oid_t layout_id, bool cached_entry) { |
| 360 | + // fetch layout objects in case we have not |
| 361 | + GetLayouts(cached_entry); |
| 362 | + auto it = layout_objects_.find(layout_id); |
| 363 | + if (it != layout_objects_.end()) { |
| 364 | + return it->second; |
| 365 | + } |
| 366 | + return nullptr; |
| 367 | +} |
| 368 | + |
| 369 | +/* @brief Evict layout from the cache. |
| 370 | + * @param layout_id Id of the layout to be deleted. |
| 371 | + * @return true if layout_id is found and evicted; false if not found. |
| 372 | + */ |
| 373 | +bool TableCatalogObject::EvictLayout(oid_t layout_id) { |
| 374 | + if (!valid_layout_objects_) return false; |
| 375 | + |
| 376 | + // find layout from the cache |
| 377 | + auto it = layout_objects_.find(layout_id); |
| 378 | + if (it == layout_objects_.end()) { |
| 379 | + return false; // layout_id not found in cache |
| 380 | + } |
| 381 | + |
| 382 | + auto layout = it->second; |
| 383 | + PELOTON_ASSERT(layout); |
| 384 | + layout_objects_.erase(it); |
| 385 | + return true; |
| 386 | +} |
| 387 | + |
306 | 388 | TableCatalog *TableCatalog::GetInstance(storage::Database *pg_catalog, |
307 | 389 | type::AbstractPool *pool, |
308 | 390 | concurrency::TransactionContext *txn) { |
|
0 commit comments