Skip to content

Commit

Permalink
Closes #613
Browse files Browse the repository at this point in the history
  • Loading branch information
borrrden committed May 28, 2016
1 parent cfc2103 commit d31afb1
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/Couchbase.Lite.Shared/Database.cs
Expand Up @@ -1122,15 +1122,15 @@ internal IEnumerable<QueryRow> QueryViewNamed(string viewName, QueryOptions opti

lastIndexedSequence = view.LastSequenceIndexed;
if(options.Stale == IndexUpdateMode.Before || lastIndexedSequence <= 0) {
var status = view.UpdateIndex();
var status = view.UpdateIndex_Internal();
if(status.IsError) {
throw Misc.CreateExceptionAndLog(Log.To.Query, status.Code, TAG,
"Failed to update index for `{0}`: {1}, ", viewName, status.Code);
}

lastIndexedSequence = view.LastSequenceIndexed;
} else if(options.Stale == IndexUpdateMode.After && lastIndexedSequence <= GetLastSequenceNumber()) {
RunAsync(d => view.UpdateIndex());
RunAsync(d => view.UpdateIndex_Internal());
}

lastChangedSequence = view.LastSequenceChangedAt;
Expand Down
17 changes: 16 additions & 1 deletion src/Couchbase.Lite.Shared/View.cs
Expand Up @@ -263,6 +263,21 @@ public void DeleteIndex()
Storage.DeleteIndex();
}

/// <summary>
/// Updates the <see cref="Couchbase.Lite.View"/>'s persistent index. Indexing
/// scans all documents that have changed since the last time the index was updated.
/// The body of each document is passed to the view's map callback, and any emitted
/// rows are added to the index. Any existing rows previously emitted by those documents,
/// that weren't re-emitted this time, are removed.
/// </summary>
public void UpdateIndex()
{
var status = UpdateIndex_Internal();
if(status.IsError) {
Log.To.View.W(TAG, "Error updating index of {0} ({1})", Name, status);
}
}

/// <summary>
/// Deletes the <see cref="Couchbase.Lite.View"/>.
/// </summary>
Expand Down Expand Up @@ -295,7 +310,7 @@ internal void Close()
Database = null;
}

internal Status UpdateIndex()
internal Status UpdateIndex_Internal()
{
var status = new Status(StatusCode.Unknown);
try {
Expand Down
32 changes: 16 additions & 16 deletions src/Couchbase.Lite.Tests.Shared/ViewsTest.cs
Expand Up @@ -163,7 +163,7 @@ public void TestPrefixMatchingString()
{
PutDocs(database);
var view = CreateView(database);
Assert.AreEqual(StatusCode.Ok, view.UpdateIndex().Code);
Assert.AreEqual(StatusCode.Ok, view.UpdateIndex_Internal().Code);

// Keys with prefix "f":
var options = new QueryOptions();
Expand Down Expand Up @@ -207,7 +207,7 @@ public void TestPrefixMatchingArray()
emit(new List<object> { doc.Get("key"), Int32.Parse(i) / 100 }, null);
}, "1");

Assert.AreEqual(StatusCode.Ok, view.UpdateIndex().Code);
Assert.AreEqual(StatusCode.Ok, view.UpdateIndex_Internal().Code);

// Keys starting with "one":
var options = new QueryOptions();
Expand Down Expand Up @@ -633,7 +633,7 @@ public void TestViewIndex()

//Assert.AreEqual(1, view.Id);
Assert.IsTrue(view.IsStale);
view.UpdateIndex();
view.UpdateIndex_Internal();

IList<IDictionary<string, object>> dumpResult = view.Storage.Dump().ToList();
WriteDebug("View dump: " + dumpResult);
Expand All @@ -647,7 +647,7 @@ public void TestViewIndex()

//no-op reindex
Assert.IsFalse(view.IsStale);
view.UpdateIndex();
view.UpdateIndex_Internal();

// Now add a doc and update a doc:
var threeUpdated = new RevisionInternal(rev3.DocID, rev3.RevID, false);
Expand All @@ -661,7 +661,7 @@ public void TestViewIndex()

// Reindex again:
Assert.IsTrue(view.IsStale);
view.UpdateIndex();
view.UpdateIndex_Internal();

// Make sure the map function was only invoked one more time (for the document that was added)
Assert.AreEqual(numTimesMapFunctionInvoked + 1, numTimesInvoked);
Expand All @@ -674,7 +674,7 @@ public void TestViewIndex()

// Reindex again:
Assert.IsTrue(view.IsStale);
view.UpdateIndex();
view.UpdateIndex_Internal();
dumpResult = view.Storage.Dump().ToList();
WriteDebug("View dump: " + dumpResult);
Assert.AreEqual(3, dumpResult.Count);
Expand Down Expand Up @@ -703,7 +703,7 @@ public void TestViewQuery()
{
PutDocs(database);
var view = CreateView(database);
view.UpdateIndex();
view.UpdateIndex_Internal();

// Query all rows:
QueryOptions options = new QueryOptions();
Expand Down Expand Up @@ -841,7 +841,7 @@ public void TestViewQueryWithDictSentinel()

var view = CreateView(database);

view.UpdateIndex();
view.UpdateIndex_Internal();

// Query all rows:
QueryOptions options = new QueryOptions();
Expand Down Expand Up @@ -1150,7 +1150,7 @@ public void TestViewReduce()
}, BuiltinReduceFunctions.Sum, "1");


view.UpdateIndex();
view.UpdateIndex_Internal();

IList<IDictionary<string, object>> dumpResult = view.Storage.Dump().ToList();
WriteDebug("View dump: " + dumpResult);
Expand Down Expand Up @@ -1270,7 +1270,7 @@ public void TestViewGrouped()
emitter(key, document["time"]);
}, BuiltinReduceFunctions.Sum, "1");

view.UpdateIndex();
view.UpdateIndex_Internal();
QueryOptions options = new QueryOptions();
options.Reduce = true;

Expand Down Expand Up @@ -1427,7 +1427,7 @@ public void TestViewGroupedStrings()
}
}, BuiltinReduceFunctions.Sum, "1.0");

view.UpdateIndex();
view.UpdateIndex_Internal();
QueryOptions options = new QueryOptions();
options.GroupLevel = 1;

Expand Down Expand Up @@ -1606,7 +1606,7 @@ public virtual void TestLargerViewQuery()
{
PutNDocs(database, 4);
View view = CreateView(database);
view.UpdateIndex();
view.UpdateIndex_Internal();

// Query all rows:
QueryOptions options = new QueryOptions();
Expand Down Expand Up @@ -1638,7 +1638,7 @@ public virtual void TestViewLinkedDocs()
}
}, null, "1.0");

view.UpdateIndex();
view.UpdateIndex_Internal();
QueryOptions options = new QueryOptions();
options.IncludeDocs = true;

Expand Down Expand Up @@ -1786,7 +1786,7 @@ public void TestViewIndexSkipsDesignDocs()
};
PutDoc(database, designDoc);

view.UpdateIndex();
view.UpdateIndex_Internal();
var rows = view.QueryWithOptions(null);
Assert.AreEqual(0, rows.Count());
}
Expand Down Expand Up @@ -1833,7 +1833,7 @@ public void TestViewQueryStartKeyDocID()
};
result.Add(PutDoc(database, dict));
var view = CreateView(database);
view.UpdateIndex();
view.UpdateIndex_Internal();

var options = new QueryOptions();
options.StartKey = "one";
Expand Down Expand Up @@ -1911,7 +1911,7 @@ public void TestViewQueryStartKeyDocID()
Assert.IsNotNull(rev2b);

// re-run query
view.UpdateIndex();
view.UpdateIndex_Internal();
rows = view.CreateQuery().Run();

// we should only see one row, with key=3.
Expand Down
Expand Up @@ -580,7 +580,7 @@ public static ICouchbaseResponseState ExecuteTemporaryViewFunction(ICouchbaseLis
}
try {
view.UpdateIndex();
view.UpdateIndex_Internal();
return QueryView(context, null, view, options);
} catch(CouchbaseLiteException e) {
response.InternalStatus = e.CBLStatus.Code;
Expand Down
Expand Up @@ -70,9 +70,9 @@ private static CouchbaseLiteResponse QueryDesignDocument(ICouchbaseListenerConte
}
if(options.Stale == IndexUpdateMode.Before || view.LastSequenceIndexed <= 0) {
view.UpdateIndex();
view.UpdateIndex_Internal();
} else if(options.Stale == IndexUpdateMode.After && view.LastSequenceIndexed < db.GetLastSequenceNumber()) {
db.RunAsync(_ => view.UpdateIndex());
db.RunAsync(_ => view.UpdateIndex_Internal());
}
// Check for conditional GET and set response Etag header:
Expand Down

0 comments on commit d31afb1

Please sign in to comment.