From d31afb1970e5dbba273d079ffe44a5b1f5baef49 Mon Sep 17 00:00:00 2001 From: Jim Borden Date: Sat, 28 May 2016 13:33:27 +0900 Subject: [PATCH] Closes #613 --- src/Couchbase.Lite.Shared/Database.cs | 4 +-- src/Couchbase.Lite.Shared/View.cs | 17 +++++++++- src/Couchbase.Lite.Tests.Shared/ViewsTest.cs | 32 +++++++++---------- .../PeerToPeer/DatabaseMethods.cs | 2 +- .../PeerToPeer/ViewMethods.cs | 4 +-- 5 files changed, 37 insertions(+), 22 deletions(-) diff --git a/src/Couchbase.Lite.Shared/Database.cs b/src/Couchbase.Lite.Shared/Database.cs index 0947e258b..6ae3ba118 100644 --- a/src/Couchbase.Lite.Shared/Database.cs +++ b/src/Couchbase.Lite.Shared/Database.cs @@ -1122,7 +1122,7 @@ internal IEnumerable 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); @@ -1130,7 +1130,7 @@ internal IEnumerable QueryViewNamed(string viewName, QueryOptions opti lastIndexedSequence = view.LastSequenceIndexed; } else if(options.Stale == IndexUpdateMode.After && lastIndexedSequence <= GetLastSequenceNumber()) { - RunAsync(d => view.UpdateIndex()); + RunAsync(d => view.UpdateIndex_Internal()); } lastChangedSequence = view.LastSequenceChangedAt; diff --git a/src/Couchbase.Lite.Shared/View.cs b/src/Couchbase.Lite.Shared/View.cs index a155f48ea..6baa0fcb6 100644 --- a/src/Couchbase.Lite.Shared/View.cs +++ b/src/Couchbase.Lite.Shared/View.cs @@ -263,6 +263,21 @@ public void DeleteIndex() Storage.DeleteIndex(); } + /// + /// Updates the '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. + /// + public void UpdateIndex() + { + var status = UpdateIndex_Internal(); + if(status.IsError) { + Log.To.View.W(TAG, "Error updating index of {0} ({1})", Name, status); + } + } + /// /// Deletes the . /// @@ -295,7 +310,7 @@ internal void Close() Database = null; } - internal Status UpdateIndex() + internal Status UpdateIndex_Internal() { var status = new Status(StatusCode.Unknown); try { diff --git a/src/Couchbase.Lite.Tests.Shared/ViewsTest.cs b/src/Couchbase.Lite.Tests.Shared/ViewsTest.cs index 180535247..1b40a55d7 100644 --- a/src/Couchbase.Lite.Tests.Shared/ViewsTest.cs +++ b/src/Couchbase.Lite.Tests.Shared/ViewsTest.cs @@ -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(); @@ -207,7 +207,7 @@ public void TestPrefixMatchingArray() emit(new List { 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(); @@ -633,7 +633,7 @@ public void TestViewIndex() //Assert.AreEqual(1, view.Id); Assert.IsTrue(view.IsStale); - view.UpdateIndex(); + view.UpdateIndex_Internal(); IList> dumpResult = view.Storage.Dump().ToList(); WriteDebug("View dump: " + dumpResult); @@ -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); @@ -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); @@ -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); @@ -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(); @@ -841,7 +841,7 @@ public void TestViewQueryWithDictSentinel() var view = CreateView(database); - view.UpdateIndex(); + view.UpdateIndex_Internal(); // Query all rows: QueryOptions options = new QueryOptions(); @@ -1150,7 +1150,7 @@ public void TestViewReduce() }, BuiltinReduceFunctions.Sum, "1"); - view.UpdateIndex(); + view.UpdateIndex_Internal(); IList> dumpResult = view.Storage.Dump().ToList(); WriteDebug("View dump: " + dumpResult); @@ -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; @@ -1427,7 +1427,7 @@ public void TestViewGroupedStrings() } }, BuiltinReduceFunctions.Sum, "1.0"); - view.UpdateIndex(); + view.UpdateIndex_Internal(); QueryOptions options = new QueryOptions(); options.GroupLevel = 1; @@ -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(); @@ -1638,7 +1638,7 @@ public virtual void TestViewLinkedDocs() } }, null, "1.0"); - view.UpdateIndex(); + view.UpdateIndex_Internal(); QueryOptions options = new QueryOptions(); options.IncludeDocs = true; @@ -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()); } @@ -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"; @@ -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. diff --git a/src/ListenerComponent/Couchbase.Lite.Listener.Shared/PeerToPeer/DatabaseMethods.cs b/src/ListenerComponent/Couchbase.Lite.Listener.Shared/PeerToPeer/DatabaseMethods.cs index b92526ecb..fb72af6c6 100644 --- a/src/ListenerComponent/Couchbase.Lite.Listener.Shared/PeerToPeer/DatabaseMethods.cs +++ b/src/ListenerComponent/Couchbase.Lite.Listener.Shared/PeerToPeer/DatabaseMethods.cs @@ -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; diff --git a/src/ListenerComponent/Couchbase.Lite.Listener.Shared/PeerToPeer/ViewMethods.cs b/src/ListenerComponent/Couchbase.Lite.Listener.Shared/PeerToPeer/ViewMethods.cs index bb0ae9229..65f5ca4a2 100644 --- a/src/ListenerComponent/Couchbase.Lite.Listener.Shared/PeerToPeer/ViewMethods.cs +++ b/src/ListenerComponent/Couchbase.Lite.Listener.Shared/PeerToPeer/ViewMethods.cs @@ -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: