forked from cmu-db/peloton
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
These should be the last files that we need from from cmu-db#1345
- Loading branch information
Showing
7 changed files
with
249 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Peloton | ||
// | ||
// sequence_functions_proxy.cpp | ||
// | ||
// Identification: src/codegen/proxy/sequence_functions_proxy.cpp | ||
// | ||
// Copyright (c) 2015-2017, Carnegie Mellon University Database Group | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "codegen/proxy/sequence_functions_proxy.h" | ||
|
||
#include "codegen/proxy/executor_context_proxy.h" | ||
|
||
namespace peloton { | ||
namespace codegen { | ||
|
||
DEFINE_METHOD(peloton::function, SequenceFunctions, Nextval); | ||
DEFINE_METHOD(peloton::function, SequenceFunctions, Currval); | ||
|
||
} // namespace codegen | ||
} // namespace peloton |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Peloton | ||
// | ||
// sequence_functions.cpp | ||
// | ||
// Identification: src/function/sequence_functions.cpp | ||
// | ||
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "function/sequence_functions.h" | ||
|
||
#include "common/macros.h" | ||
#include "executor/executor_context.h" | ||
#include "catalog/catalog.h" | ||
#include "catalog/database_catalog.h" | ||
#include "catalog/sequence_catalog.h" | ||
#include "concurrency/transaction_context.h" | ||
#include "concurrency/transaction_manager_factory.h" | ||
#include "type/value_factory.h" | ||
|
||
namespace peloton { | ||
namespace function { | ||
|
||
/* | ||
* @brief The actual implementation to get the incremented value for the specified sequence | ||
* @param sequence name | ||
* @param executor context | ||
* @return the next value for the sequence | ||
* @exception the sequence does not exist | ||
*/ | ||
uint32_t SequenceFunctions::Nextval(executor::ExecutorContext &ctx, | ||
const char *sequence_name) { | ||
PELOTON_ASSERT(sequence_name != nullptr); | ||
concurrency::TransactionContext* txn = ctx.GetTransaction(); | ||
// get the database oid for this transaction | ||
oid_t database_oid = catalog::Catalog::GetInstance() | ||
->GetDatabaseObject(ctx.GetDatabaseName(), txn)->GetDatabaseOid(); | ||
LOG_DEBUG("Get database oid: %u", database_oid); | ||
|
||
// initialize a new transaction for incrementing sequence value | ||
auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance(); | ||
auto mini_txn = txn_manager.BeginTransaction(); | ||
|
||
// evict the old cached copy of sequence | ||
txn->catalog_cache.EvictSequenceObject(sequence_name,database_oid); | ||
auto sequence_object = | ||
catalog::Catalog::GetInstance() | ||
->GetSystemCatalogs(database_oid) | ||
->GetSequenceCatalog() | ||
->GetSequence(database_oid, sequence_name, mini_txn); | ||
|
||
if (sequence_object != nullptr) { | ||
uint32_t val = sequence_object->GetNextVal(); | ||
int64_t curr_val = sequence_object->GetCurrVal(); | ||
// insert the new copy of sequence into cache for future currval | ||
txn->catalog_cache.InsertSequenceObject(sequence_object); | ||
|
||
auto ret = txn_manager.CommitTransaction(mini_txn); | ||
if (ret != ResultType::SUCCESS) { | ||
txn_manager.AbortTransaction(mini_txn); | ||
return Nextval(ctx, sequence_name); | ||
} | ||
|
||
catalog::Catalog::GetInstance() | ||
->GetSystemCatalogs(database_oid) | ||
->GetSequenceCatalog() | ||
->InsertCurrValCache(txn->GetTemporarySchemaName(), | ||
sequence_name, curr_val); | ||
return val; | ||
} else { | ||
throw SequenceException( | ||
StringUtil::Format("Sequence \"%s\" does not exist", sequence_name)); | ||
} | ||
} | ||
|
||
/* | ||
* @brief The actual implementation to get the current value for the specified sequence | ||
* @param sequence name | ||
* @param executor context | ||
* @return the current value of a sequence | ||
* @exception either the sequence does not exist, or 'call nextval before currval' | ||
*/ | ||
uint32_t SequenceFunctions::Currval(executor::ExecutorContext &ctx, | ||
const char *sequence_name) { | ||
PELOTON_ASSERT(sequence_name != nullptr); | ||
concurrency::TransactionContext* txn = ctx.GetTransaction(); | ||
// get the database oid for this transaction | ||
oid_t database_oid = catalog::Catalog::GetInstance() | ||
->GetDatabaseObject(ctx.GetDatabaseName(), txn)->GetDatabaseOid(); | ||
LOG_DEBUG("Get database oid: %u", database_oid); | ||
|
||
// get the sequence copy from cache | ||
auto sequence_catalog = catalog::Catalog::GetInstance() | ||
->GetSystemCatalogs(database_oid) | ||
->GetSequenceCatalog(); | ||
|
||
if(sequence_catalog->CheckCachedCurrValExistence( | ||
txn->GetTemporarySchemaName(), std::string(sequence_name))) { | ||
return sequence_catalog->GetCachedCurrVal( | ||
txn->GetTemporarySchemaName(), std::string(sequence_name)); | ||
} else { | ||
// get sequence from catalog | ||
auto sequence_object = sequence_catalog | ||
->GetSequence(database_oid, sequence_name, txn); | ||
if (sequence_object != nullptr) { | ||
throw SequenceException( | ||
StringUtil::Format("currval for sequence \"%s\" is undefined for this session", | ||
sequence_name)); | ||
} else { | ||
// sequence does not exist | ||
throw SequenceException( | ||
StringUtil::Format("Sequence \"%s\" does not exist", sequence_name)); | ||
} | ||
} | ||
} | ||
|
||
/* | ||
* @brief The wrapper function to get the incremented value for the specified sequence | ||
* @param sequence name | ||
* @param executor context | ||
* @return the result of executing NextVal | ||
*/ | ||
type::Value SequenceFunctions::_Nextval(const std::vector<type::Value> &args) { | ||
executor::ExecutorContext* ctx = (executor::ExecutorContext*)args[1].GetAs<uint64_t>(); | ||
uint32_t ret = SequenceFunctions::Nextval(*ctx, args[0].GetAs<const char *>()); | ||
return type::ValueFactory::GetIntegerValue(ret); | ||
} | ||
|
||
/* | ||
* @brief The wrapper function to get the current value for the specified sequence | ||
* @param sequence name | ||
* @param executor context | ||
* @return the result of executing CurrVal | ||
*/ | ||
type::Value SequenceFunctions::_Currval(const std::vector<type::Value> &args) { | ||
executor::ExecutorContext* ctx = (executor::ExecutorContext*)args[1].GetAs<uint64_t>(); | ||
uint32_t ret = SequenceFunctions::Currval(*ctx, args[0].GetAs<const char *>()); | ||
return type::ValueFactory::GetIntegerValue(ret); | ||
} | ||
|
||
} // namespace function | ||
} // namespace peloton |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Peloton | ||
// | ||
// string_functions_proxy.h | ||
// | ||
// Identification: src/include/codegen/proxy/string_functions_proxy.h | ||
// | ||
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#pragma once | ||
|
||
#include "codegen/proxy/proxy.h" | ||
#include "codegen/proxy/type_builder.h" | ||
#include "function/sequence_functions.h" | ||
|
||
namespace peloton { | ||
namespace codegen { | ||
|
||
PROXY(SequenceFunctions) { | ||
DECLARE_METHOD(Nextval); | ||
DECLARE_METHOD(Currval); | ||
}; | ||
|
||
} // namespace codegen | ||
} // namespace peloton |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Peloton | ||
// | ||
// sequence_functions.h | ||
// | ||
// Identification: src/include/function/sequence_functions.h | ||
// | ||
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#pragma once | ||
|
||
#include <cstdint> | ||
#include "type/value.h" | ||
|
||
namespace peloton { | ||
|
||
namespace executor { | ||
class ExecutorContext; | ||
} // namespace executor | ||
|
||
namespace function { | ||
|
||
class SequenceFunctions { | ||
public: | ||
|
||
// Nextval will return the next value of the given sequence | ||
static uint32_t Nextval(executor::ExecutorContext &ctx, const char *sequence_name); | ||
|
||
// Currval will return the current value of the given sequence | ||
static uint32_t Currval(executor::ExecutorContext &ctx, const char *sequence_name); | ||
|
||
// Wrapper function used for AddBuiltin Functions | ||
static type::Value _Nextval(const std::vector<type::Value> &args); | ||
static type::Value _Currval(const std::vector<type::Value> &args); | ||
}; | ||
|
||
} // namespace function | ||
} // namespace peloton |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters