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); }
/// <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); }
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); }
/// <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); }
/// <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); }
/// <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); } } }
/// <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); } } }
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); }
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); }
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); } } }
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); } } }
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); } } }
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); }
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); }
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); }
/// <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); } } }
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); }
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); }
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); }
/// <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); } } }
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); }
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); } } }
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); } } }
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); }
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); } } }
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); }
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); } } }
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()); })); }
EnumerableIList <EnhancedStackFrame> GetEnumerator() => EnumerableIList.Create(_frames);