Example #1
0
        public void testGet()
        {
            IDictionary map = new HashDictionary {
                { "one", 1 }, { "two", 2 },
                { "three", 3 }, { "four", 4 }, { "five", 5 }
            };

            IValueExtractor  extractor = new IdentityExtractor();
            IFilter          filter    = new LessFilter(extractor, 5);
            ConditionalIndex index     = createIndex(map, filter, extractor, true);

            Assert.AreEqual(1, index.Get("one"));
            Assert.AreEqual(2, index.Get("two"));
            Assert.AreEqual(3, index.Get("three"));
            Assert.AreEqual(4, index.Get("four"));
            Assert.AreEqual(ObjectUtils.NO_VALUE, index.Get("five"));

            // forward map support == false
            index = createIndex(map, filter, extractor, false);

            Assert.AreEqual(ObjectUtils.NO_VALUE, index.Get("one"));
            Assert.AreEqual(ObjectUtils.NO_VALUE, index.Get("two"));
            Assert.AreEqual(ObjectUtils.NO_VALUE, index.Get("three"));
            Assert.AreEqual(ObjectUtils.NO_VALUE, index.Get("four"));
            Assert.AreEqual(ObjectUtils.NO_VALUE, index.Get("five"));
        }
Example #2
0
        public void NearCacheListenNoneAggregateTest()
        {
            LocalNamedCache localcache = new LocalNamedCache();
            INamedCache     safecache  = CacheFactory.GetCache(CacheName);
            NearCache       nearcache  = new NearCache(localcache, safecache, CompositeCacheStrategyType.ListenNone);

            nearcache.Clear();

            Hashtable ht = new Hashtable();

            ht.Add("Aleks", 1);
            ht.Add("Ana", 2);
            ht.Add("Goran", 3);
            ht.Add("Ivan", 4);
            nearcache.InsertAll(ht);

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

            Assert.AreEqual(ht.Count, count);

            IFilter filter = new LessFilter(IdentityExtractor.Instance, 3);

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

            nearcache.Clear();
            nearcache.Release();
            Assert.IsFalse(nearcache.IsActive);

            CacheFactory.Shutdown();
        }
Example #3
0
        public void testInsert_forwardIndexFalse()
        {
            // create the ConditionalIndex to be tested
            IValueExtractor extractor = new IdentityExtractor();
            IFilter         filter    = new LessFilter(extractor, 15);
            var             mapIndex  = new ConditionalIndex(filter, extractor, true, null, false);

            // define the keys and values for the mock entries
            const string oKey    = "key";
            Object       oValue  = 1;
            const string oKey2   = "key2";
            Object       oValue2 = 2;
            const string oKey3   = "key3";
            Object       oValue3 = 25;
            ICacheEntry  entry   = new CacheEntry(oKey, oValue);
            ICacheEntry  entry2  = new CacheEntry(oKey2, oValue2);
            ICacheEntry  entry3  = new CacheEntry(oKey3, oValue3);

            // verify that the index does not contain a value for the tested keys
            Assert.AreEqual(ObjectUtils.NO_VALUE, mapIndex.Get(oKey));
            Assert.AreEqual(ObjectUtils.NO_VALUE, mapIndex.Get(oKey2));
            Assert.AreEqual(ObjectUtils.NO_VALUE, mapIndex.Get(oKey3));

            // insert into the index
            mapIndex.Insert(entry);
            mapIndex.Insert(entry2);
            mapIndex.Insert(entry3);

            // all gets should return NO_VALUE since forward index is not supported
            // verify no value from the index for key
            object oIndexValue = mapIndex.Get(oKey);

            Assert.AreEqual(ObjectUtils.NO_VALUE, oIndexValue);

            // verify no value from the index for key2
            object oIndexValue2 = mapIndex.Get(oKey2);

            Assert.AreEqual(ObjectUtils.NO_VALUE, oIndexValue2);

            // verify no value in the index for key3
            object oIndexValue3 = mapIndex.Get(oKey3);

            Assert.AreEqual(ObjectUtils.NO_VALUE, oIndexValue3);

            // verify that the value for key and key2 is the same instance
            Assert.AreSame(oIndexValue, oIndexValue2,
                           "The value for key and key2 should be the same instance.");

            // get the inverse map
            IDictionary mapInverse = mapIndex.IndexContents;

            // get the entry from the inverse map keyed by the extracted value
            var inverseEntry = mapInverse[oValue] as HashSet;

            Assert.IsNotNull(inverseEntry);

            // verify that the set of keys contains key
            Assert.IsTrue(inverseEntry.Contains(oKey),
                          "The index's inverse map should contain the key.");
        }
Example #4
0
        public void testDelete()
        {
            // create the ConditionalIndex to be tested
            IValueExtractor extractor = new IdentityExtractor();
            IFilter         filter    = new LessFilter(extractor, 15);
            var             mapIndex  = new ConditionalIndex(filter, extractor, true, null, true);

            // define the keys and values
            const string oKey       = "key";
            Object       oValue     = 1;
            Object       oExtracted = 1;
            ICacheEntry  entry      = new CacheEntry(oKey, oValue, oValue);

            // begin test

            // verify that the index does not contain a value for the tested keys
            Assert.AreEqual(ObjectUtils.NO_VALUE, mapIndex.Get(oKey));

            // insert into the index
            mapIndex.Insert(entry);

            object extractedValue = mapIndex.Get(oKey);

            Assert.AreEqual(oExtracted, extractedValue,
                            "The index should contain the extracted value for key.");

            mapIndex.Delete(entry);

            Assert.AreEqual(ObjectUtils.NO_VALUE, mapIndex.Get(oKey));
        }
        public void TestGetValues()
        {
            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("key1", 100.0);
            ht.Add("key2", 80.5);
            ht.Add("key3", 19.5);
            ht.Add("key4", 2.0);

            cache.InsertAll(ht);
            Assert.AreEqual(4, cache.Count);

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

            object[] result = cache.GetValues(lessFilter);
            Assert.AreEqual(2, result.Length);
            foreach (object o in result)
            {
                Assert.IsTrue((Convert.ToDouble(o) == 19.5) || (Convert.ToDouble(o) == 2.0));
            }

            CacheFactory.Shutdown();
        }
Example #6
0
            public void SetUp()
            {
                string nestedDependencyFilename = Path.Combine("a", "b", "variables.less");
                string nestedDependencyText     = "@import \"dependency.less\";";

                dependencyFilename = Path.Combine("a", "b", "dependency.less");
                var dependencyText = "@color: #4D926F;";

                source   = "@import \"variables.less\";#header {color: @color;}h2 {color: @color;}";
                expected = "#header{color:#4d926f}h2{color:#4d926f}";

                arguments = new Dictionary <string, object> {
                    {
                        "item",
                        new Dictionary <string, object> {
                            { "filename", Path.Combine("a", "b", "c.less") }
                        }
                    }
                };

                fileSystem = Substitute.For <IFileSystem>();
                fileSystem.FileExists(nestedDependencyFilename).Returns(true);
                fileSystem.ReadStringFromFile(nestedDependencyFilename).Returns(nestedDependencyText);
                fileSystem.FileExists(dependencyFilename).Returns(true);
                fileSystem.ReadStringFromFile(dependencyFilename).Returns(dependencyText);

                lessFilter = new LessFilter(fileSystem);
            }
Example #7
0
        public void TestConditionalRemove()
        {
            ConditionalRemove conditionalRemove  = new ConditionalRemove(new GreaterFilter(IdentityExtractor.Instance, 100), true);
            ConditionalRemove conditionalRemove1 = new ConditionalRemove(new GreaterFilter(IdentityExtractor.Instance, 100), true);

            Assert.AreEqual(conditionalRemove, conditionalRemove1);
            Assert.AreEqual(conditionalRemove.ToString(), conditionalRemove1.ToString());
            Assert.AreEqual(conditionalRemove.GetHashCode(), conditionalRemove1.GetHashCode());

            IInvocableCacheEntry entry = new LocalCache.Entry(new LocalCache(), "key1", 1200);
            Object result = conditionalRemove.Process(entry);

            Assert.IsNull(entry.Value);

            entry  = new LocalCache.Entry(new LocalCache(), "key1", 50);
            result = conditionalRemove.Process(entry);
            Assert.AreEqual(50, entry.Value);

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

            cache.Clear();

            Hashtable ht = new Hashtable();

            ht.Add("conditionalPutAllKey1", 435);
            ht.Add("conditionalPutAllKey2", 253);
            ht.Add("conditionalPutAllKey3", 200);
            ht.Add("conditionalPutAllKey4", 333);
            cache.InsertAll(ht);

            IFilter greaterThen300 = new GreaterFilter(IdentityExtractor.Instance, 300);
            IFilter lessThen300    = new LessFilter(IdentityExtractor.Instance, 300);

            Assert.IsTrue(cache.Count == 4);

            // remove key1 with greaterThen300 filter applied
            ConditionalRemove processor = new ConditionalRemove(greaterThen300, false);

            cache.Invoke("conditionalPutAllKey1", processor);
            Assert.IsTrue(cache.Count == 3);

            // remove all entries that satisfy filter criteria
            processor = new ConditionalRemove(greaterThen300, false);
            cache.InvokeAll(ht.Keys, processor);
            Assert.IsTrue(cache.Count == 2);
            Assert.IsNotNull(cache["conditionalPutAllKey2"]);
            Assert.IsNotNull(cache["conditionalPutAllKey3"]);

            processor = new ConditionalRemove(lessThen300, false);
            cache.InvokeAll(new GreaterFilter(IdentityExtractor.Instance, 200), processor);
            Assert.IsTrue(cache.Count == 1);
            Assert.IsNotNull(cache["conditionalPutAllKey3"]);

            CacheFactory.Shutdown();
        }
Example #8
0
        public void testGetIndexContents()
        {
            var map = new HashDictionary
            {
                { "one", 1 },
                { "another_one", 1 },
                { "one_more", 1 },
                { "two", 2 },
                { "three", 3 },
                { "four", 4 },
                { "four_again", 4 },
                { "five", 5 },
                { "five_a", 5 },
                { "five_b", 5 },
                { "five_c", 5 },
                { "five_d", 5 }
            };

            IValueExtractor  extractor     = new IdentityExtractor();
            IFilter          filter        = new LessFilter(extractor, 5);
            ConditionalIndex index         = createIndex(map, filter, extractor, true);
            IDictionary      indexContents = index.IndexContents;

            var setOne = indexContents[1] as HashSet;

            Assert.IsNotNull(setOne);
            Assert.AreEqual(3, setOne.Count);
            Assert.IsTrue(setOne.Contains("one"));
            Assert.IsTrue(setOne.Contains("another_one"));
            Assert.IsTrue(setOne.Contains("one_more"));

            var setTwo = indexContents[2] as HashSet;

            Assert.IsNotNull(setTwo);
            Assert.AreEqual(1, setTwo.Count);
            Assert.IsTrue(setTwo.Contains("two"));

            var setThree = indexContents[3] as HashSet;

            Assert.IsNotNull(setThree);
            Assert.AreEqual(1, setThree.Count);
            Assert.IsTrue(setThree.Contains("three"));

            var setFour = indexContents[4] as HashSet;

            Assert.IsNotNull(setFour);
            Assert.AreEqual(2, setFour.Count);
            Assert.IsTrue(setFour.Contains("four"));
            Assert.IsTrue(setFour.Contains("four_again"));

            var setFive = indexContents[5] as ICollection;

            Assert.IsNull(setFive);
        }
        public void TestRemove()
        {
            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("conditionalPutAllKey1", 435);
            ht.Add("conditionalPutAllKey2", 253);
            ht.Add("conditionalPutAllKey3", 200);
            ht.Add("conditionalPutAllKey4", 333);
            cache.InsertAll(ht);

            IFilter greaterThen300 = new GreaterFilter(IdentityExtractor.Instance, 300);
            IFilter lessThen300    = new LessFilter(IdentityExtractor.Instance, 300);
            IFilter key3           = new LikeFilter(new KeyExtractor(IdentityExtractor.Instance), "%Key3", '\\', false);

            Assert.IsTrue(cache.Count == 4);

            // remove key1 with greaterThen300 filter applied
            ConditionalRemove processor = new ConditionalRemove(greaterThen300, false);

            cache.Invoke("conditionalPutAllKey1", processor);
            Assert.IsTrue(cache.Count == 3);

            // remove all entries that satisfy filter criteria
            processor = new ConditionalRemove(greaterThen300, false);
            cache.InvokeAll(ht.Keys, processor);
            Assert.IsTrue(cache.Count == 2);
            Assert.IsNotNull(cache["conditionalPutAllKey2"]);
            Assert.IsNotNull(cache["conditionalPutAllKey3"]);

            processor = new ConditionalRemove(lessThen300, false);
            cache.InvokeAll(new GreaterFilter(IdentityExtractor.Instance, 200), processor);
            Assert.IsTrue(cache.Count == 1);
            Assert.IsNotNull(cache["conditionalPutAllKey3"]);

            processor = new ConditionalRemove(key3, false);
            cache.InvokeAll(new GreaterFilter(IdentityExtractor.Instance, 100), processor);
            Assert.IsTrue(cache.Count == 0);

            CacheFactory.Shutdown();
        }
Example #10
0
        public void TestConditional()
        {
            IFilter lessThen250 = new LessFilter(IdentityExtractor.Instance, 250);
            ConditionalProcessor condProcessor  = new ConditionalProcessor(new GreaterFilter(IdentityExtractor.Instance, 200), new ConditionalRemove(lessThen250, false));
            ConditionalProcessor condProcessor1 = new ConditionalProcessor(new GreaterFilter(IdentityExtractor.Instance, 200), new ConditionalRemove(lessThen250, false));

            Assert.AreEqual(condProcessor, condProcessor1);
            Assert.AreEqual(condProcessor.GetHashCode(), condProcessor1.GetHashCode());
            Assert.AreEqual(condProcessor.ToString(), condProcessor1.ToString());

            LocalCache.Entry entry = new LocalCache.Entry(new LocalCache(), "key1", 225);
            condProcessor.Process(entry);
            Assert.IsNull(entry.Value);

            entry = new LocalCache.Entry(new LocalCache(), "key1", 150);
            condProcessor.Process(entry);
            Assert.AreEqual(150, entry.Value);

            // testing on Remote cache
            INamedCache cache = CacheFactory.GetCache(CacheName);

            cache.Clear();

            Hashtable ht = new Hashtable();

            ht.Add("conditionalKey1", 200);
            ht.Add("conditionalKey2", 250);
            ht.Add("conditionalKey3", 300);
            ht.Add("conditionalKey4", 400);
            cache.InsertAll(ht);


            IFilter lessThen300            = new LessFilter(IdentityExtractor.Instance, 300);
            ConditionalProcessor processor = new ConditionalProcessor(new GreaterFilter(IdentityExtractor.Instance, 200), new ConditionalRemove(lessThen300, false));

            Assert.IsTrue(cache.Count == 4);
            cache.Invoke("conditionalKey4", processor);
            Assert.IsTrue(cache.Count == 4);

            cache.Invoke("conditionalKey3", processor);
            Assert.IsTrue(cache.Count == 4);

            cache.Invoke("conditionalKey2", processor);
            Assert.IsTrue(cache.Count == 3);

            CacheFactory.Shutdown();
        }
Example #11
0
            public void should_be_able_to_transform_source()
            {
                string source   = "@color: #4D926F;#header {color: @color;}h2 {color: @color;}";
                string expected = "#header{color:#4d926f}h2{color:#4d926f}";

                var arguments = new Dictionary <string, object> {
                    {
                        "item",
                        new Dictionary <string, object> {
                            { "filename", Path.Combine("a", "b", "c.less") }
                        }
                    }
                };

                var fileSystem = Substitute.For <IFileSystem>();

                var lessFilter = new LessFilter(fileSystem);

                string result = lessFilter.Execute(source, arguments);

                result.ShouldBe(expected);
            }
Example #12
0
        public void testDelete_forwardIndexFalse()
        {
            // create the ConditionalIndex to be tested
            IValueExtractor extractor = new IdentityExtractor();
            IFilter         filter    = new LessFilter(extractor, 15);
            var             mapIndex  = new ConditionalIndex(filter, extractor, true, null, false);

            // define the keys and values for the mock entries
            const string oKey       = "key";
            Object       oValue     = 1;
            Object       oExtracted = 1;
            ICacheEntry  entry      = new CacheEntry(oKey, oValue);
            ICacheEntry  delEntry   = new CacheEntry(oKey, oValue, oValue);

            // begin test

            // assert the the inverse map does not contain an entry for the extracted value
            Assert.IsFalse(mapIndex.IndexContents.Contains(oExtracted));

            // insert into the index
            mapIndex.Insert(entry);

            // assert the the inverse map does contain an entry for the extracted value
            Assert.IsTrue(mapIndex.IndexContents.Contains(oExtracted));

            mapIndex.Delete(delEntry);

            // get the inverse map
            var mapInverse = (SynchronizedDictionary)mapIndex.IndexContents;

            // get the set of keys from the inverse map keyed by the extracted
            // value for key
            var set = (HashSet)mapInverse[oExtracted];

            // verify that the set of keys does not contain key
            Assert.IsTrue(set == null || !set.Contains(oKey),
                          "The index's inverse map should not contain the key for the extracted value.");
        }
        public void TestConditional()
        {
            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("conditionalKey1", 200);
            ht.Add("conditionalKey2", 250);
            ht.Add("conditionalKey3", 300);
            ht.Add("conditionalKey4", 400);
            cache.InsertAll(ht);


            IFilter lessThen300            = new LessFilter(IdentityExtractor.Instance, 300);
            ConditionalProcessor processor = new ConditionalProcessor(
                new GreaterFilter(IdentityExtractor.Instance, 200),
                new ConditionalRemove(lessThen300, false));

            Assert.IsTrue(cache.Count == 4);
            cache.Invoke("conditionalKey4", processor);
            Assert.IsTrue(cache.Count == 4);

            cache.Invoke("conditionalKey3", processor);
            Assert.IsTrue(cache.Count == 4);

            cache.Invoke("conditionalKey2", processor);
            Assert.IsTrue(cache.Count == 3);

            CacheFactory.Shutdown();
        }
Example #14
0
        public void NearCacheListenNoneInvokeAllTest()
        {
            LocalNamedCache localcache = new LocalNamedCache();
            INamedCache     safecache  = CacheFactory.GetCache(CacheName);
            NearCache       nearcache  = new NearCache(localcache, safecache, CompositeCacheStrategyType.ListenNone);

            nearcache.Clear();

            Hashtable ht = new Hashtable();

            ht.Add("Aleks", 1);
            ht.Add("Ana", 2);
            ht.Add("Goran", 3);
            ht.Add("Ivan", 4);
            nearcache.InsertAll(ht);

            Hashtable htput = new Hashtable();

            htput.Add("Aleks", 195);
            htput.Add("Ana", 205);

            IFilter         filter    = new LessFilter(IdentityExtractor.Instance, 2);
            IEntryProcessor processor = new ConditionalPutAll(AlwaysFilter.Instance, htput);

            nearcache.InvokeAll(filter, processor);
            Assert.AreEqual(195, nearcache.BackCache["Aleks"]);
            Assert.AreEqual(2, nearcache.BackCache["Ana"]);

            nearcache.InvokeAll(htput.Keys, processor);
            Assert.AreEqual(205, nearcache.BackCache["Ana"]);

            nearcache.Clear();
            nearcache.Release();
            Assert.IsFalse(nearcache.IsActive);

            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();
        }
Example #16
0
        public void testUpdate_forwardIndexFalse()
        {
            // create the ConditionalIndex to be tested
            IValueExtractor extractor = new IdentityExtractor();
            IFilter         filter    = new LessFilter(extractor, 15);
            var             mapIndex  = new ConditionalIndex(filter, extractor, true, null, false);

            // define the keys and values
            const string oKey           = "key";
            Object       oValue         = 0;
            Object       oExtracted     = 0;
            Object       oNewValue      = 1;
            Object       oExtractedNew  = 1;
            const string oKey2          = "key2";
            Object       oValue2        = 2;
            Object       oExtracted2    = 2;
            const string oKey3          = "key3";
            Object       oValue3        = 21;
            Object       oNewValue2     = 4;
            Object       oExtractedNew2 = 4;
            ICacheEntry  entry          = new CacheEntry(oKey, oValue);
            ICacheEntry  entry2         = new CacheEntry(oKey2, oValue2, oValue);
            ICacheEntry  entry3         = new CacheEntry(oKey3, oValue3, oValue2);
            ICacheEntry  entryNew       = new CacheEntry(oKey, oNewValue, oValue);
            ICacheEntry  entryNew2      = new CacheEntry(oKey2, oNewValue2, oValue2);

            // begin test

            // verify that the index does not contain a value for the tested keys
            Assert.AreEqual(ObjectUtils.NO_VALUE, mapIndex.Get(oKey));
            Assert.AreEqual(ObjectUtils.NO_VALUE, mapIndex.Get(oKey2));
            Assert.AreEqual(ObjectUtils.NO_VALUE, mapIndex.Get(oKey3));

            // insert into the index
            mapIndex.Insert(entry);  // key, oExtracted
            mapIndex.Insert(entry2); // key, oExtracted2
            mapIndex.Insert(entry3); // key, oExtracted3

            // all gets should return NO_VALUE since forward index is not supported
            // verify no value from the index for key
            object oIndexValue = mapIndex.Get(oKey);

            Assert.AreEqual(ObjectUtils.NO_VALUE, oIndexValue);

            // verify no value from the index for key2
            object oIndexValue2 = mapIndex.Get(oKey2);

            Assert.AreEqual(ObjectUtils.NO_VALUE, oIndexValue2);

            // verify no value in the index for key3
            object oIndexValue3 = mapIndex.Get(oKey3);

            Assert.AreEqual(ObjectUtils.NO_VALUE, oIndexValue3);

            // update the index
            mapIndex.Update(entryNew);   // key, oExtractedNew
            mapIndex.Update(entryNew2);  // key2, oExtractedNew2

            // all gets should return NO_VALUE since forward index is not supported
            // verify no value from the index for key
            oIndexValue = mapIndex.Get(oKey);
            Assert.AreEqual(ObjectUtils.NO_VALUE, oIndexValue);

            oIndexValue2 = mapIndex.Get(oKey2);
            Assert.AreEqual(ObjectUtils.NO_VALUE, oIndexValue2);

            // get the inverse map
            var mapInverse = (SynchronizedDictionary)mapIndex.IndexContents;

            // get the set of keys from the inverse map keyed by the extracted
            // value for key
            var set = mapInverse[oExtractedNew] as HashSet;

            Assert.IsNotNull(set);

            // verify that the set of keys contains key
            Assert.IsTrue(set.Contains(oKey),
                          "The index's inverse map should contain the key.");

            // get the set of keys from the inverse map keyed by the old extracted
            // value for key
            set = (HashSet)mapInverse[oExtracted];

            // verify that the set of keys does not contain key
            Assert.IsTrue(set == null || !set.Contains(oKey),
                          "The index's inverse map should not contain the key for the old extracted value.");

            // get the set of keys from the inverse map keyed by the extracted
            // value for key2
            set = mapInverse[oExtractedNew2] as HashSet;
            Assert.IsNotNull(set);

            // verify that the set of keys contains key2
            Assert.IsTrue(set.Contains(oKey2),
                          "The index's inverse map should contain the key2.");

            // get the set of keys from the inverse map keyed by the old extracted
            // value for key2
            set = mapInverse[oExtracted2] as HashSet;

            // verify that the set of keys does not contain key2
            Assert.IsTrue(set == null || !set.Contains(oKey2),
                          "The index's inverse map should not contain key2 for the old extracted value.");
        }
Example #17
0
        public void testUpdate()
        {
            // create the ConditionalIndex to be tested
            IValueExtractor extractor = new IdentityExtractor();
            IFilter         filter    = new LessFilter(extractor, 15);
            var             mapIndex  = new ConditionalIndex(filter, extractor, true, null, true);

            // define the keys and values
            const string oKey           = "key";
            Object       oValue         = 0;
            Object       oExtracted     = 0;
            const string oKey2          = "key2";
            Object       oValue2        = 11;
            Object       oExtracted2    = 11;
            const string oKey3          = "key3";
            Object       oValue3        = 21;
            Object       oExtracted3    = 21;
            Object       oNewValue      = oValue2;
            Object       oExtractedNew  = 11;
            Object       oNewValue2     = 30;
            Object       oExtractedNew2 = 30;
            ICacheEntry  entry          = new CacheEntry(oKey, oValue);
            ICacheEntry  entry2         = new CacheEntry(oKey2, oValue2);
            ICacheEntry  entry3         = new CacheEntry(oKey3, oValue3);
            ICacheEntry  entryNew       = new CacheEntry(oKey, oNewValue, oValue);
            ICacheEntry  entryNew2      = new CacheEntry(oKey2, oNewValue2, oValue2);

            // begin test

            // verify that the index does not contain a value for the tested keys
            Assert.AreEqual(ObjectUtils.NO_VALUE, mapIndex.Get(oKey));
            Assert.AreEqual(ObjectUtils.NO_VALUE, mapIndex.Get(oKey2));
            Assert.AreEqual(ObjectUtils.NO_VALUE, mapIndex.Get(oKey3));

            // insert into the index
            mapIndex.Insert(entry);   //key  (extracted value : 0)
            mapIndex.Insert(entry2);  //key2 (extracted value : 2)
            mapIndex.Insert(entry3);  //key3 (extracted value : 21)

            // verify the value from the index for key
            object oIndexValue = mapIndex.Get(oKey);

            Assert.AreEqual(oExtracted, oIndexValue,
                            "The index should contain the extracted value for key.");

            // verify the value from the index for key2
            object oIndexValue2 = mapIndex.Get(oKey2);

            Assert.AreEqual(oExtracted2, oIndexValue2,
                            "The index should contain the extracted value for key2.");

            // since the extracted value (21) for key3 fails the filter check
            // LessFilter(extractor, new Integer(15)), it should not be part of
            // the index
            object oIndexValue3 = mapIndex.Get(oKey3);

            Assert.AreEqual(ObjectUtils.NO_VALUE, oIndexValue3);

            // get the inverse map
            var mapInverse = (SynchronizedDictionary)mapIndex.IndexContents;

            // assert the the inverse map does contain an entry for the
            // extracted values for key
            Assert.IsTrue(mapInverse.Contains(oExtracted));

            // assert that the set mapped to the extracted value for key contains
            // key
            var set = mapInverse[oExtracted] as HashSet;

            Assert.IsNotNull(set);
            Assert.IsTrue(set.Contains(oKey),
                          "The index's inverse map should contain the key.");

            // assert the the inverse map does contain an entry for the
            // extracted values for key2
            Assert.IsTrue(mapInverse.Contains(oExtracted2));

            // assert that the set mapped to the extracted value for key2 contains
            // key2
            set = mapInverse[oExtracted2] as HashSet;
            Assert.IsNotNull(set);
            Assert.IsTrue(set.Contains(oKey2),
                          "The index's inverse map should contain the key2.");

            // assert the the inverse map does not contain an entry for the
            // extracted value for key3
            Assert.IsFalse(mapInverse.Contains(oExtracted3));

            // update the index
            mapIndex.Update(entryNew);   // key  (extracted value : 11)
            mapIndex.Update(entryNew2);  // key2 (extracted value : 30)

            // assert the the index now contains the updated value for key
            oIndexValue = mapIndex.Get(oKey);
            Assert.AreEqual(oExtractedNew, oIndexValue,
                            "The index should contain the updated value for key.");

            // assert that the instance for the extracted value 11 is reused
            Assert.AreSame(oIndexValue, oIndexValue2,
                           "The value for key and key2 should be the same instance.");

            // verify the value for key2 is no longer available from the index
            // since the updated extracted value (30) for key2 fails the filter
            // check : LessFilter(extractor, new Integer(15)), it should not be
            // part of the index
            oIndexValue2 = mapIndex.Get(oKey2);
            Assert.AreEqual(ObjectUtils.NO_VALUE, oIndexValue2,
                            "The index should not contain the extracted value for key2.");

            // assert the inverse map does contain an entry for the
            // extracted value for key
            mapInverse = (SynchronizedDictionary)mapIndex.IndexContents;
            Assert.IsTrue(mapInverse.Contains(oExtractedNew));

            // assert that the set mapped to the old extracted value for key
            // no longer contains key... result of update
            set = mapInverse[oExtracted] as HashSet;
            Assert.IsTrue(set == null || !set.Contains(oKey),
                          "The index's inverse map should not contain key.");

            // assert that the set mapped to the extracted value for key contains
            // key
            set = mapInverse[oExtractedNew] as HashSet;
            Assert.IsNotNull(set);
            Assert.IsTrue(set.Contains(oKey), "The index's inverse map should contain key.");

            // assert that the set mapped to the old extracted value for key2
            // no longer contains key2... result of update
            set = mapInverse[oExtracted2] as HashSet;
            Assert.IsTrue(set == null || !set.Contains(oKey2),
                          "The index's inverse map should not contain key2.");

            // assert the the inverse map does not contain an entry for the new
            // extracted value for key2... fails filter check
            set = mapInverse[oExtractedNew2] as HashSet;
            Assert.IsTrue(set == null || !set.Contains(oKey2),
                          "The index's inverse map should not contain key2.");
        }
Example #18
0
        public void testInsert()
        {
            // create the ConditionalIndex to be tested
            IValueExtractor extractor = new IdentityExtractor();
            IFilter         filter    = new LessFilter(extractor, 15);
            var             mapIndex  = new ConditionalIndex(filter, extractor, true, null, true);

            // define the keys and values for the mock entries
            const string oKey    = "key";
            Object       oValue  = 11;
            const string oKey2   = "key2";
            Object       oValue2 = oValue;
            const string oKey3   = "key3";
            Object       oValue3 = 25;
            ICacheEntry  entry   = new CacheEntry(oKey, oValue);
            ICacheEntry  entry2  = new CacheEntry(oKey2, oValue2);
            ICacheEntry  entry3  = new CacheEntry(oKey3, oValue3);

            // begin test

            // verify that the index does not contain a value for the tested keys
            Assert.AreEqual(ObjectUtils.NO_VALUE, mapIndex.Get(oKey));
            Assert.AreEqual(ObjectUtils.NO_VALUE, mapIndex.Get(oKey2));
            Assert.AreEqual(ObjectUtils.NO_VALUE, mapIndex.Get(oKey3));

            // assert the the inverse map does not contain an entry for the extracted values
            Assert.IsFalse(mapIndex.IndexContents.Contains(oValue));
            Assert.IsFalse(mapIndex.IndexContents.Contains(oValue2));

            // insert into the index
            mapIndex.Insert(entry);
            mapIndex.Insert(entry2);
            mapIndex.Insert(entry3);

            // verify the value from the index for key
            object oIndexValue = mapIndex.Get(oKey);

            Assert.AreEqual(oValue, oIndexValue);

            // verify the value from the index for key2
            object oIndexValue2 = mapIndex.Get(oKey2);

            Assert.AreEqual(oValue2, oIndexValue2);

            // verify no value in the index for key3
            object oIndexValue3 = mapIndex.Get(oKey3);

            Assert.AreEqual(ObjectUtils.NO_VALUE, oIndexValue3);

            // verify that the value for key and key2 is the same instance
            Assert.AreSame(oIndexValue, oIndexValue2);

            // get the inverse map
            var mapInverse = (SynchronizedDictionary)mapIndex.IndexContents;

            // get the entry from the inverse map keyed by the extracted value
            var inverseEntry = mapInverse[oValue] as HashSet;

            Assert.IsNotNull(inverseEntry);
            Assert.IsNotEmpty(inverseEntry);
            Assert.IsTrue(inverseEntry.Contains(oKey));

            // get the entry from the inverse map keyed by the extracted value
            var inverseEntry2 = mapInverse[oIndexValue2] as HashSet;

            Assert.IsNotNull(inverseEntry2);
            Assert.IsNotEmpty(inverseEntry2);

            // verify that the set of keys contains key
            Assert.IsTrue(inverseEntry2.Contains(oKey2));

            // get the set of keys from the inverse map keyed by the extracted
            // value for key
            var set = mapInverse[oIndexValue] as HashSet;

            Assert.IsNotNull(set);

            // verify that the set of keys contains key
            Assert.IsTrue(set.Contains(oKey));

            // get the set of keys from the inverse map keyed by the extracted
            // value for key2
            set = mapInverse[oIndexValue2] as HashSet;
            Assert.IsNotNull(set);

            // verify that the set of keys contains key2
            Assert.IsTrue(set.Contains(oKey2));
        }
        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();
        }