Search code examples
ignite

How can I find the largest key in a cache


I have a cache () that has keys that increase (although not necessarily in sequence e.g. 1002, 1003, 1020, 1100)

I'm using the .Net version of Ignite (which is a wrapper around the java version)

Right now I'm getting the entire set and querying the results using Linq.

How can retrieve the largest key efficiently without using Linq.


Solution

  • An efficient way is to use SQL (with a proper index if the number is part of the value).

    You can continue to use LINQ with Ignite LINQ provider to keep the code clean: https://apacheignite-sql.readme.io/docs/linq

    Simple example with int keys:

            var cfg = new CacheConfiguration("cache-name", new QueryEntity(typeof(int), typeof(int)));
            var cache = ignite.GetOrCreateCache<int, int>(cfg);
    
            cache[1] = 1;
            cache[3] = 3;
    
            var max = cache.AsCacheQueryable().Max(e => e.Key);
    

    This is going to be translated to SQL and executed efficiently by Ignite without loading entire data set locally.