private void RefreshBlock(ChunkDictionary block) { block.IsDirty = false; // try read next chunk recently added Reader.BaseStream.Position = block.LastChunk.ActualOffset + block.LastChunk.ActualLength; Reader.Read(); FillBlock(block); }
ChunkDictionary ReadBlock(long blockIndex) { if (blocks.TryGetValue(blockIndex - 1, out var prevBlock)) { var prevChunkEndOffset = prevBlock.LastChunk.ActualOffset + prevBlock.LastChunk.ActualLength; if (prevChunkEndOffset / BlockSize >= blockIndex) // previous chunk overlaps with current block { Reader.BaseStream.Position = prevBlock.LastChunk.ActualOffset; } else { Reader.BaseStream.Position = prevChunkEndOffset; // move to the next chunk because previous is ended in previous block } Reader.Read(); } else { Reader.Position = blockIndex * BlockSize; // Seek & Read first chunk in block } ChunkDictionary block = null; if (Reader.CurrentChunk != null) { block = new ChunkDictionary() { FirstSequenceNumber = Reader.CurrentChunk.SequenceNumber, BlockIndex = blockIndex }; blocks.Add(blockIndex, FillBlock(block)); } if (block != null) { if (Reader.EndOfStream) { EndingBlockIndex = block.BlockIndex; } else if (blockIndex > EndingBlockIndex) { EndingBlockIndex = -1; } } return(block); }
private ChunkDictionary FillBlock(ChunkDictionary block) { while (Reader.CurrentChunk != null) { var chunk = Reader.CurrentChunk; if (chunk.ActualOffset / BlockSize > block.BlockIndex) { break; } block.Add(chunk.SequenceNumber, chunk); block.LastSequenceNumber = chunk.SequenceNumber; Reader.Read(); } return(block); }