diff --git a/arangod/VocBase/primary-collection.c b/arangod/VocBase/primary-collection.c index a0ba36a046..5925253252 100644 --- a/arangod/VocBase/primary-collection.c +++ b/arangod/VocBase/primary-collection.c @@ -712,6 +712,40 @@ void TRI_DebugDatafileInfoPrimaryCollection (TRI_primary_collection_t* primary) DebugDatafileInfoPrimaryCollection(primary); } +//////////////////////////////////////////////////////////////////////////////// +/// @brief iterate over all documents in the collection, using a user-defined +/// callback function. Returns the total number of documents in the collection +/// +/// The user can abort the iteration by return "false" from the callback +/// function. +/// +/// Note: the function will not acquire any locks. It is the task of the caller +/// to ensure the collection is properly locked +//////////////////////////////////////////////////////////////////////////////// + +size_t TRI_DocumentIteratorPrimaryCollection (TRI_primary_collection_t* primary, + void* data, + bool (*callback)(TRI_doc_mptr_t const*, void*)) { + if (primary->_primaryIndex._nrUsed > 0) { + void** ptr = primary->_primaryIndex._table; + void** end = ptr + primary->_primaryIndex._nrAlloc; + + for (; ptr < end; ++ptr) { + if (*ptr) { + TRI_doc_mptr_t const* d = (TRI_doc_mptr_t const*) *ptr; + + if (d->_validTo == 0) { + if (! callback(d, data)) { + break; + } + } + } + } + } + + return (size_t) primary->_primaryIndex._nrUsed; +} + //////////////////////////////////////////////////////////////////////////////// /// @} //////////////////////////////////////////////////////////////////////////////// diff --git a/arangod/VocBase/primary-collection.h b/arangod/VocBase/primary-collection.h index dd88faabe2..4311ef9287 100644 --- a/arangod/VocBase/primary-collection.h +++ b/arangod/VocBase/primary-collection.h @@ -454,6 +454,21 @@ bool TRI_CloseCompactorPrimaryCollection (TRI_primary_collection_t*, void TRI_DebugDatafileInfoPrimaryCollection (TRI_primary_collection_t*); +//////////////////////////////////////////////////////////////////////////////// +/// @brief iterate over all documents in the collection, using a user-defined +/// callback function. Returns the total number of documents in the collection +/// +/// The user can abort the iteration by return "false" from the callback +/// function. +/// +/// Note: the function will not acquire any locks. It is the task of the caller +/// to ensure the collection is properly locked +//////////////////////////////////////////////////////////////////////////////// + +size_t TRI_DocumentIteratorPrimaryCollection (TRI_primary_collection_t*, + void*, + bool (*callback)(TRI_doc_mptr_t const*, void*)); + //////////////////////////////////////////////////////////////////////////////// /// @} ////////////////////////////////////////////////////////////////////////////////