mirror of https://gitee.com/bigwinds/arangodb
fixed AqlTransaction.h
This commit is contained in:
parent
f9c14611a4
commit
ec26305a53
|
@ -28,10 +28,15 @@
|
|||
#ifndef TRIAGENS_UTILS_AQL_TRANSACTION_H
|
||||
#define TRIAGENS_UTILS_AQL_TRANSACTION_H 1
|
||||
|
||||
#include "Utils/Transaction.h"
|
||||
|
||||
struct TRI_vocbase_s;
|
||||
|
||||
namespace triagens {
|
||||
namespace arango {
|
||||
|
||||
class Transaction {
|
||||
template<typename T>
|
||||
class AqlTransaction : public Transaction<T> {
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// --SECTION-- class AqlTransaction
|
||||
|
@ -52,8 +57,9 @@ namespace triagens {
|
|||
/// @brief create the transaction
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
AqlTransaction (const TRI_vocbase_col_t* vocbase, const vector<string>& collectionNames) :
|
||||
Transaction(vocbase), _trx(0), _collectionNames(collectionNames) {
|
||||
AqlTransaction (struct TRI_vocbase_s* const vocbase,
|
||||
const vector<string>& collectionNames) :
|
||||
Transaction<T>(vocbase, new TransactionCollectionsList(vocbase, collectionNames)) {
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -61,74 +67,8 @@ namespace triagens {
|
|||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
~AqlTransaction () {
|
||||
if (_trx != 0) {
|
||||
if (_trx->_status == TRI_TRANSACTION_RUNNING) {
|
||||
// auto abort
|
||||
this->abort();
|
||||
}
|
||||
|
||||
TRI_FreeTransaction(_trx);
|
||||
_trx = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
int begin () {
|
||||
if (_trx != 0) {
|
||||
// already started
|
||||
return TRI_ERROR_TRANSACTION_INVALD_STATE;
|
||||
}
|
||||
|
||||
_trx = TRI_CreateTransaction(_vocbase->_transactionContext, TRI_TRANSACTION_READ_REPEATABLE, 0);
|
||||
if (_trx == 0) {
|
||||
return TRI_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < _collectionNames.size(); ++i) {
|
||||
if (! TRI_AddCollectionTransaction(_trx, _collectionNames[i].c_str(), TRI_TRANSACTION_READ)) {
|
||||
return TRI_ERROR_INTERNAL;
|
||||
}
|
||||
}
|
||||
|
||||
if (_trx->_status != TRI_TRANSACTION_CREATED) {
|
||||
return TRI_ERROR_TRANSACTION_INVALID_STATE;
|
||||
}
|
||||
|
||||
int res = TRI_StartTransaction(_trx);
|
||||
return res;
|
||||
}
|
||||
|
||||
int commit () {
|
||||
if (_trx == 0 || _trx->_status != TRI_TRANSACTION_RUNNING) {
|
||||
// not created or not running
|
||||
return TRI_ERROR_TRANSACTION_INVALD_STATE;
|
||||
}
|
||||
|
||||
int res = TRI_FinishTransaction(_trx);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
int abort () {
|
||||
if (_trx == 0) {
|
||||
// transaction already ended or not created
|
||||
return TRI_ERROR_NO_ERROR;
|
||||
}
|
||||
|
||||
if (_trx->_status != TRI_TRANSACTION_RUNNING) {
|
||||
return TRI_ERROR_TRANSACTION_INVALID_STATE;
|
||||
}
|
||||
|
||||
int res = TRI_AbortTransaction(_trx);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
vector<string> _collectionNames;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/// @}
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
|
|
@ -84,14 +84,6 @@ namespace triagens {
|
|||
|
||||
public:
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief create an empty list
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TransactionCollectionsList () :
|
||||
_collections() {
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief create a list with a single collection, based on the collection id
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -101,47 +93,14 @@ namespace triagens {
|
|||
TRI_transaction_type_e accessType) :
|
||||
_vocbase(vocbase),
|
||||
_collections(),
|
||||
_readOnly(true),
|
||||
_error(TRI_ERROR_NO_ERROR) {
|
||||
|
||||
addCollection(cid, accessType);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief create a list with a single collection, based on the collection name
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TransactionCollectionsList (TRI_vocbase_t* const vocbase,
|
||||
const string& name,
|
||||
TRI_transaction_type_e accessType) :
|
||||
_vocbase(vocbase),
|
||||
_collections(),
|
||||
_error(TRI_ERROR_NO_ERROR) {
|
||||
|
||||
addCollection(name, accessType);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief create a list from multiple collection ids
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TransactionCollectionsList (TRI_vocbase_t* const vocbase,
|
||||
const vector<TRI_transaction_cid_t>& readCollections,
|
||||
const vector<TRI_transaction_cid_t>& writeCollections) :
|
||||
_vocbase(vocbase),
|
||||
_collections(),
|
||||
_error(TRI_ERROR_NO_ERROR) {
|
||||
|
||||
for (size_t i = 0; i < readCollections.size(); ++i) {
|
||||
addCollection(readCollections[i], TRI_TRANSACTION_READ);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < writeCollections.size(); ++i) {
|
||||
addCollection(writeCollections[i], TRI_TRANSACTION_WRITE);
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief create a list with multiple collection names
|
||||
/// @brief create a list with multiple collection names, read/write
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TransactionCollectionsList (TRI_vocbase_t* const vocbase,
|
||||
|
@ -149,6 +108,7 @@ namespace triagens {
|
|||
const vector<string>& writeCollections) :
|
||||
_vocbase(vocbase),
|
||||
_collections(),
|
||||
_readOnly(true),
|
||||
_error(TRI_ERROR_NO_ERROR) {
|
||||
|
||||
for (size_t i = 0; i < readCollections.size(); ++i) {
|
||||
|
@ -160,6 +120,22 @@ namespace triagens {
|
|||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief create a list with multiple collection names, read-only
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TransactionCollectionsList (TRI_vocbase_t* const vocbase,
|
||||
const vector<string>& readCollections) :
|
||||
_vocbase(vocbase),
|
||||
_collections(),
|
||||
_readOnly(true),
|
||||
_error(TRI_ERROR_NO_ERROR) {
|
||||
|
||||
for (size_t i = 0; i < readCollections.size(); ++i) {
|
||||
addCollection(readCollections[i], TRI_TRANSACTION_READ);
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief destroy a collections list
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -190,7 +166,15 @@ namespace triagens {
|
|||
public:
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief get potential raised during list setup
|
||||
/// @brief whether or not the transaction is read-only
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline bool isReadOnly () const {
|
||||
return _readOnly;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief get potential error raised during list setup
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline int getError () const {
|
||||
|
@ -241,6 +225,10 @@ namespace triagens {
|
|||
|
||||
TransactionCollection* addCollection (const TRI_transaction_cid_t cid,
|
||||
TRI_transaction_type_e type) {
|
||||
if (type == TRI_TRANSACTION_WRITE) {
|
||||
_readOnly = false;
|
||||
}
|
||||
|
||||
ListType::iterator it;
|
||||
|
||||
// check if we already have the collection in our list
|
||||
|
@ -335,6 +323,12 @@ namespace triagens {
|
|||
|
||||
ListType _collections;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief whether or not the transaction is read-only
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
bool _readOnly;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief error number
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
|
Loading…
Reference in New Issue