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();
        }
Exemplo n.º 2
0
 /// <summary>
 /// Compare the GroupAggregator with another object to determine
 /// equality.
 /// </summary>
 /// <param name="o">
 /// The <b>GroupAggregator</b> to compare to.
 /// </param>
 /// <returns>
 /// <b>true</b> if this GroupAggregator and the passed object are
 /// equivalent.
 /// </returns>
 public override bool Equals(object o)
 {
     if (o is GroupAggregator)
     {
         GroupAggregator that = (GroupAggregator)o;
         return(Equals(m_extractor, that.m_extractor) && Equals(m_aggregator, that.m_aggregator));
     }
     return(false);
 }
        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();
        }
        public void TestAggregatorSerialization()
        {
            ConfigurablePofContext ctx = new ConfigurablePofContext("assembly://Coherence.Tests/Tangosol.Resources/s4hc-test-config.xml");

            Assert.IsNotNull(ctx);

            ComparableMax       comparableMax       = new ComparableMax("member1");
            ComparableMin       comparableMin       = new ComparableMin("member2");
            CompositeAggregator compositeAggregator = new CompositeAggregator();
            DecimalAverage      decimalAverage      = new DecimalAverage("member3");
            DecimalMax          decimalMax          = new DecimalMax("member4");
            DecimalMin          decimalMin          = new DecimalMin("member5");
            DecimalSum          decimalSum          = new DecimalSum("member6");
            DistinctValues      distinctValues      = new DistinctValues("member7");
            DoubleAverage       doubleAverage       = new DoubleAverage("member8");
            DoubleMax           doubleMax           = new DoubleMax("member9");
            DoubleMin           doubleMin           = new DoubleMin("member10");
            DoubleSum           doubleSum           = new DoubleSum("member11");
            GroupAggregator     groupAggregator     = new GroupAggregator();
            LongMax             longMax             = new LongMax("member12");
            LongMin             longMin             = new LongMin("member13");
            LongSum             longSum             = new LongSum("member14");
            PriorityAggregator  priorityAggregator  = new PriorityAggregator();
            Count count = new Count();

            Stream stream = new MemoryStream();

            ctx.Serialize(new DataWriter(stream), comparableMax);
            ctx.Serialize(new DataWriter(stream), comparableMin);
            ctx.Serialize(new DataWriter(stream), compositeAggregator);
            ctx.Serialize(new DataWriter(stream), decimalAverage);
            ctx.Serialize(new DataWriter(stream), decimalMax);
            ctx.Serialize(new DataWriter(stream), decimalMin);
            ctx.Serialize(new DataWriter(stream), decimalSum);
            ctx.Serialize(new DataWriter(stream), distinctValues);
            ctx.Serialize(new DataWriter(stream), doubleAverage);
            ctx.Serialize(new DataWriter(stream), doubleMax);
            ctx.Serialize(new DataWriter(stream), doubleMin);
            ctx.Serialize(new DataWriter(stream), doubleSum);
            ctx.Serialize(new DataWriter(stream), groupAggregator);
            ctx.Serialize(new DataWriter(stream), longMax);
            ctx.Serialize(new DataWriter(stream), longMin);
            ctx.Serialize(new DataWriter(stream), longSum);
            ctx.Serialize(new DataWriter(stream), priorityAggregator);
            ctx.Serialize(new DataWriter(stream), count);

            stream.Position = 0;
            Assert.AreEqual(comparableMax, ctx.Deserialize(new DataReader(stream)));
            Assert.AreEqual(comparableMin, ctx.Deserialize(new DataReader(stream)));
            Assert.AreEqual(compositeAggregator, ctx.Deserialize(new DataReader(stream)));
            Assert.AreEqual(decimalAverage, ctx.Deserialize(new DataReader(stream)));
            Assert.AreEqual(decimalMax, ctx.Deserialize(new DataReader(stream)));
            Assert.AreEqual(decimalMin, ctx.Deserialize(new DataReader(stream)));
            Assert.AreEqual(decimalSum, ctx.Deserialize(new DataReader(stream)));
            Assert.AreEqual(distinctValues, ctx.Deserialize(new DataReader(stream)));
            Assert.AreEqual(doubleAverage, ctx.Deserialize(new DataReader(stream)));
            Assert.AreEqual(doubleMax, ctx.Deserialize(new DataReader(stream)));
            Assert.AreEqual(doubleMin, ctx.Deserialize(new DataReader(stream)));
            Assert.AreEqual(doubleSum, ctx.Deserialize(new DataReader(stream)));
            Assert.AreEqual(groupAggregator, ctx.Deserialize(new DataReader(stream)));
            Assert.AreEqual(longMax, ctx.Deserialize(new DataReader(stream)));
            Assert.AreEqual(longMin, ctx.Deserialize(new DataReader(stream)));
            Assert.AreEqual(longSum, ctx.Deserialize(new DataReader(stream)));
            Assert.AreEqual(priorityAggregator.GetType(), ctx.Deserialize(new DataReader(stream)).GetType());
            Assert.AreEqual(count.GetType(), ctx.Deserialize(new DataReader(stream)).GetType());

            stream.Close();
        }