MapWriteMode.CREATE_ONLY with MapOperation.putItems

When putting multiple items into a map using MapOperation.putItems, what is the behavior, using MapWriteMode.CREATE_ONLY, if some, but not all, of the items exist already? Will all of the new items be saved? Will nothing be saved?

The transaction will fail with result code 24 (Map key exists) and none of the new items will be saved.

That’s unfortunate. I know I could split this into multiple calls to single putItem ops, but that would induce unwanted overhead for map of any significant size, and spreading those across multiple threads would likely result in a hot key error.

Is there any way to put multiple items into a map, and only write those that are new without causing a map key exists error? This doesn’t seem like it should be an error. I know I could read the existing map first, and avoid the ones that already exist, but I’d prefer to avoid that additional full read as well.

There isn’t any way to do that atomically at this time.

The policies were not meant to be a write logic mechanism. They were designed to be a safety mechanism so it would warn you if you were doing something you weren’t meant to be doing, such as over writing existing keys or creating keys when your data model logic disallows it at certain points.

I will keep this use case in mind if/when we revisit maps/lists in the future.

Java client 4.1.9 has been released.

This client release, coupled with the soon to be released server version 4.3, will allow valid items to succeed while invalid items are discarded. Here is a sample call.

Record record = client.operate(null, key,
    MapOperation.putItems(
        new MapPolicy(MapOrder.UNORDERED, MapWriteFlags.CREATE_ONLY | MapWriteFlags.PARTIAL | MapWriteFlags.NO_FAIL), binName, sourceMap)
    );
1 Like

That’s great news Brian. Thanks to the team for implementing this feature.