18
18
#include " catalog/column_catalog.h"
19
19
#include " catalog/database_catalog.h"
20
20
#include " catalog/index_catalog.h"
21
+ #include " catalog/layout_catalog.h"
21
22
#include " catalog/system_catalogs.h"
22
23
#include " concurrency/transaction_context.h"
23
24
#include " storage/data_table.h"
@@ -319,8 +320,8 @@ std::shared_ptr<ColumnCatalogObject> TableCatalogObject::GetColumnObject(
319
320
TableCatalog::TableCatalog (
320
321
storage::Database *database, UNUSED_ATTRIBUTE type::AbstractPool *pool,
321
322
UNUSED_ATTRIBUTE concurrency::TransactionContext *txn)
322
- : AbstractCatalog(TABLE_CATALOG_OID, TABLE_CATALOG_NAME,
323
- InitializeSchema ().release(), database) {
323
+ : AbstractCatalog(TABLE_CATALOG_OID, TABLE_CATALOG_NAME,
324
+ InitializeSchema ().release(), database) {
324
325
// Add indexes for pg_namespace
325
326
AddIndex ({0 }, TABLE_CATALOG_PKEY_OID, TABLE_CATALOG_NAME " _pkey" ,
326
327
IndexConstraintType::PRIMARY_KEY);
@@ -330,6 +331,95 @@ TableCatalog::TableCatalog(
330
331
IndexConstraintType::DEFAULT);
331
332
}
332
333
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
337
+ */
338
+ bool TableCatalogObject::InsertLayout (std::shared_ptr<const storage::Layout> layout) {
339
+
340
+ // Invalid object
341
+ if (layout == nullptr ) {
342
+ return false ;
343
+ }
344
+
345
+ oid_t layout_id = layout->GetLayoutId ();
346
+ // layout is already present in the cache.
347
+ if (layout_objects_.find (layout_id) != layout_objects_.end ()) {
348
+ LOG_DEBUG (" Layout %u already exists in cache!" , layout_id);
349
+ return false ;
350
+ }
351
+
352
+ valid_layout_objects_ = true ;
353
+ layout_objects_.insert (std::make_pair (layout_id, layout));
354
+ return true ;
355
+ }
356
+
357
+ /*
358
+ * @brief evict all layout objects from cache
359
+ */
360
+ void TableCatalogObject::EvictAllLayouts () {
361
+ layout_objects_.clear ();
362
+ valid_layout_objects_ = false ;
363
+ }
364
+
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.
369
+ */
370
+ std::unordered_map<oid_t , std::shared_ptr<const storage::Layout>>
371
+ TableCatalogObject::GetLayouts (bool cached_only) {
372
+ if (!valid_layout_objects_ && !cached_only) {
373
+ // get column catalog objects from pg_column
374
+ LayoutCatalog::GetInstance ()->GetLayouts (table_oid, txn);
375
+ valid_column_objects = true ;
376
+ }
377
+ return layout_objects_;
378
+ }
379
+
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.
384
+ */
385
+ std::shared_ptr<const storage::Layout>
386
+ TableCatalogObject::GetLayout (oid_t layout_id, bool cached_entry) {
387
+ // fetch layout objects in case we have not
388
+ GetLayouts (cached_entry);
389
+ auto it = layout_objects_.find (layout_id);
390
+ if (it != layout_objects_.end ()) {
391
+ return it->second ;
392
+ }
393
+ return nullptr ;
394
+ }
395
+
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.
399
+ */
400
+ bool TableCatalogObject::EvictLayout (oid_t layout_id) {
401
+ if (!valid_layout_objects_) return false ;
402
+
403
+ // find layout from the cache
404
+ auto it = layout_objects_.find (layout_id);
405
+ if (it == layout_objects_.end ()) {
406
+ return false ; // layout_id not found in cache
407
+ }
408
+
409
+ auto layout = it->second ;
410
+ PELOTON_ASSERT (layout);
411
+ layout_objects_.erase (it);
412
+ return true ;
413
+ }
414
+
415
+ TableCatalog *TableCatalog::GetInstance (storage::Database *pg_catalog,
416
+ type::AbstractPool *pool,
417
+ concurrency::TransactionContext *txn) {
418
+ static TableCatalog table_catalog{pg_catalog, pool, txn};
419
+ return &table_catalog;
420
+ }
421
+
422
+
333
423
TableCatalog::~TableCatalog () {}
334
424
335
425
/* @brief private function for initialize schema of pg_table
0 commit comments