private StreamMetadata InternalGet(string key)
        {
            StreamMetadata result    = null;
            var            blockInfo = header.Get(key);

            while (blockInfo.Offset != 0)
            {
                var block = new IndexBlock();
                blockProvider.Read(block, blockInfo.Offset, blockInfo.Length);

                blockInfo = block.Get(key, ref result);

                if (result != null)
                {
                    return(result);
                }
            }

            return(result);
        }
        /// <summary>
        /// Adds metadata of written stream to index file
        /// </summary>
        /// <param name="streamMetadata">stream metadata to be added to index</param>
        /// <exception cref="System.ArgumentException">An element with the same key already exists</exception>
        public void Set(StreamMetadata streamMetadata)
        {
            streamMetadata.ThrowIfNull(nameof(streamMetadata));
            var key = streamMetadata.Key;

            if (Contains(key))
            {
                throw new ArgumentException(nameof(key));
            }

            var blockInfo = header.Get(key);
            var block     = new IndexBlock();
            var oldLength = 0;

            if (blockInfo.Offset > 0)
            {
                if (blockInfo.Length + streamMetadata.SerializedLength < MaxIndexBlockLength)
                {
                    blockProvider.Read(block, blockInfo.Offset, blockInfo.Length);
                    oldLength = blockInfo.Length;
                }
                else
                {
                    block = new IndexBlock(blockInfo);
                }
            }
            block.Add(streamMetadata);

            var offset = blockProvider.Write(block);
            var length = block.SerializedLength;

            blockInfo = new BlockInfo()
            {
                Offset = offset, Length = length
            };

            header.Set(key, blockInfo);
            header.IndexWrittenLength   += length - oldLength;
            header.StorageWrittenLength += streamMetadata.Length;
        }