1
0
Fork 0
arangodb/arangod/Utils/Transaction.h

378 lines
13 KiB
C++

////////////////////////////////////////////////////////////////////////////////
/// @brief base transaction wrapper
///
/// @file
///
/// DISCLAIMER
///
/// Copyright 2004-2012 triAGENS GmbH, Cologne, Germany
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Copyright holder is triAGENS GmbH, Cologne, Germany
///
/// @author Jan Steemann
/// @author Copyright 2011-2012, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////
#ifndef TRIAGENS_UTILS_TRANSACTION_H
#define TRIAGENS_UTILS_TRANSACTION_H 1
#include "VocBase/transaction.h"
#include "VocBase/vocbase.h"
namespace triagens {
namespace arango {
// -----------------------------------------------------------------------------
// --SECTION-- class Transaction
// -----------------------------------------------------------------------------
template<bool E>
class Transaction {
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup ArangoDB
/// @{
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/// @brief Transaction
////////////////////////////////////////////////////////////////////////////////
private:
Transaction (const Transaction&);
Transaction& operator= (const Transaction&);
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------------------
// --SECTION-- constructors and destructors
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup ArangoDB
/// @{
////////////////////////////////////////////////////////////////////////////////
public:
////////////////////////////////////////////////////////////////////////////////
/// @brief create the transaction
////////////////////////////////////////////////////////////////////////////////
Transaction (TRI_vocbase_t* const vocbase,
TRI_transaction_t* previousTrx,
const string& trxName) :
_vocbase(vocbase),
_trx(previousTrx),
_embedded(previousTrx != 0),
_trxName(trxName) {
assert(_vocbase != 0);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief destroy the transaction
////////////////////////////////////////////////////////////////////////////////
virtual ~Transaction () {
if (_trx != 0 && ! isEmbedded()) {
TRI_FreeTransaction(_trx);
}
}
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------------------
// --SECTION-- public methods
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup ArangoDB
/// @{
////////////////////////////////////////////////////////////////////////////////
public:
////////////////////////////////////////////////////////////////////////////////
/// @brief whether or not the transaction is embedded
////////////////////////////////////////////////////////////////////////////////
inline bool isEmbedded () const {
return _embedded;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief return the name of the transaction
////////////////////////////////////////////////////////////////////////////////
inline string name () const {
return _trxName;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief get the status of the transaction
////////////////////////////////////////////////////////////////////////////////
inline TRI_transaction_status_e status () const {
if (_trx != 0) {
return _trx->_status;
}
return TRI_TRANSACTION_UNDEFINED;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief dump the transaction
////////////////////////////////////////////////////////////////////////////////
void dump () {
if (_trx != 0) {
TRI_DumpTransaction(_trx);
}
}
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------------------
// --SECTION-- virtual protected methods
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup ArangoDB
/// @{
////////////////////////////////////////////////////////////////////////////////
protected:
////////////////////////////////////////////////////////////////////////////////
/// @brief use all collection
////////////////////////////////////////////////////////////////////////////////
virtual int useCollections () = 0;
////////////////////////////////////////////////////////////////////////////////
/// @brief add all collections to the transaction
////////////////////////////////////////////////////////////////////////////////
virtual int addCollections () = 0;
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------------------
// --SECTION-- "final" public methods
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup ArangoDB
/// @{
////////////////////////////////////////////////////////////////////////////////
public:
////////////////////////////////////////////////////////////////////////////////
/// @brief begin the transaction
////////////////////////////////////////////////////////////////////////////////
int begin () {
int res;
if (isEmbedded()) {
if (! E) {
return TRI_ERROR_TRANSACTION_NESTED;
}
res = addCollections();
return res;
}
if (_trx != 0) {
// already started
return TRI_ERROR_TRANSACTION_INVALID_STATE;
}
// register usage of the underlying collections
res = useCollections();
if (res != TRI_ERROR_NO_ERROR) {
return res;
}
_trx = TRI_CreateTransaction(_vocbase->_transactionContext, TRI_TRANSACTION_READ_REPEATABLE, false); // TODO: fix this
if (_trx == 0) {
return TRI_ERROR_OUT_OF_MEMORY;
}
res = addCollections();
if (res != TRI_ERROR_NO_ERROR) {
return res;
}
if (status() != TRI_TRANSACTION_CREATED) {
return TRI_ERROR_TRANSACTION_INVALID_STATE;
}
return TRI_StartTransaction(_trx);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief commit / finish the transaction
////////////////////////////////////////////////////////////////////////////////
int commit () {
if (_trx == 0 || status() != TRI_TRANSACTION_RUNNING) {
// not created or not running
return TRI_ERROR_TRANSACTION_INVALID_STATE;
}
if (isEmbedded()) {
return TRI_ERROR_NO_ERROR;
}
int res;
if (_trx->_type == TRI_TRANSACTION_READ) {
res = TRI_FinishTransaction(_trx);
}
else {
res = TRI_CommitTransaction(_trx);
}
return res;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief abort the transaction
////////////////////////////////////////////////////////////////////////////////
int abort () {
if (_trx == 0) {
// transaction already ended or not created
return TRI_ERROR_NO_ERROR;
}
if (status() != TRI_TRANSACTION_RUNNING) {
return TRI_ERROR_TRANSACTION_INVALID_STATE;
}
int res = TRI_AbortTransaction(_trx);
return res;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief finish a transaction, based on the previous state
////////////////////////////////////////////////////////////////////////////////
int finish (const int errorNumber) {
if (_trx == 0) {
// transaction already ended or not created
return TRI_ERROR_NO_ERROR;
}
if (status() != TRI_TRANSACTION_RUNNING) {
return TRI_ERROR_TRANSACTION_INVALID_STATE;
}
int res;
if (errorNumber == TRI_ERROR_NO_ERROR) {
// there was no previous error, so we'll commit
res = commit();
}
else {
// there was a previous error, so we'll abort
this->abort();
// return original error number
res = errorNumber;
}
return res;
}
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------------------
// --SECTION-- protected variables
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup ArangoDB
/// @{
////////////////////////////////////////////////////////////////////////////////
protected:
////////////////////////////////////////////////////////////////////////////////
/// @brief the vocbase
////////////////////////////////////////////////////////////////////////////////
TRI_vocbase_t* const _vocbase;
////////////////////////////////////////////////////////////////////////////////
/// @brief the actual transaction
////////////////////////////////////////////////////////////////////////////////
TRI_transaction_t* _trx;
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------------------
// --SECTION-- private variables
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup ArangoDB
/// @{
////////////////////////////////////////////////////////////////////////////////
private:
////////////////////////////////////////////////////////////////////////////////
/// @brief whether or not the current transaction is embedded
////////////////////////////////////////////////////////////////////////////////
const bool _embedded;
////////////////////////////////////////////////////////////////////////////////
/// @brief transaction name
////////////////////////////////////////////////////////////////////////////////
string _trxName;
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////
};
}
}
#endif
// Local Variables:
// mode: outline-minor
// outline-regexp: "^\\(/// @brief\\|/// {@inheritDoc}\\|/// @addtogroup\\|/// @page\\|// --SECTION--\\|/// @\\}\\)"
// End: