Beispiel #1
0
        public async Task <IList <BillingAlertItem> > Get(IEnumerable <int> userIds)
        {
            var response = await _dynamoDbClient.BatchGetItemAsync(new BatchGetItemRequest(new Dictionary <string, KeysAndAttributes>
            {
                { _settings.TableName, new KeysAndAttributes {
                      Keys = userIds.Distinct().Select(u => new Dictionary <string, AttributeValue> {
                            { IdAttribute, new AttributeValue {
                                  N = u.ToString()
                              } }
                        }).ToList(),
                      AttributesToGet = new List <string>
                      {
                          nameof(BillingAlertItem.CustomerId).ToLower(),
                          nameof(BillingAlertItem.AlertAmountThreshold).ToLower(),
                          nameof(BillingAlertItem.TotalBillAmount).ToLower(),
                          nameof(BillingAlertItem.BillAmountLastUpdated).ToLower(),
                          nameof(BillingAlertItem.IsAlerted).ToLower(),
                      }
                  } }
            }));

            if (response.HttpStatusCode != System.Net.HttpStatusCode.OK)
            {
                throw new Exception($"Failed to get items from {nameof(BillingAlertStore)}");
            }

            var items = response.Responses.FirstOrDefault(r => r.Key == _settings.TableName).Value;

            return(items.Select(i => Map(i)).ToList());
        }
        public async Task <List <BatchGetItemResponse> > DynamoDbGetCacheVideoDataAsync(List <string> videoIds)
        {
            var listOfResponses    = new List <BatchGetItemResponse>();
            var batchVideoRequests = new List <List <Dictionary <string, AttributeValue> > >();
            var batchVideoIds      = videoIds.Select(x => new Dictionary <string, AttributeValue> {
                { "videoId", new AttributeValue {
                      S = x
                  } }
            }).ToList();

            for (int i = 0; i < batchVideoIds.Count; i += 25)
            {
                batchVideoRequests.Add(batchVideoIds.GetRange(i, Math.Min(25, batchVideoIds.Count - i)));
            }
            foreach (var batchVideo in batchVideoRequests)
            {
                var getBatchRequest = new BatchGetItemRequest {
                    RequestItems = new Dictionary <string, KeysAndAttributes> {
                        { _dynamoDbVideoTableName, new KeysAndAttributes {
                              Keys = batchVideo
                          } }
                    }
                };
                listOfResponses.Add(await _dynamoDbClient.BatchGetItemAsync(getBatchRequest));
            }
            return(listOfResponses);
        }
        public async Task <IEnumerable <WorkflowInstance> > GetWorkflowInstances(IEnumerable <string> ids)
        {
            if (ids == null)
            {
                return(new List <WorkflowInstance>());
            }

            var keys = new KeysAndAttributes()
            {
                Keys = new List <Dictionary <string, AttributeValue> >()
            };

            foreach (var id in ids)
            {
                var key = new Dictionary <string, AttributeValue>()
                {
                    {
                        "id", new AttributeValue {
                            S = id
                        }
                    }
                };
                keys.Keys.Add(key);
            }

            var request = new BatchGetItemRequest
            {
                RequestItems = new Dictionary <string, KeysAndAttributes>()
                {
                    {
                        $"{_tablePrefix}-{WORKFLOW_TABLE}", keys
                    }
                }
            };

            var result = new List <Dictionary <string, AttributeValue> >();
            BatchGetItemResponse response;

            do
            {
                response = await _client.BatchGetItemAsync(request);

                foreach (var tableResponse in response.Responses)
                {
                    result.AddRange(tableResponse.Value);
                }

                request.RequestItems = response.UnprocessedKeys;
            } while (response.UnprocessedKeys.Count > 0);

            return(result.Select(i => i.ToWorkflowInstance()));
        }
        private static async Task CallUntilCompletionAsync(IAmazonDynamoDB client, BatchGetItemRequest request, Results allResults, CancellationToken cancellationToken)
#endif
        {
            do
            {
                var serviceResponse = await client.BatchGetItemAsync(request, cancellationToken).ConfigureAwait(false);

                foreach (var kvp in serviceResponse.Responses)
                {
                    var tableName = kvp.Key;
                    var items     = kvp.Value;

                    allResults.Add(tableName, items);
                }
                request.RequestItems = serviceResponse.UnprocessedKeys;
            } while (request.RequestItems.Count > 0);
        }
Beispiel #5
0
        async IAsyncEnumerable <BatchGetItemResponse> IPaginator <BatchGetItemResponse> .PaginateAsync(CancellationToken cancellationToken = default)
        {
            if (Interlocked.Exchange(ref _isPaginatorInUse, 1) != 0)
            {
                throw new System.InvalidOperationException("Paginator has already been consumed and cannot be reused. Please create a new instance.");
            }
            var requestItems = _request.RequestItems;
            BatchGetItemResponse response;

            do
            {
                _request.RequestItems = requestItems;
                response = await _client.BatchGetItemAsync(_request, cancellationToken).ConfigureAwait(false);

                requestItems = response.UnprocessedKeys;
                cancellationToken.ThrowIfCancellationRequested();
                yield return(response);
            }while (requestItems.Count > 0);
        }
        public async Task <string[]> GetSeenItems(string[] items, CancellationToken token)
        {
            BatchGetItemResponse response = await dynamo.BatchGetItemAsync(new BatchGetItemRequest
            {
                RequestItems = new Dictionary <string, KeysAndAttributes>
                {
                    {
                        StateTableName,
                        new KeysAndAttributes
                        {
                            Keys = items.Select(x => new Dictionary <string, AttributeValue> {
                                { ItemIdKey, new AttributeValue(x.ToString()) }
                            }).ToList()
                        }
                    }
                }
            }, token);

            return(response.Responses[StateTableName].Select(x => x[ItemIdKey].S).ToArray());
        }
Beispiel #7
0
        async Task <BatchGetItemResponse> IAmazonDynamoDB.BatchGetItemAsync(BatchGetItemRequest request, CancellationToken cancellationToken)
        {
            try {
                var response = await _dynamoDBClient.BatchGetItemAsync(request, cancellationToken);

                _callback?.Invoke(new {
                    Action   = nameof(IAmazonDynamoDB.BatchGetItemAsync),
                    Request  = request,
                    Response = response
                });
                return(response);
            } catch (Exception e) {
                _callback?.Invoke(new {
                    Action    = nameof(IAmazonDynamoDB.BatchGetItemAsync),
                    Request   = request,
                    Exception = new {
                        Type    = e.GetType().FullName,
                        Message = e.Message
                    }
                });
                throw;
            }
        }
Beispiel #8
0
 public static BatchGetItemResponse BatchGetItem(this IAmazonDynamoDB client, BatchGetItemRequest request)
 {
     return(client.BatchGetItemAsync(request).GetResult());
 }
Beispiel #9
0
        public async Task <IEnumerable <TObject> > GetBatchItemsAsync <TObject>(string tableName,
                                                                                IEnumerable <object> keyValues)
        {
            var   keyValuesJsonData = keyValues.Select(JsonConvert.SerializeObject);
            var   keyValuesDocument = keyValuesJsonData.Select(Document.FromJson);
            Table table;

            try
            {
                table = Table.LoadTable(dynamoDBClient, tableName);
            }
            catch (ResourceNotFoundException e)
            {
                throw new TableNameFailException($"Not found the source '{tableName}'.", e);
            }

            foreach (var k in table.Keys)
            {
                foreach (var document in keyValuesDocument)
                {
                    if (!document.ContainsKey(k.Key))
                    {
                        throw new PrimaryKeyNameFailException($"Not found the primary key '{k.Key}' in query.", null);
                    }
                }
            }

            var request = new BatchGetItemRequest();

            request.RequestItems = new Dictionary <string, KeysAndAttributes>
            {
                {
                    tableName,
                    new KeysAndAttributes
                    {
                        Keys = keyValuesDocument.Select(d => d.ToAttributeMap()).ToList()
                    }
                }
            };

            BatchGetItemResponse response = await dynamoDBClient.BatchGetItemAsync(request);

            var items       = response.Responses[tableName];
            var resultItems = new List <TObject>();

            foreach (var item in items)
            {
                var     document = Document.FromAttributeMap(item);
                var     jsonData = document.ToJson();
                TObject data;

                try
                {
                    data = JsonConvert.DeserializeObject <TObject>(jsonData);
                }
                catch (JsonReaderException e)
                {
                    throw new IncorrectDataFormatException(
                              $"'{typeof(TObject).Name}' and object '{jsonData}' have a different data format",
                              e);
                }

                resultItems.Add(data);
            }

            return(resultItems);
        }