Exemplo n.º 1
0
        public static IChangeSet <TObject, TKey> RefreshFilteredFrom <TObject, TKey>(this ChangeAwareCache <TObject, TKey> filtered, Cache <TObject, TKey> allData, Func <TObject, bool> predicate)
            where TKey : notnull
        {
            if (allData.Count == 0)
            {
                return(ChangeSet <TObject, TKey> .Empty);
            }

            foreach (var kvp in allData.KeyValues)
            {
                var existing = filtered.Lookup(kvp.Key);
                var matches  = predicate(kvp.Value);

                if (matches)
                {
                    if (!existing.HasValue)
                    {
                        filtered.Add(kvp.Value, kvp.Key);
                    }
                }
                else
                {
                    if (existing.HasValue)
                    {
                        filtered.Remove(kvp.Key);
                    }
                }
            }

            return(filtered.CaptureChanges());
        }
Exemplo n.º 2
0
        private void ProcessItem(ChangeAwareCache <TObject, TKey> target, MergeContainer[] sourceLists, TObject item, TKey key)
        {
            //TODO: Check whether individual items should be updated

            var cached           = target.Lookup(key);
            var shouldBeInResult = MatchesConstraint(sourceLists, key);

            if (shouldBeInResult)
            {
                if (!cached.HasValue)
                {
                    target.AddOrUpdate(item, key);
                }
                else if (!ReferenceEquals(item, cached.Value))
                {
                    target.AddOrUpdate(item, key);
                }
            }
            else
            {
                if (cached.HasValue)
                {
                    target.Remove(key);
                }
            }
        }
Exemplo n.º 3
0
        public Optional <TObject> Lookup(TKey key)
        {
            Optional <TObject> result;

            lock (_locker)
                result = _cache.Lookup(key);

            return(result);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Returns an observable of any changes which match the specified key.  The sequence starts with the inital item in the cache (if there is one).
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public IObservable <Change <TObject, TKey> > Watch(TKey key)
        {
            return(Observable.Create <Change <TObject, TKey> >
                   (
                       observer =>
            {
                var initial = _innerCache.Lookup(key);
                if (initial.HasValue)
                {
                    observer.OnNext(new Change <TObject, TKey>(ChangeReason.Add, key, initial.Value));
                }

                return _changes.Subscribe(changes =>
                {
                    var matches = changes.Where(update => update.Key.Equals(key));
                    foreach (var match in matches)
                    {
                        observer.OnNext(match);
                    }
                });
            }));
        }
Exemplo n.º 5
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());
        }
Exemplo n.º 6
0
        public Optional <TObject> Lookup(TKey key)
        {
            var item = _cache.Lookup(key);

            return(item.HasValue ? item.Value : Optional.None <TObject>());
        }
Exemplo n.º 7
0
 public Optional <TObject> Lookup(TKey key)
 {
     return(_cache.Lookup(key));
 }
Exemplo n.º 8
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;
                }
            }
        }