Esempio n. 1
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);
        }
Esempio n. 2
0
		/// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception>
		public virtual void TestViewGrouped()
		{
			IDictionary<string, object> docProperties1 = new Dictionary<string, object>();
			docProperties1.Put("_id", "1");
			docProperties1.Put("artist", "Gang Of Four");
			docProperties1.Put("album", "Entertainment!");
			docProperties1.Put("track", "Ether");
			docProperties1.Put("time", 231);
			PutDoc(database, docProperties1);
			IDictionary<string, object> docProperties2 = new Dictionary<string, object>();
			docProperties2.Put("_id", "2");
			docProperties2.Put("artist", "Gang Of Four");
			docProperties2.Put("album", "Songs Of The Free");
			docProperties2.Put("track", "I Love A Man In Uniform");
			docProperties2.Put("time", 248);
			PutDoc(database, docProperties2);
			IDictionary<string, object> docProperties3 = new Dictionary<string, object>();
			docProperties3.Put("_id", "3");
			docProperties3.Put("artist", "Gang Of Four");
			docProperties3.Put("album", "Entertainment!");
			docProperties3.Put("track", "Natural's Not In It");
			docProperties3.Put("time", 187);
			PutDoc(database, docProperties3);
			IDictionary<string, object> docProperties4 = new Dictionary<string, object>();
			docProperties4.Put("_id", "4");
			docProperties4.Put("artist", "PiL");
			docProperties4.Put("album", "Metal Box");
			docProperties4.Put("track", "Memories");
			docProperties4.Put("time", 309);
			PutDoc(database, docProperties4);
			IDictionary<string, object> docProperties5 = new Dictionary<string, object>();
			docProperties5.Put("_id", "5");
			docProperties5.Put("artist", "Gang Of Four");
			docProperties5.Put("album", "Entertainment!");
			docProperties5.Put("track", "Not Great Men");
			docProperties5.Put("time", 187);
			PutDoc(database, docProperties5);
			View view = database.GetView("grouper");
			view.SetMapReduce(new _Mapper_671(), new _Reducer_681(), "1");
			Status status = new Status();
			view.UpdateIndex();
			QueryOptions options = new QueryOptions();
			options.SetReduce(true);
			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.Put("key", null);
			row1.Put("value", 1162.0);
			expectedRows.AddItem(row1);
			NUnit.Framework.Assert.AreEqual(row1.Get("key"), rows[0].GetKey());
			NUnit.Framework.Assert.AreEqual(row1.Get("value"), rows[0].GetValue());
			//now group
			options.SetGroup(true);
			status = new Status();
			rows = view.QueryWithOptions(options);
			expectedRows = new AList<IDictionary<string, object>>();
			row1 = new Dictionary<string, object>();
			IList<string> key1 = new AList<string>();
			key1.AddItem("Gang Of Four");
			key1.AddItem("Entertainment!");
			key1.AddItem("Ether");
			row1.Put("key", key1);
			row1.Put("value", 231.0);
			expectedRows.AddItem(row1);
			IDictionary<string, object> row2 = new Dictionary<string, object>();
			IList<string> key2 = new AList<string>();
			key2.AddItem("Gang Of Four");
			key2.AddItem("Entertainment!");
			key2.AddItem("Natural's Not In It");
			row2.Put("key", key2);
			row2.Put("value", 187.0);
			expectedRows.AddItem(row2);
			IDictionary<string, object> row3 = new Dictionary<string, object>();
			IList<string> key3 = new AList<string>();
			key3.AddItem("Gang Of Four");
			key3.AddItem("Entertainment!");
			key3.AddItem("Not Great Men");
			row3.Put("key", key3);
			row3.Put("value", 187.0);
			expectedRows.AddItem(row3);
			IDictionary<string, object> row4 = new Dictionary<string, object>();
			IList<string> key4 = new AList<string>();
			key4.AddItem("Gang Of Four");
			key4.AddItem("Songs Of The Free");
			key4.AddItem("I Love A Man In Uniform");
			row4.Put("key", key4);
			row4.Put("value", 248.0);
			expectedRows.AddItem(row4);
			IDictionary<string, object> row5 = new Dictionary<string, object>();
			IList<string> key5 = new AList<string>();
			key5.AddItem("PiL");
			key5.AddItem("Metal Box");
			key5.AddItem("Memories");
			row5.Put("key", key5);
			row5.Put("value", 309.0);
			expectedRows.AddItem(row5);
			NUnit.Framework.Assert.AreEqual(row1.Get("key"), rows[0].GetKey());
			NUnit.Framework.Assert.AreEqual(row1.Get("value"), rows[0].GetValue());
			NUnit.Framework.Assert.AreEqual(row2.Get("key"), rows[1].GetKey());
			NUnit.Framework.Assert.AreEqual(row2.Get("value"), rows[1].GetValue());
			NUnit.Framework.Assert.AreEqual(row3.Get("key"), rows[2].GetKey());
			NUnit.Framework.Assert.AreEqual(row3.Get("value"), rows[2].GetValue());
			NUnit.Framework.Assert.AreEqual(row4.Get("key"), rows[3].GetKey());
			NUnit.Framework.Assert.AreEqual(row4.Get("value"), rows[3].GetValue());
			NUnit.Framework.Assert.AreEqual(row5.Get("key"), rows[4].GetKey());
			NUnit.Framework.Assert.AreEqual(row5.Get("value"), rows[4].GetValue());
			//group level 1
			options.SetGroupLevel(1);
			status = new Status();
			rows = view.QueryWithOptions(options);
			expectedRows = new AList<IDictionary<string, object>>();
			row1 = new Dictionary<string, object>();
			key1 = new AList<string>();
			key1.AddItem("Gang Of Four");
			row1.Put("key", key1);
			row1.Put("value", 853.0);
			expectedRows.AddItem(row1);
			row2 = new Dictionary<string, object>();
			key2 = new AList<string>();
			key2.AddItem("PiL");
			row2.Put("key", key2);
			row2.Put("value", 309.0);
			expectedRows.AddItem(row2);
			NUnit.Framework.Assert.AreEqual(row1.Get("key"), rows[0].GetKey());
			NUnit.Framework.Assert.AreEqual(row1.Get("value"), rows[0].GetValue());
			NUnit.Framework.Assert.AreEqual(row2.Get("key"), rows[1].GetKey());
			NUnit.Framework.Assert.AreEqual(row2.Get("value"), rows[1].GetValue());
			//group level 2
			options.SetGroupLevel(2);
			status = new Status();
			rows = view.QueryWithOptions(options);
			expectedRows = new AList<IDictionary<string, object>>();
			row1 = new Dictionary<string, object>();
			key1 = new AList<string>();
			key1.AddItem("Gang Of Four");
			key1.AddItem("Entertainment!");
			row1.Put("key", key1);
			row1.Put("value", 605.0);
			expectedRows.AddItem(row1);
			row2 = new Dictionary<string, object>();
			key2 = new AList<string>();
			key2.AddItem("Gang Of Four");
			key2.AddItem("Songs Of The Free");
			row2.Put("key", key2);
			row2.Put("value", 248.0);
			expectedRows.AddItem(row2);
			row3 = new Dictionary<string, object>();
			key3 = new AList<string>();
			key3.AddItem("PiL");
			key3.AddItem("Metal Box");
			row3.Put("key", key3);
			row3.Put("value", 309.0);
			expectedRows.AddItem(row3);
			NUnit.Framework.Assert.AreEqual(row1.Get("key"), rows[0].GetKey());
			NUnit.Framework.Assert.AreEqual(row1.Get("value"), rows[0].GetValue());
			NUnit.Framework.Assert.AreEqual(row2.Get("key"), rows[1].GetKey());
			NUnit.Framework.Assert.AreEqual(row2.Get("value"), rows[1].GetValue());
			NUnit.Framework.Assert.AreEqual(row3.Get("key"), rows[2].GetKey());
			NUnit.Framework.Assert.AreEqual(row3.Get("value"), rows[2].GetValue());
		}
        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 AList<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 AList<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 AList<IDictionary<string, object>>();
            row1 = new Dictionary<string, object>();
            IList<string> key1 = new AList<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 AList<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 AList<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 AList<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 AList<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 AList<IDictionary<string, object>>();
            row1 = new Dictionary<string, object>();
            key1 = new AList<string>();
            key1.AddItem("Gang Of Four");
            row1["key"] = key1;
            row1["value"] = 853.0;

            expectedRows.AddItem(row1);
            row2 = new Dictionary<string, object>();
            key2 = new AList<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 AList<IDictionary<string, object>>();
            row1 = new Dictionary<string, object>();
            key1 = new AList<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 AList<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 AList<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);
        }