Here I have a five entries as below: (14,4); (21,1); (32,2); (113,3); (15,5); and I want to use SortedEvictionPolicy to keep below 3 entries (sort by values in descending order and get top 3 items): 15-->5 14-->4 113-->3
The actual output is: 21-->1 32-->2 113-->3 14-->4 15-->5 issue 1: The order is wrong, the class CustomizedComparator doesn't seem to work. issue 2: there are only 3 entries are expected, but it returns 5 ones, MaxSize doesn't work. Did I miss anything? The source code: package IgniteTesting.Expiry; import javax.cache.Cache.Entry; import org.apache.ignite.Ignite; import org.apache.ignite.IgniteCache; import org.apache.ignite.Ignition; import org.apache.ignite.cache.CacheMode; import org.apache.ignite.cache.eviction.sorted.SortedEvictionPolicy; import org.apache.ignite.cache.query.QueryCursor; import org.apache.ignite.cache.query.SqlQuery; import org.apache.ignite.configuration.CacheConfiguration; public class Application { public static void main( String[] args ) { try(Ignite ignite = Ignition.start("......\\test.xml")){ CacheConfiguration<Integer, Integer> cfg = new CacheConfiguration<>(); cfg.setName("foo"); cfg.setOnheapCacheEnabled(true); cfg.setCacheMode(CacheMode.LOCAL); SortedEvictionPolicy<Integer,Integer> SEP = new SortedEvictionPolicy<>(3,new CustomizedComparator()); cfg.setEvictionPolicy(SEP); cfg.setIndexedTypes(Integer.class, Integer.class); try(IgniteCache<Integer, Integer> cache=ignite.getOrCreateCache(cfg)){ cache.put(14,4); cache.put(21,1); cache.put(32,2); cache.put(113,3); cache.put(15,5); SqlQuery<Integer,Integer> sql = new SqlQuery<>(Integer.class, "select * from Integer"); try (QueryCursor<Entry<Integer, Integer>> cursor = cache.query(sql)) { for (Entry<Integer, Integer> e : cursor) System.out.println(e.getKey()+"-->"+e.getValue().toString()); } } } } } package IgniteTesting.Expiry; import java.io.Serializable; import java.util.Comparator; import org.apache.ignite.cache.eviction.EvictableEntry; public class CustomizedComparator implements Serializable,Comparator<EvictableEntry<Integer,Integer>> { private static final long serialVersionUID = 4755938152431669886L; public int compare(EvictableEntry<Integer,Integer> a, EvictableEntry<Integer,Integer> b) { return a.getValue() > b.getValue() ? -1 : a.getValue() == b.getValue() ? 0 : 1; } } -- Sent from: http://apache-ignite-users.70518.x6.nabble.com/