Ejemplo n.º 1
0
        public IEnumerable <AggregationResult> Modify(ITuple tuple, IEnumerable <IFact> facts)
        {
            var keys         = new List <TKey>();
            var resultLookup = new DefaultKeyMap <TKey, AggregationResult>();

            foreach (var fact in facts)
            {
                var source     = (TSource)fact.Value;
                var key        = (TKey)_keySelector.Invoke(tuple, fact);
                var element    = (TElement)_elementSelector.Invoke(tuple, fact);
                var oldKey     = _sourceToKey[source];
                var oldElement = _sourceToElement[source];
                _sourceToKey[source]     = key;
                _sourceToElement[source] = element;

                if (Equals(key, oldKey))
                {
                    var result = Modify(key, oldElement, element);
                    if (!resultLookup.ContainsKey(key))
                    {
                        keys.Add(key);
                        resultLookup[key] = result;
                    }
                }
                else
                {
                    var result1 = Remove(oldKey, oldElement);
                    if (!resultLookup.ContainsKey(oldKey))
                    {
                        keys.Add(oldKey);
                    }
                    resultLookup[oldKey] = result1;

                    var result2 = Add(key, element);
                    AggregationResult previousResult;
                    if (!resultLookup.TryGetValue(key, out previousResult))
                    {
                        keys.Add(key);
                        resultLookup[key] = result2;
                    }
                    else if (previousResult.Action == AggregationAction.Removed ||
                             result2.Action == AggregationAction.Added)
                    {
                        resultLookup[key] = AggregationResult.Modified(previousResult.Aggregate);
                    }
                }
            }
            var results = GetResults(keys, resultLookup);

            return(results);
        }
Ejemplo n.º 2
0
        public IEnumerable <AggregationResult> Remove(ITuple tuple, IEnumerable <IFact> facts)
        {
            var keys         = new List <TKey>();
            var resultLookup = new DefaultKeyMap <TKey, AggregationResult>();

            foreach (var fact in facts)
            {
                var source     = (TSource)fact.Value;
                var oldKey     = _sourceToKey[source];
                var oldElement = _sourceToElement[source];
                _sourceToKey.Remove(source);
                _sourceToElement.Remove(source);
                var result = Remove(oldKey, oldElement);
                if (!resultLookup.ContainsKey(oldKey))
                {
                    keys.Add(oldKey);
                }
                resultLookup[oldKey] = result;
            }
            var results = GetResults(keys, resultLookup);

            return(results);
        }
Ejemplo n.º 3
0
        public IEnumerable <AggregationResult> Add(ITuple tuple, IEnumerable <IFact> facts)
        {
            var keys         = new List <TKey>();
            var resultLookup = new DefaultKeyMap <TKey, AggregationResult>();

            foreach (var fact in facts)
            {
                var source  = (TSource)fact.Value;
                var key     = (TKey)_keySelector.Invoke(tuple, fact);
                var element = (TElement)_elementSelector.Invoke(tuple, fact);
                _sourceToKey[source]     = key;
                _sourceToElement[source] = element;
                var result = Add(key, element);
                if (!resultLookup.ContainsKey(key))
                {
                    keys.Add(key);
                    resultLookup[key] = result;
                }
            }
            var results = GetResults(keys, resultLookup);

            return(results);
        }
Ejemplo n.º 4
0
        private static IEnumerable <AggregationResult> GetResults(IEnumerable <TKey> keys, DefaultKeyMap <TKey, AggregationResult> lookup)
        {
            var results = new List <AggregationResult>();

            foreach (var key in keys)
            {
                var result = lookup[key];
                results.Add(result);
            }
            return(results);
        }