|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// Peloton |
| 4 | +// |
| 5 | +// sequence_functions.cpp |
| 6 | +// |
| 7 | +// Identification: src/function/sequence_functions.cpp |
| 8 | +// |
| 9 | +// Copyright (c) 2015-2018, Carnegie Mellon University Database Group |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "function/sequence_functions.h" |
| 14 | + |
| 15 | +#include "common/macros.h" |
| 16 | +#include "executor/executor_context.h" |
| 17 | +#include "catalog/catalog.h" |
| 18 | +#include "catalog/database_catalog.h" |
| 19 | +#include "catalog/sequence_catalog.h" |
| 20 | +#include "concurrency/transaction_context.h" |
| 21 | +#include "concurrency/transaction_manager_factory.h" |
| 22 | +#include "type/value_factory.h" |
| 23 | + |
| 24 | +namespace peloton { |
| 25 | +namespace function { |
| 26 | + |
| 27 | +/* |
| 28 | + * @brief The actual implementation to get the incremented value for the specified sequence |
| 29 | + * @param sequence name |
| 30 | + * @param executor context |
| 31 | + * @return the next value for the sequence |
| 32 | + * @exception the sequence does not exist |
| 33 | + */ |
| 34 | +uint32_t SequenceFunctions::Nextval(executor::ExecutorContext &ctx, |
| 35 | + const char *sequence_name) { |
| 36 | + PELOTON_ASSERT(sequence_name != nullptr); |
| 37 | + 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); |
| 42 | + |
| 43 | + // initialize a new transaction for incrementing sequence value |
| 44 | + auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance(); |
| 45 | + auto mini_txn = txn_manager.BeginTransaction(); |
| 46 | + |
| 47 | + // evict the old cached copy of sequence |
| 48 | + txn->catalog_cache.EvictSequenceObject(sequence_name,database_oid); |
| 49 | + auto sequence_object = |
| 50 | + catalog::Catalog::GetInstance() |
| 51 | + ->GetSystemCatalogs(database_oid) |
| 52 | + ->GetSequenceCatalog() |
| 53 | + ->GetSequence(database_oid, sequence_name, mini_txn); |
| 54 | + |
| 55 | + if (sequence_object != nullptr) { |
| 56 | + uint32_t val = sequence_object->GetNextVal(); |
| 57 | + int64_t curr_val = sequence_object->GetCurrVal(); |
| 58 | + // insert the new copy of sequence into cache for future currval |
| 59 | + txn->catalog_cache.InsertSequenceObject(sequence_object); |
| 60 | + |
| 61 | + auto ret = txn_manager.CommitTransaction(mini_txn); |
| 62 | + if (ret != ResultType::SUCCESS) { |
| 63 | + txn_manager.AbortTransaction(mini_txn); |
| 64 | + return Nextval(ctx, sequence_name); |
| 65 | + } |
| 66 | + |
| 67 | + catalog::Catalog::GetInstance() |
| 68 | + ->GetSystemCatalogs(database_oid) |
| 69 | + ->GetSequenceCatalog() |
| 70 | + ->InsertCurrValCache(txn->GetTemporarySchemaName(), |
| 71 | + sequence_name, curr_val); |
| 72 | + return val; |
| 73 | + } else { |
| 74 | + throw SequenceException( |
| 75 | + StringUtil::Format("Sequence \"%s\" does not exist", sequence_name)); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +/* |
| 80 | + * @brief The actual implementation to get the current value for the specified sequence |
| 81 | + * @param sequence name |
| 82 | + * @param executor context |
| 83 | + * @return the current value of a sequence |
| 84 | + * @exception either the sequence does not exist, or 'call nextval before currval' |
| 85 | + */ |
| 86 | +uint32_t SequenceFunctions::Currval(executor::ExecutorContext &ctx, |
| 87 | + const char *sequence_name) { |
| 88 | + PELOTON_ASSERT(sequence_name != nullptr); |
| 89 | + concurrency::TransactionContext* txn = ctx.GetTransaction(); |
| 90 | + // 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); |
| 94 | + |
| 95 | + // get the sequence copy from cache |
| 96 | + auto sequence_catalog = catalog::Catalog::GetInstance() |
| 97 | + ->GetSystemCatalogs(database_oid) |
| 98 | + ->GetSequenceCatalog(); |
| 99 | + |
| 100 | + if(sequence_catalog->CheckCachedCurrValExistence( |
| 101 | + txn->GetTemporarySchemaName(), std::string(sequence_name))) { |
| 102 | + return sequence_catalog->GetCachedCurrVal( |
| 103 | + txn->GetTemporarySchemaName(), std::string(sequence_name)); |
| 104 | + } else { |
| 105 | + // get sequence from catalog |
| 106 | + auto sequence_object = sequence_catalog |
| 107 | + ->GetSequence(database_oid, sequence_name, txn); |
| 108 | + if (sequence_object != nullptr) { |
| 109 | + throw SequenceException( |
| 110 | + StringUtil::Format("currval for sequence \"%s\" is undefined for this session", |
| 111 | + sequence_name)); |
| 112 | + } else { |
| 113 | + // sequence does not exist |
| 114 | + throw SequenceException( |
| 115 | + StringUtil::Format("Sequence \"%s\" does not exist", sequence_name)); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +/* |
| 121 | + * @brief The wrapper function to get the incremented value for the specified sequence |
| 122 | + * @param sequence name |
| 123 | + * @param executor context |
| 124 | + * @return the result of executing NextVal |
| 125 | + */ |
| 126 | +type::Value SequenceFunctions::_Nextval(const std::vector<type::Value> &args) { |
| 127 | + executor::ExecutorContext* ctx = (executor::ExecutorContext*)args[1].GetAs<uint64_t>(); |
| 128 | + uint32_t ret = SequenceFunctions::Nextval(*ctx, args[0].GetAs<const char *>()); |
| 129 | + return type::ValueFactory::GetIntegerValue(ret); |
| 130 | +} |
| 131 | + |
| 132 | +/* |
| 133 | + * @brief The wrapper function to get the current value for the specified sequence |
| 134 | + * @param sequence name |
| 135 | + * @param executor context |
| 136 | + * @return the result of executing CurrVal |
| 137 | + */ |
| 138 | +type::Value SequenceFunctions::_Currval(const std::vector<type::Value> &args) { |
| 139 | + executor::ExecutorContext* ctx = (executor::ExecutorContext*)args[1].GetAs<uint64_t>(); |
| 140 | + uint32_t ret = SequenceFunctions::Currval(*ctx, args[0].GetAs<const char *>()); |
| 141 | + return type::ValueFactory::GetIntegerValue(ret); |
| 142 | +} |
| 143 | + |
| 144 | +} // namespace function |
| 145 | +} // namespace peloton |
0 commit comments