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

Commit f688f47

Browse files
committed
This compiles and passes all the tests except for the sequence test (obviously)
1 parent 1e21a52 commit f688f47

File tree

11 files changed

+86
-54
lines changed

11 files changed

+86
-54
lines changed

src/catalog/catalog_cache.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,14 @@ std::shared_ptr<DatabaseCatalogObject> CatalogCache::GetDatabaseObject(
112112
return it->second;
113113
}
114114

115+
std::vector<std::shared_ptr<DatabaseCatalogObject>> CatalogCache::GetAllDatabaseObjects() {
116+
std::vector<std::shared_ptr<DatabaseCatalogObject>> databases;
117+
for (auto it : database_objects_cache) {
118+
databases.push_back(it.second);
119+
}
120+
return (databases);
121+
}
122+
115123
/*@brief search table catalog object from all cached database objects
116124
* @param table_oid
117125
* @return table catalog object; if not found return null

src/catalog/database_catalog.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ bool DatabaseCatalog::DeleteDatabase(oid_t database_oid,
314314
values.push_back(type::ValueFactory::GetIntegerValue(database_oid).Copy());
315315

316316
// evict cache
317-
txn->catalog_cache.EvictDatabaseObject(database_oid);
317+
txn->GetCatalogCache()->EvictDatabaseObject(database_oid);
318318

319319
return DeleteWithIndexScan(index_offset, values, txn);
320320
}
@@ -325,7 +325,7 @@ std::shared_ptr<DatabaseCatalogObject> DatabaseCatalog::GetDatabaseObject(
325325
throw CatalogException("Transaction is invalid!");
326326
}
327327
// try get from cache
328-
auto database_object = txn->catalog_cache.GetDatabaseObject(database_oid);
328+
auto database_object = txn->GetCatalogCache()->GetDatabaseObject(database_oid);
329329
if (database_object) return database_object;
330330

331331
// cache miss, get from pg_database
@@ -341,7 +341,7 @@ std::shared_ptr<DatabaseCatalogObject> DatabaseCatalog::GetDatabaseObject(
341341
auto database_object =
342342
std::make_shared<DatabaseCatalogObject>((*result_tiles)[0].get(), txn);
343343
// insert into cache
344-
bool success = txn->catalog_cache.InsertDatabaseObject(database_object);
344+
bool success = txn->GetCatalogCache()->InsertDatabaseObject(database_object);
345345
PELOTON_ASSERT(success == true);
346346
(void)success;
347347
return database_object;
@@ -364,7 +364,7 @@ std::shared_ptr<DatabaseCatalogObject> DatabaseCatalog::GetDatabaseObject(
364364
throw CatalogException("Transaction is invalid!");
365365
}
366366
// try get from cache
367-
auto database_object = txn->catalog_cache.GetDatabaseObject(database_name);
367+
auto database_object = txn->GetCatalogCache()->GetDatabaseObject(database_name);
368368
if (database_object) return database_object;
369369

370370
// cache miss, get from pg_database
@@ -382,7 +382,7 @@ std::shared_ptr<DatabaseCatalogObject> DatabaseCatalog::GetDatabaseObject(
382382
std::make_shared<DatabaseCatalogObject>((*result_tiles)[0].get(), txn);
383383
if (database_object) {
384384
// insert into cache
385-
bool success = txn->catalog_cache.InsertDatabaseObject(database_object);
385+
bool success = txn->GetCatalogCache()->InsertDatabaseObject(database_object);
386386
PELOTON_ASSERT(success == true);
387387
(void)success;
388388
}

src/catalog/index_catalog.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,10 @@ bool IndexCatalog::DeleteIndex(oid_t index_oid,
173173
std::vector<type::Value> values;
174174
values.push_back(type::ValueFactory::GetIntegerValue(index_oid).Copy());
175175

176-
auto index_object = txn->catalog_cache.GetCachedIndexObject(index_oid);
176+
auto index_object = txn->GetCatalogCache()->GetCachedIndexObject(index_oid);
177177
if (index_object) {
178178
auto table_object =
179-
txn->catalog_cache.GetCachedTableObject(index_object->GetTableOid());
179+
txn->GetCatalogCache()->GetCachedTableObject(index_object->GetTableOid());
180180
table_object->EvictAllIndexObjects();
181181
}
182182

@@ -189,7 +189,7 @@ std::shared_ptr<IndexCatalogObject> IndexCatalog::GetIndexObject(
189189
throw CatalogException("Transaction is invalid!");
190190
}
191191
// try get from cache
192-
auto index_object = txn->catalog_cache.GetCachedIndexObject(index_oid);
192+
auto index_object = txn->GetCatalogCache()->GetCachedIndexObject(index_oid);
193193
if (index_object) {
194194
return index_object;
195195
}
@@ -231,7 +231,7 @@ std::shared_ptr<IndexCatalogObject> IndexCatalog::GetIndexObject(
231231
}
232232
// try get from cache
233233
auto index_object =
234-
txn->catalog_cache.GetCachedIndexObject(index_name, schema_name);
234+
txn->GetCatalogCache()->GetCachedIndexObject(index_name, schema_name);
235235
if (index_object) {
236236
return index_object;
237237
}

src/catalog/table_catalog.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ bool TableCatalog::DeleteTable(oid_t table_oid,
501501
values.push_back(type::ValueFactory::GetIntegerValue(table_oid).Copy());
502502

503503
// evict from cache
504-
auto table_object = txn->catalog_cache.GetCachedTableObject(table_oid);
504+
auto table_object = txn->GetCatalogCache()->GetCachedTableObject(table_oid);
505505
if (table_object) {
506506
auto database_object =
507507
DatabaseCatalog::GetInstance()->GetDatabaseObject(database_oid, txn);
@@ -522,7 +522,7 @@ std::shared_ptr<TableCatalogObject> TableCatalog::GetTableObject(
522522
throw CatalogException("Transaction is invalid!");
523523
}
524524
// try get from cache
525-
auto table_object = txn->catalog_cache.GetCachedTableObject(table_oid);
525+
auto table_object = txn->GetCatalogCache()->GetCachedTableObject(table_oid);
526526
if (table_object) return table_object;
527527

528528
// cache miss, get from pg_table
@@ -568,7 +568,7 @@ std::shared_ptr<TableCatalogObject> TableCatalog::GetTableObject(
568568
throw CatalogException("Transaction is invalid!");
569569
}
570570
// try get from cache
571-
auto database_object = txn->catalog_cache.GetDatabaseObject(database_oid);
571+
auto database_object = txn->GetCatalogCache()->GetDatabaseObject(database_oid);
572572
if (database_object) {
573573
auto table_object =
574574
database_object->GetTableObject(table_name, schema_name, true);
@@ -662,7 +662,7 @@ bool TableCatalog::UpdateVersionId(oid_t update_val, oid_t table_oid,
662662
type::ValueFactory::GetIntegerValue(update_val).Copy());
663663

664664
// get table object, then evict table object
665-
auto table_object = txn->catalog_cache.GetCachedTableObject(table_oid);
665+
auto table_object = txn->GetCatalogCache()->GetCachedTableObject(table_oid);
666666
if (table_object) {
667667
auto database_object =
668668
DatabaseCatalog::GetInstance()->GetDatabaseObject(database_oid, txn);

src/concurrency/transaction_context.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ void TransactionContext::Init(const size_t thread_id,
8787
gc_set_.reset(new GCSet());
8888
gc_object_set_.reset(new GCObjectSet());
8989

90+
// TODO: I think that we need to delete this object when
91+
// we delete the TransactionContext. It probably shouldn't
92+
// be a shared_ptr since the TransactionContext owns it
93+
catalog_cache.reset(new catalog::CatalogCache());
94+
9095
on_commit_triggers_.reset();
9196
}
9297

src/function/sequence_functions.cpp

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,32 @@ uint32_t SequenceFunctions::Nextval(executor::ExecutorContext &ctx,
3535
const char *sequence_name) {
3636
PELOTON_ASSERT(sequence_name != nullptr);
3737
concurrency::TransactionContext* txn = ctx.GetTransaction();
38-
// get the database oid for this transaction
39-
oid_t database_oid = catalog::Catalog::GetInstance()
40-
->GetDatabaseObject(ctx.GetDatabaseName(), txn)->GetDatabaseOid();
41-
LOG_DEBUG("Get database oid: %u", database_oid);
38+
39+
// HACK: Assume that there is only one database in our cache
40+
auto all_databases = txn->GetCatalogCache()->GetAllDatabaseObjects();
41+
PELOTON_ASSERT(all_databases.empty() == false);
42+
auto database_catalog = all_databases[0];
43+
LOG_DEBUG("Get database oid: %u", database_catalog->GetDatabaseOid());
4244

4345
// initialize a new transaction for incrementing sequence value
4446
auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
4547
auto mini_txn = txn_manager.BeginTransaction();
4648

4749
// evict the old cached copy of sequence
48-
txn->catalog_cache.EvictSequenceObject(sequence_name,database_oid);
50+
txn->GetCatalogCache()->EvictSequenceObject(sequence_name,
51+
database_catalog->GetDatabaseOid());
4952
auto sequence_object =
5053
catalog::Catalog::GetInstance()
51-
->GetSystemCatalogs(database_oid)
54+
->GetSystemCatalogs(database_catalog->GetDatabaseOid())
5255
->GetSequenceCatalog()
53-
->GetSequence(database_oid, sequence_name, mini_txn);
56+
->GetSequence(database_catalog->GetDatabaseOid(),
57+
sequence_name, mini_txn);
5458

5559
if (sequence_object != nullptr) {
5660
uint32_t val = sequence_object->GetNextVal();
5761
int64_t curr_val = sequence_object->GetCurrVal();
5862
// insert the new copy of sequence into cache for future currval
59-
txn->catalog_cache.InsertSequenceObject(sequence_object);
63+
txn->GetCatalogCache()->InsertSequenceObject(sequence_object);
6064

6165
auto ret = txn_manager.CommitTransaction(mini_txn);
6266
if (ret != ResultType::SUCCESS) {
@@ -65,9 +69,9 @@ uint32_t SequenceFunctions::Nextval(executor::ExecutorContext &ctx,
6569
}
6670

6771
catalog::Catalog::GetInstance()
68-
->GetSystemCatalogs(database_oid)
72+
->GetSystemCatalogs(database_catalog->GetDatabaseOid())
6973
->GetSequenceCatalog()
70-
->InsertCurrValCache(txn->GetTemporarySchemaName(),
74+
->InsertCurrValCache("FIXME_SCHEMA",
7175
sequence_name, curr_val);
7276
return val;
7377
} else {
@@ -88,23 +92,24 @@ uint32_t SequenceFunctions::Currval(executor::ExecutorContext &ctx,
8892
PELOTON_ASSERT(sequence_name != nullptr);
8993
concurrency::TransactionContext* txn = ctx.GetTransaction();
9094
// get the database oid for this transaction
91-
oid_t database_oid = catalog::Catalog::GetInstance()
92-
->GetDatabaseObject(ctx.GetDatabaseName(), txn)->GetDatabaseOid();
93-
LOG_DEBUG("Get database oid: %u", database_oid);
95+
// HACK: Assume that there is only one database in our cache
96+
auto all_databases = txn->GetCatalogCache()->GetAllDatabaseObjects();
97+
PELOTON_ASSERT(all_databases.empty() == false);
98+
auto database_catalog = all_databases[0];
9499

95100
// get the sequence copy from cache
96101
auto sequence_catalog = catalog::Catalog::GetInstance()
97-
->GetSystemCatalogs(database_oid)
102+
->GetSystemCatalogs(database_catalog->GetDatabaseOid())
98103
->GetSequenceCatalog();
99104

100105
if(sequence_catalog->CheckCachedCurrValExistence(
101-
txn->GetTemporarySchemaName(), std::string(sequence_name))) {
106+
"FIXME_SCHEMA", std::string(sequence_name))) {
102107
return sequence_catalog->GetCachedCurrVal(
103-
txn->GetTemporarySchemaName(), std::string(sequence_name));
108+
"FIXME_SCHEMA", std::string(sequence_name));
104109
} else {
105110
// get sequence from catalog
106111
auto sequence_object = sequence_catalog
107-
->GetSequence(database_oid, sequence_name, txn);
112+
->GetSequence(database_catalog->GetDatabaseOid(), sequence_name, txn);
108113
if (sequence_object != nullptr) {
109114
throw SequenceException(
110115
StringUtil::Format("currval for sequence \"%s\" is undefined for this session",

src/include/catalog/catalog_cache.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#pragma once
1414

1515
#include <mutex>
16+
#include <vector>
1617
#include <unordered_map>
1718

1819
#include "common/internal_types.h"
@@ -35,7 +36,6 @@ class TableCatalogObject;
3536
class IndexCatalogObject;
3637

3738
class CatalogCache {
38-
friend class Transaction;
3939
friend class DatabaseCatalog;
4040
friend class TableCatalog;
4141
friend class IndexCatalog;
@@ -56,6 +56,12 @@ class CatalogCache {
5656
std::shared_ptr<DatabaseCatalogObject> GetDatabaseObject(
5757
const std::string &name);
5858

59+
/**
60+
* @brief Retrieve a vector of all the DatabaseObjects cached
61+
* @return
62+
*/
63+
std::vector<std::shared_ptr<DatabaseCatalogObject>> GetAllDatabaseObjects();
64+
5965
std::shared_ptr<TableCatalogObject> GetCachedTableObject(oid_t table_oid);
6066
std::shared_ptr<IndexCatalogObject> GetCachedIndexObject(oid_t index_oid);
6167
std::shared_ptr<IndexCatalogObject> GetCachedIndexObject(

src/include/concurrency/transaction_context.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ class TransactionContext : public Printable {
6969
// Mutators and Accessors
7070
//===--------------------------------------------------------------------===//
7171

72+
/**
73+
* @brief Get the CatalogCache for this txn
74+
* @return the CatalogCache for this txn
75+
*/
76+
inline std::shared_ptr<catalog::CatalogCache> GetCatalogCache() const {
77+
return (catalog_cache);
78+
}
79+
7280
/**
7381
* @brief Gets the thread identifier.
7482
*
@@ -283,14 +291,14 @@ class TransactionContext : public Printable {
283291
return isolation_level_;
284292
}
285293

286-
/** cache for table catalog objects */
287-
catalog::CatalogCache catalog_cache;
288-
289294
private:
290295
//===--------------------------------------------------------------------===//
291296
// Data members
292297
//===--------------------------------------------------------------------===//
293298

299+
/** cache for table catalog objects */
300+
std::shared_ptr<catalog::CatalogCache> catalog_cache;
301+
294302
/** transaction id */
295303
txn_id_t txn_id_;
296304

src/include/planner/plan_util.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class PlanUtil {
6262
* @return set of affected index object ids
6363
*/
6464
static const std::set<oid_t> GetAffectedIndexes(
65-
catalog::CatalogCache &catalog_cache,
65+
std::shared_ptr<catalog::CatalogCache> catalog_cache,
6666
const parser::SQLStatement &sql_stmt);
6767

6868
/**
@@ -73,7 +73,7 @@ class PlanUtil {
7373
* @return vector of affected column ids with triplet format
7474
*/
7575
static const std::vector<col_triplet> GetIndexableColumns(
76-
catalog::CatalogCache &catalog_cache,
76+
std::shared_ptr<catalog::CatalogCache> catalog_cache,
7777
std::unique_ptr<parser::SQLStatementList> sql_stmt_list,
7878
const std::string &db_name);
7979

src/planner/plan_util.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ namespace peloton {
3434
namespace planner {
3535

3636
const std::set<oid_t> PlanUtil::GetAffectedIndexes(
37-
catalog::CatalogCache &catalog_cache,
37+
std::shared_ptr<catalog::CatalogCache> catalog_cache,
3838
const parser::SQLStatement &sql_stmt) {
3939
std::set<oid_t> index_oids;
4040
std::string db_name, table_name, schema_name;
@@ -56,7 +56,7 @@ const std::set<oid_t> PlanUtil::GetAffectedIndexes(
5656
table_name = delete_stmt.GetTableName();
5757
schema_name = delete_stmt.GetSchemaName();
5858
}
59-
auto indexes_map = catalog_cache.GetDatabaseObject(db_name)
59+
auto indexes_map = catalog_cache->GetDatabaseObject(db_name)
6060
->GetTableObject(table_name, schema_name)
6161
->GetIndexObjects();
6262
for (auto &index : indexes_map) {
@@ -69,7 +69,7 @@ const std::set<oid_t> PlanUtil::GetAffectedIndexes(
6969
db_name = update_stmt.table->GetDatabaseName();
7070
table_name = update_stmt.table->GetTableName();
7171
schema_name = update_stmt.table->GetSchemaName();
72-
auto table_object = catalog_cache.GetDatabaseObject(db_name)
72+
auto table_object = catalog_cache->GetDatabaseObject(db_name)
7373
->GetTableObject(table_name, schema_name);
7474

7575
auto &update_clauses = update_stmt.updates;
@@ -105,7 +105,7 @@ const std::set<oid_t> PlanUtil::GetAffectedIndexes(
105105
}
106106

107107
const std::vector<col_triplet> PlanUtil::GetIndexableColumns(
108-
catalog::CatalogCache &catalog_cache,
108+
std::shared_ptr<catalog::CatalogCache> catalog_cache,
109109
std::unique_ptr<parser::SQLStatementList> sql_stmt_list,
110110
const std::string &db_name) {
111111
std::vector<col_triplet> column_oids;
@@ -132,7 +132,7 @@ const std::vector<col_triplet> PlanUtil::GetIndexableColumns(
132132
try {
133133
auto plan = optimizer->BuildPelotonPlanTree(sql_stmt_list, txn);
134134

135-
auto db_object = catalog_cache.GetDatabaseObject(db_name);
135+
auto db_object = catalog_cache->GetDatabaseObject(db_name);
136136
database_id = db_object->GetDatabaseOid();
137137

138138
// Perform a breadth first search on plan tree

0 commit comments

Comments
 (0)