Search code examples
gemfirespring-data-gemfire

Update the value in region ONLY IF value.status is 'XXX'


We are trying to use Gemfire in our work. We have a region where we store each request coming in and it goes through its lifecycle (For example, states are A --> B --> C --> D)

But we also have a requirement that we need to update the state to C only if the current state is B (as the state D is getting updated Async by some other process). We used to achieve it in cassandra by using ONLY IF key word. Looking for something similar in Gemfire. Obviously we cannot do Read, Check State and Update because its not atomic.

Other option was to do this by taking a distribted lock and then perform check-update as mentioned above. But this option comes with a performance overhead.

We were also thinking of attaching a CacheWriter and check the state in beforeUpdate(..). But came to know that what we get as parameter to beforeUpdate is a copy of the value and not the real value.

Does anyone have an idea of how to achieve it in a atomic fashion that we can try?


Solution

  • What you are looking for is this, Region.replace(key, oldValue, newValue). This is an atomic operation.

    UPDATE: I should also clarify that is not currently possible to inspect certain properties of the mapped object value (e.g. someObject.state = XYZ) to ascertain whether to perform the update/replace. For that you will need a properly implemented Object.equals() method.