public async Task <CosmosDbResponse <IEnumerable <T> > > Resolve <T>(IQueryable <T> query)
        {
            var result  = new CosmosDbResponse <IEnumerable <T> >();
            var results = new List <T>();

            Stopwatch watch = Stopwatch.StartNew();

            // FeedIterator<T> iterator = query.ToFeedIterator(); // << NOT WORKING!!

            QueryDefinition  queryDef = query.ToQueryDefinition();
            FeedIterator <T> iterator = _container.GetItemQueryIterator <T>(queryDef);

            while (iterator.HasMoreResults)
            {
                FeedResponse <T> response = await iterator.ReadNextAsync();

                results.AddRange(response);

                result.RequestCharge += response.RequestCharge;
                result.ActivityId     = response.ActivityId;
                result.ETag           = response.ETag;
            }

            watch.Stop();

            result.ExecutionTime = watch.Elapsed;
            result.Result        = results;
            result.StatusCode    = HttpStatusCode.OK;

            return(result);
        }
Beispiel #2
0
        public async Task <CosmosDbResponse <T> > Delete <T>(string partitionKey, string id)
        {
            PartitionKey pk = PartitionKey.None;

            if (!string.IsNullOrWhiteSpace(partitionKey))
            {
                pk = new PartitionKey(partitionKey);
            }

            Stopwatch watch = Stopwatch.StartNew();

            try
            {
                ItemResponse <T> itemResponse = await _container.DeleteItemAsync <T>(id, pk);

                watch.Stop();

                CosmosDbResponse <T> response = itemResponse.ToCosmosDbResponse(watch.Elapsed);
                return(response);
            }
            catch (CosmosException cex)
            {
                watch.Stop();
                return(cex.ToCosmosDbResponse <T>(watch.Elapsed));
            }
        }
        public async Task <CosmosDbResponse <IEnumerable <T> > > GetAll <T>()
        {
            var result  = new CosmosDbResponse <IEnumerable <T> >();
            var results = new List <T>();

            Stopwatch watch = Stopwatch.StartNew();

            FeedIterator <T> resultSet = _container.GetItemQueryIterator <T>();

            while (resultSet.HasMoreResults)
            {
                FeedResponse <T> response = await resultSet.ReadNextAsync();

                results.AddRange(response);

                result.RequestCharge += response.RequestCharge;
                result.ActivityId     = response.ActivityId;
                result.ETag           = response.ETag;
            }

            watch.Stop();

            result.ExecutionTime = watch.Elapsed;
            result.Result        = results;
            result.StatusCode    = HttpStatusCode.OK;

            return(result);
        }
Beispiel #4
0
        public async Task <CosmosDbResponse <IEnumerable <T> > > GetByQuery <T>(string partitionKey, string query, IReadOnlyDictionary <string, object> parameters)
        {
            var queryDef = new QueryDefinition(query);

            if (parameters != null)
            {
                foreach (KeyValuePair <string, object> kvp in parameters)
                {
                    queryDef.WithParameter(kvp.Key, kvp.Value);
                }
            }

            var options = new QueryRequestOptions();

            if (!string.IsNullOrWhiteSpace(partitionKey))
            {
                options.PartitionKey = new PartitionKey(partitionKey);
            }

            var       response = new CosmosDbResponse <IEnumerable <T> >();
            var       results  = new List <T>();
            Stopwatch watch    = Stopwatch.StartNew();

            try
            {
                FeedIterator <T> resultSetIterator = _container.GetItemQueryIterator <T>(queryDef, requestOptions: options);
                while (resultSetIterator.HasMoreResults)
                {
                    FeedResponse <T> feedResponse = await resultSetIterator.ReadNextAsync();

                    results.AddRange(feedResponse);

                    response.RequestCharge += feedResponse.RequestCharge;
                    response.ActivityId     = feedResponse.ActivityId;
                    response.ETag           = feedResponse.ETag;
                    response.Diagnostics    = feedResponse.Diagnostics.ToString();
                }

                watch.Stop();
                response.StatusCode = HttpStatusCode.OK;
            }
            catch (CosmosException cex)
            {
                watch.Stop();
                response.Error          = cex;
                response.ActivityId     = cex.ActivityId;
                response.StatusCode     = cex.StatusCode;
                response.RequestCharge += cex.RequestCharge;
                response.Diagnostics    = cex.Diagnostics.ToString();
            }
            finally
            {
                response.ExecutionTime = watch.Elapsed;
                response.Result        = results;
            }

            return(response);
        }
Beispiel #5
0
        public static CosmosDbResponse <T> ToCosmosDbResponse <T>(this ArgumentNullException anxException, TimeSpan?duration = null)
        {
            var res = new CosmosDbResponse <T>()
            {
                Error = anxException,

                ExecutionTime = duration ?? TimeSpan.Zero
            };

            return(res);
        }
Beispiel #6
0
        public static CosmosDbResponse <IEnumerable <T> > ToCosmosDbQueryResponse <T>(this ArgumentNullException anxException, TimeSpan?duration = null)
        {
            var res = new CosmosDbResponse <IEnumerable <T> >()
            {
                Error = anxException,

                ExecutionTime = duration ?? TimeSpan.Zero,

                Result = new List <T>()
            };

            return(res);
        }
        public static CosmosDbResponse <T> ToCosmosDbResponse <T>(this Response <T> sdkResponse, TimeSpan?duration = null)
        {
            var res = new CosmosDbResponse <T>()
            {
                StatusCode = sdkResponse.StatusCode,

                RequestCharge = sdkResponse.RequestCharge,
                ExecutionTime = duration ?? TimeSpan.Zero,

                ActivityId = sdkResponse.ActivityId,
                ETag       = sdkResponse.ETag,

                Result = sdkResponse.Resource
            };

            return(res);
        }
        public async Task <CosmosDbResponse <int> > ResolveCount <T>(IQueryable <T> query)
        {
            var response = new CosmosDbResponse <int>();

            Stopwatch watch = Stopwatch.StartNew();

            try
            {
                QueryDefinition queryDef             = query.ToQueryDefinition();
                QueryDefinition countQueryDefinition = new QueryDefinition(ConvertToCountQuery(queryDef.QueryText));
                FeedIterator    feedIterator         = _container.GetItemQueryStreamIterator(countQueryDefinition);
                while (feedIterator.HasMoreResults)
                {
                    ResponseMessage rm = await feedIterator.ReadNextAsync();

                    string  raw     = await new StreamReader(rm.Content).ReadToEndAsync();
                    JObject jObject = JObject.Parse(raw);
                    int     count   = jObject["Documents"][0]["$1"].Value <int>();
                    response.Result = count;

                    response.StatusCode    = rm.StatusCode;
                    response.RequestCharge = rm.Headers.RequestCharge;
                    response.ActivityId    = rm.Headers.ActivityId;
                    response.ETag          = rm.Headers.ETag;
                    response.Diagnostics   = rm.Diagnostics.ToString();
                }

                watch.Stop();
                response.StatusCode = HttpStatusCode.OK;
            }
            catch (CosmosException cex)
            {
                watch.Stop();
                response.Error          = cex;
                response.ActivityId     = cex.ActivityId;
                response.StatusCode     = cex.StatusCode;
                response.RequestCharge += cex.RequestCharge;
                response.Diagnostics    = cex.Diagnostics.ToString();
            }
            finally
            {
                response.ExecutionTime = watch.Elapsed;
            }

            return(response);
        }
Beispiel #9
0
        public async Task <CosmosDbResponse <IEnumerable <T> > > ResolveWithStreamIterator <T>(IQueryable <T> query)
        {
            var response = new CosmosDbResponse <IEnumerable <T> >();
            var results  = new List <T>();

            Stopwatch watch = Stopwatch.StartNew();

            try
            {
                QueryDefinition queryDef = query.ToQueryDefinition();

                var streamIterator = _container.GetItemQueryStreamIterator(queryDef);
                while (streamIterator.HasMoreResults)
                {
                    var responseMessage = await streamIterator.ReadNextAsync();

                    var raw  = await new StreamReader(responseMessage.Content).ReadToEndAsync();
                    var data = JsonConvert.DeserializeObject <List <T> >(JObject.Parse(raw)["Documents"].ToString(), new JsonSerializerSettings()
                    {
                        NullValueHandling     = NullValueHandling.Ignore,
                        TypeNameHandling      = TypeNameHandling.All,
                        MissingMemberHandling = MissingMemberHandling.Ignore
                    });
                    results.AddRange(data);
                }
                watch.Stop();
                response.StatusCode = HttpStatusCode.OK;
            }
            catch (CosmosException cex)
            {
                watch.Stop();
                response.Error          = cex;
                response.ActivityId     = cex.ActivityId;
                response.StatusCode     = cex.StatusCode;
                response.RequestCharge += cex.RequestCharge;
                response.Diagnostics    = cex.Diagnostics.ToString();
            }
            finally
            {
                response.ExecutionTime = watch.Elapsed;
                response.Result        = results;
            }

            return(response);
        }
Beispiel #10
0
        public static CosmosDbResponse <T> ToCosmosDbResponse <T>(this CosmosException sdkException, TimeSpan?duration = null)
        {
            var res = new CosmosDbResponse <T>()
            {
                Error = sdkException,

                StatusCode = sdkException.StatusCode,

                RequestCharge = sdkException.RequestCharge,
                ExecutionTime = duration ?? TimeSpan.Zero,

                ActivityId = sdkException.ActivityId,

                Diagnostics = sdkException.Diagnostics.ToString()
            };

            return(res);
        }
        public async Task <CosmosDbResponse <T> > Get <T>(string partitionKey, string id)
        {
            PartitionKey pk = PartitionKey.Null;

            if (!string.IsNullOrWhiteSpace(partitionKey))
            {
                pk = new PartitionKey(partitionKey);
            }

            Stopwatch        watch    = Stopwatch.StartNew();
            ItemResponse <T> response = await _container.ReadItemAsync <T>(id, pk);

            watch.Stop();

            CosmosDbResponse <T> result = response.ToCosmosDbResponse(watch.Elapsed);

            return(result);
        }
Beispiel #12
0
        public async Task <CosmosDbResponse <IEnumerable <T> > > Resolve <T>(IQueryable <T> query)
        {
            var response = new CosmosDbResponse <IEnumerable <T> >();
            var results  = new List <T>();

            Stopwatch watch = Stopwatch.StartNew();

            try
            {
                QueryDefinition  queryDef     = query.ToQueryDefinition();
                FeedIterator <T> feedIterator = _container.GetItemQueryIterator <T>(queryDef);

                while (feedIterator.HasMoreResults)
                {
                    FeedResponse <T> feedResponse = await feedIterator.ReadNextAsync();

                    results.AddRange(feedResponse);

                    response.RequestCharge += feedResponse.RequestCharge;
                    response.ActivityId     = feedResponse.ActivityId;
                    response.ETag           = feedResponse.ETag;
                    response.Diagnostics    = feedResponse.Diagnostics.ToString();
                }

                watch.Stop();
                response.StatusCode = HttpStatusCode.OK;
            }
            catch (CosmosException cex)
            {
                watch.Stop();
                response.Error          = cex;
                response.ActivityId     = cex.ActivityId;
                response.StatusCode     = cex.StatusCode;
                response.RequestCharge += cex.RequestCharge;
                response.Diagnostics    = cex.Diagnostics.ToString();
            }
            finally
            {
                response.ExecutionTime = watch.Elapsed;
                response.Result        = results;
            }

            return(response);
        }
        public async Task <CosmosDbResponse <T> > Add <T>(string partitionKey, T entity)
        {
            PartitionKey pk = PartitionKey.Null;

            if (!string.IsNullOrWhiteSpace(partitionKey))
            {
                pk = new PartitionKey(partitionKey);
            }

            Stopwatch watch = Stopwatch.StartNew();

            ItemResponse <T> response = await _container.CreateItemAsync(entity, pk, new ItemRequestOptions()
            {
                IndexingDirective = IndexingDirective.Default
            });

            watch.Stop();

            CosmosDbResponse <T> result = response.ToCosmosDbResponse(watch.Elapsed);

            return(result);
        }