Looking into ChronicleMap (2.1.7), and I'm not exactly clear on the proper usage of WriteContext when calling acquireUsingLocked();
The scenario I'm interested in is a function where I need to take 2 actions atomically from the viewpoint of the function caller. One of them is adding an entry to the map, if not present. The other one should only take place if there was previously no value for the given key in the map.
If there was previously no value for a given key in the map, and this second action fails, the map should not be updated, so that subsequent tests would find no value for the corresponding key.
If there was already an entry in the map for the given key, I don't want to update its original value, and I don't want to undertake this second action. But, I do need to use the original entry's value in order to construct the return value for the caller.
The documentation for WriteContext.created() says that it returns true only if the entry was previously present. If the entry was previously present, is it possible to get a reference to the previous value from the context (or some other way - like calling map.get() within the WriteContext scope)?
What does WriteContext.value() refer to? The previous entry's value, or the potentially updated one I provided in the call to acquireUsingLocked()?
Also, if there wasn't previously an entry in the map, and I don't want to update the map, should I call WriteContext.removeEntry(), or WriteContext.dontPutOnClose()?
Here's an example of the logic I'm thinking I need:
EntryData newValue = new EntryData();
EntryData originalValue = null;
try (WriteContext<String, EntryData> context = _map.acquireUsingLocked(key, newValue) ) {
if ( !context.created() ) {
if ( doSomething() ) {
result = createResult(newValue);
}
else {
context.removeEntry();
result = null;
}
}
else {
context.dontPutOnClose();
originalValue = context.value();
result = createResult(originalValue);
}
}