mirror of https://gitee.com/bigwinds/arangodb
refactored and simplified document CRUD methods
This commit is contained in:
parent
6e22aef697
commit
d9f34fd88e
|
@ -1021,8 +1021,13 @@ bool RestDocumentHandler::deleteDocument () {
|
|||
|
||||
WriteTransaction trx(&ca);
|
||||
|
||||
TRI_doc_operation_context_t context;
|
||||
TRI_InitContextPrimaryCollection(&context, trx.primary(), policy, forceSync);
|
||||
context._expectedRid = revision;
|
||||
context._previousRid = &rid;
|
||||
|
||||
// unlocking is performed in destroy()
|
||||
res = trx.primary()->destroy(trx.primary(), (TRI_voc_key_t) key.c_str(), revision, &rid, policy, false, forceSync);
|
||||
res = trx.primary()->destroy(&context, (TRI_voc_key_t) key.c_str());
|
||||
|
||||
trx.end();
|
||||
|
||||
|
|
|
@ -722,15 +722,24 @@ static v8::Handle<v8::Value> ReplaceVocbaseCol (TRI_vocbase_t* vocbase,
|
|||
forceSync = TRI_ObjectToBoolean(argv[3]);
|
||||
}
|
||||
|
||||
TRI_voc_rid_t oldRid = 0;
|
||||
|
||||
TRI_doc_operation_context_t context;
|
||||
TRI_InitContextPrimaryCollection(&context, primary, policy, forceSync);
|
||||
context._expectedRid = rid;
|
||||
context._previousRid = &oldRid;
|
||||
context._release = true;
|
||||
|
||||
// .............................................................................
|
||||
// inside a write transaction
|
||||
// .............................................................................
|
||||
|
||||
primary->beginWrite(primary);
|
||||
|
||||
TRI_voc_rid_t oldRid = 0;
|
||||
TRI_doc_mptr_t mptr = primary->update(primary, shaped, key, rid, &oldRid, policy, true, forceSync);
|
||||
if (key) TRI_FreeString(TRI_CORE_MEM_ZONE, key);
|
||||
TRI_doc_mptr_t mptr = primary->update(&context, shaped, key);
|
||||
if (key) {
|
||||
TRI_FreeString(TRI_CORE_MEM_ZONE, key);
|
||||
}
|
||||
|
||||
// .............................................................................
|
||||
// outside a write transaction
|
||||
|
@ -1240,9 +1249,15 @@ static v8::Handle<v8::Value> DeleteVocbaseCol (TRI_vocbase_t* vocbase,
|
|||
|
||||
TRI_primary_collection_t* primary = collection->_collection;
|
||||
TRI_voc_rid_t oldRid;
|
||||
|
||||
TRI_doc_operation_context_t context;
|
||||
TRI_InitContextPrimaryCollection(&context, primary, policy, forceSync);
|
||||
context._release = true;
|
||||
context._expectedRid = rid;
|
||||
context._previousRid = &oldRid;
|
||||
|
||||
primary->beginWrite(primary);
|
||||
int res = primary->destroy(primary, key, rid, &oldRid, policy, true, forceSync);
|
||||
int res = primary->destroy(&context, key);
|
||||
if (key) {
|
||||
TRI_FreeString(TRI_CORE_MEM_ZONE, key);
|
||||
}
|
||||
|
@ -4785,6 +4800,9 @@ static v8::Handle<v8::Value> JS_TruncateVocbaseCol (v8::Arguments const& argv) {
|
|||
|
||||
TRI_doc_mptr_t const** ptr;
|
||||
TRI_doc_mptr_t const** end;
|
||||
|
||||
TRI_doc_operation_context_t context;
|
||||
TRI_InitContextPrimaryCollection(&context, primary, policy, forceSync);
|
||||
|
||||
primary->beginWrite(collection->_collection);
|
||||
|
||||
|
@ -4797,12 +4815,15 @@ static v8::Handle<v8::Value> JS_TruncateVocbaseCol (v8::Arguments const& argv) {
|
|||
TRI_PushBackVectorPointer(&documents, (void*) *ptr);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// now delete all documents. this will modify the index as well
|
||||
for (size_t i = 0; i < documents._length; ++i) {
|
||||
TRI_doc_mptr_t const* d = (TRI_doc_mptr_t const*) documents._buffer[i];
|
||||
|
||||
context._expectedRid = d->_rid;
|
||||
context._previousRid = &oldRid;
|
||||
|
||||
int res = primary->destroy(primary, d->_key, d->_rid, &oldRid, policy, false, forceSync);
|
||||
int res = primary->destroy(&context, d->_key);
|
||||
if (res != TRI_ERROR_NO_ERROR) {
|
||||
// an error occurred, but we simply go on because truncate should remove all documents
|
||||
}
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -119,6 +119,37 @@ static TRI_doc_collection_info_t* Figures (TRI_primary_collection_t* primary) {
|
|||
return info;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief size of a primary collection
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
static TRI_voc_size_t Count (TRI_primary_collection_t* primary) {
|
||||
TRI_doc_mptr_t const* mptr;
|
||||
TRI_voc_size_t result;
|
||||
void** end;
|
||||
void** ptr;
|
||||
|
||||
TRI_READ_LOCK_DOCUMENTS_INDEXES_PRIMARY_COLLECTION(primary);
|
||||
|
||||
ptr = primary->_primaryIndex._table;
|
||||
end = ptr + primary->_primaryIndex._nrAlloc;
|
||||
result = 0;
|
||||
|
||||
for (; ptr < end; ++ptr) {
|
||||
if (*ptr != NULL) {
|
||||
mptr = *ptr;
|
||||
|
||||
if (mptr->_deletion == 0) {
|
||||
++result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TRI_READ_UNLOCK_DOCUMENTS_INDEXES_PRIMARY_COLLECTION(primary);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief hashs a datafile identifier
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -389,6 +420,7 @@ void TRI_InitPrimaryCollection (TRI_primary_collection_t* collection,
|
|||
collection->_capConstraint = NULL;
|
||||
|
||||
collection->figures = Figures;
|
||||
collection->size = Count;
|
||||
|
||||
TRI_InitBarrierList(&collection->_barrierList, collection);
|
||||
|
||||
|
@ -560,11 +592,11 @@ void TRI_InitContextPrimaryCollection (TRI_doc_operation_context_t* const contex
|
|||
context->_collection = collection;
|
||||
context->_policy = policy;
|
||||
context->_expectedRid = 0;
|
||||
context->_previousRid = 0;
|
||||
context->_previousRid = NULL;
|
||||
context->_lock = false;
|
||||
context->_release = false;
|
||||
context->_sync = forceSync || collection->base._waitForSync;
|
||||
context->_allowRollback = false;
|
||||
context->_allowRollback = true;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
|
|
@ -108,18 +108,22 @@ typedef enum {
|
|||
TRI_doc_update_policy_e;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief typedef for arbitrary operation parameters
|
||||
/// @brief typedef for arbitrary collection operation parameters
|
||||
/// the context controls behavior such as revision check, locking/unlocking
|
||||
///
|
||||
/// the context struct needs to be passed as a parameter for CRUD operations
|
||||
/// this makes function signatures a lot easier
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
typedef struct TRI_doc_operation_context_s {
|
||||
struct TRI_primary_collection_s* _collection;
|
||||
TRI_doc_update_policy_e _policy;
|
||||
TRI_voc_rid_t _expectedRid;
|
||||
TRI_voc_rid_t* _previousRid;
|
||||
bool _lock : 1;
|
||||
bool _release : 1;
|
||||
bool _sync : 1;
|
||||
bool _allowRollback : 1;
|
||||
struct TRI_primary_collection_s* _collection; // collection to be used
|
||||
TRI_doc_update_policy_e _policy; // the update policy
|
||||
TRI_voc_rid_t _expectedRid; // the expected revision id of a document. only used if set and for update/delete
|
||||
TRI_voc_rid_t* _previousRid; // a variable that the previous revsion id found in the database will be pushed into. only used if set and for update/delete
|
||||
bool _release : 1; // release the write lock after the operation
|
||||
bool _sync : 1; // force syncing to disk after successful operation
|
||||
bool _allowRollback : 1; // allow rollback of operation. this is normally true except for contexts created by rollback operations
|
||||
bool _lock : 1; // currently unused
|
||||
}
|
||||
TRI_doc_operation_context_t;
|
||||
|
||||
|
@ -337,11 +341,10 @@ typedef struct TRI_primary_collection_s {
|
|||
TRI_doc_mptr_t (*createJson) (struct TRI_doc_operation_context_s*, TRI_df_marker_type_e, TRI_json_t const*, void const*);
|
||||
TRI_doc_mptr_t (*read) (struct TRI_primary_collection_s*, TRI_voc_key_t);
|
||||
|
||||
TRI_doc_mptr_t (*update) (struct TRI_primary_collection_s*, TRI_shaped_json_t const*, TRI_voc_key_t, TRI_voc_rid_t, TRI_voc_rid_t*, TRI_doc_update_policy_e, bool, bool);
|
||||
TRI_doc_mptr_t (*update) (struct TRI_doc_operation_context_s*, TRI_shaped_json_t const*, TRI_voc_key_t);
|
||||
TRI_doc_mptr_t (*updateJson) (struct TRI_doc_operation_context_s*, TRI_json_t const*, TRI_voc_key_t);
|
||||
// , TRI_voc_rid_t, TRI_voc_rid_t*, TRI_doc_update_policy_e, bool, bool);
|
||||
|
||||
int (*destroy) (struct TRI_primary_collection_s* collection, TRI_voc_key_t, TRI_voc_rid_t, TRI_voc_rid_t*, TRI_doc_update_policy_e, bool, bool);
|
||||
int (*destroy) (struct TRI_doc_operation_context_s*, TRI_voc_key_t);
|
||||
|
||||
TRI_doc_collection_info_t* (*figures) (struct TRI_primary_collection_s* collection);
|
||||
TRI_voc_size_t (*size) (struct TRI_primary_collection_s* collection);
|
||||
|
|
Loading…
Reference in New Issue