简体   繁体   English

Hazelcast IMap localkeyset排序

[英]Hazelcast IMap localkeyset sorting

Are the localkeyset for an IMap sorted? IMap的localkeyset是否已排序? Or, is there anyway I can proxy the backing data structure to be sorted? 或者,无论如何,我可以代理要排序的支持数据结构吗?

I am storing temporal keys in the IMap, and from time to time looking for the oldest local key. 我将临时键存储在IMap中,并不时寻找最早的本地键。 As of now, I am doing a new TreeSet(imap.localKeyset()), however, was wondering if anyway I could make the local backing data structure sorted itself. 到目前为止,我正在做一个新的TreeSet(imap.localKeyset()),但是想知道是否我可以对本地支持数据结构进行排序。

Thanks, Sutanu 谢谢,Sutanu

No, the localKeySet is not sorted and is just a regular Set. 不,localKeySet不会排序,而只是常规Set。 If you want to maintain the order, I would rather prefer to use Queue instead of TreeMap(unless you want to sort it based on some other parameter instead of TimeStamp of addition) and implement a localEntryListener to control this Queue. 如果要保持顺序,我宁愿使用Queue而不是TreeMap(除非您要基于其他参数而不是附加的TimeStamp对其进行排序),并实现localEntryListener来控制此Queue。
Another option is to use TreeSet. 另一种选择是使用TreeSet。

public class HazelcastNode {

    private static Queue localEntryQueue = new LinkedList<>();

    public HazelcastNode(){
        HazelcastInstance hz = Hazelcast.newHazelcastInstance();

        hz.getMap("hzMap").addLocalEntryListener(new MyLocalMapListner());
    }

    public static void main(String[] args){

        HazelcastNode node = new HazelcastNode();

        //Get the Head of the queue which will be the oldest local entry
        HazelcastNode.localEntryQueue.peek();
    }

    private class MyLocalMapListner implements MapListener,EntryAddedListener,
            EntryRemovedListener,
            EntryUpdatedListener,
            EntryEvictedListener,
            EntryExpiredListener{

        @Override
        public void entryAdded(EntryEvent entryEvent) {
            HazelcastNode.localEntryQueue.add(entryEvent.getKey());
        }

        @Override
        public void entryEvicted(EntryEvent entryEvent) {
            HazelcastNode.localEntryQueue.remove(entryEvent.getKey());
        }

        @Override
        public void entryExpired(EntryEvent entryEvent) {
            HazelcastNode.localEntryQueue.remove(entryEvent.getKey());
        }

        @Override
        public void entryRemoved(EntryEvent entryEvent) {
            HazelcastNode.localEntryQueue.remove(entryEvent.getKey());
        }

        @Override
        public void entryUpdated(EntryEvent entryEvent) {
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM