public void TestEntryTouch() { 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); object result = cache["key2"]; result = cache["key2"]; result = cache["key2"]; result = cache["key1"]; ICacheEntry[] results = cache.GetEntries(new EqualsFilter(new KeyExtractor(IdentityExtractor.Instance), "key2")); // TODO : we don't get back the actual entry!!! // Assert.AreEqual(3, (results[0] as LocalCache.Entry).TouchCount); results = cache.GetEntries(new EqualsFilter(new KeyExtractor(IdentityExtractor.Instance), "key1")); // Assert.AreEqual(1, (results[0] as LocalCache.Entry).TouchCount); CacheFactory.Shutdown(); }
public void TestGetEntries() { INamedCache cache = CacheFactory.GetCache(CacheName); cache.Clear(); IDictionary dict = new Hashtable(); for (int i = 1; i <= 10; i++) { dict.Add(GetKeyObject("key" + i), i); } cache.InsertAll(dict); IFilter filter = new GreaterFilter(IdentityExtractor.Instance, 5); ICacheEntry[] entries = cache.GetEntries(filter); Assert.AreEqual(entries.Length, 5); object[] keys = new object[5]; object[] values = new object[5]; for (int i = 0; i < 5; i++) { ICacheEntry entry = entries[i]; keys[i] = entry.Key; values[i] = entry.Value; } for (int i = 6; i <= 10; i++) { Assert.Contains(GetKeyObject("key" + i), keys); Assert.Contains(i, values); } entries = cache.GetEntries(filter, SafeComparer.Instance); Assert.AreEqual(entries.Length, 5); keys = new object[5]; values = new object[5]; for (int i = 0; i < 5; i++) { ICacheEntry entry = entries[i]; keys[i] = entry.Key; values[i] = entry.Value; } for (int i = 6; i <= 10; i++) { Assert.Contains(GetKeyObject("key" + i), keys); Assert.Contains(i, values); } Assert.AreEqual(values[0], 6); Assert.AreEqual(values[1], 7); Assert.AreEqual(values[2], 8); Assert.AreEqual(values[3], 9); Assert.AreEqual(values[4], 10); CacheFactory.Shutdown(); }
/// <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 TestPofExtractorWithFilter() { INamedCache cache = CacheFactory.GetCache(CacheName); cache.Clear(); for (int i = 1901; i <= 2000; i++) { PortablePersonLite customer = new PortablePersonLite(); customer.Name = "Name" + i; customer.DOB = new DateTime(i, 1, 1); cache.Insert(i, customer); } DateTime criteria = new DateTime(1970, 1, 1); IValueExtractor extractor = new PofExtractor(null, 2); IFilter filter2 = new LessEqualsFilter(extractor, criteria); Assert.AreEqual(cache.GetEntries(filter2).Length, 70, "Expected: 70; Result was: " + cache.GetEntries(filter2).Length); CacheFactory.Shutdown(); }
public void TestFilters() { 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(); cache.Insert("5", "5"); cache.Insert(1, "10"); cache.Insert("g", "15"); cache.Insert("b", "20"); cache.Insert("1", "105"); IFilter[] filters = new IFilter[] { new EqualsFilter(IdentityExtractor.Instance, "20"), new LikeFilter(IdentityExtractor.Instance, "1%", '\\', true) }; AnyFilter anyFilter = new AnyFilter(filters); ICollection results = cache.GetKeys(anyFilter); Assert.AreEqual(4, results.Count); Assert.IsTrue(CollectionUtils.Contains(results, 1)); Assert.IsTrue(CollectionUtils.Contains(results, "g")); Assert.IsTrue(CollectionUtils.Contains(results, "b")); Assert.IsTrue(CollectionUtils.Contains(results, "1")); filters = new IFilter[] { new EqualsFilter(IdentityExtractor.Instance, "20"), new LikeFilter(IdentityExtractor.Instance, "5%", '\\', true) }; anyFilter = new AnyFilter(filters); ICacheEntry[] entries = cache.GetEntries(anyFilter); Assert.AreEqual(2, entries.Length); Assert.Contains("b", new object[] { entries[0].Key, entries[1].Key }); Assert.Contains("5", new object[] { entries[0].Key, entries[1].Key }); Assert.Contains("20", new object[] { entries[0].Value, entries[1].Value }); Assert.Contains("5", new object[] { entries[0].Value, entries[1].Value }); CacheFactory.Shutdown(); }
public void TestComparer() { INamedCache cache = CacheFactory.GetCache("dist-comparator-cache"); Random r = new Random(); for (int i = 0; i < 10000; i++) { AirDealComparer.AirDeal deal = new AirDealComparer.AirDeal(i, "SFO", "JFK", r.NextDouble()); cache.Add(deal.Oid, deal); } IValueExtractor ve = new ReflectionExtractor("getOrigAirport"); cache.AddIndex(ve, true, null); IFilter primaryFilter = new EqualsFilter(ve, "SFO"); IFilter filterLimit = new LimitFilter(primaryFilter, 40); ICollection setReturn = cache.GetEntries(filterLimit, new AirDealComparer()); Assert.AreEqual(setReturn.Count, 40); }
public void TestLimitFilter() { INamedCache cache = CacheFactory.GetCache(CacheName); LimitFilter limitFilter = new LimitFilter(new AlwaysFilter(), 10); IDictionary <Int32, String> mapReturn = new Dictionary <Int32, String>(); bool entryReturned = true; int totalCount = 0, uniqueCount = 0; cache.Clear(); for (int i = 0; i < CACHE_SIZE; i++) { cache.Insert(i, "value" + i); } while (entryReturned) { entryReturned = false; foreach (ICacheEntry entry in cache.GetEntries(limitFilter)) { ++totalCount; entryReturned = true; if (!mapReturn.ContainsKey((int)entry.Key)) { mapReturn.Add((Int32)entry.Key, (String)entry.Value); ++uniqueCount; } } limitFilter.NextPage(); } ; Assert.AreEqual(CACHE_SIZE, totalCount); Assert.AreEqual(totalCount, uniqueCount); }
/// <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------"); }