private CacheBlock GetBlock(long blockIndex) { if (BlockDict.TryGetValue(blockIndex, out LinkedListNode <CacheBlock> node)) { if (Blocks.First != node) { Blocks.Remove(node); Blocks.AddFirst(node); } return(node.Value); } node = Blocks.Last; FlushBlock(node.Value); CacheBlock block = node.Value; Blocks.RemoveLast(); BlockDict.Remove(block.Index); FlushBlock(block); ReadBlock(block, blockIndex); Blocks.AddFirst(node); BlockDict.Add(blockIndex, node); return(block); }
private CacheBlock GetBlock(long blockIndex) { if (BlockDict.TryGetValue(blockIndex, out LinkedListNode <CacheBlock> node)) { if (Blocks.First != node) { Blocks.Remove(node); Blocks.AddFirst(node); } // If a supposedly active block is null, something's really wrong if (node is null) { throw new NullReferenceException("CachedStorage cache block is unexpectedly null."); } return(node.Value); } // An inactive node shouldn't be null, but we'll fix it if it is anyway node = Blocks.Last ?? new LinkedListNode <CacheBlock>(new CacheBlock { Buffer = new byte[BlockSize], Index = -1 }); FlushBlock(node.Value); CacheBlock block = node.Value; Blocks.RemoveLast(); if (block.Index != -1) { BlockDict.Remove(block.Index); } FlushBlock(block); ReadBlock(block, blockIndex); Blocks.AddFirst(node); BlockDict.Add(blockIndex, node); return(block); }
private CacheBlock GetBlock(long blockIndex) { if (BlockDict.TryGetValue(blockIndex, out LinkedListNode <CacheBlock> node)) { if (Blocks.First != node) { Blocks.Remove(node); Blocks.AddFirst(node); } return(node !.Value); } // An inactive node shouldn't be null, but we'll fix it if it is anyway node = Blocks.Last ?? new LinkedListNode <CacheBlock>(new CacheBlock { Buffer = new byte[BlockSize], Index = -1 }); FlushBlock(node.Value); CacheBlock block = node.Value; Blocks.RemoveLast(); if (block.Index != -1) { BlockDict.Remove(block.Index); } FlushBlock(block); ReadBlock(block, blockIndex); Blocks.AddFirst(node); BlockDict.Add(blockIndex, node); return(block); }