示例#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 TestViewGroupedStrings()
        {
            IDictionary <string, object> docProperties1 = new Dictionary <string, object>();

            docProperties1["name"] = "Alice";
            PutDoc(database, docProperties1);
            IDictionary <string, object> docProperties2 = new Dictionary <string, object>();

            docProperties2["name"] = "Albert";
            PutDoc(database, docProperties2);
            IDictionary <string, object> docProperties3 = new Dictionary <string, object>();

            docProperties3["name"] = "Naomi";
            PutDoc(database, docProperties3);
            IDictionary <string, object> docProperties4 = new Dictionary <string, object>();

            docProperties4["name"] = "Jens";
            PutDoc(database, docProperties4);
            IDictionary <string, object> docProperties5 = new Dictionary <string, object>();

            docProperties5["name"] = "Jed";
            PutDoc(database, docProperties5);
            View view = database.GetView("default/names");

            view.SetMapReduce(new _Mapper_852(), new _Reducer_862(), "1.0");
            view.UpdateIndex();
            QueryOptions options = new QueryOptions();

            options.SetGroupLevel(1);
            IList <QueryRow> rows = view.QueryWithOptions(options);
            IList <IDictionary <string, object> > expectedRows = new AList <IDictionary <string, object
                                                                                         > >();
            IDictionary <string, object> row1 = new Dictionary <string, object>();

            row1["key"]   = "A";
            row1["value"] = 2;
            expectedRows.AddItem(row1);
            IDictionary <string, object> row2 = new Dictionary <string, object>();

            row2["key"]   = "J";
            row2["value"] = 2;
            expectedRows.AddItem(row2);
            IDictionary <string, object> row3 = new Dictionary <string, object>();

            row3["key"]   = "N";
            row3["value"] = 1;
            expectedRows.AddItem(row3);
            NUnit.Framework.Assert.AreEqual(row1["key"], rows[0].Key);
            NUnit.Framework.Assert.AreEqual(row1["value"], rows[0].GetValue());
            NUnit.Framework.Assert.AreEqual(row2["key"], rows[1].Key);
            NUnit.Framework.Assert.AreEqual(row2["value"], rows[1].GetValue());
            NUnit.Framework.Assert.AreEqual(row3["key"], rows[2].Key);
            NUnit.Framework.Assert.AreEqual(row3["value"], rows[2].GetValue());
        }
示例#3
0
        /// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception>
        public virtual void TestLargerViewQuery()
        {
            PutNDocs(database, 4);
            View view = CreateView(database);

            view.UpdateIndex();
            // Query all rows:
            QueryOptions     options = new QueryOptions();
            Status           status  = new Status();
            IList <QueryRow> rows    = view.QueryWithOptions(options);
        }
示例#4
0
        /// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception>
        public virtual void TestLargerViewQuery()
        {
            PutNDocs(database, 4);
            View view = CreateView(database);

            view.UpdateIndex();

            // Query all rows:
            QueryOptions     options = new QueryOptions();
            IList <QueryRow> rows    = view.QueryWithOptions(options).ToList();

            Assert.IsNotNull(rows);
            Assert.IsTrue(rows.Count > 0);
        }
示例#5
0
        /// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception>
        public virtual void TestViewLinkedDocs()
        {
            PutLinkedDocs(database);
            View view = database.GetView("linked");

            view.SetMapReduce(new _Mapper_1079(), null, "1");
            view.UpdateIndex();
            QueryOptions options = new QueryOptions();

            options.SetIncludeDocs(true);
            // required for linked documents
            IList <QueryRow> rows = view.QueryWithOptions(options);

            NUnit.Framework.Assert.IsNotNull(rows);
            NUnit.Framework.Assert.AreEqual(5, rows.Count);
            object[][] expected = new object[][] { new object[] { "22222", "hello", 0, null,
                                                                  "22222" }, new object[] { "22222", "hello", 1, "11111", "11111" }, new object[]
                                                   { "33333", "world", 0, null, "33333" }, new object[] { "33333", "world", 1, "22222"
                                                                                                          , "22222" }, new object[] { "33333", "world", 2, "11111", "11111" } };
            for (int i = 0; i < rows.Count; i++)
            {
                QueryRow row = rows[i];
                IDictionary <string, object> rowAsJson = row.AsJSONDictionary();
                Log.D(Tag, string.Empty + rowAsJson);
                IList <object> key = (IList <object>)rowAsJson["key"];
                IDictionary <string, object> doc = (IDictionary <string, object>)rowAsJson.Get("doc"
                                                                                               );
                string id = (string)rowAsJson["id"];
                NUnit.Framework.Assert.AreEqual(expected[i][0], id);
                NUnit.Framework.Assert.AreEqual(2, key.Count);
                NUnit.Framework.Assert.AreEqual(expected[i][1], key[0]);
                NUnit.Framework.Assert.AreEqual(expected[i][2], key[1]);
                if (expected[i][3] == null)
                {
                    NUnit.Framework.Assert.IsNull(row.GetValue());
                }
                else
                {
                    NUnit.Framework.Assert.AreEqual(expected[i][3], ((IDictionary <string, object>)row
                                                                     .GetValue())["_id"]);
                }
                NUnit.Framework.Assert.AreEqual(expected[i][4], doc["_id"]);
            }
        }
示例#6
0
        public void TestViewGroupedStrings()
        {
            IDictionary <string, object> docProperties1 = new Dictionary <string, object>();

            docProperties1["name"] = "Alice";
            PutDoc(database, docProperties1);
            IDictionary <string, object> docProperties2 = new Dictionary <string, object>();

            docProperties2["name"] = "Albert";
            PutDoc(database, docProperties2);
            IDictionary <string, object> docProperties3 = new Dictionary <string, object>();

            docProperties3["name"] = "Naomi";
            PutDoc(database, docProperties3);
            IDictionary <string, object> docProperties4 = new Dictionary <string, object>();

            docProperties4["name"] = "Jens";
            PutDoc(database, docProperties4);
            IDictionary <string, object> docProperties5 = new Dictionary <string, object>();

            docProperties5["name"] = "Jed";
            PutDoc(database, docProperties5);

            View view = database.GetView("default/names");

            view.SetMapReduce((document, emitter) =>
            {
                string name = (string)document["name"];
                if (name != null)
                {
                    emitter(Sharpen.Runtime.Substring(name, 0, 1), 1);
                }
            }, (keys, values, rereduce) => View.TotalValues(values.ToList()), "1.0");

            view.UpdateIndex();
            QueryOptions options = new QueryOptions();

            options.SetGroupLevel(1);

            IList <QueryRow> rows = view.QueryWithOptions(options).ToList();
            IList <IDictionary <string, object> > expectedRows = new List <IDictionary <string, object> >();
            IDictionary <string, object>          row1         = new Dictionary <string, object>();

            row1["key"]   = "A";
            row1["value"] = 2;
            expectedRows.AddItem(row1);

            IDictionary <string, object> row2 = new Dictionary <string, object>();

            row2["key"]   = "J";
            row2["value"] = 2;
            expectedRows.AddItem(row2);

            IDictionary <string, object> row3 = new Dictionary <string, object>();

            row3["key"]   = "N";
            row3["value"] = 1;
            expectedRows.AddItem(row3);

            Assert.AreEqual(row1["key"], rows[0].Key);
            Assert.AreEqual(row1["value"], rows[0].Value);
            Assert.AreEqual(row2["key"], rows[1].Key);
            Assert.AreEqual(row2["value"], rows[1].Value);
            Assert.AreEqual(row3["key"], rows[2].Key);
            Assert.AreEqual(row3["value"], rows[2].Value);
        }
示例#7
0
        public void TestViewGrouped()
        {
            IDictionary <string, object> docProperties1 = new Dictionary <string, object>();

            docProperties1["_id"]    = "1";
            docProperties1["artist"] = "Gang Of Four";
            docProperties1["album"]  = "Entertainment!";
            docProperties1["track"]  = "Ether";
            docProperties1["time"]   = 231;
            PutDoc(database, docProperties1);

            IDictionary <string, object> docProperties2 = new Dictionary <string, object>();

            docProperties2["_id"]    = "2";
            docProperties2["artist"] = "Gang Of Four";
            docProperties2["album"]  = "Songs Of The Free";
            docProperties2["track"]  = "I Love A Man In Uniform";
            docProperties2["time"]   = 248;
            PutDoc(database, docProperties2);

            IDictionary <string, object> docProperties3 = new Dictionary <string, object>();

            docProperties3["_id"]    = "3";
            docProperties3["artist"] = "Gang Of Four";
            docProperties3["album"]  = "Entertainment!";
            docProperties3["track"]  = "Natural's Not In It";
            docProperties3["time"]   = 187;
            PutDoc(database, docProperties3);

            IDictionary <string, object> docProperties4 = new Dictionary <string, object>();

            docProperties4["_id"]    = "4";
            docProperties4["artist"] = "PiL";
            docProperties4["album"]  = "Metal Box";
            docProperties4["track"]  = "Memories";
            docProperties4["time"]   = 309;
            PutDoc(database, docProperties4);

            IDictionary <string, object> docProperties5 = new Dictionary <string, object>();

            docProperties5["_id"]    = "5";
            docProperties5["artist"] = "Gang Of Four";
            docProperties5["album"]  = "Entertainment!";
            docProperties5["track"]  = "Not Great Men";
            docProperties5["time"]   = 187;
            PutDoc(database, docProperties5);

            View view = database.GetView("grouper");

            view.SetMapReduce((IDictionary <string, object> document, EmitDelegate emitter) =>
            {
                IList <object> key = new List <object>();
                key.AddItem(document["artist"]);
                key.AddItem(document["album"]);
                key.AddItem(document["track"]);
                emitter(key, document["time"]);
            }, (IEnumerable <object> keys, IEnumerable <object> values, bool rereduce) => {
                return(View.TotalValues(values.ToList()));
            }, "1");

            view.UpdateIndex();
            QueryOptions options = new QueryOptions();

            options.SetReduce(true);

            IList <QueryRow> rows = view.QueryWithOptions(options).ToList();
            IList <IDictionary <string, object> > expectedRows = new List <IDictionary <string, object> >();
            IDictionary <string, object>          row1         = new Dictionary <string, object>();

            row1["key"]   = null;
            row1["value"] = 1162.0;
            expectedRows.AddItem(row1);
            Assert.AreEqual(row1["key"], rows[0].Key);
            Assert.AreEqual(row1["value"], rows[0].Value);

            //now group
            options.SetGroup(true);
            rows         = view.QueryWithOptions(options).ToList();
            expectedRows = new List <IDictionary <string, object> >();
            row1         = new Dictionary <string, object>();
            IList <string> key1 = new List <string>();

            key1.AddItem("Gang Of Four");
            key1.AddItem("Entertainment!");
            key1.AddItem("Ether");
            row1["key"]   = key1;
            row1["value"] = 231.0;
            expectedRows.AddItem(row1);

            IDictionary <string, object> row2 = new Dictionary <string, object>();
            IList <string> key2 = new List <string>();

            key2.AddItem("Gang Of Four");
            key2.AddItem("Entertainment!");
            key2.AddItem("Natural's Not In It");
            row2["key"]   = key2;
            row2["value"] = 187.0;
            expectedRows.AddItem(row2);

            IDictionary <string, object> row3 = new Dictionary <string, object>();
            IList <string> key3 = new List <string>();

            key3.AddItem("Gang Of Four");
            key3.AddItem("Entertainment!");
            key3.AddItem("Not Great Men");
            row3["key"]   = key3;
            row3["value"] = 187.0;
            expectedRows.AddItem(row3);

            IDictionary <string, object> row4 = new Dictionary <string, object>();
            IList <string> key4 = new List <string>();

            key4.AddItem("Gang Of Four");
            key4.AddItem("Songs Of The Free");
            key4.AddItem("I Love A Man In Uniform");
            row4["key"]   = key4;
            row4["value"] = 248.0;
            expectedRows.AddItem(row4);

            IDictionary <string, object> row5 = new Dictionary <string, object>();
            IList <string> key5 = new List <string>();

            key5.AddItem("PiL");
            key5.AddItem("Metal Box");
            key5.AddItem("Memories");
            row5["key"]   = key5;
            row5["value"] = 309.0;
            expectedRows.AddItem(row5);
            Assert.AreEqual(row1["key"], ((JArray)rows[0].Key).Values <String>().ToList());
            Assert.AreEqual(row1["value"], rows[0].Value);
            Assert.AreEqual(row2["key"], ((JArray)rows[1].Key).Values <String>().ToList());
            Assert.AreEqual(row2["value"], rows[1].Value);
            Assert.AreEqual(row3["key"], ((JArray)rows[2].Key).Values <String>().ToList());
            Assert.AreEqual(row3["value"], rows[2].Value);
            Assert.AreEqual(row4["key"], ((JArray)rows[3].Key).Values <String>().ToList());
            Assert.AreEqual(row4["value"], rows[3].Value);
            Assert.AreEqual(row5["key"], ((JArray)rows[4].Key).Values <String>().ToList());
            Assert.AreEqual(row5["value"], rows[4].Value);

            //group level 1
            options.SetGroupLevel(1);
            rows         = view.QueryWithOptions(options).ToList();
            expectedRows = new List <IDictionary <string, object> >();
            row1         = new Dictionary <string, object>();
            key1         = new List <string>();
            key1.AddItem("Gang Of Four");
            row1["key"]   = key1;
            row1["value"] = 853.0;

            expectedRows.AddItem(row1);
            row2 = new Dictionary <string, object>();
            key2 = new List <string>();
            key2.AddItem("PiL");
            row2["key"]   = key2;
            row2["value"] = 309.0;
            expectedRows.AddItem(row2);
            Assert.AreEqual(row1["key"], ((JArray)rows[0].Key).Values <String>().ToList());
            Assert.AreEqual(row1["value"], rows[0].Value);
            Assert.AreEqual(row2["key"], ((JArray)rows[1].Key).Values <String>().ToList());
            Assert.AreEqual(row2["value"], rows[1].Value);

            //group level 2
            options.SetGroupLevel(2);
            rows         = view.QueryWithOptions(options).ToList();
            expectedRows = new List <IDictionary <string, object> >();
            row1         = new Dictionary <string, object>();
            key1         = new List <string>();
            key1.AddItem("Gang Of Four");
            key1.AddItem("Entertainment!");
            row1["key"]   = key1;
            row1["value"] = 605.0;
            expectedRows.AddItem(row1);
            row2 = new Dictionary <string, object>();
            key2 = new List <string>();
            key2.AddItem("Gang Of Four");
            key2.AddItem("Songs Of The Free");
            row2["key"]   = key2;
            row2["value"] = 248.0;
            expectedRows.AddItem(row2);
            row3 = new Dictionary <string, object>();
            key3 = new List <string>();
            key3.AddItem("PiL");
            key3.AddItem("Metal Box");
            row3["key"]   = key3;
            row3["value"] = 309.0;
            expectedRows.AddItem(row3);
            Assert.AreEqual(row1["key"], ((JArray)rows[0].Key).Values <String>().ToList());
            Assert.AreEqual(row1["value"], rows[0].Value);
            Assert.AreEqual(row2["key"], ((JArray)rows[1].Key).Values <String>().ToList());
            Assert.AreEqual(row2["value"], rows[1].Value);
            Assert.AreEqual(row3["key"], ((JArray)rows[2].Key).Values <String>().ToList());
            Assert.AreEqual(row3["value"], rows[2].Value);
        }
示例#8
0
        /// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception>
        public virtual void TestViewLinkedDocs()
        {
            PutLinkedDocs(database);
            View view = database.GetView("linked");

            view.SetMapReduce((document, emitter) =>
            {
                if (document.ContainsKey("value"))
                {
                    emitter(new object[] { document["value"], 0 }, null);
                }
                if (document.ContainsKey("ancestors"))
                {
                    IList <object> ancestors = (IList <object>)document["ancestors"];
                    for (int i = 0; i < ancestors.Count; i++)
                    {
                        IDictionary <string, object> value = new Dictionary <string, object>();
                        value["_id"] = ancestors[i];
                        emitter(new object[] { document["value"], i + 1 }, value);
                    }
                }
            }, null, "1.0");

            view.UpdateIndex();
            QueryOptions options = new QueryOptions();

            options.SetIncludeDocs(true);

            // required for linked documents
            IList <QueryRow> rows = view.QueryWithOptions(options).ToList();

            Assert.IsNotNull(rows);
            Assert.AreEqual(5, rows.Count);

            object[][] expected = new object[][] {
                new object[] { "22222", "hello", 0, null, "22222" },
                new object[] { "22222", "hello", 1, "11111", "11111" },
                new object[] { "33333", "world", 0, null, "33333" },
                new object[] { "33333", "world", 1, "22222", "22222" },
                new object[] { "33333", "world", 2, "11111", "11111" }
            };

            for (int i = 0; i < rows.Count; i++)
            {
                QueryRow row = rows[i];
                IDictionary <string, object> rowAsJson = row.AsJSONDictionary();
                Log.D(Tag, string.Empty + rowAsJson);
                IList <object> key = (IList <object>)rowAsJson["key"];
                IDictionary <string, object> doc = (IDictionary <string, object>)rowAsJson.Get("doc"
                                                                                               );
                string id = (string)rowAsJson["id"];
                Assert.AreEqual(expected[i][0], id);
                Assert.AreEqual(2, key.Count);
                Assert.AreEqual(expected[i][1], key[0]);
                Assert.AreEqual(expected[i][2], key[1]);
                if (expected[i][3] == null)
                {
                    Assert.IsNull(row.Value);
                }
                else
                {
                    Assert.AreEqual(expected[i][3], ((IDictionary <string, object>)row.Value)["_id"]);
                }
                Assert.AreEqual(expected[i][4], doc["_id"]);
            }
        }
示例#9
0
        /// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception>
        public virtual void TestViewQuery()
        {
            PutDocs(database);
            View view = CreateView(database);

            view.UpdateIndex();
            // Query all rows:
            QueryOptions                 options      = new QueryOptions();
            IList <QueryRow>             rows         = view.QueryWithOptions(options);
            IList <object>               expectedRows = new AList <object>();
            IDictionary <string, object> dict5        = new Dictionary <string, object>();

            dict5["id"]  = "55555";
            dict5["key"] = "five";
            expectedRows.AddItem(dict5);
            IDictionary <string, object> dict4 = new Dictionary <string, object>();

            dict4["id"]  = "44444";
            dict4["key"] = "four";
            expectedRows.AddItem(dict4);
            IDictionary <string, object> dict1 = new Dictionary <string, object>();

            dict1["id"]  = "11111";
            dict1["key"] = "one";
            expectedRows.AddItem(dict1);
            IDictionary <string, object> dict3 = new Dictionary <string, object>();

            dict3["id"]  = "33333";
            dict3["key"] = "three";
            expectedRows.AddItem(dict3);
            IDictionary <string, object> dict2 = new Dictionary <string, object>();

            dict2["id"]  = "22222";
            dict2["key"] = "two";
            expectedRows.AddItem(dict2);
            NUnit.Framework.Assert.AreEqual(5, rows.Count);
            NUnit.Framework.Assert.AreEqual(dict5["key"], rows[0].Key);
            NUnit.Framework.Assert.AreEqual(dict5["value"], rows[0].GetValue());
            NUnit.Framework.Assert.AreEqual(dict4["key"], rows[1].Key);
            NUnit.Framework.Assert.AreEqual(dict4["value"], rows[1].GetValue());
            NUnit.Framework.Assert.AreEqual(dict1["key"], rows[2].Key);
            NUnit.Framework.Assert.AreEqual(dict1["value"], rows[2].GetValue());
            NUnit.Framework.Assert.AreEqual(dict3["key"], rows[3].Key);
            NUnit.Framework.Assert.AreEqual(dict3["value"], rows[3].GetValue());
            NUnit.Framework.Assert.AreEqual(dict2["key"], rows[4].Key);
            NUnit.Framework.Assert.AreEqual(dict2["value"], rows[4].GetValue());
            // Start/end key query:
            options          = new QueryOptions();
            options.StartKey = "a";
            options.EndKey   = "one";
            rows             = view.QueryWithOptions(options);
            expectedRows     = new AList <object>();
            expectedRows.AddItem(dict5);
            expectedRows.AddItem(dict4);
            expectedRows.AddItem(dict1);
            NUnit.Framework.Assert.AreEqual(3, rows.Count);
            NUnit.Framework.Assert.AreEqual(dict5["key"], rows[0].Key);
            NUnit.Framework.Assert.AreEqual(dict5["value"], rows[0].GetValue());
            NUnit.Framework.Assert.AreEqual(dict4["key"], rows[1].Key);
            NUnit.Framework.Assert.AreEqual(dict4["value"], rows[1].GetValue());
            NUnit.Framework.Assert.AreEqual(dict1["key"], rows[2].Key);
            NUnit.Framework.Assert.AreEqual(dict1["value"], rows[2].GetValue());
            // Start/end query without inclusive end:
            options.SetInclusiveEnd(false);
            rows         = view.QueryWithOptions(options);
            expectedRows = new AList <object>();
            expectedRows.AddItem(dict5);
            expectedRows.AddItem(dict4);
            NUnit.Framework.Assert.AreEqual(2, rows.Count);
            NUnit.Framework.Assert.AreEqual(dict5["key"], rows[0].Key);
            NUnit.Framework.Assert.AreEqual(dict5["value"], rows[0].GetValue());
            NUnit.Framework.Assert.AreEqual(dict4["key"], rows[1].Key);
            NUnit.Framework.Assert.AreEqual(dict4["value"], rows[1].GetValue());
            // Reversed:
            options.SetDescending(true);
            options.StartKey = "o";
            options.EndKey   = "five";
            options.SetInclusiveEnd(true);
            rows         = view.QueryWithOptions(options);
            expectedRows = new AList <object>();
            expectedRows.AddItem(dict4);
            expectedRows.AddItem(dict5);
            NUnit.Framework.Assert.AreEqual(2, rows.Count);
            NUnit.Framework.Assert.AreEqual(dict4["key"], rows[0].Key);
            NUnit.Framework.Assert.AreEqual(dict4["value"], rows[0].GetValue());
            NUnit.Framework.Assert.AreEqual(dict5["key"], rows[1].Key);
            NUnit.Framework.Assert.AreEqual(dict5["value"], rows[1].GetValue());
            // Reversed, no inclusive end:
            options.SetInclusiveEnd(false);
            rows         = view.QueryWithOptions(options);
            expectedRows = new AList <object>();
            expectedRows.AddItem(dict4);
            NUnit.Framework.Assert.AreEqual(1, rows.Count);
            NUnit.Framework.Assert.AreEqual(dict4["key"], rows[0].Key);
            NUnit.Framework.Assert.AreEqual(dict4["value"], rows[0].GetValue());
            // Specific keys:
            options = new QueryOptions();
            IList <object> keys = new AList <object>();

            keys.AddItem("two");
            keys.AddItem("four");
            options.SetKeys(keys);
            rows         = view.QueryWithOptions(options);
            expectedRows = new AList <object>();
            expectedRows.AddItem(dict4);
            expectedRows.AddItem(dict2);
            NUnit.Framework.Assert.AreEqual(2, rows.Count);
            NUnit.Framework.Assert.AreEqual(dict4["key"], rows[0].Key);
            NUnit.Framework.Assert.AreEqual(dict4["value"], rows[0].GetValue());
            NUnit.Framework.Assert.AreEqual(dict2["key"], rows[1].Key);
            NUnit.Framework.Assert.AreEqual(dict2["value"], rows[1].GetValue());
        }
示例#10
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();
        }