public ValueTask <TEntry> GetOneAsync <TId, TEntry>(TId id, CancellationToken cancellation = default) where TId : IEquatable <TId> where TEntry : class { if (id == null) { throw new ArgumentNullException(nameof(id)); } return(GetTypedStore <TEntry>().GetOneAsync(DataPropertyHelper.BuildPredicate <TId, TEntry>(id), cancellation)); }
private static Expression <Func <TEntry, bool> > BuildPredicate <TEntry>(TEntry comparand, Expression <Func <TEntry, TEntry, bool> > equalityComparer) { Assert(comparand != null); Assert(equalityComparer != null); var idSelector = DataPropertyHelper.BuildPredicate(comparand); var comparandConstant = Expression.Constant(comparand, typeof(TEntry)); var parameter = equalityComparer.Parameters.First(); var equality = ParameterExpressionReplacer.ReplaceParameter(equalityComparer.Body, equalityComparer.Parameters.Last(), comparandConstant); var idEquality = ParameterExpressionReplacer.ReplaceParameter(idSelector.Body, idSelector.Parameters.First(), parameter); var body = Expression.AndAlso(idEquality, equality); return(Expression.Lambda <Func <TEntry, bool> >(body, parameter)); }
public static async ValueTask <IEntryState <TId, TData> > GetEntryAsync <TId, TData>(this IEntryStateStorage <TId, TData> entryStorage, IEntryState <TId, TData> comparand, CancellationToken cancellation = default) where TData : class { if (entryStorage == null) { throw new ArgumentNullException(nameof(entryStorage)); } if (comparand == null) { throw new ArgumentNullException(nameof(comparand)); } var entries = await entryStorage.GetEntriesAsync(DataPropertyHelper.BuildPredicate(comparand), cancellation); return(entries.FirstOrDefault()); }
private async Task <bool> CheckComparandToBeUpToDate <TEntry>(TEntry comparand, Expression <Func <TEntry, TEntry, bool> > equalityComparer, CancellationToken cancellation) where TEntry : class { var result = await GetOneAsync(DataPropertyHelper.BuildPredicate(comparand), cancellation); if (comparand == null) { return(result == null); } if (result == null) { return(false); } return(equalityComparer.Compile(preferInterpretation: true).Invoke(comparand, result)); }
public async ValueTask <TEntry> GetOrAdd <TEntry>(TEntry entry, CancellationToken cancellation = default) where TEntry : class { if (entry == null) { throw new ArgumentNullException(nameof(entry)); } while (!await AddAsync(entry, cancellation)) { var result = await GetOneAsync(DataPropertyHelper.BuildPredicate(entry), cancellation); if (result != null) { return(result); } } return(entry); }
public async Task RemoveEntityFromProjectionAsync(ProjectionSourceDescriptor projectionSource, ProjectionTargetDescriptor removedProjection, CancellationToken cancellation) { var(originalMetadata, metadata) = await GetMetadataAsync(removedProjection, cancellation); if (metadata == null) { Assert(false); return; } var removed = metadata.ProjectionSources .RemoveFirstWhere(p => p.Id == projectionSource.SourceId && p.Type == projectionSource.SourceType.GetUnqualifiedTypeName()); Assert(removed != null); if (!metadata.ProjectionSources.Any()) { _targetMetadataCache[removedProjection] = new ProjectionTargetMetadataCacheEntry(originalMetadata, metadata: null, touched: true); var predicate = DataPropertyHelper.BuildPredicate <TProjectionId, TProjection>(metadata.TargetId); var projection = await _database.GetAsync(predicate, cancellation).FirstOrDefault(); if (projection != null) { _targetsToDelete.Add(projection); //await _database.RemoveAsync(projection, cancellation); } } _targetMetadataCache[removedProjection] = new ProjectionTargetMetadataCacheEntry(originalMetadata, metadata, touched: true); }