diff --git a/src/Libplanet.RocksDBStore/RocksDBStore.cs b/src/Libplanet.RocksDBStore/RocksDBStore.cs index a54d27eb7d..fc403b820c 100644 --- a/src/Libplanet.RocksDBStore/RocksDBStore.cs +++ b/src/Libplanet.RocksDBStore/RocksDBStore.cs @@ -153,7 +153,6 @@ public partial class RocksDBStore : BaseStore private readonly ReaderWriterLockSlim _rwBlockCommitLock; private readonly ReaderWriterLockSlim _rwNextStateRootHashLock; private readonly ReaderWriterLockSlim _rwEvidenceLock; - private bool _pruned = false; private bool _disposed = false; private object _chainForkDeleteLock = new object(); private LruCache>> _indexCache; @@ -284,8 +283,6 @@ public RocksDBStore( new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); _rwEvidenceLock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); - _pruned = ListChainIds().Count() <= 1; - _blockDbCache = new LruCache(dbConnectionCacheSize); _blockDbCache.SetPreRemoveDataMethod(db => { @@ -300,6 +297,8 @@ public RocksDBStore( }); } + private bool IsPruned => ListChainIds().Count() <= 1; + public static bool MigrateChainDBFromColumnFamilies(string path) { var opt = new DbOptions(); @@ -1820,7 +1819,7 @@ private IEnumerable IterateIndexes( Guid chainId, long offset, long? limit, - bool includeDeleted) => _pruned + bool includeDeleted) => IsPruned ? IterateIndexesPruned(chainId, offset, limit, includeDeleted) : IterateIndexesUnpruned(chainId, offset, limit, includeDeleted); @@ -1836,15 +1835,25 @@ private IEnumerable IterateIndexesPruned( } long count = 0; + long limitUpperBound = CountIndex(chainId) - offset; + long actualLimit = limit is { } l + ? Math.Min(l, limitUpperBound) + : limitUpperBound; + + if (actualLimit <= 0) + { + yield break; + } + foreach (BlockHash hash in IterateIndexesInnerPruned(chainId, offset)) { - if (count >= limit) + yield return hash; + count += 1; + + if (count >= actualLimit) { yield break; } - - yield return hash; - count += 1; } }