public void RecordCalculatedResults(string k1, string k2, decimal pr) { if (calculatedResults.ContainsKey(k1)) { var innerDictionary = calculatedResults[k1]; if (innerDictionary.ContainsKey(k2)) { throw new NotSupportedException("This pattern expects only one entry per k1k2 pair"); } else { //ToDo: Better understanding/handling of exceptions here try { innerDictionary.Add(k2, pr); } catch { new Exception($"adding {pr} to {k1}'s innerDictionary keyed by {k2} failed"); } } } else { var innerDictionary = new ConcurrentObservableDictionary <string, decimal>(); try { innerDictionary.Add(k2, pr); } catch { new Exception($"adding {pr} to the new innerDictionary keyed by {k2} failed"); } try { calculatedResults.Add(k1, innerDictionary); } catch { new Exception($"adding the new innerDictionary to calculatedResults keyed by {k1} failed"); } }; }
protected virtual async Task UpdateAllData() { var values = await Database.StringGetAsync(CurrentCachedKeys).ConfigureAwait(false); for (var i = 0; i < CurrentCachedKeys.Length; ++i) { Cache.AddOrUpdate(CurrentCachedKeys[i], values[i]); } foreach (var cacheKey in Cache.Keys) { if (CachedKeys.ContainsKey(cacheKey)) { continue; } _tasks.Add(Task.Run(() => { Cache.TryRemove(cacheKey, out _); })); } if (_tasks.Count == 0) { return; } Task.WaitAll(_tasks.ToArray()); _tasks.Clear(); }
/// <summary> /// Attempts to add a new member variable or property at runtime to the current object instance. /// This method is called automatically when the object is created as "dynamic" and a variable or property is implicitly set. /// </summary> /// <param name="binder"></param> /// <param name="value"></param> /// <returns>True if a member variable already existed and was updated, otherwise false if the variable was newly /// added to the object.</returns> /// <exception cref="Exception">This method will raise an exception if <see cref="value"/> has a different type than /// the value that is already set.</exception> public override bool TrySetMember(SetMemberBinder binder, object value) { if (_values.ContainsKey(binder.Name)) { var type = _values[binder.Name].GetType(); if (value.GetType() == type) { SetValue(binder.Name, value); return(true); } else { throw new Exception("Value " + value + " is not of type " + type.Name); } } else { SetValue(binder.Name, value); return(false); } }
public void TestManyOperations() { // Create some random, but unique items // Use a fixed seed for consistency in results Random random = new Random(1); HashSet <int> baseItemsSet = new HashSet <int>(); while (baseItemsSet.Count < 1_100_000) { baseItemsSet.Add(random.Next()); } // Create 2 collections, 1 to test, and 1 to compare against var testCollection = new ConcurrentObservableDictionary <string, string>(); var list = new List <KeyValuePair <string, string> >(); // Create 1,000,000 items to add and insert var itemsToAdd = baseItemsSet .Take(1_000_000) .Select(x => Swordfish.NET.Collections.KeyValuePair.Create($"Key {x}", $"Value {x}")) .ToList(); // Create 100,000 items to insert var itemsToInsert = baseItemsSet .Skip(1_000_000) .Take(100_000) .Select(x => Swordfish.NET.Collections.KeyValuePair.Create($"Insert Key {x}", $"Insert Value {x}")) .ToList(); // Create items to remove var itemsToRemove = itemsToInsert .Take(1000) .ToList(); foreach (var item in itemsToAdd) { testCollection.Add(item.Key, item.Value); list.Add(item); } // Check items are equal count Assert.IsTrue(list.Count == testCollection.Count, "Added Items correct count"); // Check items are equal order var allEqualAfterAdd = list .Zip(testCollection, (a, b) => (a.Key == b.Key) && (a.Value == b.Value)) .All(a => a); Assert.IsTrue(allEqualAfterAdd, "Added items correct order"); // Test inserting items int insertIndex = itemsToInsert.Count + 100; foreach (var item in itemsToInsert) { // We have the function but it's there for other reasons testCollection.Insert(insertIndex, item); list.Insert(insertIndex, item); insertIndex--; } // Check items are equal count Assert.IsTrue(list.Count == testCollection.Count, "Items correct count after inserting"); // Check items are equal order var allEqualAfterInsert = list .Zip(testCollection, (a, b) => (a.Key == b.Key) && (a.Value == b.Value)) .All(a => a); Assert.IsTrue(allEqualAfterAdd, "Items correct order after insert"); // Test removing items foreach (var item in itemsToRemove) { testCollection.Remove(item.Key); list.Remove(item); } // Check items are equal count Assert.IsTrue(list.Count == testCollection.Count, "Items correct count after removing"); // Check items are equal order var allEqualAfterRemove = list .Zip(testCollection, (a, b) => (a.Key == b.Key) && (a.Value == b.Value)) .All(a => a); Assert.IsTrue(allEqualAfterRemove, "Items correct order after removing"); // Test contains var containsAll = list .All(kv => testCollection.Contains(kv)); Assert.IsTrue(containsAll, "Contains all the items is true"); var containsNone = itemsToRemove .Any(kv => testCollection.ContainsKey(kv.Key)); Assert.IsFalse(containsNone, "Contains any of the removed items is false"); // Test removing at int removeAtIndex = list.Count - 30; while (removeAtIndex >= 0 && list.Count > 0) { list.RemoveAt(removeAtIndex); testCollection.RemoveAt(removeAtIndex); removeAtIndex -= 30; } // Check items are equal count Assert.IsTrue(list.Count == testCollection.Count, "Items correct count after removing at index"); // Check items are equal order var allEqualAfterRemoveAt = list .Zip(testCollection, (a, b) => (a.Key == b.Key) && (a.Value == b.Value)) .All(a => a); Assert.IsTrue(allEqualAfterRemoveAt, "Items correct order after removing at index"); }