BitSharper.Store.BoundedOverheadBlockStore.Get C# (CSharp) Method

Get() public method

public Get ( Sha256Hash hash ) : StoredBlock
hash Sha256Hash
return StoredBlock
        public StoredBlock Get(Sha256Hash hash)
        {
            lock (this)
            {
                // Check the memory cache first.
                StoredBlock fromMem;
                if (_blockCache.TryGetValue(hash, out fromMem))
                {
                    return fromMem;
                }
                if (_notFoundCache.TryGetValue(hash, out fromMem) && fromMem == _notFoundMarker)
                {
                    return null;
                }

                try
                {
                    var fromDisk = GetRecord(hash);
                    StoredBlock block = null;
                    if (fromDisk == null)
                    {
                        _notFoundCache[hash] = _notFoundMarker;
                        while (_notFoundCache.Count > 2050)
                        {
                            _notFoundCache.RemoveAt(0);
                        }
                    }
                    else
                    {
                        block = fromDisk.ToStoredBlock(_params);
                        _blockCache[hash] = block;
                        while (_blockCache.Count > 2050)
                        {
                            _blockCache.RemoveAt(0);
                        }
                    }
                    return block;
                }
                catch (IOException e)
                {
                    throw new BlockStoreException(e);
                }
                catch (ProtocolException e)
                {
                    throw new BlockStoreException(e);
                }
            }
        }

Usage Example

        public void TestStorage()
        {
            var temp = new FileInfo(Path.GetTempFileName());
            try
            {
                Console.WriteLine(temp.FullName);
                var @params = NetworkParameters.UnitTests();
                var to = new EcKey().ToAddress(@params);
                StoredBlock b1;
                using (var store = new BoundedOverheadBlockStore(@params, temp))
                {
                    // Check the first block in a new store is the genesis block.
                    var genesis = store.GetChainHead();
                    Assert.AreEqual(@params.GenesisBlock, genesis.Header);

                    // Build a new block.
                    b1 = genesis.Build(genesis.Header.CreateNextBlock(to).CloneAsHeader());
                    store.Put(b1);
                    store.SetChainHead(b1);
                }
                // Check we can get it back out again if we rebuild the store object.
                using (var store = new BoundedOverheadBlockStore(@params, temp))
                {
                    var b2 = store.Get(b1.Header.Hash);
                    Assert.AreEqual(b1, b2);
                    // Check the chain head was stored correctly also.
                    Assert.AreEqual(b1, store.GetChainHead());
                }
            }
            finally
            {
                temp.Delete();
            }
        }