Commit ec223d38 authored by Katherine Walker's avatar Katherine Walker

PHPLIB-252: Allow seeking to advance chunk iterator instead of always re-querying

parent 0d3496dd
......@@ -186,9 +186,26 @@ class ReadableStream
$this->chunkOffset = (integer) floor($offset / $this->chunkSize);
$this->bufferOffset = $offset % $this->chunkSize;
if ($lastChunkOffset !== $this->chunkOffset) {
/* If we are seeking to a previous chunk, we need to reinitialize the
* chunk iterator.
*/
if ($lastChunkOffset > $this->chunkOffset) {
$this->buffer = null;
$this->chunksIterator = null;
return;
}
/* If we are seeking to a subsequent chunk, we do not need to
* reinitalize the chunk iterator. Instead, we can simply move forward
* to $this->chunkOffset.
*/
$numChunks = $this->chunkOffset - $lastChunkOffset;
$i = 0;
if ($this->chunksIterator !== null) {
while ($i < $numChunks) {
$this->chunksIterator->next();
$i++;
}
}
}
......
......@@ -199,4 +199,32 @@ class ReadableStreamFunctionalTest extends FunctionalTestCase
$stream->seek(11);
}
public function testSeekPreviousChunk()
{
$fileDocument = $this->collectionWrapper->findFileById('length-10');
$stream = new ReadableStream($this->collectionWrapper, $fileDocument);
$stream->readBytes(1);
$stream->seek(5);
$stream->seek(2);
$stream->readBytes(1);
}
public function testSeekSubsequentChunk()
{
$fileDocument = $this->collectionWrapper->findFileById('length-10');
$observer = $this->getMockBuilder(ReadableStream::class)
->setConstructorArgs(array($this->collectionWrapper, $fileDocument))
->getMock();
$observer->expects($this->never())
->method('initChunksIterator');
$observer->readBytes(1);
$observer->seek(5);
$observer->seek(2);
$observer->readBytes(1);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment