private /*async*/ ObservableCollection <Dictionary <string, string> > LogQueryResultsAsync(Couchbase.Lite.View cbView) { var orderedQuery = cbView.CreateQuery(); orderedQuery.Descending = true; var llista = new ObservableCollection <Dictionary <string, string> >(); try { //var results = await orderedQuery.RunAsync (); var results = orderedQuery.Run(); Console.WriteLine("Found rows: {0}", results.Count); results.ToList().ForEach(result => { var doc = result.Document; Dictionary <string, string> aux = new Dictionary <string, string> { { "id", result.DocumentId }, { "nomCursa", doc.GetProperty <string>("nomCursa") }, { "dorsal", doc.GetProperty <string>("dorsal") }, { "posicio", doc.GetProperty <string>("posicio") }, { "distancia", doc.GetProperty <string>("distancia") }, { "posicioCategoria", doc.GetProperty <string>("posicioCategoria") }, { "categoria", doc.GetProperty <string>("categoria") }, { "club", doc.GetProperty <string>("club") }, { "iniciCursa", doc.GetProperty <string>("iniciCursa") }, { "tempsReal", doc.GetProperty <string>("tempsReal") }, { "tempsOficial", doc.GetProperty <string>("tempsOficial") }, { "iniciReal", doc.GetProperty <string>("iniciReal") }, { "horaMeta", doc.GetProperty <string>("horaMeta") }, { "ritme", doc.GetProperty <string>("ritme") }, { "km5", doc.GetProperty <string>("km5") }, { "horaKm5", doc.GetProperty <string>("horaKm5") }, { "km10", doc.GetProperty <string>("km10") }, { "horaKm10", doc.GetProperty <string>("horaKm10") }, { "tipus", doc.GetProperty <string>("tipus") }, { "esportista", doc.GetProperty <string>("esportista") } }; Console.WriteLine("Found document with id: {0}", result.DocumentId); var c = db.DocumentCount; Console.WriteLine("num DESPUES DE QUERY: " + c); llista.Add(aux); }); cbView.DeleteIndex(); Console.WriteLine("Num en la query: " + llista.Count); } catch (CouchbaseLiteException e) { Console.WriteLine("Error querying view", e.Message); } return(llista); }
/// <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(); }