static async Task <(string itemIdText, Guid itemId, long size)[]> GetItemAndSizeListAsync(IAmazonDynamoDB client, string tableName, Guid ownerId, Guid scoreId) { var partitionKey = ScoreItemDatabaseUtils.ConvertToPartitionKey(ownerId); var score = ScoreDatabaseUtils.ConvertToBase64(scoreId); var request = new QueryRequest() { TableName = tableName, ExpressionAttributeNames = new Dictionary <string, string>() { ["#owner"] = ScoreItemDatabasePropertyNames.OwnerId, ["#item"] = ScoreItemDatabasePropertyNames.ItemId, }, ExpressionAttributeValues = new Dictionary <string, AttributeValue>() { [":owner"] = new AttributeValue(partitionKey), [":score"] = new AttributeValue(score), }, KeyConditionExpression = "#owner = :owner and begins_with(#item, :score)", }; try { var response = await client.QueryAsync(request); return(response.Items .Where(x => x[ScoreItemDatabasePropertyNames.ItemId].S != ScoreItemDatabaseConstant.ItemIdSummary) .Select(GetItemAndSizeAsync).ToArray()); } catch (Exception ex) { Console.WriteLine(ex.Message); throw; }
static async Task PutDataAsync( IAmazonDynamoDB client, string tableName, ScoreItemDatabaseItemDataBase itemData, long maxSize, DateTimeOffset now) { var(items, partitionKey, _, item, totalSize) = ScoreItemDatabaseUtils.CreateDynamoDbValue(itemData, now); var actions = new List <TransactWriteItem>() { new TransactWriteItem() { Update = new Update() { Key = new Dictionary <string, AttributeValue>() { [ScoreItemDatabasePropertyNames.OwnerId] = new AttributeValue(partitionKey), [ScoreItemDatabasePropertyNames.ItemId] = new AttributeValue(ScoreItemDatabaseConstant.ItemIdSummary), }, ExpressionAttributeNames = new Dictionary <string, string>() { ["#size"] = ScoreItemDatabasePropertyNames.Size, }, ExpressionAttributeValues = new Dictionary <string, AttributeValue>() { [":itemSize"] = new AttributeValue() { N = totalSize.ToString() }, [":maxSize"] = new AttributeValue() { N = (maxSize - totalSize).ToString() }, }, ConditionExpression = "#size < :maxSize", UpdateExpression = "ADD #size :itemSize", TableName = tableName, }, }, new TransactWriteItem() { Put = new Put() { Item = items, TableName = tableName, ExpressionAttributeNames = new Dictionary <string, string>() { ["#item"] = ScoreItemDatabasePropertyNames.ItemId, }, ConditionExpression = "attribute_not_exists(#item)", } }, }; try { await client.TransactWriteItemsAsync(new TransactWriteItemsRequest() { TransactItems = actions, ReturnConsumedCapacity = ReturnConsumedCapacity.TOTAL }); } catch (ResourceNotFoundException ex) { Console.WriteLine(ex.Message); throw; } catch (InternalServerErrorException ex) { Console.WriteLine(ex.Message); throw; } catch (TransactionCanceledException ex) { Console.WriteLine(ex.Message); throw; } catch (Exception ex) { Console.WriteLine(ex.Message); throw; } }
static async Task <ScoreItemDatabaseItemDataBase[]> GetAsync(IAmazonDynamoDB client, string tableName, Guid ownerId) { var partitionKey = ScoreItemDatabaseUtils.ConvertToPartitionKey(ownerId); var request = new QueryRequest() { TableName = tableName, ExpressionAttributeNames = new Dictionary <string, string>() { ["#owner"] = ScoreItemDatabasePropertyNames.OwnerId, }, ExpressionAttributeValues = new Dictionary <string, AttributeValue>() { [":owner"] = new AttributeValue(partitionKey), }, KeyConditionExpression = "#owner = :owner", Limit = 500, }; try { var response = await client.QueryAsync(request); var items = response.Items.ToList(); while (0 < response.LastEvaluatedKey?.Count) { var nextRequest = new QueryRequest() { TableName = tableName, ExpressionAttributeNames = new Dictionary <string, string>() { ["#owner"] = ScoreItemDatabasePropertyNames.OwnerId, ["#item"] = ScoreItemDatabasePropertyNames.ItemId, }, ExpressionAttributeValues = new Dictionary <string, AttributeValue>() { [":owner"] = new AttributeValue(partitionKey), [":item"] = new AttributeValue(response .LastEvaluatedKey[ScoreItemDatabasePropertyNames.ItemId].S), }, KeyConditionExpression = "#owner = :owner and :item < #item", Limit = 500, }; var nextResponse = await client.QueryAsync(nextRequest); items.AddRange(nextResponse.Items); response = nextResponse; } return(items .Where(x => x[ScoreItemDatabasePropertyNames.ItemId].S != ScoreItemDatabaseConstant.ItemIdSummary) .Select(ScoreItemDatabaseUtils.ConvertFromDynamoDbValue) .ToArray()); } catch (Exception ex) { Console.WriteLine(ex.Message); throw; } }