private IChangeSet <TDestination, TKey> ProcessUpdates(ChangeAwareCache <TransformedItemContainer, TKey> cache, TransformResult[] transformedItems)
        {
            // check for errors and callback if a handler has been specified
            var errors = transformedItems.Where(t => !t.Success).ToArray();

            if (errors.Length > 0)
            {
                errors.ForEach(t => _exceptionCallback?.Invoke(new Error <TSource, TKey>(t.Error, t.Change.Current, t.Change.Key)));
            }

            foreach (var result in transformedItems.Where(t => t.Success))
            {
                TKey key = result.Key;
                switch (result.Change.Reason)
                {
                case ChangeReason.Add:
                case ChangeReason.Update:
                    cache.AddOrUpdate(result.Container.Value, key);
                    break;

                case ChangeReason.Remove:
                    cache.Remove(key);
                    break;

                case ChangeReason.Refresh:
                    cache.Refresh(key);
                    break;
                }
            }

            var changes     = cache.CaptureChanges();
            var transformed = changes.Select(change => new Change <TDestination, TKey>(change.Reason, change.Key, change.Current.Destination, change.Previous.Convert(x => x.Destination), change.CurrentIndex, change.PreviousIndex));

            return(new ChangeSet <TDestination, TKey>(transformed));
        }
Exemple #2
0
        private IChangeSet <TDestination, TKey> ProcessUpdates(ChangeAwareCache <TDestination, TKey> cache, IEnumerable <TransformResult> transformedItems)
        {
            foreach (var result in transformedItems)
            {
                if (result.Success)
                {
                    switch (result.Change.Reason)
                    {
                    case ChangeReason.Add:
                    case ChangeReason.Update:
                        cache.AddOrUpdate(result.Destination.Value, result.Key);
                        break;

                    case ChangeReason.Remove:
                        cache.Remove(result.Key);
                        break;

                    case ChangeReason.Refresh:
                        cache.Refresh(result.Key);
                        break;
                    }
                }
                else
                {
                    _exceptionCallback(new Error <TSource, TKey>(result.Error, result.Change.Current, result.Change.Key));
                }
            }

            return(cache.CaptureChanges());
        }
        private IChangeSet <TDestination, TKey> ProcessUpdates(ChangeAwareCache <TransformedItemContainer, TKey> cache, IEnumerable <TransformResult> transformedItems)
        {
            foreach (var result in transformedItems)
            {
                if (result.Success)
                {
                    switch (result.Change.Reason)
                    {
                    case ChangeReason.Add:
                    case ChangeReason.Update:
                        cache.AddOrUpdate(result.Container.Value, result.Key);
                        break;

                    case ChangeReason.Remove:
                        cache.Remove(result.Key);
                        break;

                    case ChangeReason.Refresh:
                        cache.Refresh(result.Key);
                        break;
                    }
                }
                else
                {
                    _exceptionCallback(new Error <TSource, TKey>(result.Error, result.Change.Current, result.Change.Key));
                }
            }

            var changes     = cache.CaptureChanges();
            var transformed = changes.Select(change => new Change <TDestination, TKey>(change.Reason,
                                                                                       change.Key,
                                                                                       change.Current.Destination,
                                                                                       change.Previous.Convert(x => x.Destination),
                                                                                       change.CurrentIndex,
                                                                                       change.PreviousIndex));

            return(new ChangeSet <TDestination, TKey>(transformed));
        }
Exemple #4
0
        private IChangeSet <TObject, TKey> UpdateCombined(IChangeSet <TObject, TKey> updates)
        {
            //child caches have been updated before we reached this point.

            foreach (var update in updates)
            {
                TKey key = update.Key;
                switch (update.Reason)
                {
                case ChangeReason.Add:
                case ChangeReason.Update:
                {
                    // get the current key.
                    //check whether the item should belong to the cache
                    var cached    = _combinedCache.Lookup(key);
                    var contained = cached.HasValue;
                    var match     = MatchesConstraint(key);

                    if (match)
                    {
                        if (contained)
                        {
                            if (!ReferenceEquals(update.Current, cached.Value))
                            {
                                _combinedCache.AddOrUpdate(update.Current, key);
                            }
                        }
                        else
                        {
                            _combinedCache.AddOrUpdate(update.Current, key);
                        }
                    }
                    else
                    {
                        if (contained)
                        {
                            _combinedCache.Remove(key);
                        }
                    }
                }

                break;

                case ChangeReason.Remove:
                {
                    var  cached           = _combinedCache.Lookup(key);
                    var  contained        = cached.HasValue;
                    bool shouldBeIncluded = MatchesConstraint(key);

                    if (shouldBeIncluded)
                    {
                        var firstOne = _sourceCaches.Select(s => s.Lookup(key))
                                       .SelectValues()
                                       .First();

                        if (!cached.HasValue)
                        {
                            _combinedCache.AddOrUpdate(firstOne, key);
                        }
                        else if (!ReferenceEquals(firstOne, cached.Value))
                        {
                            _combinedCache.AddOrUpdate(firstOne, key);
                        }
                    }
                    else
                    {
                        if (contained)
                        {
                            _combinedCache.Remove(key);
                        }
                    }
                }

                break;

                case ChangeReason.Refresh:
                {
                    _combinedCache.Refresh(key);
                }

                break;
                }
            }

            return(_combinedCache.CaptureChanges());
        }
Exemple #5
0
        public static void FilterChanges <TObject, TKey>(this ChangeAwareCache <TObject, TKey> cache, IChangeSet <TObject, TKey> changes, Func <TObject, bool> predicate)
            where TKey : notnull
        {
            foreach (var change in changes.ToConcreteType())
            {
                var key = change.Key;
                switch (change.Reason)
                {
                case ChangeReason.Add:
                {
                    var current = change.Current;
                    if (predicate(current))
                    {
                        cache.AddOrUpdate(current, key);
                    }
                }

                break;

                case ChangeReason.Update:
                {
                    var current = change.Current;
                    if (predicate(current))
                    {
                        cache.AddOrUpdate(current, key);
                    }
                    else
                    {
                        cache.Remove(key);
                    }
                }

                break;

                case ChangeReason.Remove:
                    cache.Remove(key);
                    break;

                case ChangeReason.Refresh:
                {
                    var existing = cache.Lookup(key);
                    if (predicate(change.Current))
                    {
                        if (!existing.HasValue)
                        {
                            cache.AddOrUpdate(change.Current, key);
                        }
                        else
                        {
                            cache.Refresh(key);
                        }
                    }
                    else
                    {
                        if (existing.HasValue)
                        {
                            cache.Remove(key);
                        }
                    }
                }

                break;
                }
            }
        }
Exemple #6
0
 public void Refresh()
 {
     _cache.Refresh();
 }