public View GetExistingView(string name) { View view = null; if (views != null) { view = views.Get(name); } if (view != null) { return view; } view = new View(this, name); if (view.GetViewId() == 0) { return null; } return RegisterView(view); }
/// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception> public virtual void TestViewIndex() { int numTimesMapFunctionInvoked = 0; IDictionary <string, object> dict1 = new Dictionary <string, object>(); dict1["key"] = "one"; IDictionary <string, object> dict2 = new Dictionary <string, object>(); dict2["key"] = "two"; IDictionary <string, object> dict3 = new Dictionary <string, object>(); dict3["key"] = "three"; IDictionary <string, object> dictX = new Dictionary <string, object>(); dictX["clef"] = "quatre"; RevisionInternal rev1 = PutDoc(database, dict1); RevisionInternal rev2 = PutDoc(database, dict2); RevisionInternal rev3 = PutDoc(database, dict3); PutDoc(database, dictX); View view = database.GetView("aview"); var numTimesInvoked = 0; MapDelegate mapBlock = (IDictionary <string, object> document, EmitDelegate emitter) => { numTimesInvoked += 1; NUnit.Framework.Assert.IsNotNull(document["_id"]); NUnit.Framework.Assert.IsNotNull(document["_rev"]); if (document["key"] != null) { emitter(document["key"], null); } }; view.SetMap(mapBlock, "1"); NUnit.Framework.Assert.AreEqual(1, view.GetViewId()); NUnit.Framework.Assert.IsTrue(view.IsStale()); view.UpdateIndex(); IList <IDictionary <string, object> > dumpResult = view.Dump(); Log.V(Tag, "View dump: " + dumpResult); NUnit.Framework.Assert.AreEqual(3, dumpResult.Count); NUnit.Framework.Assert.AreEqual("\"one\"", dumpResult[0]["key"]); NUnit.Framework.Assert.AreEqual(1, dumpResult[0]["seq"]); NUnit.Framework.Assert.AreEqual("\"two\"", dumpResult[2]["key"]); NUnit.Framework.Assert.AreEqual(2, dumpResult[2]["seq"]); NUnit.Framework.Assert.AreEqual("\"three\"", dumpResult[1]["key"]); NUnit.Framework.Assert.AreEqual(3, dumpResult[1]["seq"]); //no-op reindex NUnit.Framework.Assert.IsFalse(view.IsStale()); view.UpdateIndex(); // Now add a doc and update a doc: RevisionInternal threeUpdated = new RevisionInternal(rev3.GetDocId(), rev3.GetRevId (), false, database); numTimesMapFunctionInvoked = mapBlock.GetNumTimesInvoked(); IDictionary <string, object> newdict3 = new Dictionary <string, object>(); newdict3["key"] = "3hree"; threeUpdated.SetProperties(newdict3); Status status = new Status(); rev3 = database.PutRevision(threeUpdated, rev3.GetRevId(), false, status); NUnit.Framework.Assert.IsTrue(status.IsSuccessful()); // Reindex again: NUnit.Framework.Assert.IsTrue(view.IsStale()); view.UpdateIndex(); // Make sure the map function was only invoked one more time (for the document that was added) NUnit.Framework.Assert.AreEqual(mapBlock.GetNumTimesInvoked(), numTimesMapFunctionInvoked + 1); IDictionary <string, object> dict4 = new Dictionary <string, object>(); dict4["key"] = "four"; RevisionInternal rev4 = PutDoc(database, dict4); RevisionInternal twoDeleted = new RevisionInternal(rev2.GetDocId(), rev2.GetRevId (), true, database); database.PutRevision(twoDeleted, rev2.GetRevId(), false, status); NUnit.Framework.Assert.IsTrue(status.IsSuccessful()); // Reindex again: NUnit.Framework.Assert.IsTrue(view.IsStale()); view.UpdateIndex(); dumpResult = view.Dump(); Log.V(Tag, "View dump: " + dumpResult); NUnit.Framework.Assert.AreEqual(3, dumpResult.Count); NUnit.Framework.Assert.AreEqual("\"one\"", dumpResult[2]["key"]); NUnit.Framework.Assert.AreEqual(1, dumpResult[2]["seq"]); NUnit.Framework.Assert.AreEqual("\"3hree\"", dumpResult[0]["key"]); NUnit.Framework.Assert.AreEqual(5, dumpResult[0]["seq"]); NUnit.Framework.Assert.AreEqual("\"four\"", dumpResult[1]["key"]); NUnit.Framework.Assert.AreEqual(6, dumpResult[1]["seq"]); // Now do a real query: IList <QueryRow> rows = view.QueryWithOptions(null); NUnit.Framework.Assert.AreEqual(3, rows.Count); NUnit.Framework.Assert.AreEqual("one", rows[2].Key); NUnit.Framework.Assert.AreEqual(rev1.GetDocId(), rows[2].DocumentId); NUnit.Framework.Assert.AreEqual("3hree", rows[0].Key); NUnit.Framework.Assert.AreEqual(rev3.GetDocId(), rows[0].DocumentId); NUnit.Framework.Assert.AreEqual("four", rows[1].Key); NUnit.Framework.Assert.AreEqual(rev4.GetDocId(), rows[1].DocumentId); view.DeleteIndex(); }