Remove Microsoft.Extensions.CopyOnWriteDictionary.Sources

This code will move to aspnet/AspNetCore
\n\nCommit migrated from 468940be7f
This commit is contained in:
Nate McMaster 2018-12-13 10:43:02 -08:00
parent 2d001bb583
commit e8d84a1962
4 changed files with 0 additions and 521 deletions

View File

@ -1,155 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections;
using System.Collections.Generic;
namespace Microsoft.Extensions.Internal
{
internal class CopyOnWriteDictionary<TKey, TValue> : IDictionary<TKey, TValue>
{
private readonly IDictionary<TKey, TValue> _sourceDictionary;
private readonly IEqualityComparer<TKey> _comparer;
private IDictionary<TKey, TValue> _innerDictionary;
public CopyOnWriteDictionary(
IDictionary<TKey, TValue> sourceDictionary,
IEqualityComparer<TKey> comparer)
{
if (sourceDictionary == null)
{
throw new ArgumentNullException(nameof(sourceDictionary));
}
if (comparer == null)
{
throw new ArgumentNullException(nameof(comparer));
}
_sourceDictionary = sourceDictionary;
_comparer = comparer;
}
private IDictionary<TKey, TValue> ReadDictionary
{
get
{
return _innerDictionary ?? _sourceDictionary;
}
}
private IDictionary<TKey, TValue> WriteDictionary
{
get
{
if (_innerDictionary == null)
{
_innerDictionary = new Dictionary<TKey, TValue>(_sourceDictionary,
_comparer);
}
return _innerDictionary;
}
}
public virtual ICollection<TKey> Keys
{
get
{
return ReadDictionary.Keys;
}
}
public virtual ICollection<TValue> Values
{
get
{
return ReadDictionary.Values;
}
}
public virtual int Count
{
get
{
return ReadDictionary.Count;
}
}
public virtual bool IsReadOnly
{
get
{
return false;
}
}
public virtual TValue this[TKey key]
{
get
{
return ReadDictionary[key];
}
set
{
WriteDictionary[key] = value;
}
}
public virtual bool ContainsKey(TKey key)
{
return ReadDictionary.ContainsKey(key);
}
public virtual void Add(TKey key, TValue value)
{
WriteDictionary.Add(key, value);
}
public virtual bool Remove(TKey key)
{
return WriteDictionary.Remove(key);
}
public virtual bool TryGetValue(TKey key, out TValue value)
{
return ReadDictionary.TryGetValue(key, out value);
}
public virtual void Add(KeyValuePair<TKey, TValue> item)
{
WriteDictionary.Add(item);
}
public virtual void Clear()
{
WriteDictionary.Clear();
}
public virtual bool Contains(KeyValuePair<TKey, TValue> item)
{
return ReadDictionary.Contains(item);
}
public virtual void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{
ReadDictionary.CopyTo(array, arrayIndex);
}
public bool Remove(KeyValuePair<TKey, TValue> item)
{
return WriteDictionary.Remove(item);
}
public virtual IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{
return ReadDictionary.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}

View File

@ -1,166 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
namespace Microsoft.Extensions.Internal
{
internal struct CopyOnWriteDictionaryHolder<TKey, TValue>
{
private readonly Dictionary<TKey, TValue> _source;
private Dictionary<TKey, TValue> _copy;
public CopyOnWriteDictionaryHolder(Dictionary<TKey, TValue> source)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
_source = source;
_copy = null;
}
public CopyOnWriteDictionaryHolder(CopyOnWriteDictionaryHolder<TKey, TValue> source)
{
_source = source._copy ?? source._source;
_copy = null;
}
public bool HasBeenCopied => _copy != null;
public Dictionary<TKey, TValue> ReadDictionary
{
get
{
if (_copy != null)
{
return _copy;
}
else if (_source != null)
{
return _source;
}
else
{
// Default-Constructor case
_copy = new Dictionary<TKey, TValue>();
return _copy;
}
}
}
public Dictionary<TKey, TValue> WriteDictionary
{
get
{
if (_copy == null && _source == null)
{
// Default-Constructor case
_copy = new Dictionary<TKey, TValue>();
}
else if (_copy == null)
{
_copy = new Dictionary<TKey, TValue>(_source, _source.Comparer);
}
return _copy;
}
}
public Dictionary<TKey, TValue>.KeyCollection Keys
{
get
{
return ReadDictionary.Keys;
}
}
public Dictionary<TKey, TValue>.ValueCollection Values
{
get
{
return ReadDictionary.Values;
}
}
public int Count
{
get
{
return ReadDictionary.Count;
}
}
public bool IsReadOnly
{
get
{
return false;
}
}
public TValue this[TKey key]
{
get
{
return ReadDictionary[key];
}
set
{
WriteDictionary[key] = value;
}
}
public bool ContainsKey(TKey key)
{
return ReadDictionary.ContainsKey(key);
}
public void Add(TKey key, TValue value)
{
WriteDictionary.Add(key, value);
}
public bool Remove(TKey key)
{
return WriteDictionary.Remove(key);
}
public bool TryGetValue(TKey key, out TValue value)
{
return ReadDictionary.TryGetValue(key, out value);
}
public void Add(KeyValuePair<TKey, TValue> item)
{
((ICollection<KeyValuePair<TKey, TValue>>)WriteDictionary).Add(item);
}
public void Clear()
{
WriteDictionary.Clear();
}
public bool Contains(KeyValuePair<TKey, TValue> item)
{
return ((ICollection<KeyValuePair<TKey, TValue>>)ReadDictionary).Contains(item);
}
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{
((ICollection<KeyValuePair<TKey, TValue>>)ReadDictionary).CopyTo(array, arrayIndex);
}
public bool Remove(KeyValuePair<TKey, TValue> item)
{
return ((ICollection<KeyValuePair<TKey, TValue>>)WriteDictionary).Remove(item);
}
public Dictionary<TKey, TValue>.Enumerator GetEnumerator()
{
return ReadDictionary.GetEnumerator();
}
}
}

View File

@ -1,91 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;
namespace Microsoft.Extensions.Internal
{
public class CopyOnWriteDictionaryHolderTest
{
[Fact]
public void ReadOperation_DelegatesToSourceDictionary_IfNoMutationsArePerformed()
{
// Arrange
var source = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase)
{
{ "test-key", "test-value" },
{ "key2", "key2-value" }
};
var holder = new CopyOnWriteDictionaryHolder<string, object>(source);
// Act and Assert
Assert.Equal("key2-value", holder["key2"]);
Assert.Equal(2, holder.Count);
Assert.Equal(new string[] { "test-key", "key2" }, holder.Keys.ToArray());
Assert.Equal(new object[] { "test-value", "key2-value" }, holder.Values.ToArray());
Assert.True(holder.ContainsKey("test-key"));
object value;
Assert.False(holder.TryGetValue("different-key", out value));
Assert.False(holder.HasBeenCopied);
Assert.Same(source, holder.ReadDictionary);
}
[Fact]
public void ReadOperation_DoesNotDelegateToSourceDictionary_OnceAValueIsChanged()
{
// Arrange
var source = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase)
{
{ "key1", "value1" },
{ "key2", "value2" }
};
var holder = new CopyOnWriteDictionaryHolder<string, object>(source);
// Act
holder["key2"] = "value3";
// Assert
Assert.Equal("value2", source["key2"]);
Assert.Equal(2, holder.Count);
Assert.Equal("value1", holder["key1"]);
Assert.Equal("value3", holder["key2"]);
Assert.True(holder.HasBeenCopied);
Assert.NotSame(source, holder.ReadDictionary);
}
[Fact]
public void ReadOperation_DoesNotDelegateToSourceDictionary_OnceValueIsAdded()
{
// Arrange
var source = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase)
{
{ "key1", "value1" },
{ "key2", "value2" }
};
var holder = new CopyOnWriteDictionaryHolder<string, object>(source);
// Act
holder.Add("key3", "value3");
holder.Remove("key1");
// Assert
Assert.Equal(2, source.Count);
Assert.Equal("value1", source["key1"]);
Assert.Equal(2, holder.Count);
Assert.Equal("value2", holder["KeY2"]);
Assert.Equal("value3", holder["key3"]);
Assert.True(holder.HasBeenCopied);
Assert.NotSame(source, holder.ReadDictionary);
}
}
}

View File

@ -1,109 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using Moq;
using Xunit;
namespace Microsoft.Extensions.Internal
{
public class CopyOnWriteDictionaryTest
{
[Fact]
public void ReadOperation_DelegatesToSourceDictionary_IfNoMutationsArePerformed()
{
// Arrange
var values = new List<object>();
var enumerator = Mock.Of<IEnumerator<KeyValuePair<string, object>>>();
var sourceDictionary = new Mock<IDictionary<string, object>>(MockBehavior.Strict);
sourceDictionary
.SetupGet(d => d.Count)
.Returns(100)
.Verifiable();
sourceDictionary
.SetupGet(d => d.Values)
.Returns(values)
.Verifiable();
sourceDictionary
.Setup(d => d.ContainsKey("test-key"))
.Returns(value: true)
.Verifiable();
sourceDictionary
.Setup(d => d.GetEnumerator())
.Returns(enumerator)
.Verifiable();
sourceDictionary
.Setup(d => d["key2"])
.Returns("key2-value")
.Verifiable();
object value;
sourceDictionary.Setup(d => d.TryGetValue("different-key", out value))
.Returns(false)
.Verifiable();
var copyOnWriteDictionary = new CopyOnWriteDictionary<string, object>(sourceDictionary.Object,
StringComparer.OrdinalIgnoreCase);
// Act and Assert
Assert.Equal("key2-value", copyOnWriteDictionary["key2"]);
Assert.Equal(100, copyOnWriteDictionary.Count);
Assert.Same(values, copyOnWriteDictionary.Values);
Assert.True(copyOnWriteDictionary.ContainsKey("test-key"));
Assert.Same(enumerator, copyOnWriteDictionary.GetEnumerator());
Assert.False(copyOnWriteDictionary.TryGetValue("different-key", out value));
sourceDictionary.Verify();
}
[Fact]
public void ReadOperation_DoesNotDelegateToSourceDictionary_OnceAValueIsChanged()
{
// Arrange
var values = new List<object>();
var sourceDictionary = new Dictionary<string, object>
{
{ "key1", "value1" },
{ "key2", "value2" }
};
var copyOnWriteDictionary = new CopyOnWriteDictionary<string, object>(
sourceDictionary,
StringComparer.OrdinalIgnoreCase);
// Act
copyOnWriteDictionary["key2"] = "value3";
// Assert
Assert.Equal("value2", sourceDictionary["key2"]);
Assert.Equal(2, copyOnWriteDictionary.Count);
Assert.Equal("value1", copyOnWriteDictionary["key1"]);
Assert.Equal("value3", copyOnWriteDictionary["key2"]);
}
[Fact]
public void ReadOperation_DoesNotDelegateToSourceDictionary_OnceDictionaryIsModified()
{
// Arrange
var values = new List<object>();
var sourceDictionary = new Dictionary<string, object>
{
{ "key1", "value1" },
{ "key2", "value2" }
};
var copyOnWriteDictionary = new CopyOnWriteDictionary<string, object>(
sourceDictionary,
StringComparer.OrdinalIgnoreCase);
// Act
copyOnWriteDictionary.Add("key3", "value3");
copyOnWriteDictionary.Remove("key1");
// Assert
Assert.Equal(2, sourceDictionary.Count);
Assert.Equal("value1", sourceDictionary["key1"]);
Assert.Equal(2, copyOnWriteDictionary.Count);
Assert.Equal("value2", copyOnWriteDictionary["KeY2"]);
Assert.Equal("value3", copyOnWriteDictionary["key3"]);
}
}
}