diff --git a/Libplanet.Store/BaseIndex.cs b/Libplanet.Store/BaseIndex.cs deleted file mode 100644 index 8b8895e33e6..00000000000 --- a/Libplanet.Store/BaseIndex.cs +++ /dev/null @@ -1,105 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; - -namespace Libplanet.Store -{ - public abstract class BaseIndex : IDictionary - { - protected BaseIndex(IStore store) - { - Store = store; - } - - public abstract ICollection Keys { get; } - - public abstract ICollection Values { get; } - - public abstract int Count { get; } - - public abstract bool IsReadOnly { get; } - - protected IStore Store { get; } - - public abstract TVal this[TKey key] { get; set; } - - public void Add(TKey key, TVal value) - { - this[key] = value; - } - - public void Add(KeyValuePair item) - { - Add(item.Key, item.Value); - } - - public void Clear() - { - foreach (TKey key in Keys) - { - Remove(key); - } - } - - public abstract bool Contains(KeyValuePair item); - - public abstract bool ContainsKey(TKey key); - - public void CopyTo(KeyValuePair[] array, int arrayIndex) - { - if (array == null) - { - throw new ArgumentNullException(nameof(array)); - } - - if (arrayIndex < 0) - { - throw new ArgumentOutOfRangeException(nameof(arrayIndex)); - } - - if (Count > array.Length + arrayIndex) - { - throw new ArgumentException(); - } - - foreach (KeyValuePair kv in this) - { - array[arrayIndex++] = kv; - } - } - - public IEnumerator> GetEnumerator() - { - foreach (var key in Keys) - { - yield return new KeyValuePair(key, this[key]); - } - } - - public abstract bool Remove(TKey key); - - public bool Remove(KeyValuePair item) - { - return Remove(item.Key); - } - - public bool TryGetValue(TKey key, out TVal value) - { - try - { - value = this[key]; - return true; - } - catch (KeyNotFoundException) - { - value = default!; - return false; - } - } - - IEnumerator IEnumerable.GetEnumerator() - { - return GetEnumerator(); - } - } -} diff --git a/Libplanet.Store/BlockSet.cs b/Libplanet.Store/BlockSet.cs index 2fb0c5ceee9..75e9b502f2f 100644 --- a/Libplanet.Store/BlockSet.cs +++ b/Libplanet.Store/BlockSet.cs @@ -1,4 +1,5 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Linq; using Caching; @@ -6,31 +7,32 @@ namespace Libplanet.Store { - public class BlockSet : BaseIndex + public class BlockSet : IDictionary { private readonly LRUCache _cache; + private readonly IStore _store; public BlockSet(IStore store, int cacheSize = 4096) - : base(store) { _cache = new LRUCache(cacheSize, Math.Max(cacheSize / 64, 8)); + _store = store; } - public override ICollection Keys => - Store.IterateBlockHashes().ToList(); + public ICollection Keys => + _store.IterateBlockHashes().ToList(); - public override ICollection Values => - Store.IterateBlockHashes() + public ICollection Values => + _store.IterateBlockHashes() .Select(GetBlock) .Where(block => block is { }) .Select(block => block!) .ToList(); - public override int Count => (int)Store.CountBlocks(); + public int Count => (int)_store.CountBlocks(); - public override bool IsReadOnly => false; + public bool IsReadOnly => false; - public override Block this[BlockHash key] + public Block this[BlockHash key] { get { @@ -65,42 +67,106 @@ public override Block this[BlockHash key] } value.ValidateTimestamp(); - Store.PutBlock(value); + _store.PutBlock(value); _cache.AddReplace(value.Hash, value); } } - public override bool Contains(KeyValuePair item) => - Store.ContainsBlock(item.Key); + public bool Contains(KeyValuePair item) => + _store.ContainsBlock(item.Key); - public override bool ContainsKey(BlockHash key) => - Store.ContainsBlock(key); + public bool ContainsKey(BlockHash key) => + _store.ContainsBlock(key); - public override bool Remove(BlockHash key) + public bool Remove(BlockHash key) { - bool deleted = Store.DeleteBlock(key); + bool deleted = _store.DeleteBlock(key); _cache.Remove(key); return deleted; } + public void Add(BlockHash key, Block value) + { + this[key] = value; + } + + public bool TryGetValue(BlockHash key, out Block value) + { + try + { + value = this[key]; + return true; + } + catch (KeyNotFoundException) + { + value = default!; + return false; + } + } + + public void Add(KeyValuePair item) => Add(item.Key, item.Value); + + public void Clear() + { + foreach (BlockHash key in Keys) + { + Remove(key); + } + } + + public void CopyTo(KeyValuePair[] array, int arrayIndex) + { + if (array == null) + { + throw new ArgumentNullException(nameof(array)); + } + + if (arrayIndex < 0) + { + throw new ArgumentOutOfRangeException(nameof(arrayIndex)); + } + + if (Count > array.Length + arrayIndex) + { + throw new ArgumentException(); + } + + foreach (KeyValuePair kv in this) + { + array[arrayIndex++] = kv; + } + } + + public bool Remove(KeyValuePair item) => Remove(item.Key); + + public IEnumerator> GetEnumerator() + { + foreach (var key in Keys) + { + yield return new KeyValuePair(key, this[key]); + } + } + + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + private Block? GetBlock(BlockHash key) { if (_cache.TryGet(key, out Block cached)) { - if (Store.ContainsBlock(key)) + if (_store.ContainsBlock(key)) { return cached; } else { - // The cached block had been deleted on Store... + // The cached block had been deleted on _store... _cache.Remove(key); } } - Block? fetched = Store.GetBlock(key); + Block? fetched = _store.GetBlock(key); if (fetched is { }) { _cache.AddReplace(key, fetched); diff --git a/Libplanet.Tests/Store/BlockSetTest.cs b/Libplanet.Tests/Store/BlockSetTest.cs index 18da2ecd181..d2b2730ab36 100644 --- a/Libplanet.Tests/Store/BlockSetTest.cs +++ b/Libplanet.Tests/Store/BlockSetTest.cs @@ -56,10 +56,10 @@ public void CanIterateItems() [Fact] public void CanCountItem() { - Assert.Equal(0, _set.Count); + Assert.Empty(_set); _set[_fx.Block1.Hash] = _fx.Block1; - Assert.Equal(1, _set.Count); + Assert.Single(_set); _set[_fx.Block2.Hash] = _fx.Block2; _set[_fx.Block3.Hash] = _fx.Block3;