Skip to content

Commit

Permalink
MapReduceIndexer keeps better track of lastSequenceIndexed
Browse files Browse the repository at this point in the history
Allows caller to specify the value, in case it's greater than any
sequence that was passed to the indexer (i.e. a doc was skipped.)

For couchbase/couchbase-lite-ios#1379
  • Loading branch information
snej committed Aug 16, 2016
1 parent a59fac4 commit 9c0af6b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
6 changes: 6 additions & 0 deletions C/c4View.cc
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,12 @@ struct c4Indexer : public MapReduceIndexer, c4Internal::InstanceCounted {
addIndex(&view->_index);
}

void finished() {
MapReduceIndexer::finished(_lastSequenceIndexed);
}

C4Database* _db;
sequence _lastSequenceIndexed {0};
#if C4DB_THREADSAFE
std::vector<C4View*> _views;
#endif
Expand Down Expand Up @@ -301,6 +306,7 @@ C4DocEnumerator* c4indexer_enumerateDocuments(C4Indexer *indexer, C4Error *outEr
setEnumFilter(e, [docTypes,indexer](const Document &doc,
C4DocumentFlags flags,
slice docType) {
indexer->_lastSequenceIndexed = doc.sequence();
if ((flags & kExists) && !(flags & kDeleted)
&& (!docTypes || docTypes->count(docType) > 0))
return true;
Expand Down
11 changes: 7 additions & 4 deletions CBForest/MapReduceIndex.cc
Original file line number Diff line number Diff line change
Expand Up @@ -357,11 +357,14 @@ namespace cbforest {
return false;
}

void finish(bool success) {
if (success)
void finish(sequence finalSequence) {
if (finalSequence > 0) {
index->_lastSequenceIndexed = std::max(index->_lastSequenceIndexed,
finalSequence);
index->saveState(*_transaction);
else
} else {
_transaction->abort();
}
}

private:
Expand Down Expand Up @@ -411,7 +414,7 @@ namespace cbforest {

MapReduceIndexer::~MapReduceIndexer() {
for (auto writer = _writers.begin(); writer != _writers.end(); ++writer) {
(*writer)->finish(_finished);
(*writer)->finish(_finishedSequence);
delete *writer;
}
}
Expand Down
9 changes: 5 additions & 4 deletions CBForest/MapReduceIndex.hh
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,6 @@ namespace cbforest {
/** If set, indexing will only occur if this index needs to be updated. */
void triggerOnIndex(MapReduceIndex* index) {_triggerIndex = index;}

/** Returns true if indexing completed successfully. */
void finished() {_finished = true;}

/** Determines at which sequence indexing should start.
Returns UINT64_MAX if no re-indexing is necessary. */
sequence startingSequence();
Expand Down Expand Up @@ -131,11 +128,15 @@ namespace cbforest {
key/value pairs. */
void skipDocInView(slice docID, sequence docSequence, unsigned viewNumber);

/** Call when all documents have been indexed. Pass the last sequence that was enumerated
(usually the database's lastSequence).*/
void finished(sequence seq =1) {_finishedSequence = seq;}

private:
std::vector<MapReduceIndexWriter*> _writers;
MapReduceIndex* _triggerIndex {nullptr};
sequence _latestDbSequence {0};
bool _finished {false};
sequence _finishedSequence {0};
bool _allDocTypes {false};
std::set<slice> _docTypes;

Expand Down

0 comments on commit 9c0af6b

Please sign in to comment.