コード例 #1
0
        public void AddOrUpdate(IEnumerable <TObject> items)
        {
            if (items == null)
            {
                throw new ArgumentNullException(nameof(items));
            }

            if (_keySelector == null)
            {
                throw new KeySelectorException("A key selector must be specified");
            }

            if (items is IList <TObject> list)
            {
                //zero allocation enumerator
                var enumerable = EnumerableIList.Create(list);
                foreach (var item in enumerable)
                {
                    _cache.AddOrUpdate(item, _keySelector(item));
                }
            }
            else
            {
                foreach (var item in items)
                {
                    _cache.AddOrUpdate(item, _keySelector(item));
                }
            }
        }
        /// <summary>
        /// Demystifies the given <paramref name="exception"/> and tracks the original stack traces for the whole exception tree.
        /// </summary>
        private static T Demystify <T>(this T exception, Dictionary <Exception, string> originalStacksTracker) where T : Exception
        {
            try
            {
                if (originalStacksTracker?.ContainsKey(exception) == false)
                {
                    originalStacksTracker[exception] = exception.GetStackTracesString();
                }

                var stackTrace = new EnhancedStackTrace(exception);

                if (stackTrace.FrameCount > 0)
                {
                    exception.SetStackTracesString(stackTrace.ToString());
                }

                if (exception is AggregateException aggEx)
                {
                    foreach (var ex in EnumerableIList.Create(aggEx.InnerExceptions))
                    {
                        ex.Demystify(originalStacksTracker);
                    }
                }

                exception.InnerException?.Demystify(originalStacksTracker);
            }
            catch
            {
                // Processing exceptions shouldn't throw exceptions; if it fails
            }

            return(exception);
        }
コード例 #3
0
        /// <nodoc />
        private static T Demystify <T>(this T exception, Dictionary <Exception, string> originalStacks) where T : Exception
        {
            try
            {
                if (!originalStacks.ContainsKey(exception))
                {
                    originalStacks[exception] = (string)stackTraceString.GetValue(exception);
                }

                var stackTrace = new EnhancedStackTrace(exception);

                if (stackTrace.FrameCount > 0)
                {
                    stackTraceString.SetValue(exception, stackTrace.ToString());
                }

                var aggEx = exception as AggregateException;
                if (aggEx != null)
                {
                    foreach (var ex in EnumerableIList.Create(aggEx.InnerExceptions))
                    {
                        ex.Demystify(originalStacks);
                    }
                }

                exception.InnerException?.Demystify(originalStacks);
            }
#pragma warning disable ERP022 // Processing exceptions shouldn't throw exceptions; if it fails
            catch
            {
            }
#pragma warning restore ERP022

            return(exception);
        }
コード例 #4
0
      public void ChangeSetAllocations2()
      {
          // Arrange
          var iList = Enumerable.Range(1, 100)
                      .Select(j => new Person("P" + j, j))
                      .Select(p => new Change <Person, string>(ChangeReason.Add, p.Name, p))
                      .ToList();

          var changes = new ChangeSet <Person, string>(iList);

          // Act
          EnumerableIList <Change <Person, string> > eIList = changes;
          var startAllocs = GC.GetAllocatedBytesForCurrentThread();

          // Assert
          var i     = 0;
          var count = changes.Count;

          foreach (var item in eIList)
          {
              i++;
          }
          var endAllocs = GC.GetAllocatedBytesForCurrentThread();

          Assert.Equal(startAllocs, endAllocs);
          Assert.Equal(iList.Count, i);
      }
コード例 #5
0
        /// <summary>
        ///     Demystifies the given <paramref name="exception" /> and tracks the original stack traces for the whole exception
        ///     tree.
        /// </summary>
        public static T Demystify <T>(this T exception) where T : Exception
        {
            try
            {
                var stackTrace = new EnhancedStackTrace(exception);

                if (stackTrace.FrameCount > 0)
                {
                    exception.SetStackTracesString(stackTrace.ToString());
                }

                if (InteropHelper.Types.AggregateException.IsInstanceOfType(exception))
                {
                    var innerExceptions =
                        InteropHelper.Types.InnerExceptions.GetValue(exception, null) as ReadOnlyCollection <Exception>;
                    foreach (var ex in EnumerableIList.Create(innerExceptions))
                    {
                        ex.Demystify();
                    }
                }

                exception.InnerException?.Demystify();
            }
            catch
            {
                // Processing exceptions shouldn't throw exceptions; if it fails
            }

            return(exception);
        }
コード例 #6
0
        /// <summary>
        /// Demystifies the given <paramref name="exception"/> and tracks the original stack traces for the whole exception tree.
        /// </summary>
        public static T Demystify <T>(this T exception) where T : Exception
        {
            try
            {
                var stackTrace = new EnhancedStackTrace(exception);

                if (stackTrace.FrameCount > 0)
                {
                    exception.SetStackTracesString(stackTrace.ToString());
                }

                if (exception is AggregateException aggEx)
                {
                    foreach (var ex in EnumerableIList.Create(aggEx.InnerExceptions))
                    {
                        ex.Demystify();
                    }
                }

                exception.InnerException?.Demystify();
            }
            catch
            {
                // Processing exceptions shouldn't throw exceptions; if it fails
            }

            return(exception);
        }
コード例 #7
0
        /// <summary>
        /// Removes the item matching the specified keys.
        /// </summary>
        /// <param name="keys">The keys.</param>
        public void Remove(IEnumerable <TKey> keys)
        {
            if (keys == null)
            {
                throw new ArgumentNullException(nameof(keys));
            }

            if (_data == null)
            {
                return;
            }

            if (keys is IList <TKey> list)
            {
                EnsureInitialised(list.Count);
                var enumerable = EnumerableIList.Create(list);
                foreach (var item in enumerable)
                {
                    Remove(item);
                }
            }
            else
            {
                EnsureInitialised();
                foreach (var key in keys)
                {
                    Remove(key);
                }
            }
        }
コード例 #8
0
 /// <summary>
 /// Removes the item matching the specified keys.
 /// </summary>
 /// <param name="keys">The keys.</param>
 public void Remove(IEnumerable <TKey> keys)
 {
     if (_data == null)
     {
         return;
     }
     ;
     if (keys is IList <TKey> list)
     {
         EnsureInitialised(list.Count);
         var enumerable = EnumerableIList.Create(list);
         foreach (var item in enumerable)
         {
             Remove(item);
         }
     }
     else
     {
         EnsureInitialised();
         foreach (var key in keys)
         {
             Remove(key);
         }
     }
 }
コード例 #9
0
ファイル: default.cs プロジェクト: benaadams/Ben.Enumerable
        public void CanEnumerateDefault()
        {
            // Arrange
            EnumerableIList <int> eIList = default(EnumerableIList <int>);

            // Act
            var i = 0;

            foreach (var item in eIList)
            {
                i++;
            }

            // Assert
            Assert.Equal(0, i);
        }
コード例 #10
0
ファイル: default.cs プロジェクト: benaadams/Ben.Enumerable
        public void CanEnumerateEmpty()
        {
            // Arrange
            EnumerableIList <int> eIList = EnumerableIList <int> .Empty;

            // Act
            var i = 0;

            foreach (var item in eIList)
            {
                i++;
            }

            // Assert
            Assert.Equal(0, i);
        }
コード例 #11
0
 public void AddOrUpdate(IEnumerable <KeyValuePair <TKey, TObject> > itemsPairs)
 {
     if (itemsPairs is IList <KeyValuePair <TKey, TObject> > list)
     {
         // zero allocation enumerator
         foreach (var item in EnumerableIList.Create(list))
         {
             _cache.AddOrUpdate(item.Value, item.Key);
         }
     }
     else
     {
         foreach (var item in itemsPairs)
         {
             _cache.AddOrUpdate(item.Value, item.Key);
         }
     }
 }
コード例 #12
0
ファイル: Cache.cs プロジェクト: lummoj/DynamicData
 public void Remove(IEnumerable <TKey> keys)
 {
     if (keys is IList <TKey> list)
     {
         var enumerable = EnumerableIList.Create(list);
         foreach (var item in enumerable)
         {
             Remove(item);
         }
     }
     else
     {
         foreach (var key in keys)
         {
             Remove(key);
         }
     }
 }
コード例 #13
0
 private void ProcessChanges(ChangeAwareCache <TObject, TKey> target, MergeContainer[] sourceLists, IEnumerable <KeyValuePair <TKey, TObject> > items)
 {
     // check whether the item should be removed from the list (or in the case of And, added)
     if (items is IList <KeyValuePair <TKey, TObject> > list)
     {
         // zero allocation enumerator
         foreach (var item in EnumerableIList.Create(list))
         {
             ProcessItem(target, sourceLists, item.Value, item.Key);
         }
     }
     else
     {
         foreach (var item in items)
         {
             ProcessItem(target, sourceLists, item.Value, item.Key);
         }
     }
 }
コード例 #14
0
        public void Empty()
        {
            // Arrange
            int[]                 array  = System.Array.Empty <int>();
            IList <int>           iList  = array;
            EnumerableIList <int> eIList = array;

            // Assert
            var i = 0;

            foreach (var item in eIList)
            {
                Assert.Equal(iList[i], item);

                i++;
            }

            Assert.Equal(iList.Count, i);
        }
コード例 #15
0
        public void EnumerationSame()
        {
            // Arrange
            IList <int> iList = new[] { 1, 2, 3, 4, 5, 6, 7 }.ToImmutableArray();

            // Act
            EnumerableIList <int> eIList = EnumerableIList.Create(iList);

            // Assert
            var i = 0;

            foreach (var item in eIList)
            {
                Assert.Equal(iList[i], item);

                i++;
            }

            Assert.Equal(iList.Count, i);
        }
コード例 #16
0
        public void Empty()
        {
            // Arrange
            IList <int> iList = new List <int>();

            // Act
            EnumerableIList <int> eIList = EnumerableIList.Create(iList);

            // Assert
            var i = 0;

            foreach (var item in eIList)
            {
                Assert.Equal(iList[i], item);

                i++;
            }

            Assert.Equal(iList.Count, i);
        }
コード例 #17
0
 /// <summary>
 /// Raises an evaluate change for the specified keys
 /// </summary>
 public void Refresh(IEnumerable <TKey> keys)
 {
     if (keys is IList <TKey> list)
     {
         EnsureInitialised(list.Count);
         var enumerable = EnumerableIList.Create(list);
         foreach (var key in enumerable)
         {
             Refresh(key);
         }
     }
     else
     {
         EnsureInitialised();
         foreach (var key in keys)
         {
             Refresh(key);
         }
     }
 }
コード例 #18
0
        public void EnumerationSame()
        {
            // Arrange
            int[] array = new[] { 1, 2, 3, 4, 5, 6, 7 };

            IList <int>           iList  = array;
            EnumerableIList <int> eIList = array;

            // Assert
            var i = 0;

            foreach (var item in eIList)
            {
                Assert.Equal(iList[i], item);

                i++;
            }

            Assert.Equal(iList.Count, i);
        }
コード例 #19
0
        public static StringBuilder AppendDemystified(this StringBuilder builder, Exception exception)
        {
            try
            {
                var stackTrace = new EnhancedStackTrace(exception);

                builder.Append(exception.GetType());
                if (!string.IsNullOrEmpty(exception.Message))
                {
                    builder.Append(": ").Append(exception.Message);
                }
                builder.Append(Environment.NewLine);

                if (stackTrace.FrameCount > 0)
                {
                    stackTrace.Append(builder);
                }

                if (InteropHelper.Types.AggregateException.IsInstanceOfType(exception))
                {
                    var innerExceptions =
                        InteropHelper.Types.InnerExceptions.GetValue(exception, null) as ReadOnlyCollection <Exception>;
                    foreach (var ex in EnumerableIList.Create(innerExceptions))
                    {
                        builder.AppendInnerException(ex);
                    }
                }

                if (exception.InnerException != null)
                {
                    builder.AppendInnerException(exception.InnerException);
                }
            }
            catch
            {
                // Processing exceptions shouldn't throw exceptions; if it fails
            }

            return(builder);
        }
コード例 #20
0
      public void WithAllocations()
      {
          // Arrange
          IList <int> iList = new[] { 1, 2, 3, 4, 5, 6, 7 }.ToImmutableArray();

          // Act
          EnumerableIList <int> eIList = EnumerableIList.Create(iList);
          var startAllocs = GC.GetAllocatedBytesForCurrentThread();

          // Assert
          var i = 0;

          foreach (var item in eIList)
          {
              i++;
          }

          var endAllocs = GC.GetAllocatedBytesForCurrentThread();

          Assert.Equal(startAllocs, endAllocs);
          Assert.Equal(iList.Count, i);
      }
コード例 #21
0
        /// <summary>
        /// Removes the item matching the specified keys.
        /// </summary>
        /// <param name="keys">The keys.</param>
        public void Remove(IEnumerable <TKey> keys)
        {
            if (keys is null)
            {
                throw new ArgumentNullException(nameof(keys));
            }

            if (keys is IList <TKey> list)
            {
                foreach (var item in EnumerableIList.Create(list))
                {
                    Remove(item);
                }
            }
            else
            {
                foreach (var key in keys)
                {
                    Remove(key);
                }
            }
        }
コード例 #22
0
        public static StringBuilder AppendDemystified(this StringBuilder builder, Exception exception)
        {
            try
            {
                var stackTrace = new EnhancedStackTrace(exception);

                builder.Append(exception.GetType());
                if (!string.IsNullOrEmpty(exception.Message))
                {
                    builder.Append(": ").Append(exception.Message);
                }
                builder.Append(Environment.NewLine);

                if (stackTrace.FrameCount > 0)
                {
                    stackTrace.Append(builder);
                }

                if (exception is AggregateException aggEx)
                {
                    foreach (var ex in EnumerableIList.Create(aggEx.InnerExceptions))
                    {
                        builder.AppendInnerException(ex);
                    }
                }

                if (exception.InnerException != null)
                {
                    builder.AppendInnerException(exception.InnerException);
                }
            }
            catch
            {
                // Processing exceptions shouldn't throw exceptions; if it fails
            }

            return(builder);
        }
コード例 #23
0
        public void Refresh(IEnumerable <TKey> keys)
        {
            if (keys is null)
            {
                throw new ArgumentNullException(nameof(keys));
            }

            if (keys is IList <TKey> list)
            {
                // zero allocation enumerator
                foreach (var item in EnumerableIList.Create(list))
                {
                    Refresh(item);
                }
            }
            else
            {
                foreach (var key in keys)
                {
                    Refresh(key);
                }
            }
        }
コード例 #24
0
        public void Remove(IEnumerable <KeyValuePair <TKey, TObject> > items)
        {
            if (items is null)
            {
                throw new ArgumentNullException(nameof(items));
            }

            if (items is IList <TObject> list)
            {
                // zero allocation enumerator
                foreach (var key in EnumerableIList.Create(list))
                {
                    Remove(key);
                }
            }
            else
            {
                foreach (var key in items)
                {
                    Remove(key);
                }
            }
        }
コード例 #25
0
        public void EnumerationSame()
        {
            // Arrange
            List <int> list = new List <int> {
                1, 2, 3, 4, 5, 6, 7
            };
            IList <int> iList = list;

            // Act
            EnumerableIList <int> eIList = list;

            // Assert
            var i = 0;

            foreach (var item in eIList)
            {
                Assert.Equal(iList[i], item);

                i++;
            }

            Assert.Equal(iList.Count, i);
        }
コード例 #26
0
 public void Remove(IEnumerable <TKey> keys)
 {
     if (keys == null)
     {
         throw new ArgumentNullException(nameof(keys));
     }
     if (keys is IList <TKey> list)
     {
         //zero allocation enumerator
         var enumerable = EnumerableIList.Create(list);
         foreach (var key in enumerable)
         {
             Remove(key);
         }
     }
     else
     {
         foreach (var key in keys)
         {
             Remove(key);
         }
     }
 }
コード例 #27
0
        public void NoAllocations()
        {
            // Arrange
            int[] array = new[] { 1, 2, 3, 4, 5, 6, 7 };

            IList <int>           iList  = array;
            EnumerableIList <int> eIList = array;

            // Act
            var startAllocs = GC.GetAllocatedBytesForCurrentThread();

            // Assert
            var i = 0;

            foreach (var item in eIList)
            {
                i++;
            }

            var endAllocs = GC.GetAllocatedBytesForCurrentThread();

            Assert.Equal(startAllocs, endAllocs);
            Assert.Equal(iList.Count, i);
        }
コード例 #28
0
        public void Remove(IEnumerable <TObject> items)
        {
            if (items == null)
            {
                throw new ArgumentNullException(nameof(items));
            }

            if (items is IList <TObject> list)
            {
                //zero allocation enumerator
                var enumerable = EnumerableIList.Create(list);
                foreach (var item in enumerable)
                {
                    Remove(item);
                }
            }
            else
            {
                foreach (var item in items)
                {
                    Remove(item);
                }
            }
        }
コード例 #29
0
        public IObservable <IChangeSet <TObject, TKey> > Run()
        {
            return(Observable.Create <IChangeSet <TObject, TKey> >(
                       observer =>
            {
                long orderItemWasAdded = -1;
                var locker = new object();

                if (_expireAfter is null && _limitSizeTo < 1)
                {
                    return _source.Scan(
                        new ChangeAwareCache <TObject, TKey>(),
                        (state, latest) =>
                    {
                        if (latest is IList <TObject> list)
                        {
                            // zero allocation enumerator
                            var enumerableList = EnumerableIList.Create(list);
                            if (!_singleValueSource)
                            {
                                state.Remove(state.Keys.Except(enumerableList.Select(_keySelector)).ToList());
                            }

                            foreach (var item in enumerableList)
                            {
                                state.AddOrUpdate(item, _keySelector(item));
                            }
                        }
                        else
                        {
                            var enumerable = latest.ToList();
                            if (!_singleValueSource)
                            {
                                state.Remove(state.Keys.Except(enumerable.Select(_keySelector)).ToList());
                            }

                            foreach (var item in enumerable)
                            {
                                state.AddOrUpdate(item, _keySelector(item));
                            }
                        }

                        return state;
                    }).Select(state => state.CaptureChanges()).SubscribeSafe(observer);
                }

                var cache = new ChangeAwareCache <ExpirableItem <TObject, TKey>, TKey>();
                var sizeLimited = _source.Synchronize(locker).Scan(
                    cache,
                    (state, latest) =>
                {
                    latest.Select(
                        t =>
                    {
                        var key = _keySelector(t);
                        return CreateExpirableItem(t, key, ref orderItemWasAdded);
                    }).ForEach(ei => cache.AddOrUpdate(ei, ei.Key));

                    if (_limitSizeTo > 0 && state.Count > _limitSizeTo)
                    {
                        var toRemove = state.Count - _limitSizeTo;

                        // remove oldest items
                        cache.KeyValues.OrderBy(exp => exp.Value.Index).Take(toRemove).ForEach(ei => cache.Remove(ei.Key));
                    }

                    return state;
                }).Select(state => state.CaptureChanges()).Publish();

                var timeLimited = (_expireAfter is null ? Observable.Never <IChangeSet <ExpirableItem <TObject, TKey>, TKey> >() : sizeLimited).Filter(ei => ei.ExpireAt != DateTime.MaxValue).MergeMany(
                    grouping =>
                {
                    var expireAt = grouping.ExpireAt.Subtract(_scheduler.Now.DateTime);
                    return Observable.Timer(expireAt, _scheduler).Select(_ => grouping);
                }).Synchronize(locker).Select(
                    item =>
                {
                    cache.Remove(item.Key);
                    return cache.CaptureChanges();
                });

                var publisher = sizeLimited.Merge(timeLimited).Cast(ei => ei.Value).NotEmpty().SubscribeSafe(observer);

                return new CompositeDisposable(publisher, sizeLimited.Connect());
            }));
        }
コード例 #30
0
 EnumerableIList <EnhancedStackFrame> GetEnumerator() => EnumerableIList.Create(_frames);