コード例 #1
0
        /// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception>
        public virtual void TestViewReduce()
        {
            IDictionary <string, object> docProperties1 = new Dictionary <string, object>();

            docProperties1["_id"]  = "CD";
            docProperties1["cost"] = 8.99;
            PutDoc(database, docProperties1);
            IDictionary <string, object> docProperties2 = new Dictionary <string, object>();

            docProperties2["_id"]  = "App";
            docProperties2["cost"] = 1.95;
            PutDoc(database, docProperties2);
            IDictionary <string, object> docProperties3 = new Dictionary <string, object>();

            docProperties3["_id"]  = "Dessert";
            docProperties3["cost"] = 6.50;
            PutDoc(database, docProperties3);
            View view = database.GetView("totaler");

            view.SetMapReduce((document, emitter) => {
                NUnit.Framework.Assert.IsNotNull(document.Get("_id"));
                NUnit.Framework.Assert.IsNotNull(document.Get("_rev"));
                object cost = document.Get("cost");
                if (cost != null)
                {
                    emitter(document.Get("_id"), cost);
                }
            }, (IList <object> keys, IList <object> values, bool rereduce) => {
                return(View.TotalValues(values));
            }, "1");
            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("\"App\"", dumpResult[0]["key"]);
            NUnit.Framework.Assert.AreEqual("1.95", dumpResult[0]["value"]);
            NUnit.Framework.Assert.AreEqual(2, dumpResult[0]["seq"]);
            NUnit.Framework.Assert.AreEqual("\"CD\"", dumpResult[1]["key"]);
            NUnit.Framework.Assert.AreEqual("8.99", dumpResult[1]["value"]);
            NUnit.Framework.Assert.AreEqual(1, dumpResult[1]["seq"]);
            NUnit.Framework.Assert.AreEqual("\"Dessert\"", dumpResult[2]["key"]);
            NUnit.Framework.Assert.AreEqual("6.5", dumpResult[2]["value"]);
            NUnit.Framework.Assert.AreEqual(3, dumpResult[2]["seq"]);
            QueryOptions options = new QueryOptions();

            options.SetReduce(true);
            IList <QueryRow> reduced = view.QueryWithOptions(options);

            NUnit.Framework.Assert.AreEqual(1, reduced.Count);
            object value       = reduced[0].GetValue();
            Number numberValue = (Number)value;

            NUnit.Framework.Assert.IsTrue(Math.Abs(numberValue - 17.44) < 0.001);
        }
コード例 #2
0
        /// <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();
        }