Exemple #1
0
        /// <inheritdoc/>
        public override void PutTransaction <T>(Transaction <T> tx)
        {
            if (_txCache.ContainsKey(tx.Id))
            {
                return;
            }

            byte[] key = TxKey(tx.Id);
            if (!(_txIndexDb.Get(key) is null))
            {
                return;
            }

            long   timestamp = tx.Timestamp.ToUnixTimeSeconds();
            string txDbName  = $"epoch{(int)timestamp / _txEpochUnitSeconds}";

            _rwTxLock.EnterWriteLock();
            try
            {
                if (!_txDbCache.TryGetValue(txDbName, out RocksDb txDb))
                {
                    txDb = RocksDBUtils.OpenRocksDb(_options, TxDbPath(txDbName));
                    _txDbCache.AddOrUpdate(txDbName, txDb);
                }

                txDb.Put(key, tx.Serialize(true));
                _txIndexDb.Put(key, RocksDBStoreBitConverter.GetBytes(txDbName));
                _txCache.AddOrUpdate(tx.Id, tx);
            }
            finally
            {
                _rwTxLock.ExitWriteLock();
            }
        }
Exemple #2
0
        /// <inheritdoc cref="IStore"/>
        public bool ContainsBlock(HashDigest <SHA256> blockHash)
        {
            if (_blockCache.ContainsKey(blockHash))
            {
                return(true);
            }

            return(_store.ContainsBlock(blockHash));
        }
Exemple #3
0
        /// <inheritdoc/>
        public override void PutTransaction <T>(Transaction <T> tx)
        {
            if (_txCache.ContainsKey(tx.Id))
            {
                return;
            }

            WriteContentAddressableFile(_txs, TxPath(tx.Id), tx.Serialize(true));
            _txCache.AddOrUpdate(tx.Id, tx);
        }
Exemple #4
0
        /// <inheritdoc/>
        public override void PutBlock <T>(Block <T> block)
        {
            if (_blockCache.ContainsKey(block.Hash))
            {
                return;
            }

            byte[] key = BlockKey(block.Hash);

            if (!(_blockIndexDb.Get(key) is null))
            {
                return;
            }

            long timestamp = block.Timestamp.ToUnixTimeSeconds();

            foreach (Transaction <T> tx in block.Transactions)
            {
                PutTransaction(tx);
            }

            _rwBlockLock.EnterWriteLock();
            try
            {
                string  blockDbName = $"epoch{timestamp / _blockEpochUnitSeconds}";
                RocksDb blockDb;
                lock (_blockDbCache)
                {
                    if (!_blockDbCache.TryGetValue(blockDbName, out blockDb))
                    {
                        blockDb = RocksDBUtils.OpenRocksDb(_options, BlockDbPath(blockDbName));
                        _blockDbCache.AddOrUpdate(blockDbName, blockDb);
                    }
                }

                BlockDigest digest = BlockDigest.FromBlock(block);
                byte[]      value  = digest.Serialize();
                blockDb.Put(key, value);
                _blockIndexDb.Put(key, RocksDBStoreBitConverter.GetBytes(blockDbName));
                _blockCache.AddOrUpdate(block.Hash, digest);
            }
            catch (Exception e)
            {
                LogUnexpectedException(nameof(PutBlock), e);
            }
            finally
            {
                _rwBlockLock.ExitWriteLock();
            }
        }
Exemple #5
0
        public byte[] Get(byte[] key)
        {
            if (_cache.ContainsKey(key))
            {
                return(_cache[key]);
            }

            if (_keyValueStore.Get(key) is byte[] bytes)
            {
                _cache[key] = bytes;
                return(bytes);
            }

            throw new KeyNotFoundException("There were no elements that correspond to the key" +
                                           $" (hex: {ByteUtil.Hex(key)}).");
        }
        public void Add_ItemIsAdded_ItemExistsInCache()
        {
            var lru = new LruCache<string, string>(1);
            lru.Add("k1", "v1");

            Assert.IsTrue(lru.ContainsKey("k1"));
        }
Exemple #7
0
        /// <inheritdoc/>
        public override void PutTransaction <T>(Transaction <T> tx)
        {
            if (_txCache.ContainsKey(tx.Id))
            {
                return;
            }

            byte[] key = TxKey(tx.Id);

            if (!(_txDb.Get(key) is null))
            {
                return;
            }

            _txDb.Put(key, tx.Serialize(true));
            _txCache.AddOrUpdate(tx.Id, tx);
        }
Exemple #8
0
        /// <inheritdoc cref="IStore.ContainsBlock(BlockHash)"/>
        public bool ContainsBlock(BlockHash blockHash)
        {
            if (_blockCache.ContainsKey(blockHash))
            {
                return(true);
            }

            return(_store.ContainsBlock(blockHash));
        }
Exemple #9
0
        protected void DeleteByGettingWhere(T t)
        {
            var twhere = GetWhereByObject(t);

            if (LruCache.ContainsKey(twhere))
            {
                LruCache[twhere] = LruCache[twhere].Where(item => !t.PkeyEquals(item)).ToList();
                HitInfo.tweakedDirectly++;
            }
        }
Exemple #10
0
        public void ContainsTest(bool shouldExist)
        {
            var cache = new LruCache <string, TestData>(10);

            if (shouldExist)
            {
                cache.AddOrUpdate("1", new TestData());
            }
            bool exists = cache.ContainsKey("1");

            Assert.AreEqual(shouldExist, exists);
        }
        public void Add_MaxSizeIsReached_OldestItemDiscarded()
        {
            var lru = new LruCache<string, string>(5);
            lru.Add("k1", "v1");
            lru.Add("k2", "v2");
            lru.Add("k3", "v3");
            lru.Add("k4", "v4");
            lru.Add("k5", "v5");
            lru.Add("k6", "v6");

            Assert.IsFalse(lru.ContainsKey("k1"));
        }
Exemple #12
0
        /// <inheritdoc/>
        public override void PutBlock <T>(Block <T> block)
        {
            if (_blockCache.ContainsKey(block.Hash))
            {
                return;
            }

            UPath path = BlockPath(block.Hash);

            if (_blocks.FileExists(path))
            {
                return;
            }

            foreach (Transaction <T> tx in block.Transactions)
            {
                PutTransaction(tx);
            }

            WriteContentAddressableFile(_blocks, path, block.ToBlockDigest().Serialize());
            _blockCache.AddOrUpdate(block.Hash, block.ToBlockDigest());
        }
Exemple #13
0
        public void TestSizeIsLimited()
        {
            var cache = new LruCache<string, int>(10);

            for (int i = 0; i < 20; i++) {
                cache[i.ToString()] = i;
            }

            Assert.AreEqual(10, cache.Count);
            for (int i = 10; i < 20; i++) {
                Assert.IsTrue(cache.ContainsKey(i.ToString()));
            }
        }
Exemple #14
0
        public void LruUsageTest()
        {
            const int maxSize = 10;
            var       ttl     = new TimeSpan(0, 1, 0, 0);
            var       maxAge  = new TimeSpan(0, 1, 0, 0);

            var target = new LruCache <string, string>(maxSize, ttl, maxAge);

            // Fill the LRU with "1" through "10"
            for (var i = 1; i <= maxSize; i++)
            {
                var s = i.ToString();
                target.Add(s, "item " + s);
                Thread.Sleep(10);
            }

            // Use "10", then "9", etc.
            for (var i = maxSize; i >= 1; i--)
            {
                var s = i.ToString();
                target.TryGetValue(s, out var value);
            }

            // Add a new item to push the least recently used out -- which should be item "10"
            var s1 = (maxSize + 1).ToString();

            target.Add(s1, "item " + s1);

            Assert.Equal(maxSize, target.Count);  // "Cache has exceeded maximum size"
            var s0 = maxSize.ToString();

            Assert.False(target.ContainsKey(s0), "Least recently used item was not expelled");
            for (var i = 1; i < maxSize; i++)
            {
                var s = i.ToString();
                Assert.True(target.ContainsKey(s), "Recently used item " + s + " was incorrectly expelled");
            }
        }
Exemple #15
0
        /// <inheritdoc/>
        public override void PutBlock <T>(Block <T> block)
        {
            if (_blockCache.ContainsKey(block.Hash))
            {
                return;
            }

            byte[] key = BlockKey(block.Hash);

            if (!(_blockDb.Get(key) is null))
            {
                return;
            }

            foreach (Transaction <T> tx in block.Transactions)
            {
                PutTransaction(tx);
            }

            byte[] value = block.ToBlockDigest().Serialize();
            _blockDb.Put(key, value);
            _blockCache.AddOrUpdate(block.Hash, block.ToBlockDigest());
        }
Exemple #16
0
        public void CacheAccessShouldBumpItToTheFront()
        {
            var cache = new LruCache<int, int>(3);

            cache[1] = 1;
            cache[2] = 2;
            cache[3] = 3;

            var a = cache[1];
            cache[4] = 4;

            Assert.AreEqual(cache[1], 1);
            Assert.AreEqual(cache[3], 3);
            Assert.AreEqual(cache[4], 4);
            Assert.IsFalse(cache.ContainsKey(2));
        }
Exemple #17
0
        public void LruMaximumSizeTest()
        {
            const int maxSize = 10;
            var       ttl     = new TimeSpan(0, 1, 0, 0);
            var       maxAge  = new TimeSpan(0, 1, 0, 0);

            var target = new LruCache <string, string>(maxSize, ttl, maxAge);

            for (var i = 1; i <= maxSize + 5; i++)
            {
                var s = i.ToString();
                target.Add(s, "item " + s);
                Thread.Sleep(10);
            }

            Assert.Equal(maxSize, target.Count);  // "LRU grew larger than maximum size"
            for (var i = 1; i <= 5; i++)
            {
                var s = i.ToString();
                Assert.False(target.ContainsKey(s), "'Older' entry is still in cache");
            }
        }
Exemple #18
0
 /// <inheritdoc cref="IStore.ContainsBlock(BlockHash)"/>
 public bool ContainsBlock(BlockHash blockHash) =>
 _blockCache.ContainsKey(blockHash) || _store.ContainsBlock(blockHash);
        public void ContainsKey_KeyDoestNotExist_ReturnsFalse()
        {
            var lru = new LruCache<string, string>(1);

            Assert.IsFalse(lru.ContainsKey("k1"));
        }
        public void ContainsKey_KeyExists_ReturnsTrue()
        {
            var lru = new LruCache<string, string>(1);
            lru.Add("k1", "v1");

            Assert.IsTrue(lru.ContainsKey("k1"));
        }
        public void Resize_NewSizeCausesTrim_OnlyOldestItemsDiscarded()
        {
            var lru = new LruCache<string, string>(5);
            lru.Add("k1", "v1");
            lru.Add("k2", "v2");
            lru.Add("k3", "v3");
            lru.Add("k4", "v4");
            lru.Add("k5", "v5");

            lru.Resize(3);

            Assert.IsFalse(lru.ContainsKey("k1"));
            Assert.IsFalse(lru.ContainsKey("k2"));
            Assert.IsTrue(lru.ContainsKey("k3"));
            Assert.IsTrue(lru.ContainsKey("k4"));
            Assert.IsTrue(lru.ContainsKey("k5"));
        }
        public void Remove_ItemIsRemoved_ContainsKeyReturnsFalse()
        {
            var lru = new LruCache<string, string>(1);
            lru.Add("k1", "v1");

            lru.Remove("k1");

            Assert.IsFalse(lru.ContainsKey("k1"));
        }