コード例 #1
0
        // This processes 25 quotes at a time since the max is 25 as per dynamoDB batch write
        private async Task WriteBatch(IEnumerable <Quote> batchQuotes)
        {
            // There is this possibility when there is nothing to approve
            if (batchQuotes.Count() == 0)
            {
                Console.WriteLine("Batch empty");
                return;
            }

            Console.WriteLine("Writing batch");
            Dictionary <string, List <WriteRequest> > writeRequests = new Dictionary <string, List <WriteRequest> >();

            writeRequests.Add(DataDefinitions.QUOTES_TABLE, new List <WriteRequest>());
            foreach (Quote quote in batchQuotes)
            {
                PutRequest request = new PutRequest(QuoteExtensions.AttributesOfQuote(quote));
                writeRequests[DataDefinitions.QUOTES_TABLE].Add(new WriteRequest(request));
            }

            BatchWriteItemRequest batchRequest = new BatchWriteItemRequest
            {
                RequestItems = writeRequests
            };

            Console.WriteLine("Before writing to dynamodb");
            BatchWriteItemResponse batchResponse = await _client.BatchWriteItemAsync(batchRequest);

            Console.WriteLine($"quotes batch response: {batchResponse.HttpStatusCode}");
        }
コード例 #2
0
        /// <summary>
        /// Unmarshaller the response from the service to the response class.
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override AmazonWebServiceResponse Unmarshall(JsonUnmarshallerContext context)
        {
            BatchWriteItemResponse response = new BatchWriteItemResponse();

            context.Read();
            int targetDepth = context.CurrentDepth;

            while (context.ReadAtDepth(targetDepth))
            {
                if (context.TestExpression("ConsumedCapacity", targetDepth))
                {
                    var unmarshaller = new ListUnmarshaller <ConsumedCapacity, ConsumedCapacityUnmarshaller>(ConsumedCapacityUnmarshaller.Instance);
                    response.ConsumedCapacity = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("ItemCollectionMetrics", targetDepth))
                {
                    var unmarshaller = new DictionaryUnmarshaller <string, List <ItemCollectionMetrics>, StringUnmarshaller, ListUnmarshaller <ItemCollectionMetrics, ItemCollectionMetricsUnmarshaller> >(StringUnmarshaller.Instance, new ListUnmarshaller <ItemCollectionMetrics, ItemCollectionMetricsUnmarshaller>(ItemCollectionMetricsUnmarshaller.Instance));
                    response.ItemCollectionMetrics = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("UnprocessedItems", targetDepth))
                {
                    var unmarshaller = new DictionaryUnmarshaller <string, List <WriteRequest>, StringUnmarshaller, ListUnmarshaller <WriteRequest, WriteRequestUnmarshaller> >(StringUnmarshaller.Instance, new ListUnmarshaller <WriteRequest, WriteRequestUnmarshaller>(WriteRequestUnmarshaller.Instance));
                    response.UnprocessedItems = unmarshaller.Unmarshall(context);
                    continue;
                }
            }

            return(response);
        }
        internal static async Task <List <BatchWriteItemResponse> > BuildDelete <Item>(List <Item> items, string tableName, ItemConverter <Item> itemConverter, AmazonDynamoDBClient dynamodbClient)
        {
            Item[] items25 = new Item[MAX_LENGTH_OF_BATCH];
            List <BatchWriteItemResponse> responses = new List <BatchWriteItemResponse>();

            for (int index = 0; index < items.Count; index += 25)
            {
                int remaining = items.Count;
                remaining = remaining - index;

                if (items25.Length > remaining)
                {
                    items.CopyTo(index, items25, 0, remaining);
                }
                else
                {
                    items.CopyTo(index, items25, 0, items25.Length);
                }

                BatchWriteItemResponse response = await BuildDelete(items25, tableName, itemConverter, dynamodbClient);

                responses.Add(response);
            }

            return(responses);
        }
コード例 #4
0
        public override AmazonWebServiceResponse Unmarshall(JsonUnmarshallerContext context)
        {
            BatchWriteItemResponse response = new BatchWriteItemResponse();

            context.Read();

            UnmarshallResult(context, response);
            return(response);
        }
コード例 #5
0
        public override AmazonWebServiceResponse Unmarshall(JsonUnmarshallerContext context)
        {
            BatchWriteItemResponse response = new BatchWriteItemResponse();

            context.Read();
            response.BatchWriteItemResult = BatchWriteItemResultUnmarshaller.GetInstance().Unmarshall(context);

            return(response);
        }
コード例 #6
0
        private void AddItems(List <Dictionary <string, AttributeValue> > items)
        {
            if (items.Count == 0)
            {
                return;
            }

            if (items.Count == 1)
            {
                PutItemResponse response;
                using (var client = CreateClient())
                {
                    response = client.PutItem(_tableName, items[0]);
                }

                if (response.HttpStatusCode != HttpStatusCode.OK)
                {
                    throw CreateInvalidResponseException(
                              response.ResponseMetadata == null ? null : response.ResponseMetadata.Metadata,
                              response.HttpStatusCode);
                }

                return;
            }

            BatchWriteItemResponse batchWriteItemResponse = null;

            do
            {
                var writeRequest = batchWriteItemResponse == null
                                        ? new Dictionary <string, List <WriteRequest> >()
                {
                    { _tableName, items.Select(item => new WriteRequest(new PutRequest(item))).ToList() }
                }
                                        : batchWriteItemResponse.UnprocessedItems;

                using (var client = CreateClient())
                {
                    batchWriteItemResponse = client.BatchWriteItem(writeRequest);
                }
            } while (batchWriteItemResponse.UnprocessedItems != null && batchWriteItemResponse.UnprocessedItems.Count > 0);

            if (batchWriteItemResponse.HttpStatusCode != HttpStatusCode.OK)
            {
                throw CreateInvalidResponseException(
                          batchWriteItemResponse.ResponseMetadata == null ? null : batchWriteItemResponse.ResponseMetadata.Metadata,
                          batchWriteItemResponse.HttpStatusCode);
            }
        }
コード例 #7
0
        private static void UnmarshallResult(JsonUnmarshallerContext context, BatchWriteItemResponse response)
        {
            int originalDepth = context.CurrentDepth;
            int targetDepth   = originalDepth + 1;

            while (context.Read())
            {
                if (context.TestExpression("UnprocessedItems", targetDepth))
                {
                    context.Read();
                    response.UnprocessedItems = new Dictionary <String, List <WriteRequest> >();
                    if (context.CurrentTokenType == JsonToken.Null)
                    {
                        continue;
                    }
                    KeyValueUnmarshaller <string, List <WriteRequest>, StringUnmarshaller, ListUnmarshaller <WriteRequest, WriteRequestUnmarshaller> > unmarshaller = new KeyValueUnmarshaller <string, List <WriteRequest>, StringUnmarshaller, ListUnmarshaller <WriteRequest, WriteRequestUnmarshaller> >(StringUnmarshaller.GetInstance(), new ListUnmarshaller <WriteRequest, WriteRequestUnmarshaller>(WriteRequestUnmarshaller.GetInstance()));
                    while (context.Read())
                    {
                        JsonToken token = context.CurrentTokenType;
                        if (token == JsonToken.ArrayStart || token == JsonToken.ObjectStart)
                        {
                            continue;
                        }
                        if (token == JsonToken.ArrayEnd || token == JsonToken.ObjectEnd)
                        {
                            break;
                        }
                        KeyValuePair <string, List <WriteRequest> > kvp = unmarshaller.Unmarshall(context);
                        response.UnprocessedItems.Add(kvp.Key, kvp.Value);
                    }
                    continue;
                }

                if (context.TestExpression("ItemCollectionMetrics", targetDepth))
                {
                    context.Read();
                    response.ItemCollectionMetrics = new Dictionary <String, List <ItemCollectionMetrics> >();
                    if (context.CurrentTokenType == JsonToken.Null)
                    {
                        continue;
                    }
                    KeyValueUnmarshaller <string, List <ItemCollectionMetrics>, StringUnmarshaller, ListUnmarshaller <ItemCollectionMetrics, ItemCollectionMetricsUnmarshaller> > unmarshaller = new KeyValueUnmarshaller <string, List <ItemCollectionMetrics>, StringUnmarshaller, ListUnmarshaller <ItemCollectionMetrics, ItemCollectionMetricsUnmarshaller> >(StringUnmarshaller.GetInstance(), new ListUnmarshaller <ItemCollectionMetrics, ItemCollectionMetricsUnmarshaller>(ItemCollectionMetricsUnmarshaller.GetInstance()));
                    while (context.Read())
                    {
                        JsonToken token = context.CurrentTokenType;
                        if (token == JsonToken.ArrayStart || token == JsonToken.ObjectStart)
                        {
                            continue;
                        }
                        if (token == JsonToken.ArrayEnd || token == JsonToken.ObjectEnd)
                        {
                            break;
                        }
                        KeyValuePair <string, List <ItemCollectionMetrics> > kvp = unmarshaller.Unmarshall(context);
                        response.ItemCollectionMetrics.Add(kvp.Key, kvp.Value);
                    }
                    continue;
                }

                if (context.TestExpression("ConsumedCapacity", targetDepth))
                {
                    context.Read();
                    response.ConsumedCapacity = new List <ConsumedCapacity>();
                    if (context.CurrentTokenType == JsonToken.Null)
                    {
                        continue;
                    }
                    ConsumedCapacityUnmarshaller unmarshaller = ConsumedCapacityUnmarshaller.GetInstance();
                    while (context.Read())
                    {
                        JsonToken token = context.CurrentTokenType;
                        if (token == JsonToken.ArrayStart)
                        {
                            continue;
                        }
                        if (token == JsonToken.ArrayEnd)
                        {
                            break;
                        }
                        response.ConsumedCapacity.Add(unmarshaller.Unmarshall(context));
                    }
                    continue;
                }

                if (context.CurrentDepth <= originalDepth)
                {
                    return;
                }
            }

            return;
        }