示例#1
0
        public async Task <bool> ExistsAsync <T>(string id) where T : MasterDocument
        {
            if (id.IsNullOrWhiteSpace())
            {
                new ArgumentNullException(nameof(id));
            }

            var partitionId = IdToPartitionId(id);
            var query       = GetQueryAsync <T>(partitionId).Where(d => d.Id == id).Select(d => d.Id).Take(1).AsDocumentQuery();

            double totalRU = 0;

            try
            {
                var response = await query.ExecuteNextAsync <T>();

                totalRU += response.RequestCharge;
                return(response.Any());
            }
            catch (Exception ex)
            {
                throw new CosmosDataException(id, partitionId, ex);
            }
            finally
            {
                logger.Metric($"CosmosDB RU, @master - exists id '{id}'.", totalRU);
            }
        }
示例#2
0
        //public Task<FeedResponse<TResult>> GetQueryAsync<T, TResult>(T item, Expression<Func<T, bool>> whereQuery, Expression<Func<T, TResult>> selector) where T : MasterDocument
        //{
        //    if (item == null) new ArgumentNullException(nameof(item));
        //    if (item.Id.IsNullOrEmpty()) throw new ArgumentNullException(nameof(item.Id), item.GetType().Name);
        //    if (whereQuery == null) new ArgumentNullException(nameof(whereQuery));
        //    if (selector == null) new ArgumentNullException(nameof(selector));

        //    return GetQueryAsync<T, TResult>(IdToPartitionId(item.Id), whereQuery, selector);
        //}
        //public async Task<FeedResponse<TResult>> GetQueryAsync<T, TResult>(string partitionId, Expression<Func<T, bool>> whereQuery, Expression<Func<T, TResult>> selector) where T : MasterDocument
        //{
        //    if (partitionId.IsNullOrWhiteSpace()) new ArgumentNullException(nameof(partitionId));
        //    if (whereQuery == null) new ArgumentNullException(nameof(whereQuery));
        //    if (selector == null) new ArgumentNullException(nameof(selector));

        //    var query = GetQueryAsync<T>(partitionId).Where(whereQuery).Select(selector).AsDocumentQuery();

        //    double totalRU = 0;
        //    try
        //    {
        //        var response = await query.ExecuteNextAsync<TResult>();
        //        totalRU += response.RequestCharge;
        //        return response;
        //    }
        //    catch (Exception ex)
        //    {
        //        throw new CosmosDataException(partitionId, ex);
        //    }
        //    finally
        //    {
        //        logger.Metric($"CosmosDB RU get query partitionId '{partitionId}'.", totalRU);
        //    }
        //}

        //public Task<int> GetQueryCountAsync<T>(T item, Expression<Func<T, bool>> whereQuery) where T : MasterDocument
        //{
        //    if (item == null) new ArgumentNullException(nameof(item));
        //    if (item.Id.IsNullOrEmpty()) throw new ArgumentNullException(nameof(item.Id), item.GetType().Name);
        //    if (whereQuery == null) new ArgumentNullException(nameof(whereQuery));

        //    return GetQueryCountAsync<T>(IdToPartitionId(item.Id), whereQuery);
        //}
        //public Task<int> GetQueryCountAsync<T>(string partitionId, Expression<Func<T, bool>> whereQuery) where T : MasterDocument
        //{
        //    if (partitionId.IsNullOrWhiteSpace()) new ArgumentNullException(nameof(partitionId));
        //    if (whereQuery == null) new ArgumentNullException(nameof(whereQuery));

        //    double totalRU = 0;
        //    try
        //    {
        //        var result = GetQueryAsync<T>(partitionId).Where(whereQuery).CountAsync();
        //        // Unable to get totalRU from RequestCharge...
        //        return result;
        //    }
        //    catch (Exception ex)
        //    {
        //        throw new CosmosDataException(partitionId, ex);
        //    }
        //    finally
        //    {
        //        logger.Trace($"CosmosDB RU ?'{totalRU}'?, get query count type '{typeof(T)}'.", totalRU);
        //    }
        //}

        private async Task <T> ReadDocumentAsync <T>(string id, string partitionId, bool required, bool delete = false) where T : MasterDocument
        {
            if (id.IsNullOrWhiteSpace())
            {
                new ArgumentNullException(nameof(id));
            }
            if (partitionId.IsNullOrWhiteSpace())
            {
                new ArgumentNullException(nameof(partitionId));
            }

            double totalRU = 0;

            try
            {
                var documentUri    = GetDocumentLink <T>(id);
                var requestOptions = new RequestOptions {
                    PartitionKey = new PartitionKey(partitionId)
                };
                var item = await client.ReadDocumentAsync <T>(documentUri, requestOptions);

                totalRU += item.RequestCharge;
                if (delete)
                {
                    var deleteResponse = await client.DeleteDocumentAsync(documentUri, requestOptions);

                    totalRU += deleteResponse.RequestCharge;
                }
                if (item != null)
                {
                    await item.Document.ValidateObjectAsync();
                }
                return(item);
            }
            catch (DocumentClientException ex)
            {
                if (ex.StatusCode == HttpStatusCode.NotFound && !required)
                {
                    return(default(T));
                }
                throw new CosmosDataException(id, partitionId, $"{typeof(T).Name} not found. The master seed has probably not been executed.", ex);
            }
            catch (Exception ex)
            {
                throw new CosmosDataException(id, partitionId, ex);
            }
            finally
            {
                logger.Metric($"CosmosDB RU, @master - read document id '{id}', partitionId '{partitionId}'.", totalRU);
            }
        }