1
votes

I have a hazelcast IMap in which I have overridden the load, store etc other functions so that the backup is taken in my MongoDB database also. So when there is an addition to the hazelcast IMap there is a corresponding backup also taken in MongoDB. But how do I override the replace function? I want that whenever there is an update to the hazelcast map's existing entry, there should be an update to the corresponding MongoDB document too.

Edit: This is the code of my store method

public void store(Key key, Doc value) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String json = objectMapper.writeValueAsString(value);
            BasicDBObject document = (BasicDBObject) JSON.parse(json);
            this.collection.insertOne(new Document(document));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

I am using MapStore and overriding the load, store etc functions, to enable a backup on MongoDB too . Now I have a MongoDB collection which has entries with autogenerated ids. My hazelcast map however fetches 5 fields and makes a Key Object, and stores the document corresponding to the key object. The map is basically <Key, Doc> format where Key itself is a class of five fields.

So when i use replace function, ( map.replace(Key, Doc) ) , the Doc corresponding to Key is updated in the hazelcast map, but MongoDB inserts another document with the id as a combination of some system parameters like timestamp etc and the value as the correct document. I however want it to update the same entry, but where should I specify the code for mongodb to pickup the "id" field from the document and update that document itself?

Edit 2:

I have realised that I need to update the store function to make it search for an existing document with same id and update it, if it doesn't exist then insert it. Now the question is, the id i am obtaining when hazelcast is trying to call the store function is totally different, a combination of timestamp, machine identifier, process identifier etc. Why is that?

When I get an object from a map, it has the id that mongodb had, but when I modify and then insert it back, it has a different id?

1

1 Answers

0
votes

@shagufta-oliveyu-methwani, can you clarify what you mean by override the replace function?

I believe you have a MapStore to load/store data from a backend storage. If that's the case, when you call map.update, your MapStore store method will be invoked before data stored in Hazelcast map.

Please see the related documentation: http://docs.hazelcast.org/docs/latest/manual/html-single/index.html#loading-and-storing-persistent-data

You can find a sample project here: https://github.com/hazelcast/hazelcast-code-samples/tree/master/distributed-map/mapstore

If you're not using MapSore, this is the correct way to interact Hazelcast map with a 3rd party data store.