0
votes

can i somehow use linkedHashMap in Hazelcast (java spring). I need to get unique records from hazelcast shared in-memory cache but in order in which I inserted them. I found in hazelcast documentation (https://docs.hazelcast.org/docs/latest-dev/manual/html-single/) they offers distributed implementations of common data structures. But map doesnt preserves elements order and list or queue dont remove duplicite data. Do you know if i can use linkedHashMap or somehow get unique data and preserves their order?

1
How many entries will you likely have ? Do they have a field such as @CreatedDate or similar that would enable you to reliably deduce insertion order ? - Neil Stevenson

1 Answers

0
votes

Ordered or linked storage isn't compatible with the goals of a data grid - highly concurrent and distributed storage.

Ordered retrieval is possible. Hazelcast's Paging Predicate with a comparator would do it. Or the volume is not too high, you could retreive the entry set and sort it yourself.

The catch is, you have to provide the field to order upon.

If your data already has some sort of sequence number or timestamp that is always unique, this is easy.

If not, perhaps something like Atomic Long would do it. A getAndIncrement() would give you a unique number to use for each insert.

Watch though, this has a race condition if two or more threads insert concurrently. To solve this you'd need some sort of singleton @Service running somewhere to do the "get next seqno ; inset` step.

And if you restart the grid, the seqno in the atomic counter will need repositioned to the right place.