public ProvenBlockHeaderStore(
            IDateTimeProvider dateTimeProvider,
            ILoggerFactory loggerFactory,
            IProvenBlockHeaderRepository provenBlockHeaderRepository,
            INodeStats nodeStats,
            IInitialBlockDownloadState initialBlockDownloadState)
        {
            Guard.NotNull(loggerFactory, nameof(loggerFactory));
            Guard.NotNull(provenBlockHeaderRepository, nameof(provenBlockHeaderRepository));
            Guard.NotNull(nodeStats, nameof(nodeStats));

            this.logger = loggerFactory.CreateLogger(this.GetType().FullName);
            this.provenBlockHeaderRepository = provenBlockHeaderRepository;
            this.initialBlockDownloadState   = initialBlockDownloadState;

            this.lockObject   = new object();
            this.pendingBatch = new SortedDictionary <int, ProvenBlockHeader>();
            this.Cache        = new MemorySizeCache <int, ProvenBlockHeader>(this.MemoryCacheSizeLimitInBytes);

            this.performanceCounter = new BackendPerformanceCounter(dateTimeProvider);

            if (nodeStats.DisplayBenchStats)
            {
                nodeStats.RegisterStats(this.AddBenchStats, StatsType.Benchmark, this.GetType().Name);
            }

            nodeStats.RegisterStats(this.AddComponentStats, StatsType.Component, this.GetType().Name);
        }
        public void CacheKeepsMostRecentlyUsedItems()
        {
            var cache = new MemorySizeCache <int, string>(100);

            for (int i = 0; i < 15; i++)
            {
                cache.AddOrUpdate(i, RandomUtils.GetInt32().ToString(), 10);

                if (i == 8)
                {
                    // Use first 3 items.
                    for (int k = 0; k < 3; ++k)
                    {
                        cache.TryGetValue(k, out string unused);
                    }
                }
            }

            // Cache should have 0-2 & 8-14.
            for (int i = 0; i < 15; i++)
            {
                bool success = cache.TryGetValue(i, out string unused);

                if (i < 3 || i > 7)
                {
                    Assert.True(success);
                }
                else
                {
                    Assert.False(success);
                }
            }

            Assert.Equal(10, cache.Count);
        }
        /// <summary>
        /// Initializes a new instance of the object.
        /// </summary>
        /// <param name="dateTimeProvider">Provider of time functions.</param>
        /// <param name="loggerFactory">Factory to create a logger for this type.</param>
        /// <param name="provenBlockHeaderRepository">Persistent interface of the <see cref="ProvenBlockHeader"/> DBreeze repository.</param>
        /// <param name="nodeLifetime">Allows consumers to perform clean-up during a graceful shutdown.</param>
        /// <param name="nodeStats">Registers an action used to append node stats when collected.</param>
        /// <param name="asyncLoopFactory">Factory for creating and also possibly starting application defined tasks inside async loop.</param>
        public ProvenBlockHeaderStore(
            IDateTimeProvider dateTimeProvider,
            ILoggerFactory loggerFactory,
            IProvenBlockHeaderRepository provenBlockHeaderRepository,
            INodeLifetime nodeLifetime,
            INodeStats nodeStats,
            IAsyncLoopFactory asyncLoopFactory)
        {
            Guard.NotNull(loggerFactory, nameof(loggerFactory));
            Guard.NotNull(provenBlockHeaderRepository, nameof(provenBlockHeaderRepository));
            Guard.NotNull(nodeLifetime, nameof(nodeLifetime));
            Guard.NotNull(nodeStats, nameof(nodeStats));
            Guard.NotNull(asyncLoopFactory, nameof(asyncLoopFactory));

            this.logger = loggerFactory.CreateLogger(this.GetType().FullName);
            this.provenBlockHeaderRepository = provenBlockHeaderRepository;
            this.nodeLifetime = nodeLifetime;

            this.lockObject   = new object();
            this.PendingBatch = new Dictionary <int, ProvenBlockHeader>();
            this.Cache        = new MemorySizeCache <int, ProvenBlockHeader>(this.MemoryCacheSizeLimitInBytes);

            this.asyncLoopFactory = asyncLoopFactory;

            this.performanceCounter = new BackendPerformanceCounter(dateTimeProvider);
            nodeStats.RegisterStats(this.AddBenchStats, StatsType.Benchmark);
            nodeStats.RegisterStats(this.AddComponentStats, StatsType.Component);
        }
Ejemplo n.º 4
0
        public void CacheDoesNotExceedMaxSizeLimit()
        {
            long maxSize = 100;

            var cache = new MemorySizeCache <int, string>(maxSize);

            for (int i = 0; i < 10; i++)
            {
                cache.AddOrUpdate(i, "item", 20); // fixed size to exceed the limit
            }

            Assert.True(maxSize >= cache.TotalSize);
        }
        public void CacheDoesNotExceedMaxSizeLimit()
        {
            long maxSize = 100;

            var cache = new MemorySizeCache <int, string>(maxSize);

            for (int i = 0; i < 10; i++)
            {
                var item = RandomUtils.GetInt32().ToString();
                cache.AddOrUpdate(i, item, item.Length);
            }

            Assert.True(maxSize >= cache.TotalSize);
        }
Ejemplo n.º 6
0
        public void CacheKeepsMostRecentlyAddedItemsNoneWereUsed()
        {
            int maxItemsCount   = 10;
            int itemsCountToAdd = 100;

            var cache = new MemorySizeCache <int, string>(100);

            for (int i = 0; i < itemsCountToAdd; i++)
            {
                cache.AddOrUpdate(i, "item", 10); // fixed size
            }

            for (int i = itemsCountToAdd - maxItemsCount; i < itemsCountToAdd; i++)
            {
                bool success = cache.TryGetValue(i, out string value);

                Assert.True(success);
            }

            Assert.Equal(maxItemsCount, cache.Count);
        }
        public void CacheKeepsMostRecentlyAddedItemsNoneWereUsed()
        {
            int maxItemsCount   = 10;
            int itemsCountToAdd = 100;

            var cache = new MemorySizeCache <int, string>(100);

            for (int i = 0; i < itemsCountToAdd; i++)
            {
                var item = RandomUtils.GetInt32().ToString();
                cache.AddOrUpdate(i, item, item.Length);
            }

            for (int i = itemsCountToAdd - maxItemsCount; i < itemsCountToAdd; i++)
            {
                bool success = cache.TryGetValue(i, out string value);

                Assert.True(success);
            }

            Assert.Equal(maxItemsCount, cache.Count);
        }
        public void CanManuallyRemoveItemsFromTheCache()
        {
            var cache = new MemorySizeCache <int, string>(100);

            for (int i = 0; i < 5; i++)
            {
                cache.AddOrUpdate(i, i + "VALUE", 10);
            }

            for (int i = 0; i < 3; i++)
            {
                cache.Remove(i);
            }

            Assert.Equal(2, cache.Count);

            // Check if cache still has the same values that were added.
            for (int i = 3; i < 5; i++)
            {
                cache.TryGetValue(i, out string value);
                Assert.Equal(i + "VALUE", value);
            }
        }