protected override async Task InternalDeleteAllItemsAsync <T>(CancellationToken cancellationToken = default)
        {
            Logger.LogTrace("InternalDeleteAllItemsAsync: {EntityType}", typeof(T));

            var iter = _provider.Container
                       .GetItemLinqQueryable <T>(true)
                       .Where(x => x.EntityType == TypeKeyCache.GetEntityTypeKey <T>())
                       .ToFeedIterator();

            while (iter.HasMoreResults)
            {
                foreach (var entity in await iter.ReadNextAsync(cancellationToken).ConfigureAwait(false))
                {
                    try
                    {
                        await _provider.Container.DeleteItemAsync <T>(
                            partitionKey : GetPartitionKey <T>(),
                            id : entity.EntityId,
                            cancellationToken : cancellationToken).ConfigureAwait(false);
                    }
                    catch (CosmosException nfex) when(nfex.StatusCode == HttpStatusCode.NotFound)
                    {
                        Logger.LogWarning("InternalDeleteItemAsync: Entity with Id not found {EntityId}",
                                          entity.EntityId);
                    }
                    catch (CosmosException e) when(LogCosmosError(e))
                    {
                    }
                }
            }
        }
Esempio n. 2
0
        protected override async Task <T> InternalLoadItemAsync <T>(string entityId, CancellationToken token = default(CancellationToken))
        {
            if (string.IsNullOrEmpty(entityId))
            {
                throw new ArgumentNullException(nameof(entityId));
            }

            Logger.LogTrace("InternalLoadItemAsync: {EntityType} {EntityId}", typeof(T), entityId);

            token.ThrowIfCancellationRequested();

            try
            {
                //var feedOptions = new FeedOptions() { EnableCrossPartitionQuery = false };
                ////type filtern wegen partition key verletzungen
                //var query = _provider.CreateQuery<T>(feedOptions).Where(x => x.EntityType == TypeKeyCache.GetEntityTypeKey<T>());
                //query = query.Where(x => x.EntityId == entityId);

                //var jo = JObject.Parse(query.ToString());
                //var countQuerySql = jo["query"].Value<string>();
                //countQuerySql = countQuerySql.Replace("*", "VALUE COUNT(1)");

                //var countQuerySql = $"SELECT VALUE COUNT(1) FROM root WHERE ((root[\"entity_type\"] = \"{TypeKeyCache.GetEntityTypeKey<T>()}\") AND (root[\"id\"] = \"{entityId}\"))";
                //var countQuery = _provider.Client.Value.CreateDocumentQuery<int>(_provider.CollectionUri, new SqlQuerySpec(countQuerySql));
                //token.ThrowIfCancellationRequested();

                //var totalCount = await countQuery.TakeOneAsync(token).ConfigureAwait(false);

                //if (totalCount == 0)
                //{
                //    Logger.LogDebug("No Value count");
                //    return null;
                //}

                var response = await _provider.Client.Value.ReadDocumentAsync(
                    _provider.GetDocumentLink(entityId),
                    new RequestOptions { PartitionKey = new PartitionKey(TypeKeyCache.GetEntityTypeKey <T>()) },
                    token);

                Logger.LogTrace("ReadDocumentAsync: EntityId: {EntityId} / StatusCode: {StatusCode} / RequestUnits: {RequestCharge} ", entityId,
                                response.StatusCode, response.RequestCharge);

                var result = response.Resource.ToString();
                var item   = JsonConvert.DeserializeObject <T>(result);
                return(item);
            }
            catch (DocumentClientException exnf) when(exnf.Error.Code == "NotFound")
            {
                Logger.LogTrace("Entity with Id not found {EntityId}", entityId);
                return(null);
            }
            catch (DocumentClientException e)
            {
                Logger.LogError(e, "Error in CosmosDb Load {ErrorCode} {Message}", e.Error.Code, e.Message);
                throw;
            }
        }
Esempio n. 3
0
        protected override async Task InternalDeleteItemAsync <T>(string entityId, CancellationToken token = default(CancellationToken))
        {
            if (string.IsNullOrEmpty(entityId))
            {
                throw new ArgumentNullException(nameof(entityId));
            }

            Logger.LogTrace("InternalDeleteItemAsync: {EntityType} {EntityId}", typeof(T), entityId);

            token.ThrowIfCancellationRequested();

            await _provider.DeleteItemAsync(entityId, TypeKeyCache.GetEntityTypeKey <T>());
        }
        public override async Task <IEnumerable <T> > LoadItemsAsync <T>(Expression <Func <T, bool> > where,
                                                                         CancellationToken cancellationToken = default)
        {
            Logger.LogTrace("LoadItemsAsync: {EntityType} with where", typeof(T));

            cancellationToken.ThrowIfCancellationRequested();

            try
            {
                var iter = _provider.Container
                           .GetItemLinqQueryable <T>(true)
                           .Where(x => x.EntityType == TypeKeyCache.GetEntityTypeKey <T>())
                           .Where(where)
                           .ToFeedIterator()
                ;

                var results = new List <T>();

                while (iter.HasMoreResults)
                {
                    foreach (var entity in await iter.ReadNextAsync(cancellationToken).ConfigureAwait(false))
                    {
                        results.Add(entity);
                    }
                }

                return(results);
            }
            catch (CosmosException nfex) when(nfex.StatusCode == HttpStatusCode.NotFound)
            {
                Logger.LogWarning("LoadItemsAsync: Items not found  {ErrorCode} {StatusCode} {Message}",
                                  nfex.StatusCode,
                                  nfex.SubStatusCode,
                                  nfex.Message);
                return(new T[0]);
            }
            catch (CosmosException e)
            {
                Logger.LogError(e,
                                "LoadItemsAsync: CosmosException {ErrorCode} {StatusCode} {Message}",
                                e.StatusCode,
                                e.SubStatusCode,
                                e.Message);
                throw;
            }
        }
 private PartitionKey GetPartitionKey <T>() where T : Entity
 {
     return(new PartitionKey(TypeKeyCache.GetEntityTypeKey <T>()));
 }
Esempio n. 6
0
        public override async Task <T[]> LoadItemsAsync <T>(Expression <Func <T, bool> > where, CancellationToken token = default(CancellationToken))
        {
            Logger.LogTrace("LoadItemsAsync: {EntityType} {Expression}", typeof(T), where);

            token.ThrowIfCancellationRequested();


            var feedOptions = new FeedOptions {
                MaxItemCount = -1, EnableCrossPartitionQuery = false
            };

            //type filtern wegen partition key verletzungen
            var source = _provider.CreateQuery <T>(feedOptions).Where(x => x.EntityType == TypeKeyCache.GetEntityTypeKey <T>()).Where(where);
            var xx     = await source.ToArrayAsync(token, Logger).ConfigureAwait(false);

            return(xx);
        }