Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Stream Seek Index
S2 and Snappy streams can have indexes. These indexes will allow random seeking within the compressed data.
The index can either be appended to the stream as a skippable block or returned for separate storage.
When the index is appended to a stream it will be skipped by regular decoders,
so the output remains compatible with other decoders.
Creating an Index
To automatically add an index to a stream, add
WriterAddIndex()
option to your writer.Then the index will be added to the stream when
Close()
is called.If you want to store the index separately, you can use
CloseIndex()
instead of the regularClose()
.This will return the index. Note that
CloseIndex()
should only be called once, and you shouldn't callClose()
.The
index
can then be used needing to read from the stream.This means the index can be used without needing to seek to the end of the stream
or for manually forwarding streams. See below.
Using Indexes
To use indexes there is a
ReadSeeker(random bool, index []byte) (*ReadSeeker, error)
function available.Calling ReadSeeker will return an io.ReadSeeker compatible version of the reader.
If 'random' is specified the returned io.Seeker can be used for random seeking, otherwise only forward seeking is supported.
Enabling random seeking requires the original input to support the io.Seeker interface.
Get a seeker to seek forward. Since no index is provided, the index is read from the stream.
This requires that an index was added and that
r
supports the io.Seeker interface.A custom index can be specified which will be used if supplied.
When using a custom index, it will not be read from the input stream.
This will read the index from
index
. Since we specify non-random (forward only) seekingr
does not have to be an io.SeekerFinally, since we specify that we want to do random seeking
r
must be an io.Seeker.The returned ReadSeeker contains a shallow reference to the existing Reader,
meaning changes performed to one is reflected in the other.
Manually Forwarding Streams
Indexes can also be read outside the decoder using the Index type.
This can be used for parsing indexes, either separate or in streams.
In some cases it may not be possible to serve a seekable stream.
This can for instance be an HTTP stream, where the Range request
is sent at the start of the stream.
With a little bit of extra code it is still possible to forward
It is possible to load the index manually like this:
This can be used to figure out how much to offset the compressed stream:
The
compressedOffset
is the number of bytes that should be skippedfrom the beginning of the compressed file.
The
uncompressedOffset
will then be offset of the uncompressed bytes returnedwhen decoding from that position. This will always be <= wantOffset.
When creating a decoder it must be specified that it should not expect a frame header
at the beginning of the stream. Assuming the io.Reader
r
has been forwarded tocompressedOffset
we create the decoder like this:
We are not completely done. We still need to forward the stream the uncompressed bytes we didn't want.
This is done using the regular "Skip" function:
This will ensure that we are at exactly the offset we want, and reading from
dec
will start at the requested offset.