Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
mongo-php-library
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sinan
mongo-php-library
Commits
be949d91
Commit
be949d91
authored
Feb 12, 2018
by
Katherine Walker
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix buffer bug
parent
ec223d38
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
107 additions
and
22 deletions
+107
-22
ReadableStream.php
src/GridFS/ReadableStream.php
+13
-7
ReadableStreamFunctionalTest.php
tests/GridFS/ReadableStreamFunctionalTest.php
+94
-15
No files found.
src/GridFS/ReadableStream.php
View file @
be949d91
...
...
@@ -186,11 +186,21 @@ class ReadableStream
$this
->
chunkOffset
=
(
integer
)
floor
(
$offset
/
$this
->
chunkSize
);
$this
->
bufferOffset
=
$offset
%
$this
->
chunkSize
;
if
(
$lastChunkOffset
===
$this
->
chunkOffset
)
{
return
;
}
if
(
$this
->
chunksIterator
===
null
)
{
return
;
}
// Clear the buffer since the current chunk will be changed
$this
->
buffer
=
null
;
/* 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
;
}
...
...
@@ -200,12 +210,8 @@ class ReadableStream
* to $this->chunkOffset.
*/
$numChunks
=
$this
->
chunkOffset
-
$lastChunkOffset
;
$i
=
0
;
if
(
$this
->
chunksIterator
!==
null
)
{
while
(
$i
<
$numChunks
)
{
$this
->
chunksIterator
->
next
();
$i
++
;
}
for
(
$i
=
0
;
$i
<
$numChunks
;
$i
++
)
{
$this
->
chunksIterator
->
next
();
}
}
...
...
tests/GridFS/ReadableStreamFunctionalTest.php
View file @
be949d91
...
...
@@ -5,6 +5,8 @@ namespace MongoDB\Tests\GridFS;
use
MongoDB\BSON\Binary
;
use
MongoDB\GridFS\CollectionWrapper
;
use
MongoDB\GridFS\ReadableStream
;
use
MongoDB\Tests\CommandObserver
;
use
stdClass
;
/**
* Functional tests for the internal ReadableStream class.
...
...
@@ -200,31 +202,108 @@ class ReadableStreamFunctionalTest extends FunctionalTestCase
$stream
->
seek
(
11
);
}
public
function
testSeekPreviousChunk
()
/**
* @dataProvider providePreviousChunkSeekOffsetAndBytes
*/
public
function
testSeekPreviousChunk
(
$offset
,
$length
,
$expectedBytes
)
{
$fileDocument
=
$this
->
collectionWrapper
->
findFileById
(
'length-10'
);
$stream
=
new
ReadableStream
(
$this
->
collectionWrapper
,
$fileDocument
);
$stream
->
readBytes
(
1
);
$stream
->
seek
(
5
);
$stream
->
seek
(
2
);
$stream
->
readBytes
(
1
);
// Read to initialize and advance the chunk iterator to the last chunk
$this
->
assertSame
(
'abcdefghij'
,
$stream
->
readBytes
(
10
));
$commands
=
[];
(
new
CommandObserver
)
->
observe
(
function
()
use
(
$stream
,
$offset
,
$length
,
$expectedBytes
)
{
$stream
->
seek
(
$offset
);
$this
->
assertSame
(
$expectedBytes
,
$stream
->
readBytes
(
$length
));
},
function
(
stdClass
$command
)
use
(
&
$commands
)
{
$commands
[]
=
key
((
array
)
$command
);
}
);
$this
->
assertSame
([
'find'
],
$commands
);
}
public
function
testSeekSubsequentChunk
()
public
function
providePreviousChunkSeekOffsetAndBytes
()
{
return
[
[
0
,
4
,
'abcd'
],
[
2
,
4
,
'cdef'
],
[
4
,
4
,
'efgh'
],
[
6
,
4
,
'ghij'
],
];
}
/**
* @dataProvider provideSameChunkSeekOffsetAndBytes
*/
public
function
testSeekSameChunk
(
$offset
,
$length
,
$expectedBytes
)
{
$fileDocument
=
$this
->
collectionWrapper
->
findFileById
(
'length-10'
);
$stream
=
new
ReadableStream
(
$this
->
collectionWrapper
,
$fileDocument
);
// Read to initialize and advance the chunk iterator to the middle chunk
$this
->
assertSame
(
'abcdef'
,
$stream
->
readBytes
(
6
));
$observer
=
$this
->
getMockBuilder
(
ReadableStream
::
class
)
->
setConstructorArgs
(
array
(
$this
->
collectionWrapper
,
$fileDocument
))
->
getMock
();
$commands
=
[];
$observer
->
expects
(
$this
->
never
())
->
method
(
'initChunksIterator'
);
(
new
CommandObserver
)
->
observe
(
function
()
use
(
$stream
,
$offset
,
$length
,
$expectedBytes
)
{
$stream
->
seek
(
$offset
);
$this
->
assertSame
(
$expectedBytes
,
$stream
->
readBytes
(
$length
));
},
function
(
stdClass
$command
)
use
(
&
$commands
)
{
$commands
[]
=
key
((
array
)
$command
);
}
);
$observer
->
readBytes
(
1
);
$observer
->
seek
(
5
);
$observer
->
seek
(
2
);
$observer
->
readBytes
(
1
);
$this
->
assertSame
([],
$commands
);
}
public
function
provideSameChunkSeekOffsetAndBytes
()
{
return
[
[
4
,
4
,
'efgh'
],
[
6
,
4
,
'ghij'
],
];
}
/**
* @dataProvider provideSubsequentChunkSeekOffsetAndBytes
*/
public
function
testSeekSubsequentChunk
(
$offset
,
$length
,
$expectedBytes
)
{
$fileDocument
=
$this
->
collectionWrapper
->
findFileById
(
'length-10'
);
$stream
=
new
ReadableStream
(
$this
->
collectionWrapper
,
$fileDocument
);
// Read to initialize the chunk iterator to the first chunk
$this
->
assertSame
(
'a'
,
$stream
->
readBytes
(
1
));
$commands
=
[];
(
new
CommandObserver
)
->
observe
(
function
()
use
(
$stream
,
$offset
,
$length
,
$expectedBytes
)
{
$stream
->
seek
(
$offset
);
$this
->
assertSame
(
$expectedBytes
,
$stream
->
readBytes
(
$length
));
},
function
(
stdClass
$command
)
use
(
&
$commands
)
{
$commands
[]
=
key
((
array
)
$command
);
}
);
$this
->
assertSame
([],
$commands
);
}
public
function
provideSubsequentChunkSeekOffsetAndBytes
()
{
return
[
[
4
,
4
,
'efgh'
],
[
6
,
4
,
'ghij'
],
[
8
,
2
,
'ij'
],
];
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment