Example #1
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.ValueOrDefault(), 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());
        }
Example #2
0
        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.Any())
            {
                errors.ForEach(t => _exceptionCallback(new Error <TSource, TKey>(t.Error, t.Change.Current, t.Change.Key)));
            }

            foreach (var result in transformedItems.Where(t => t.Success))
            {
                var 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));
        }
Example #3
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)
            {
                var 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;
                    var 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());
        }
Example #4
0
 public void Refresh()
 {
     _cache.Refresh();
 }
Example #5
0
        public static void FilterChanges <TObject, TKey>(this ChangeAwareCache <TObject, TKey> cache,
                                                         IChangeSet <TObject, TKey> changes,
                                                         Func <TObject, bool> predicate)
        {
            foreach (var change in changes)
            {
                var key = change.Key;
                switch (change.Reason)
                {
                case ChangeReason.Add:
                {
                    if (predicate(change.Current))
                    {
                        cache.AddOrUpdate(change.Current, key);
                    }
                }
                break;

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

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

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