/// <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 TestNamedCacheIndex() { INamedCache cache = CacheFactory.GetCache(CacheName); IValueExtractor extractor = IdentityExtractor.Instance; cache.Clear(); cache.AddIndex(extractor, false, null); cache.RemoveIndex(extractor); IComparer comparer = new SafeComparer(); extractor = new KeyExtractor(IdentityExtractor.Instance); cache.AddIndex(extractor, true, comparer); cache.RemoveIndex(extractor); CacheFactory.ReleaseCache(cache); 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); }
/// <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------"); }