public TValue Put(TKey key, TValue value) { TValue oldValue = default(TValue); cache.AddOrUpdate(key, value, (k, v) => { oldValue = cache[key]; return(value); }); return(oldValue); }
public bool TryGetNode <TNode>(IStorageHandle handle, out TNode tnode, ISerializer <TNode> serializer) { if (_serializer == null) { _serializer = (ISerializer <Node>)serializer; } var fetch = new FetchFromStore <TNode> { DirtyCache = _dirty, Storage = _store, Serializer = serializer, }; _cache.AddOrUpdate(handle, ref fetch); if (fetch.Success) { tnode = fetch.Value; return(true); } tnode = default(TNode); return(false); }
public static void TestExceptions() { var dictionary = new LurchTable <string, int>(); Assert.Null(Record.Exception( () => dictionary.TryAdd(null, 0))); // "TestExceptions: FAILED. TryAdd threw ANE when null key is passed"); Assert.Null(Record.Exception( () => dictionary.ContainsKey(null))); // "TestExceptions: FAILED. Contains threw ANE when null key is passed"); int item; Assert.Null(Record.Exception( () => dictionary.TryRemove(null, out item))); // "TestExceptions: FAILED. TryRemove threw ANE when null key is passed"); Assert.Null(Record.Exception( () => dictionary.TryGetValue(null, out item))); // "TestExceptions: FAILED. TryGetValue threw ANE when null key is passed"); Assert.Throws <KeyNotFoundException>( () => { var x = dictionary[null]; }); // "TestExceptions: FAILED. this[] threw ANE when null key is passed"); (it should throw KeyNotFoundException instead) Assert.Throws <KeyNotFoundException>( () => { var x = dictionary["1"]; }); // "TestExceptions: FAILED. this[] didn't throw KeyNotFoundException!"); Assert.Null(Record.Exception( () => dictionary[null] = 1)); // "TestExceptions: FAILED. this[] threw ANE when null key is passed"); //Assert.Throws<ArgumentNullException>( // () => dictionary.GetOrAdd(null, (k, m) => 0, 0)); //// "TestExceptions: FAILED. GetOrAdd didn't throw ANE when null key is passed"); //Assert.Throws<ArgumentNullException>( // () => dictionary.GetOrAdd("1", null, 0)); //// "TestExceptions: FAILED. GetOrAdd didn't throw ANE when null valueFactory is passed"); Assert.Null(Record.Exception( () => dictionary.GetOrAdd(null, (k) => 0))); // "TestExceptions: FAILED. GetOrAdd threw ANE when null key is passed"); Assert.Throws <ArgumentNullException>( () => dictionary.GetOrAdd("1", null)); // "TestExceptions: FAILED. GetOrAdd didn't throw ANE when null valueFactory is passed"); Assert.Null(Record.Exception( () => dictionary.GetOrAdd(null, 0))); // "TestExceptions: FAILED. GetOrAdd threw ANE when null key is passed"); //Assert.Throws<ArgumentNullException>( // () => dictionary.AddOrUpdate(null, (k, m) => 0, (k, v, m) => 0, 42)); //// "TestExceptions: FAILED. AddOrUpdate didn't throw ANE when null key is passed"); //Assert.Throws<ArgumentNullException>( // () => dictionary.AddOrUpdate("1", (k, m) => 0, null, 42)); //// "TestExceptions: FAILED. AddOrUpdate didn't throw ANE when null updateFactory is passed"); //Assert.Throws<ArgumentNullException>( // () => dictionary.AddOrUpdate("1", null, (k, v, m) => 0, 42)); //// "TestExceptions: FAILED. AddOrUpdate didn't throw ANE when null addFactory is passed"); Assert.Null(Record.Exception( () => dictionary.AddOrUpdate(null, (k) => 0, (k, v) => 0))); // "TestExceptions: FAILED. AddOrUpdate threw ANE when null key is passed"); Assert.Throws <ArgumentNullException>( () => dictionary.AddOrUpdate("1", null, (k, v) => 0)); // "TestExceptions: FAILED. AddOrUpdate didn't throw ANE when null updateFactory is passed"); Assert.Throws <ArgumentNullException>( () => dictionary.AddOrUpdate(null, (k) => 0, null)); // "TestExceptions: FAILED. AddOrUpdate didn't throw ANE when null addFactory is passed"); // Duplicate key. dictionary.TryAdd("1", 1); AssertExtensions.Throws <ArgumentException>(null, () => ((IDictionary <string, int>)dictionary).Add("1", 2)); }
private static void TestGetOrAddOrUpdate(int cLevel, int initSize, int threads, int addsPerThread, bool isAdd) { LurchTable <int, int> dict = new LurchTable <int, int>(/*cLevel,*/ 1); int count = threads; using (ManualResetEvent mre = new ManualResetEvent(false)) { for (int i = 0; i < threads; i++) { int ii = i; Task.Run( () => { for (int j = 0; j < addsPerThread; j++) { if (isAdd) { //call one of the overloads of GetOrAdd switch (j % 2) { case 0: dict.GetOrAdd(j, -j); break; case 1: dict.GetOrAdd(j, x => - x); break; //case 2: // J2N TODO: Implement this overload // dict.GetOrAdd(j, (x, m) => x * m, -1); // break; } } else { switch (j % 2) { case 0: dict.AddOrUpdate(j, -j, (k, v) => - j); break; case 1: dict.AddOrUpdate(j, (k) => - k, (k, v) => - k); break; //case 2: // J2N TODO: Implement this overload // dict.AddOrUpdate(j, (k, m) => k * m, (k, v, m) => k * m, -1); // break; } } } if (Interlocked.Decrement(ref count) == 0) { mre.Set(); } }); } mre.WaitOne(); } foreach (var pair in dict) { Assert.Equal(pair.Key, -pair.Value); } SCG.List <int> gotKeys = new SCG.List <int>(); foreach (var pair in dict) { gotKeys.Add(pair.Key); } gotKeys.Sort(); SCG.List <int> expectKeys = new SCG.List <int>(); for (int i = 0; i < addsPerThread; i++) { expectKeys.Add(i); } Assert.Equal(expectKeys.Count, gotKeys.Count); for (int i = 0; i < expectKeys.Count; i++) { Assert.True(expectKeys[i].Equals(gotKeys[i]), string.Format("* Test '{4}': Level={0}, initSize={1}, threads={2}, addsPerThread={3})" + Environment.NewLine + "> FAILED. The set of keys in the dictionary is are not the same as the expected.", cLevel, initSize, threads, addsPerThread, isAdd ? "GetOrAdd" : "GetOrUpdate")); } // Finally, let's verify that the count is reported correctly. Assert.Equal(addsPerThread, dict.Count); Assert.Equal(addsPerThread, dict.ToArray().Length); }