public static T GetSheet <T>(this IAccountStateDelta states) where T : ISheet, new()
        {
            var address = Addresses.GetSheetAddress <T>();

            try
            {
                var    csv = GetSheetCsv <T>(states);
                byte[] hash;
                using (var sha256 = SHA256.Create())
                {
                    hash = sha256.ComputeHash(Encoding.UTF8.GetBytes(csv));
                }

                var cacheKey = address.ToHex() + ByteUtil.Hex(hash);
                if (SheetsCache.TryGetValue(cacheKey, out var cached))
                {
                    return((T)cached);
                }

                var sheet = new T();
                sheet.Set(csv);
                SheetsCache.AddOrUpdate(cacheKey, sheet);
                return(sheet);
            }
            catch (Exception e)
            {
                Log.Error(e, "Unexpected error occurred during GetSheet<{TypeName}>()", typeof(T).FullName);
                throw;
            }
        }
Exemplo n.º 2
0
        public void RemoveItem()
        {
            var cache = new LruCache <string, TestData>(10);
            var data  = new TestData
            {
                TestValue1 = "null",
                TestValue2 = "null",
            };

            cache.SetPreRemoveDataMethod(value =>
            {
                data.TestValue1 = value.TestValue1;
                data.TestValue2 = value.TestValue2;
                return(true);
            });

            cache.AddOrUpdate("1", new TestData
            {
                TestValue1 = "10",
                TestValue2 = "20"
            });
            cache.AddOrUpdate("2", new TestData
            {
                TestValue1 = "30",
                TestValue2 = "40"
            });

            cache.Remove("1");
            Assert.Equals("10", data.TestValue1);
            Assert.Equals("20", data.TestValue2);

            cache.Remove("2");
            Assert.Equals("30", data.TestValue1);
            Assert.Equals("40", data.TestValue2);
        }
Exemplo n.º 3
0
        /// <inheritdoc/>
        public override BlockDigest?GetBlockDigest(HashDigest <SHA256> blockHash)
        {
            if (_blockCache.TryGetValue(blockHash, out BlockDigest cachedDigest))
            {
                return(cachedDigest);
            }

            byte[] key = BlockKey(blockHash);
            if (!(_blockIndexDb.Get(key) is byte[] blockDbNameBytes))
            {
                return(null);
            }

            _rwBlockLock.EnterReadLock();
            try
            {
                string blockDbName = RocksDBStoreBitConverter.GetString(blockDbNameBytes);
                if (!_blockDbCache.TryGetValue(blockDbName, out RocksDb blockDb))
                {
                    blockDb = RocksDBUtils.OpenRocksDb(_options, BlockDbPath(blockDbName));
                    _blockDbCache.AddOrUpdate(blockDbName, blockDb);
                }

                byte[] blockBytes = blockDb.Get(key);

                BlockDigest blockDigest = BlockDigest.Deserialize(blockBytes);

                _blockCache.AddOrUpdate(blockHash, blockDigest);
                return(blockDigest);
            }
            finally
            {
                _rwBlockLock.ExitReadLock();
            }
        }
Exemplo n.º 4
0
        /// <inheritdoc/>
        public override Transaction <T> GetTransaction <T>(TxId txid)
        {
            if (_txCache.TryGetValue(txid, out object cachedTx))
            {
                return((Transaction <T>)cachedTx);
            }

            UPath path = TxPath(txid);

            if (!_txs.FileExists(path))
            {
                return(null);
            }

            byte[] bytes;
            try
            {
                bytes = _txs.ReadAllBytes(path);
            }
            catch (FileNotFoundException)
            {
                return(null);
            }

            Transaction <T> tx = Transaction <T> .Deserialize(bytes, false);

            _txCache.AddOrUpdate(txid, tx);
            return(tx);
        }
Exemplo n.º 5
0
        public void InsertReorder()
        {
            var cache = new LruCache <string, TestData>(10);

            for (int index = 0; index < 10; ++index)
            {
                var data = new TestData
                {
                    TestValue1 = index.ToString()
                };
                cache.AddOrUpdate(index.ToString(), data);
            }
            Assert.AreEqual(10, cache.Count, "Cache size incorrect");

            cache.AddOrUpdate("0", new TestData());
            Assert.AreEqual(10, cache.Count, "Item shouldn't have duplicated in cache");

            var next = new TestData
            {
                TestValue1 = "11"
            };

            cache.AddOrUpdate(next.TestValue1, next);
            var firstData = cache.Get("0");

            Assert.IsNotNull(firstData, "Item shouldn't have been removed from cache");
        }
Exemplo n.º 6
0
        public void PeekTest()
        {
            var cache = new LruCache <string, TestData>(10);

            for (int index = 0; index < 10; ++index)
            {
                cache.AddOrUpdate(index.ToString(), new TestData());
            }
            Assert.AreEqual(10, cache.Count, "Cache size incorrect");

            var cached = cache.Peek("0");

            Assert.IsNotNull(cached, "Item should exist in cache");

            cache.AddOrUpdate("11", new TestData());
            bool thrown = false;

            try
            {
                cache.Get("0");
            }
            catch (KeyNotFoundException)
            {
                thrown = true;
            }
            Assert.IsTrue(thrown, "Item should have been removed from cache");
        }
Exemplo n.º 7
0
        public override Block <T> this[HashDigest <SHA256> key]
        {
            get
            {
                Block <T> block = GetBlock(key);
                if (block is null)
                {
                    throw new KeyNotFoundException(
                              $"The given hash[{key}] was not found in this set."
                              );
                }

                if (!block.Hash.Equals(key))
                {
                    throw new InvalidBlockHashException(
                              $"The given hash[{key}] was not equal to actual[{block.Hash}].");
                }

                return(block);
            }

            set
            {
                if (!value.Hash.Equals(key))
                {
                    throw new InvalidBlockHashException(
                              $"{value}.hash does not match to {key}");
                }

                value.Validate(DateTimeOffset.UtcNow);
                Store.PutBlock(value);
                _cache.AddOrUpdate(value.Hash, value);
            }
        }
        public long?TestAndSet(KeyImpression impression)
        {
            long?defaultReturn = null;

            if (impression == null)
            {
                return(defaultReturn);
            }

            ulong hash = _impressionHasher.Process(impression);

            try
            {
                long?previous = _cache.Get(hash);
                _cache.AddOrUpdate(hash, impression.time);

                return(Math.Min(previous.Value, impression.time));
            }
            catch (Exception)
            {
                _cache.AddOrUpdate(hash, impression.time);

                return(defaultReturn);
            }
        }
Exemplo n.º 9
0
        public override BlockDigest?GetBlockDigest(BlockHash blockHash)
        {
            if (_blockCache.TryGetValue(blockHash, out BlockDigest cachedDigest))
            {
                return(cachedDigest);
            }

            UPath path = BlockPath(blockHash);

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

            BlockDigest blockDigest;

            try
            {
                blockDigest = BlockDigest.Deserialize(_blocks.ReadAllBytes(path));
            }
            catch (FileNotFoundException)
            {
                return(null);
            }

            _blockCache.AddOrUpdate(blockHash, blockDigest);
            return(blockDigest);
        }
Exemplo n.º 10
0
        public void CanUpdateExistingValue()
        {
            var cache = new LruCache <string, int>(10);

            cache.AddOrUpdate("1", 1);
            cache.TryGet("1", out int val);
            Assert.AreEqual(1, val);
            cache.AddOrUpdate("1", 10);
            cache.TryGet("1", out val);
            Assert.AreEqual(10, val);
        }
Exemplo n.º 11
0
        public void EnumeratorChangedUpdateMethodNoChange()
        {
            var cache = new LruCache <int, int>(10);

            cache.SetUpdateMethod((a, b) =>
            {
                if (a != b)
                {
                    a = b;
                    return(true);
                }
                else
                {
                    return(false);
                }
            });
            for (int index = 0; index < 10; ++index)
            {
                cache.Add(index, index);
            }

            var  enumerator = cache.GetEnumerator();
            bool success    = enumerator.MoveNext();

            Assert.IsTrue(success, "Enumerator should move");

            cache.AddOrUpdate(9, 9);
            success = enumerator.MoveNext();
            Assert.IsTrue(success, "Enumerator should move");
        }
Exemplo n.º 12
0
        public void TestLruCacheTryGetValue()
        {
            RegionEndpoint regionEndpoint;
            var            lru = new LruCache <string, RegionEndpoint>(5);

            lru.AddOrUpdate("my-bucket-us-east-1", RegionEndpoint.USEast1);
            lru.AddOrUpdate("my-bucket-us-west-2", RegionEndpoint.USWest2);
            lru.AddOrUpdate("my-bucket-ap-northeast-2", RegionEndpoint.APNortheast2);
            lru.AddOrUpdate("my-bucket-sa-east-1", RegionEndpoint.SAEast1);

            lru.TryGetValue("my-bucket-us-west-2", out regionEndpoint);
            Assert.AreEqual(RegionEndpoint.USWest2, regionEndpoint);

            lru.TryGetValue("my-bucket-ap-northeast-2", out regionEndpoint);
            Assert.AreEqual(RegionEndpoint.APNortheast2, regionEndpoint);
        }
        public V8Script Compile(string scriptId, string code, bool addToCache = true, int?cacheExpirationSeconds = null)
        {
            CachedV8Script cachedScript;

            if (TryGetCached(scriptId, out cachedScript))
            {
                return(cachedScript.Script);
            }

            V8Script compiledScript = _v8Runtime.Compile(scriptId, code);

            if (addToCache)
            {
                if (!cacheExpirationSeconds.HasValue)
                {
                    cacheExpirationSeconds = _settings.ScriptCacheExpirationSeconds;
                }
                if (cacheExpirationSeconds > 0)
                {
                    var cacheEntry = new CachedV8Script(compiledScript, cacheExpirationSeconds.Value);
                    _scriptCache.AddOrUpdate(scriptId, cacheEntry, (key, original) => cacheEntry);
                }
            }

            return(compiledScript);
        }
Exemplo n.º 14
0
        public void InsertOverCapacity()
        {
            var cache = new LruCache <string, TestData>(10);

            for (int index = 0; index <= 10; ++index)
            {
                var data = new TestData
                {
                    TestValue1 = index.ToString()
                };
                cache.AddOrUpdate(index.ToString(), data);
            }

            bool thrown = false;

            try
            {
                cache.Get("0");
            }
            catch (KeyNotFoundException)
            {
                thrown = true;
            }
            Assert.IsTrue(thrown, "Item should have been removed from cache");
        }
Exemplo n.º 15
0
        public void TestLruCacheTryGetValue()
        {
            RegionEndpoint regionEndpoint;
            var lru = new LruCache<string, RegionEndpoint>(5);

            lru.AddOrUpdate("my-bucket-us-east-1", RegionEndpoint.USEast1);
            lru.AddOrUpdate("my-bucket-us-west-2", RegionEndpoint.USWest2);
            lru.AddOrUpdate("my-bucket-ap-northeast-2", RegionEndpoint.APNortheast2);
            lru.AddOrUpdate("my-bucket-sa-east-1", RegionEndpoint.SAEast1);

            lru.TryGetValue("my-bucket-us-west-2", out regionEndpoint);
            Assert.AreEqual(RegionEndpoint.USWest2, regionEndpoint);

            lru.TryGetValue("my-bucket-ap-northeast-2", out regionEndpoint);
            Assert.AreEqual(RegionEndpoint.APNortheast2, regionEndpoint);
        }
Exemplo n.º 16
0
        public BlockDigest?GetBlockDigest(HashDigest <SHA256> blockHash)
        {
            if (_blockCache.TryGetValue(blockHash, out BlockDigest cachedDigest))
            {
                return(cachedDigest);
            }

            var blockDigest = _store.GetBlockDigest(blockHash);

            if (!(blockDigest is null))
            {
                _blockCache.AddOrUpdate(blockHash, blockDigest.Value);
            }

            return(blockDigest);
        }
Exemplo n.º 17
0
        /// <inheritdoc/>
        public override Transaction <T> GetTransaction <T>(TxId txid)
        {
            if (_txCache.TryGetValue(txid, out object cachedTx))
            {
                return((Transaction <T>)cachedTx);
            }

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

            string txDbName = RocksDBStoreBitConverter.GetString(txDbNameBytes);

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

                byte[] txBytes = txDb.Get(key);

                Transaction <T> tx = Transaction <T> .Deserialize(txBytes, false);

                _txCache.AddOrUpdate(txid, tx);
                return(tx);
            }
            catch (Exception e)
            {
                LogUnexpectedException(nameof(GetTransaction), e);
                return(null);
            }
            finally
            {
                _rwTxLock.ExitReadLock();
            }
        }
Exemplo n.º 18
0
        public void RefreshNotExists()
        {
            var cache = new LruCache <string, TestData>(10);

            cache.AddOrUpdate("0", new TestData());
            bool check = cache.Refresh("1");

            Assert.IsFalse(check, "Item should not have refreshed in cache");
        }
Exemplo n.º 19
0
        public void CacheEvictsLeastRecentlyAccessedItemFirst()
        {
            var cache = new LruCache <string, int>(3);

            cache.AddOrUpdate("1", 1);
            cache.AddOrUpdate("2", 2);
            cache.AddOrUpdate("3", 3);
            // 1 is moved to head of list
            Assert.IsTrue(cache.TryGet("1", out _));
            // 4 is added to head of list, which evicts 2, the least recently used item
            cache.AddOrUpdate("4", 4);
            // 2 should be evicted
            Assert.IsFalse(cache.TryGet("2", out _));
            // 5 is moved to head of list
            cache.AddOrUpdate("5", 4);
            // 3 should be evicted
            Assert.IsFalse(cache.TryGet("3", out _));
        }
Exemplo n.º 20
0
        public void RefreshTest()
        {
            var cache = new LruCache <string, TestData>(10);

            for (int index = 0; index < 10; ++index)
            {
                cache.AddOrUpdate(index.ToString(), new TestData());
            }
            Assert.AreEqual(10, cache.Count, "Cache size incorrect");

            bool cached = cache.Refresh("0");

            Assert.IsTrue(cached, "Item should have refreshed in cache");

            cache.AddOrUpdate("11", new TestData());
            var firstData = cache.Get("0");

            Assert.IsNotNull(firstData, "Item should't have been removed from cache");
        }
Exemplo n.º 21
0
        private LruCache <string, string> GetCache(int maxItems, int count)
        {
            var cache = new LruCache <string, string>(maxItems);

            for (int i = 0; i < count; i++)
            {
                cache.AddOrUpdate("key" + i, "value" + i);
            }
            return(cache);
        }
Exemplo n.º 22
0
        public void UpdateItem()
        {
            var cache = new LruCache <string, TestData>(10);

            cache.SetUpdateMethod((a, b) =>
            {
                a.TestValue1 = b.TestValue1;
                a.TestValue2 = b.TestValue2;
                return(true);
            });
            cache.SetCopyMethod((a) =>
            {
                return(new TestData
                {
                    TestValue1 = a.TestValue1,
                    TestValue2 = a.TestValue2,
                });
            });

            var data = new TestData
            {
                TestValue1 = "1",
                TestValue2 = "2"
            };

            cache.AddOrUpdate("1", data);

            var newData = new TestData
            {
                TestValue1 = "A",
                TestValue2 = "B",
            };

            cache.AddOrUpdate("1", newData);

            var cachedData = cache.Get("1");

            Assert.AreNotEqual(newData.TestValue1, data.TestValue1, "TestValue1 shouldn't match");
            Assert.AreNotEqual(newData.TestValue2, data.TestValue2, "TestValue2 shouldn't match");
            Assert.AreEqual(newData.TestValue1, cachedData.TestValue1, "TestValue1 didn't update");
            Assert.AreEqual(newData.TestValue2, cachedData.TestValue2, "TestValue2 didn't update");
        }
Exemplo n.º 23
0
        /// <inheritdoc/>
        public override Transaction <T> GetTransaction <T>(TxId txid)
        {
            if (_txCache.TryGetValue(txid, out object cachedTx))
            {
                return((Transaction <T>)cachedTx);
            }

            byte[] key   = TxKey(txid);
            byte[] bytes = _txDb.Get(key);

            if (bytes is null)
            {
                return(null);
            }

            Transaction <T> tx = Transaction <T> .Deserialize(bytes, false);

            _txCache.AddOrUpdate(txid, tx);
            return(tx);
        }
Exemplo n.º 24
0
        public void TryPeekNotFound()
        {
            var cache = new LruCache <int, string>(10);

            cache.AddOrUpdate(1, "1");

            bool result = cache.TryPeek(2, out string data);

            Assert.IsFalse(result, "Item should not have been found");
            Assert.IsNull(data, "Data should be null");
        }
Exemplo n.º 25
0
        public void TryPeekFound()
        {
            var cache = new LruCache <int, int>(10);

            cache.AddOrUpdate(1, 1);

            bool result = cache.TryPeek(1, out int data);

            Assert.IsTrue(result, "Item should have been found");
            Assert.AreEqual(1, data, "Data should match");
        }
Exemplo n.º 26
0
        /// <inheritdoc/>
        public override BlockDigest?GetBlockDigest(HashDigest <SHA256> blockHash)
        {
            if (_blockCache.TryGetValue(blockHash, out BlockDigest cachedDigest))
            {
                return(cachedDigest);
            }

            byte[] key   = BlockKey(blockHash);
            byte[] bytes = _blockDb.Get(key);

            if (bytes is null)
            {
                return(null);
            }

            BlockDigest blockDigest = BlockDigest.Deserialize(bytes);

            _blockCache.AddOrUpdate(blockHash, blockDigest);
            return(blockDigest);
        }
Exemplo n.º 27
0
        public void InsertsAndGets()
        {
            var cache = new LruCache <int, int>(2);

            cache.AddOrUpdate(1, 1);
            cache.AddOrUpdate(2, 2);

            int retrieved = cache.Get(1);

            Assert.AreEqual(1, retrieved);

            cache.AddOrUpdate(3, 3);
            bool thrown = false;

            try
            {
                cache.Get(2);
            }
            catch (KeyNotFoundException)
            {
                thrown = true;
            }
            Assert.IsTrue(thrown);

            cache.AddOrUpdate(4, 4);
            thrown = false;
            try
            {
                cache.Get(1);
            }
            catch (KeyNotFoundException)
            {
                thrown = true;
            }
            Assert.IsTrue(thrown);

            retrieved = cache.Get(3);
            Assert.AreEqual(3, retrieved);
            retrieved = cache.Get(4);
            Assert.AreEqual(4, retrieved);
        }
Exemplo n.º 28
0
        public void TestFindOldestItem()
        {
            var cache = new LruCache <string, string>(10);

            cache.AddOrUpdate("key1", "value1");
            cache.AddOrUpdate("key2", "value2");
            cache.AddOrUpdate("key3", "value3");

            // current order: key1 -> key2 -> key3
            Assert.AreEqual("key1", cache.FindOldestItem().Key);

            cache.AddOrUpdate("key1", "value1Updated");

            // current order: key2 -> key3 -> key1
            Assert.AreEqual("key2", cache.FindOldestItem().Key);

            cache.Evict("key2");

            // current order: key3 -> key1
            Assert.AreEqual("key3", cache.FindOldestItem().Key);
        }
Exemplo n.º 29
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);
        }
Exemplo n.º 30
0
        private IEnumerable <DiscoveryEndpointBase> ProcessInvokeEndpointOperation(string cacheKey, Func <IList <DiscoveryEndpointBase> > InvokeEndpointOperation, bool endpointRequired)
        {
            IList <DiscoveryEndpointBase> endpoints = null;

            try
            {
                endpoints = InvokeEndpointOperation();
                if (endpoints != null && endpoints.Count > 0)
                {
                    _cache.AddOrUpdate(cacheKey, endpoints);
                }
                else
                {
                    endpoints = null;
                    if (!endpointRequired)
                    {
                        //Since it is not required that endpoint discovery is used for this operation, cache
                        //a null endpoint with an expiration time of 60 seconds so that requests are not
                        //excessively sent.
                        var cacheEndpoints = new List <DiscoveryEndpointBase>();
                        cacheEndpoints.Add(new DiscoveryEndpoint(null, 1));
                        _cache.AddOrUpdate(cacheKey, cacheEndpoints);
                    }
                    _logger.InfoFormat("The request to discover endpoints did not return any endpoints.");
                }
            }
            catch (Exception exception)
            {
                _logger.Error(exception, "An unhandled exception occurred while trying to discover endpoints.");
            }

            if (endpoints == null && endpointRequired)
            {
                throw new AmazonClientException("Failed to discover the endpoint for the request. Requests will not succeed until an endpoint can be retrieved or an endpoint is manually specified.");
            }

            return(endpoints);
        }
Exemplo n.º 31
0
        /// <inheritdoc cref="BaseStore.GetBlockDigest(BlockHash)"/>
        public override BlockDigest?GetBlockDigest(BlockHash blockHash)
        {
            if (_blockCache.TryGetValue(blockHash, out BlockDigest cachedDigest))
            {
                return(cachedDigest);
            }

            UPath path = BlockPath(blockHash);

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

            BlockDigest blockDigest;

            try
            {
                IValue value = Codec.Decode(_blocks.ReadAllBytes(path));
                if (!(value is Bencodex.Types.Dictionary dict))
                {
                    throw new DecodingException(
                              $"Expected {typeof(Bencodex.Types.Dictionary)} but " +
                              $"{value.GetType()}");
                }

                blockDigest = new BlockDigest(dict);
            }
            catch (FileNotFoundException)
            {
                return(null);
            }

            _blockCache.AddOrUpdate(blockHash, blockDigest);
            return(blockDigest);
        }
Exemplo n.º 32
0
 private LruCache<string, string> GetCache(int maxItems, int count)
 {
     var cache = new LruCache<string, string>(maxItems);
     for (int i = 0; i < count; i++)
     {
         cache.AddOrUpdate("key" + i, "value" + i);
     }
     return cache;
 }