- Newest
- Most votes
- Most comments
For me, I would perhaps use a secondary index for this. Lets imagine each of the items in the pool are a separate item and your table looks like this:
CallNo | Date | Other | isTaken | User |
---|---|---|---|---|
1234 | 2022-09-01T00:00 | Data | No | |
9876 | 2022-09-02T16:00 | Data | Jeff | |
5678 | 2022-09-01T10:00 | Data | No | |
4567 | 2022-09-01T12:00 | Data | Lee |
No, each of the available calls have the attribute isTaken
with a value of "No". So then we have a Global Secondary Index which is sparse in nature, and we decide the partition key for that is the isTaken
column while the sort key is Date
, in which only items from the table with a value for isTaken
will be stored in the index, ordered by the date:
isTaken | Date | CallNo | Other |
---|---|---|---|
No | 2022-09-01T00:00 | 1234 | Data |
No | 2022-09-01T10:00 | 5678 | Data |
So now you can see the only items in the index are the calls which are in the Available queue. So when an operator wishes to retrieve an available call, they do a Query
operation on the index defining a partition key of No
and a Limit=1
, in which they will retrieve the oldest item in the waiting queue:
aws dynamodb query \
--table-name test1 \
--index-name available \
--key-condition-expression "isTaken = :val" \
--expression-attribute-values '{":val":{"S":"No"}}' \
--limit 1
isTaken | Date | CallNo | Other |
---|---|---|---|
No | 2022-09-01T00:00 | 1234 | Data |
Once an operator receives their item, they must lock it to themselves by adding their User
name to it and removing the isTaken
value to remove it from the available queue:
aws dynamodb update-item \
--table-name test1 \
--key '{"CallNo": {"N": "1234"}}' \
--update-expression "SET #attr_one = :attr_val REMOVE #attr_two " \
--expression-attribute-names '{"#attr_one":"User", "#attr_two: "isTaken"}' \
--expression-attribute-values '{":attr_one":{"S": "Lee"}}' \
--condition-expression "attribute_exists(#attr_two)"
The update will remove the item from the GSI by removing the value for isTaken
, it uses a condition expression only to update that item if isTaken
already exists, if it evaluates to false, the update will fail which means another user has already taken that call and you have to poll the available queue for the next available call.
After this, your table will look like below:
CallNo | Date | Other | isTaken | User |
---|---|---|---|---|
1234 | 2022-09-01T00:00 | Data | Lee | |
9876 | 2022-09-02T16:00 | Data | Jeff | |
5678 | 2022-09-01T10:00 | Data | No | |
4567 | 2022-09-01T12:00 | Data | Lee |
And the Available index:
isTaken | Date | CallNo | Other |
---|---|---|---|
No | 2022-09-01T10:00 | 5678 | Data |
Hello - You can update (or perform general CRUD operations) on a single item by following this guide.
The TLDR is essentially:
DynamoDB provides four operations for basic create, read, update, and delete (CRUD) functionality. All these operations are atomic.
PutItem — Create an item.
GetItem — Read an item.
UpdateItem — Update an item.
DeleteItem — Delete an item.
Each of these operations requires that you specify the primary key of the item that you want to work with. For example, to read an item using GetItem, you must specify the partition key and sort key (if applicable) for that item.
Thanks, but being more specific: I need to update just one record by the condition status and not for the primary key. Based on this I could have several records, but I need to update only one with a code to know what agent has taken the record.
Does make sense?
So you are adding a pool of numbers into an item? You'd need to use filter expressions - https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Query.html#Query.FilterExpression
It might make more sense for each number to be it's own item though. It would make adding and querying attributes much simpler.
Relevant content
- Accepted Answerasked a year ago
- asked a year ago
- AWS OFFICIALUpdated 2 months ago
- AWS OFFICIALUpdated 2 months ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 7 months ago
Thanks Leeroy, sounds very good!
You're welcome Armand, please feel free to accept the answer should it satisfy your use-case.