Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append and truncate list in single DynamoDB update operation?

I'd like to have what is essentially a capped list in a DynamoDB item. I want to be able to prepend an item to the front of the list, and then drop the item off the back if it exceeds a certain size.

In my examples I'll set the cap to 3, prepend the element "6", and show before/after states:

                    { } -> { "myList": [6] }
      { "myList": [1] } -> { "myList": [6, 1] }
{ "myList": [3, 2, 1] } -> { "myList": [6, 3, 2] }

But I can't use the following update expression to combine a SET and REMOVE operation on myList:

# (":empty" set to an empty list in the value map)
SET myList = list_append(:v, if_not_exists(myList, :empty)) REMOVE myList[3]

Attempting to use this expression results in an exception (using the Document API in Java):

Invalid UpdateExpression: Two document paths overlap with each other; must remove or rewrite one of these paths

It'll work if I split the SET and REMOVE into two different update requests. Is there any trick that would let me do it in a single request?

like image 230
ryryguy Avatar asked Sep 09 '16 21:09

ryryguy


1 Answers

Currently in DynamoDB, you cannot have the same document path in more than one operation, even if they are separate operations. This is because DynamoDB cannot guarantee the order of operation executions in the same update expression.

like image 89
gruuuvy Avatar answered Oct 20 '22 00:10

gruuuvy