public void ShouldRunAggregateKeysAndTimeout()
        {
            ArrayList keys = new ArrayList {
                "Foo-Key"
            };

            Assert.Throws(Is.TypeOf(typeof(RequestTimeoutException)), () => m_cache.Aggregate(keys, m_priorityAggregatorDefault));
        }
        public void TestLongMaxAggregator()
        {
            LongMax agg1 = new LongMax(IdentityExtractor.Instance);

            Assert.IsNotNull(agg1);
            Assert.AreSame(IdentityExtractor.Instance, agg1.Extractor);

            LongMax agg2 = new LongMax("dummy");

            Assert.IsNotNull(agg2);
            Assert.IsInstanceOf(typeof(ReflectionExtractor), agg2.Extractor);

            LongMax agg3 = new LongMax("another.dummy");

            Assert.IsNotNull(agg3);
            Assert.IsInstanceOf(typeof(ChainedExtractor), agg3.Extractor);

            ArrayList al = new ArrayList();

            al.Add(new TestInvocableCacheEntry("Ivan", 173));
            al.Add(new TestInvocableCacheEntry("Milica", 173));
            al.Add(new TestInvocableCacheEntry("Goran", 185.1));
            al.Add(new TestInvocableCacheEntry("Ana", 164.08));
            Assert.AreEqual(185, agg1.Aggregate(al));

            al = new ArrayList();
            al.Add(new TestInvocableCacheEntry("Ivan", 173));
            al.Add(new TestInvocableCacheEntry("Milica", 173));
            al.Add(new TestInvocableCacheEntry("Goran", 185));
            al.Add(new TestInvocableCacheEntry("Ana", 164));
            Assert.AreEqual(185, agg1.Aggregate(al));

            // aggragation on remote cache
            INamedCache cache = CacheFactory.GetCache(CacheName);

            cache.Clear();

            Hashtable ht = new Hashtable();

            ht.Add("key1", 1011.21);
            ht.Add("key2", 80.5);
            ht.Add("key3", 19.5);
            ht.Add("key4", 2);
            ht.Add("key5", 1010);
            cache.InsertAll(ht);

            IEntryAggregator aggregator = new LongMax(IdentityExtractor.Instance);
            object           result     = cache.Aggregate(cache.Keys, aggregator);

            Assert.AreEqual(1011, result);

            cache.Insert("key5", Int32.MaxValue);
            result = cache.Aggregate(cache.Keys, aggregator);
            Assert.AreEqual(Int32.MaxValue, result);

            CacheFactory.Shutdown();
        }
        public void TestDoubleAverageAggregator()
        {
            DoubleAverage agg1 = new DoubleAverage(IdentityExtractor.Instance);

            Assert.IsNotNull(agg1);
            Assert.AreSame(IdentityExtractor.Instance, agg1.Extractor);

            DoubleAverage agg2 = new DoubleAverage("dummy");

            Assert.IsNotNull(agg2);
            Assert.IsInstanceOf(typeof(ReflectionExtractor), agg2.Extractor);

            DoubleAverage agg3 = new DoubleAverage("another.dummy");

            Assert.IsNotNull(agg3);
            Assert.IsInstanceOf(typeof(ChainedExtractor), agg3.Extractor);

            ArrayList al = new ArrayList();

            al.Add(new TestInvocableCacheEntry("Ivan", 173.6));
            al.Add(new TestInvocableCacheEntry("Milica", 173.22));
            al.Add(new TestInvocableCacheEntry("Goran", 185.1));
            al.Add(new TestInvocableCacheEntry("Ana", 164.08));
            Assert.AreEqual((173.6 + 173.22 + 185.1 + 164.08) / 4, agg1.Aggregate(al));

            // aggragation on remote cache
            INamedCache cache = CacheFactory.GetCache(CacheName);

            cache.Clear();

            Hashtable ht = new Hashtable();

            ht.Add("doubleAvgKey1", 100);
            ht.Add("doubleAvgKey2", 80.5);
            ht.Add("doubleAvgKey3", 19.5);
            ht.Add("doubleAvgKey4", 2);
            cache.InsertAll(ht);

            IEntryAggregator aggregator = new DoubleAverage(IdentityExtractor.Instance);
            object           result     = cache.Aggregate(cache.Keys, aggregator);

            Assert.AreEqual(50.5, result);

            cache.Insert("comparableKey5", null);
            result = cache.Aggregate(cache.Keys, aggregator);
            Assert.AreEqual(50.5, result);

            IFilter filter = new AlwaysFilter();

            result = cache.Aggregate(filter, aggregator);
            Assert.AreEqual(50.5, result);

            CacheFactory.Shutdown();
        }
        public void TestComparableMaxAggregator()
        {
            ComparableMax agg1 = new ComparableMax(IdentityExtractor.Instance);

            Assert.IsNotNull(agg1);
            Assert.AreSame(IdentityExtractor.Instance, agg1.Extractor);

            ComparableMax agg2 = new ComparableMax("dummy");

            Assert.IsNotNull(agg2);
            Assert.IsInstanceOf(typeof(ReflectionExtractor), agg2.Extractor);

            ComparableMax agg3 = new ComparableMax("another.dummy");

            Assert.IsNotNull(agg3);
            Assert.IsInstanceOf(typeof(ChainedExtractor), agg3.Extractor);

            ArrayList al = new ArrayList();

            al.Add(new TestInvocableCacheEntry("Ivan", 173));
            al.Add(new TestInvocableCacheEntry("Goran", 185));
            al.Add(new TestInvocableCacheEntry("Ana", 164));
            Assert.AreEqual(185, agg1.Aggregate(al));

            string[] array = new string[] { "Ana", "Ivan", "Goran" };
            Assert.AreEqual("Ivan", agg1.AggregateResults(array));

            // aggragation on remote cache
            INamedCache cache = CacheFactory.GetCache(CacheName);

            cache.Clear();

            Hashtable ht = new Hashtable();

            ht.Add("comparableMaxKey1", 435);
            ht.Add("comparableMaxKey2", 253);
            ht.Add("comparableMaxKey3", 3);
            ht.Add("comparableMaxKey4", null);
            ht.Add("comparableMaxKey5", -3);
            cache.InsertAll(ht);

            IEntryAggregator aggregator = new ComparableMax(IdentityExtractor.Instance);
            object           max        = cache.Aggregate(cache.Keys, aggregator);

            Assert.AreEqual(max, 435);

            IFilter filter = new AlwaysFilter();

            max = cache.Aggregate(filter, aggregator);
            Assert.AreEqual(max, 435);

            CacheFactory.Shutdown();
        }
Example #5
0
        /// <summary>
        /// Create indexes in the cache and query it for data.
        /// </summary>
        /// <param name="cache">
        /// Cache to query.
        /// </param>
        /// <param name="ff">
        /// The <see cref="FilterFactory"/> used to convert string to
        /// <b>IFilters</b>.
        /// </param>
        public void Query(INamedCache cache, FilterFactory ff)
        {
            Console.WriteLine("------QueryLanguageExample begins------");

            // Add indexes to make queries more efficient
            // Ordered index applied to fields used in range and like filter queries
            cache.AddIndex(ff.CreateExtractor("age"), /*fOrdered*/ true, /*comparator*/ null);
            cache.AddIndex(ff.CreateExtractor("key().lastName"), /*fOrdered*/ true, /*comparator*/ null);
            cache.AddIndex(ff.CreateExtractor("homeAddress.city"), /*fOrdered*/ true, /*comparator*/ null);
            cache.AddIndex(ff.CreateExtractor("homeAddress.state"), /*fOrdered*/ false, /*comparator*/ null);
            cache.AddIndex(ff.CreateExtractor("workAddress.state"), /*fOrdered*/ false, /*comparator*/ null);

            // Find all contacts who live in Massachusetts
            ICollection results = cache.GetEntries(ff.CreateFilter("homeAddress.state = 'MA'"));

            PrintResults("MA Residents", results);

            // Find all contacts who live in Massachusetts and work elsewhere
            results = cache.GetEntries(ff.CreateFilter("homeAddress.state is 'MA' and workAddress is not 'MA'"));
            PrintResults("MA Residents, Work Elsewhere", results);

            // Find all contacts whose city name begins with 'S'
            results = cache.GetEntries(ff.CreateFilter("homeAddress.city like 'S%'"));
            PrintResults("City Begins with S", results);

            const int age = 58;

            object[] env = new object[] { age };
            // Find all contacts who are older than nAge
            results = cache.GetEntries(ff.CreateFilter("age > ?1", env));
            PrintResults("Age > " + age, results);

            // Find all contacts with last name beginning with 'S' that live
            // in Massachusetts. Uses both key and value in the query
            results = cache.GetEntries(ff.CreateFilter("lastName like 'S%' and homeAddress.state = 'MA'"));
            PrintResults("Last Name Begins with S and State Is MA", results);
            // Count contacts who are older than nAge for the entire cache dataset.
            Console.WriteLine("count > {0} : {1}", age,
                              cache.Aggregate(ff.CreateFilter("age > ?1", env), new Count()));

            // Find minimum age for the entire cache dataset.
            IFilter always = ff.CreateFilter("true");

            Console.WriteLine("min age: {0}", cache.Aggregate(always, new LongMin("getAge")));

            // Calculate average age for the entire cache dataset.
            Console.WriteLine("avg age: {0}", cache.Aggregate(always, new DoubleAverage("getAge")));

            // Find maximum age for the entire cache dataset.
            Console.WriteLine("max age: {0}", cache.Aggregate(always, new LongMax("getAge")));

            Console.WriteLine("------QueryLanguageExample completed------");
        }
        public void TestLongSumAggregator()
        {
            LongSum agg1 = new LongSum(IdentityExtractor.Instance);

            Assert.IsNotNull(agg1);
            Assert.AreSame(IdentityExtractor.Instance, agg1.Extractor);

            LongSum agg2 = new LongSum("dummy");

            Assert.IsNotNull(agg2);
            Assert.IsInstanceOf(typeof(ReflectionExtractor), agg2.Extractor);

            LongSum agg3 = new LongSum("another.dummy");

            Assert.IsNotNull(agg3);
            Assert.IsInstanceOf(typeof(ChainedExtractor), agg3.Extractor);

            ArrayList al = new ArrayList();

            al.Add(new TestInvocableCacheEntry("Ivan", 173.6));
            al.Add(new TestInvocableCacheEntry("Milica", 173.22));
            al.Add(new TestInvocableCacheEntry("Goran", 185.1));
            al.Add(new TestInvocableCacheEntry("Ana", 164.08));
            Assert.AreEqual(174 + 173 + 185 + 164, agg1.Aggregate(al));

            // aggragation on remote cache
            INamedCache cache = CacheFactory.GetCache(CacheName);

            cache.Clear();

            Hashtable ht = new Hashtable();

            ht.Add("key1", 100);
            ht.Add("key2", 80);
            ht.Add("key3", 19);
            ht.Add("key4", 2);
            ht.Add("key5", null);

            cache.InsertAll(ht);

            IEntryAggregator aggregator = new LongSum(IdentityExtractor.Instance);
            object           result     = cache.Aggregate(cache.Keys, aggregator);

            Assert.AreEqual(201, result);

            IFilter filter = new GreaterFilter(IdentityExtractor.Instance, 1);

            result = cache.Aggregate(filter, aggregator);
            Assert.AreEqual(201, result);

            CacheFactory.Shutdown();
        }
        public void TestDecimalMinAggregator()
        {
            DecimalMin agg1 = new DecimalMin(IdentityExtractor.Instance);

            Assert.IsNotNull(agg1);
            Assert.AreSame(IdentityExtractor.Instance, agg1.Extractor);

            DecimalMin agg2 = new DecimalMin("dummy");

            Assert.IsNotNull(agg2);
            Assert.IsInstanceOf(typeof(ReflectionExtractor), agg2.Extractor);

            DecimalMin agg3 = new DecimalMin("another.dummy");

            Assert.IsNotNull(agg3);
            Assert.IsInstanceOf(typeof(ChainedExtractor), agg3.Extractor);

            ArrayList al = new ArrayList();

            al.Add(new TestInvocableCacheEntry("Ivan", -173.6M));
            al.Add(new TestInvocableCacheEntry("Goran", 185.1M));
            al.Add(new TestInvocableCacheEntry("Ana", 1643426876432.08M));
            Assert.AreEqual(-173.6M, agg1.Aggregate(al));

            // aggragation on remote cache
            INamedCache cache = CacheFactory.GetCache(CacheName);

            cache.Clear();

            Hashtable ht = new Hashtable();

            ht.Add("key1", 100M);
            ht.Add("key2", 80.523423423423M);
            ht.Add("key3", 4643321321426876432.08M);
            ht.Add("key4", 1643426876432.08M);
            ht.Add("key5", 1011M);
            cache.InsertAll(ht);

            IEntryAggregator aggregator = new DecimalMin(IdentityExtractor.Instance);
            object           result     = cache.Aggregate(cache.Keys, aggregator);

            Assert.AreEqual(80.523423423423M, result);

            cache.Insert("key10", -10.23896128635231234M);
            IFilter filter = new AlwaysFilter();

            result = cache.Aggregate(filter, aggregator);
            Assert.AreEqual(-10.23896128635231234M, result);

            CacheFactory.Shutdown();
        }
        public void TestComparableComparerAggregator()
        {
            INamedCache cache = CacheFactory.GetCache(CacheName);

            cache.Clear();

            Hashtable ht = new Hashtable();

            ht.Add("1", new Address("street", "city", "state", "00000"));
            ht.Add("2", new Address("street", "city", "state", "20000"));
            ht.Add("3", new Address("street", "city", "state", "60000"));
            ht.Add("4", new Address("street", "city", "state", "50000"));
            ht.Add("5", new Address("street", "city", "state", "10000"));
            cache.InsertAll(ht);

            ArrayList al = new ArrayList(ht.Values);

            // setup for ComparableMax tests

            ComparableMax maxAgg = new ComparableMax(IdentityExtractor.Instance,
                                                     new SimpleAddressComparer());

            Assert.IsNotNull(maxAgg);
            Assert.AreSame(IdentityExtractor.Instance, maxAgg.Extractor);

            // local aggregation
            Assert.AreEqual("60000", ((Address)maxAgg.AggregateResults(al)).ZIP);

            // remote aggregation
            Address maxAddr = (Address)cache.Aggregate(new AlwaysFilter(), maxAgg);

            Assert.AreEqual("60000", maxAddr.ZIP);

            // setup for ComparableMin tests

            ComparableMin minAgg = new ComparableMin(IdentityExtractor.Instance,
                                                     new SimpleAddressComparer());

            Assert.IsNotNull(minAgg);
            Assert.AreSame(IdentityExtractor.Instance, minAgg.Extractor);

            // local aggregation
            Assert.AreEqual("00000", ((Address)minAgg.AggregateResults(al)).ZIP);

            // remote aggregation
            Address minAddr = (Address)cache.Aggregate(new AlwaysFilter(), minAgg);

            Assert.AreEqual("00000", minAddr.ZIP);

            CacheFactory.Shutdown();
        }
        public void TestDecimalSumAggregator()
        {
            DecimalSum agg1 = new DecimalSum(IdentityExtractor.Instance);

            Assert.IsNotNull(agg1);
            Assert.AreSame(IdentityExtractor.Instance, agg1.Extractor);

            DecimalSum agg2 = new DecimalSum("dummy");

            Assert.IsNotNull(agg2);
            Assert.IsInstanceOf(typeof(ReflectionExtractor), agg2.Extractor);

            DecimalSum agg3 = new DecimalSum("another.dummy");

            Assert.IsNotNull(agg3);
            Assert.IsInstanceOf(typeof(ChainedExtractor), agg3.Extractor);

            ArrayList al = new ArrayList();

            al.Add(new TestInvocableCacheEntry("Ivan", 132131273.643M));
            al.Add(new TestInvocableCacheEntry("Goran", -0.43432M));
            al.Add(new TestInvocableCacheEntry("Ana", 0M));
            Assert.AreEqual((132131273.643M + -0.43432M + 0M), agg1.Aggregate(al));

            // aggragation on remote cache
            INamedCache cache = CacheFactory.GetCache(CacheName);

            cache.Clear();

            Hashtable ht = new Hashtable();

            ht.Add("key1", 100M);
            ht.Add("key2", 80.5M);
            ht.Add("key3", 19.589328917623187963289176M);
            ht.Add("key4", 1M);

            cache.InsertAll(ht);

            IEntryAggregator aggregator = new DecimalSum(IdentityExtractor.Instance);
            object           result     = cache.Aggregate(cache.Keys, aggregator);

            Assert.AreEqual((100M + 80.5M + 19.589328917623187963289176M + 1M), result);

            IFilter filter = new AlwaysFilter();

            result = cache.Aggregate(filter, aggregator);
            Assert.AreEqual((100M + 80.5M + 19.589328917623187963289176M + 1M), result);

            CacheFactory.Shutdown();
        }
        public void TestCompositeAggregator()
        {
            IEntryAggregator agg1 = CompositeAggregator.CreateInstance(
                new IEntryAggregator[] { GroupAggregator.CreateInstance(IdentityExtractor.Instance,
                                                                        new Count()),
                                         new LongMax((IdentityExtractor.Instance)) });
            ArrayList al = new ArrayList();

            al.Add(new TestInvocableCacheEntry("key1", 173));
            al.Add(new TestInvocableCacheEntry("key2", 173));
            al.Add(new TestInvocableCacheEntry("key3", 185));
            al.Add(new TestInvocableCacheEntry("key4", 164));
            al.Add(new TestInvocableCacheEntry("key5", 164));
            al.Add(new TestInvocableCacheEntry("key6", 164));
            object result = agg1.Aggregate(al);

            if (result is IList)
            {
                IDictionary results = (IDictionary)((IList)result)[0];

                Assert.AreEqual(results[185], 1);
                Assert.AreEqual(results[164], 3);

                Assert.AreEqual(((IList)result)[1], 185);
            }

            // aggragation on remote cache
            INamedCache cache = CacheFactory.GetCache(CacheName);

            cache.Clear();

            HashDictionary hd = new HashDictionary();

            hd.Add("Key1", 435);
            hd.Add("Key2", 253);
            hd.Add("Key3", 435);
            hd.Add("Key4", 435);
            hd.Add(null, -3);
            cache.InsertAll(hd);

            IEntryAggregator aggregator = CompositeAggregator.CreateInstance(
                new IEntryAggregator[] { GroupAggregator.CreateInstance(IdentityExtractor.Instance,
                                                                        new Count()),
                                         new LongMax((IdentityExtractor.Instance)) });

            result = cache.Aggregate(cache.Keys, aggregator);

            if (result is IList)
            {
                IDictionary results = (IDictionary)((IList)result)[0];

                Assert.AreEqual(results[435], 3);
                Assert.AreEqual(results[-3], 1);

                Assert.AreEqual(((IList)result)[1], 435);
            }

            CacheFactory.Shutdown();
        }
        public void TestCountAggregator()
        {
            Count agg1 = new Count();

            Assert.IsNotNull(agg1);

            ArrayList al = new ArrayList();

            al.Add(new TestInvocableCacheEntry("Ivan", 173));
            al.Add(new TestInvocableCacheEntry("Goran", 185));
            al.Add(new TestInvocableCacheEntry("Ana", 164));
            Assert.AreEqual(3, agg1.Aggregate(al));

            Assert.AreEqual(10, agg1.AggregateResults(new object[] { agg1.Aggregate(al), 7 }));

            // aggragation on remote cache
            INamedCache cache = CacheFactory.GetCache(CacheName);

            cache.Clear();

            Hashtable ht = new Hashtable();

            ht.Add("countKey1", 435);
            ht.Add("countKey2", 253);
            ht.Add("countKey3", 3);
            ht.Add("countKey4", null);
            ht.Add("countKey5", -3);
            cache.InsertAll(ht);

            IEntryAggregator aggregator = new Count();
            object           count      = cache.Aggregate(cache.Keys, aggregator);

            Assert.AreEqual(ht.Count, count);

            IFilter filter = new GreaterFilter(IdentityExtractor.Instance, 100);

            count = cache.Aggregate(filter, aggregator);
            Assert.AreEqual(2, count);

            CacheFactory.Shutdown();
        }
        public void TestQueryRecorder()
        {
            // aggragation on remote cache
            INamedCache cache = CacheFactory.GetCache(CacheName);

            cache.Clear();

            Hashtable ht = new Hashtable();

            for (int i = 0; i < 200; ++i)
            {
                ht.Add("countKey" + i, i);
            }

            cache.InsertAll(ht);

            IFilter filter = new OrFilter(
                new GreaterFilter(IdentityExtractor.Instance, 100),
                new LessFilter(IdentityExtractor.Instance, 30));
            //explain
            QueryRecorder aggregator = new QueryRecorder(QueryRecorder.RecordType.Explain);
            IQueryRecord  record     = (IQueryRecord)cache.Aggregate(filter, aggregator);

            //Console.WriteLine(record.ToString());
            Assert.AreEqual(1, record.Results.Count);
            Assert.AreEqual(1, ((IPartialResult)record.Results[0]).Steps.Count);
            Assert.AreEqual(2, ((IStep)((IPartialResult)record.Results[0]).Steps[0]).Steps.Count);

            //trace
            aggregator = new QueryRecorder(QueryRecorder.RecordType.Trace);

            record = (IQueryRecord)cache.Aggregate(filter, aggregator);

            //Console.WriteLine(record.ToString());
            Assert.AreEqual(1, record.Results.Count);
            Assert.AreEqual(2, ((IPartialResult)record.Results[0]).Steps.Count);
            Assert.AreEqual(2, ((IStep)((IPartialResult)record.Results[0]).Steps[0]).Steps.Count);
            Assert.AreEqual(2, ((IStep)((IPartialResult)record.Results[0]).Steps[1]).Steps.Count);

            CacheFactory.Shutdown();
        }
        public void TestReducerAggregator()
        {
            INamedCache cache = CacheFactory.GetCache(CacheName);

            cache.Clear();

            Hashtable ht = new Hashtable();

            ht.Add("1", new Address("street1", "city", "state", "00000"));
            ht.Add("2", new Address("street2", "city", "state", "20000"));
            ht.Add("3", new Address("street3", "city", "state", "60000"));
            ht.Add("4", new Address("street4", "city", "state", "50000"));
            ht.Add("5", new Address("street5", "city", "state", "10000"));
            cache.InsertAll(ht);

            // Remote execution, java method names
            ReducerAggregator agent = new ReducerAggregator(new MultiExtractor("getStreet,getZip"));
            IDictionary       m     = (IDictionary)cache.Aggregate(AlwaysFilter.Instance, agent);

            Assert.AreEqual(m.Count, cache.Count);

            object[] results = (object[])m["1"];
            Assert.AreEqual(2, results.Length);
            Assert.AreEqual("street1", results[0]);
            Assert.AreEqual("00000", results[1]);

            results = (object[])m["2"];
            Assert.AreEqual(2, results.Length);
            Assert.AreEqual("street2", results[0]);
            Assert.AreEqual("20000", results[1]);

            // local aggregation
            ReducerAggregator localAgent = new ReducerAggregator(new MultiExtractor("Street,ZIP"));

            m = (IDictionary)localAgent.AggregateResults(ht);

            results = (object[])m["1"];
            Assert.AreEqual(2, results.Length);
            Assert.AreEqual("street1", results[0]);
            Assert.AreEqual("00000", results[1]);

            results = (object[])m["2"];
            Assert.AreEqual(2, results.Length);
            Assert.AreEqual("street2", results[0]);
            Assert.AreEqual("20000", results[1]);

            CacheFactory.Shutdown();
        }
        /// <summary>
        /// Create indexes in the cache and query it for data.
        /// </summary>
        /// <param name="cache">
        /// The target cache to query.
        /// </param>
        public virtual void Query(INamedCache cache)
        {
            Console.WriteLine("------QueryExample begins------");
            // Add indexes to make queries more efficient
            // Ordered index applied to fields used in range and like filter queries
            cache.AddIndex(new ReflectionExtractor("getAge"), /*fOrdered*/ true,
                           /*comparator*/ null);
            cache.AddIndex(new KeyExtractor(new ReflectionExtractor("getLastName")),
                           /*fOrdered*/ true, /*comparator*/ null);
            cache.AddIndex(new ChainedExtractor("getHomeAddress.getCity"),
                           /*fOrdered*/ true, /*comparator*/ null);
            cache.AddIndex(new ChainedExtractor("getHomeAddress.getState"),
                           /*fOrdered*/ false, /*comparator*/ null);
            cache.AddIndex(new ChainedExtractor("getWorkAddress.getState"),
                           /*fOrdered*/ false, /*comparator*/ null);

            // Find all contacts who live in Massachusetts
            ICacheEntry[] aCacheEntry = cache.GetEntries(new EqualsFilter(
                                                             "getHomeAddress.getState", "MA"));
            PrintResults("MA Residents", aCacheEntry);

            // Find all contacts who live in Massachusetts and work elsewhere
            aCacheEntry = cache.GetEntries(new AndFilter(
                                               new EqualsFilter("getHomeAddress.getState", "MA"),
                                               new NotEqualsFilter("getWorkAddress.getState", "MA")));
            PrintResults("MA Residents, Work Elsewhere", aCacheEntry);

            // Find all contacts whose city name begins with 'S'
            aCacheEntry = cache.GetEntries(
                new LikeFilter("getHomeAddress.getCity", "S%"));
            PrintResults("City Begins with S", aCacheEntry);

            int nAge = 58;

            // Find all contacts who are older than nAge
            aCacheEntry = cache.GetEntries(new GreaterFilter("getAge", nAge));
            PrintResults("Age > " + nAge, aCacheEntry);

            // Find all contacts with last name beginning with 'S' that live
            // in Massachusetts. Uses both key and value in the query.
            aCacheEntry = cache.GetEntries(new AndFilter(
                                               new LikeFilter(new KeyExtractor("getLastName"), "S%",
                                                              (char)0, false),
                                               new EqualsFilter("getHomeAddress.getState", "MA")));
            PrintResults("Last Name Begins with S and State Is MA", aCacheEntry);

            // Count contacts who are older than nAge
            Console.WriteLine("count > " + nAge + ": " + cache.Aggregate(
                                  new GreaterFilter("getAge", nAge), new Count()));

            // Find minimum age
            Console.WriteLine("min age: " + cache.Aggregate(
                                  AlwaysFilter.Instance, new LongMin("getAge")));

            // Calculate average age
            Console.WriteLine("avg age: " + cache.Aggregate(
                                  AlwaysFilter.Instance, new DoubleAverage("getAge")));

            // Find maximum age
            Console.WriteLine("max age: " + cache.Aggregate(
                                  AlwaysFilter.Instance, new LongMax("getAge")));
            Console.WriteLine("------QueryExample completed------");
        }
        protected void DoTestTopN(string sCache)
        {
            INamedCache cache = CacheFactory.GetCache(sCache);

            cache.Clear();

            TopNAggregator agent = new TopNAggregator(
                IdentityExtractor.Instance, SafeComparer.Instance, 10);

            object[] aoEmpty = new Object[0];
            object[] aoResult;

            aoResult = (object[])cache.Aggregate(
                NullImplementation.GetCollection(), agent);
            AssertArrayEquals(aoEmpty, aoResult, "null collection");

            aoResult = (object[])cache.Aggregate(
                new ArrayList(new object[] { "1" }), agent);
            AssertArrayEquals(aoEmpty, aoResult, "singleton collection");

            aoResult = (object[])cache.Aggregate((IFilter)null, agent);
            AssertArrayEquals(aoEmpty, aoResult, "null filter");

            aoResult = (object[])cache.Aggregate(AlwaysFilter.Instance, agent);
            AssertArrayEquals(aoEmpty, aoResult, "AlwaysFilter");

            Hashtable ht    = new Hashtable();
            int       cKeys = 10000;

            for (int i = 1; i <= cKeys; i++)
            {
                ht.Add(i.ToString(), i);
            }
            cache.InsertAll(ht);

            object[] aoTop10 = new object[10];
            for (int i = 0; i < 10; i++)
            {
                aoTop10[i] = cKeys - i;
            }

            aoResult = (object[])cache.Aggregate(
                NullImplementation.GetCollection(), agent);
            AssertArrayEquals(aoEmpty, aoResult);

            aoResult = (object[])cache.Aggregate(
                new ArrayList(new object[] { "1" }), agent);
            AssertArrayEquals(new object[] { 1 }, aoResult);

            aoResult = (object[])cache.Aggregate(
                new ArrayList(new object[] { "1" }), agent);
            AssertArrayEquals(new object[] { 1 }, aoResult);

            aoResult = (object[])cache.Aggregate((IFilter)null, agent);
            AssertArrayEquals(aoTop10, aoResult);

            aoResult = (object[])cache.Aggregate(AlwaysFilter.Instance, agent);
            AssertArrayEquals(aoTop10, aoResult);

            // test duplicate values
            cache.Clear();

            cKeys = 100;
            ht    = new Hashtable(cKeys);
            for (int i = 1; i <= cKeys; ++i)
            {
                ht.Add(i.ToString(), i / 2);
            }
            cache.InsertAll(ht);

            aoTop10 = new object[10];
            for (int i = 0; i < 10; ++i)
            {
                aoTop10[i] = (cKeys - i) / 2;
            }

            aoResult = (object[])cache.Aggregate((IFilter)null, agent);
            AssertArrayEquals(aoTop10, aoResult);

            CacheFactory.Shutdown();
        }
        public void TestAggregate()
        {
            IConfigurableCacheFactory ccf = CacheFactory.ConfigurableCacheFactory;

            IXmlDocument config = XmlHelper.LoadXml("assembly://Coherence.Tests/Tangosol.Resources/s4hc-local-cache-config.xml");

            ccf.Config = config;

            INamedCache cache = CacheFactory.GetCache("local-default");

            cache.Clear();


            Hashtable ht = new Hashtable();

            ht.Add("comparableMaxKey1", 100);
            ht.Add("comparableMaxKey2", 80.5);
            ht.Add("comparableMaxKey3", 19.5);
            ht.Add("comparableMaxKey4", 2);
            cache.InsertAll(ht);

            IEntryAggregator aggregator = new DoubleAverage(IdentityExtractor.Instance);
            object           result     = cache.Aggregate(cache.Keys, aggregator);

            Assert.AreEqual(50.5, result);

            cache.Insert("comparableKey5", null);
            result = cache.Aggregate(cache.Keys, aggregator);
            Assert.AreEqual(50.5, result);

            IFilter alwaysFilter = new AlwaysFilter();

            result = cache.Aggregate(alwaysFilter, aggregator);
            Assert.AreEqual(50.5, result);

            cache.Clear();

            ht = new Hashtable();
            ht.Add("comparableMaxKey1", 435);
            ht.Add("comparableMaxKey2", 253);
            ht.Add("comparableMaxKey3", 3);
            ht.Add("comparableMaxKey4", null);
            ht.Add("comparableMaxKey5", -3);
            cache.InsertAll(ht);

            aggregator = new ComparableMax(IdentityExtractor.Instance);
            object max = cache.Aggregate(cache.Keys, aggregator);

            Assert.AreEqual(max, 435);

            max = cache.Aggregate(alwaysFilter, aggregator);
            Assert.AreEqual(max, 435);

            cache.Clear();

            ht = new Hashtable();
            ht.Add("comparableMaxKey1", 435);
            ht.Add("comparableMaxKey2", 253);
            ht.Add("comparableMaxKey3", 3);
            ht.Add("comparableMaxKey4", 3);
            ht.Add("comparableMaxKey5", 3);
            ht.Add("comparableMaxKey6", null);
            ht.Add("comparableMaxKey7", null);
            cache.InsertAll(ht);

            aggregator = new DistinctValues(IdentityExtractor.Instance);
            result     = cache.Aggregate(cache.Keys, aggregator);
            Assert.AreEqual(3, ((ICollection)result).Count);
            foreach (object o in ht.Values)
            {
                Assert.IsTrue(((IList)result).Contains(o) || o == null);
            }

            IFilter lessFilter = new LessFilter(IdentityExtractor.Instance, 100);

            result = cache.Aggregate(lessFilter, aggregator);
            Assert.AreEqual(1, ((ICollection)result).Count);

            cache.Clear();

            ht = new Hashtable();
            ht.Add("key1", 100);
            ht.Add("key2", 80.5);
            ht.Add("key3", 19.5);
            ht.Add("key4", 2);

            cache.InsertAll(ht);

            aggregator = new DoubleSum(IdentityExtractor.Instance);
            result     = cache.Aggregate(cache.Keys, aggregator);
            Assert.AreEqual(202, result);

            IFilter filter = new AlwaysFilter();

            result = cache.Aggregate(filter, aggregator);
            Assert.AreEqual(202, result);


            cache.Clear();

            ht = new Hashtable();
            ht.Add("key1", 100);
            ht.Add("key2", 80);
            ht.Add("key3", 19);
            ht.Add("key4", 2);
            ht.Add("key5", null);

            cache.InsertAll(ht);

            aggregator = new LongSum(IdentityExtractor.Instance);
            result     = cache.Aggregate(cache.Keys, aggregator);
            Assert.AreEqual(201, result);

            IFilter greaterFilter = new GreaterFilter(IdentityExtractor.Instance, 1);

            result = cache.Aggregate(greaterFilter, aggregator);
            Assert.AreEqual(201, result);

            CacheFactory.Shutdown();
        }
        public void TestDistinctValuesAggregator()
        {
            DistinctValues agg1 = new DistinctValues(IdentityExtractor.Instance);

            Assert.IsNotNull(agg1);
            Assert.AreSame(IdentityExtractor.Instance, agg1.Extractor);

            DistinctValues agg2 = new DistinctValues("dummy");

            Assert.IsNotNull(agg2);
            Assert.IsInstanceOf(typeof(ReflectionExtractor), agg2.Extractor);

            DistinctValues agg3 = new DistinctValues("another.dummy");

            Assert.IsNotNull(agg3);
            Assert.IsInstanceOf(typeof(ChainedExtractor), agg3.Extractor);

            ArrayList al = new ArrayList();

            al.Add(new TestInvocableCacheEntry("Ivan", 173));
            al.Add(new TestInvocableCacheEntry("Milica", 173));
            al.Add(new TestInvocableCacheEntry("Goran", 185));
            al.Add(new TestInvocableCacheEntry("Ana", 164));
            ICollection coll = (ICollection)agg1.Aggregate(al);

            Assert.AreEqual(3, coll.Count);

            agg1 = new DistinctValues(IdentityExtractor.Instance);
            al.Clear();
            coll = (ICollection)agg1.Aggregate(al);
            Assert.AreEqual(0, coll.Count);
            Assert.IsInstanceOf(typeof(NullImplementation.NullCollection), coll);

            string[] array1 = new string[] { "Ana", "Ivan", "Goran" };
            string[] array2 = new string[] { "Aleks", "Jason", "Ana" };
            coll = (ICollection)agg1.AggregateResults(new object[] { array1, array2 });
            Assert.AreEqual(6, coll.Count);

            // aggragation on remote cache
            Hashtable ht = new Hashtable();

            ht.Add("distinctValuesKey1", 435);
            ht.Add("distinctValuesKey2", 253);
            ht.Add("distinctValuesKey3", 3);
            ht.Add("distinctValuesKey4", 3);
            ht.Add("distinctValuesKey5", 3);
            ht.Add("distinctValuesKey6", null);
            ht.Add("distinctValuesKey7", null);

            INamedCache cache = CacheFactory.GetCache(CacheName);

            cache.Clear();
            cache.InsertAll(ht);

            IEntryAggregator aggregator = new DistinctValues(IdentityExtractor.Instance);
            object           result     = cache.Aggregate(cache.Keys, aggregator);

            Assert.AreEqual(3, ((ICollection)result).Count);
            foreach (object o in ht.Values)
            {
                Assert.IsTrue(((IList)result).Contains(o) || o == null);
            }
            IFilter filter = new LessFilter(IdentityExtractor.Instance, 100);

            result = cache.Aggregate(filter, aggregator);
            Assert.AreEqual(1, ((ICollection)result).Count);

            //test case for COHNET-109
            filter = new LessFilter(IdentityExtractor.Instance, 0);
            result = cache.Aggregate(filter, aggregator);
            Assert.AreEqual(0, ((ICollection)result).Count);
            Assert.IsInstanceOf(typeof(NullImplementation.NullCollection), result);

            CacheFactory.Shutdown();
        }
        public void TestGroupAggregator()
        {
            GroupAggregator agg1 = GroupAggregator.CreateInstance(IdentityExtractor.Instance,
                                                                  new Count());

            Assert.IsNotNull(agg1);
            Assert.AreSame(IdentityExtractor.Instance, agg1.Extractor);
            Assert.IsInstanceOf(typeof(Count), agg1.Aggregator);

            GroupAggregator agg2 = GroupAggregator.CreateInstance(IdentityExtractor.Instance,
                                                                  new Count(),
                                                                  new LessFilter(
                                                                      IdentityExtractor.Instance, 3));

            Assert.IsNotNull(agg2);
            Assert.IsInstanceOf(typeof(IdentityExtractor), agg2.Extractor);

            GroupAggregator agg3 =
                GroupAggregator.CreateInstance("dummy", new Count());

            Assert.IsNotNull(agg3);
            Assert.IsInstanceOf(typeof(ReflectionExtractor), agg3.Extractor);
            agg3 = GroupAggregator.CreateInstance("dummy.test", new Count());
            Assert.IsNotNull(agg3);
            Assert.IsInstanceOf(typeof(ChainedExtractor), agg3.Extractor);
            agg3 = GroupAggregator.CreateInstance("dummy.test1, dummy.test2", new Count());
            Assert.IsNotNull(agg3);
            Assert.IsInstanceOf(typeof(MultiExtractor), agg3.Extractor);

            ArrayList al = new ArrayList();

            al.Add(new TestInvocableCacheEntry("key1", 173));
            al.Add(new TestInvocableCacheEntry("key2", 173));
            al.Add(new TestInvocableCacheEntry("key3", 185));
            al.Add(new TestInvocableCacheEntry("key4", 164));
            al.Add(new TestInvocableCacheEntry("key5", 164));
            al.Add(new TestInvocableCacheEntry("key6", 164));
            object result = agg2.Aggregate(al);

            if (result is IDictionary)
            {
                Assert.AreEqual(((IDictionary)result)[173], 2);
                Assert.AreEqual(((IDictionary)result)[185], 1);
                Assert.AreEqual(((IDictionary)result)[164], null);
            }

            // aggragation on remote cache
            INamedCache cache = CacheFactory.GetCache(CacheName);

            cache.Clear();

            Hashtable ht = new Hashtable();

            ht.Add("Key1", 435);
            ht.Add("Key2", 253);
            ht.Add("Key3", 435);
            ht.Add("Key4", 435);
            ht.Add("Key5", -3);
            cache.InsertAll(ht);

            IEntryAggregator aggregator = GroupAggregator.CreateInstance(IdentityExtractor.Instance,
                                                                         new Count());

            result = cache.Aggregate(cache.Keys, aggregator);
            if (result is IDictionary)
            {
                Assert.AreEqual(((IDictionary)result)[435], 3);
                Assert.AreEqual(((IDictionary)result)[-3], 1);
            }
            CacheFactory.Shutdown();
        }