Skip to content
Advertisement

Aerospike: atomically create a list if it does not exists and append items to this list

The bin in aerospike is a Map[String -> List]

I am trying to achieve the behavior as such:

bin.computeIfAbsent(key, k -> new List()).addAll(itemsToAdd)

Is there a way to do this atomically in Aerospike without implementing a UDF? If I read the documentation correctly, if it was a Map[String -> Map], I could have used CTX.mapKeyCreate to create the inner Map on demand, but I don’t see anything similar for creating a List

UPD: Here is what I am trying to do

I have a stream of triplets:

{"pk","attr","value"}

I need to sink this stream into aerospike set aggregating by pk and attr in the following format:

{
   "PK":"pk",
   "mapBin": {
      "attr": ["value"]
    }
}

So let’s say there are three items in the stream: {"pk","attr1","value1"},{"pk","attr2","value2"},{"pk","attr1","value3"}

They need to land in the aerospike as such:

{
   "PK":"pk",
   "mapBin": {
      "attr1": ["value1","value3"],
      "attr2": ["value2"]
    }
}

To insert a new item {"pk","attr2","value2"} I need to perform several actions:

  1. Get the list at mapBin[attr2]
  2. If it does not exist, insert empty List into the map
  3. Do ListOperation.append to append the item to existing list

Question is: is there a way to do it atomically without UDF?

Advertisement

Answer

You should be able to perform these actions atomically with the following (Java syntax):

record = client.operate(null, pk, 
    ListOperation.append("mapBin", Value.get("value2"), 
    CTX.mapKeyCreate(Value.get("attr2"), 
        MapOrder.UNORDERED)));

You can use ListOperation.appendItems() if there are multiple items to be appended to a list, and multiple ListOperations for different lists in the map within the same operate(), all of which will be executed atomically.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement