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
6676e7ef
Commit
6676e7ef
authored
Dec 05, 2016
by
Jeremy Mikola
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
PHPLIB-244: Use strings instead of memory stream for GridFS upload buffering
parent
9dfb2c5f
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
28 additions
and
31 deletions
+28
-31
StreamWrapper.php
src/GridFS/StreamWrapper.php
+1
-1
WritableStream.php
src/GridFS/WritableStream.php
+25
-28
WritableStreamFunctionalTest.php
tests/GridFS/WritableStreamFunctionalTest.php
+2
-2
No files found.
src/GridFS/StreamWrapper.php
View file @
6676e7ef
...
...
@@ -150,7 +150,7 @@ class StreamWrapper
}
try
{
return
$this
->
stream
->
insertChunk
s
(
$data
);
return
$this
->
stream
->
writeByte
s
(
$data
);
}
catch
(
Exception
$e
)
{
trigger_error
(
sprintf
(
'%s: %s'
,
get_class
(
$e
),
$e
->
getMessage
()),
\E_USER_WARNING
);
return
false
;
...
...
src/GridFS/WritableStream.php
View file @
6676e7ef
...
...
@@ -6,6 +6,7 @@ use MongoDB\BSON\Binary;
use
MongoDB\BSON\ObjectId
;
use
MongoDB\BSON\UTCDateTime
;
use
MongoDB\Exception\InvalidArgumentException
;
use
MongoDB\Exception\RuntimeException
;
/**
* WritableStream abstracts the process of writing a GridFS file.
...
...
@@ -16,8 +17,7 @@ class WritableStream
{
private
static
$defaultChunkSizeBytes
=
261120
;
private
$buffer
;
private
$bufferLength
=
0
;
private
$buffer
=
''
;
private
$chunkOffset
=
0
;
private
$chunkSize
;
private
$collectionWrapper
;
...
...
@@ -76,7 +76,6 @@ class WritableStream
$this
->
chunkSize
=
$options
[
'chunkSizeBytes'
];
$this
->
collectionWrapper
=
$collectionWrapper
;
$this
->
buffer
=
fopen
(
'php://memory'
,
'w+b'
);
$this
->
ctx
=
hash_init
(
'md5'
);
$this
->
file
=
[
...
...
@@ -113,14 +112,10 @@ class WritableStream
return
;
}
rewind
(
$this
->
buffer
);
$cached
=
stream_get_contents
(
$this
->
buffer
);
if
(
strlen
(
$cached
)
>
0
)
{
$this
->
insertChunk
(
$cached
);
if
(
strlen
(
$this
->
buffer
)
>
0
)
{
$this
->
insertChunkFromBuffer
();
}
fclose
(
$this
->
buffer
);
$this
->
fileCollectionInsert
();
$this
->
isClosed
=
true
;
}
...
...
@@ -151,36 +146,31 @@ class WritableStream
* Inserts binary data into GridFS via chunks.
*
* Data will be buffered internally until chunkSizeBytes are accumulated, at
* which point a chunk's worth of data will be inserted and the buffer
* reset.
* which point a chunk document will be inserted and the buffer reset.
*
* @param string $
toWrite
Binary data to write
* @param string $
data
Binary data to write
* @return integer
*/
public
function
insertChunks
(
$toWrite
)
public
function
writeBytes
(
$data
)
{
if
(
$this
->
isClosed
)
{
// TODO: Should this be an error condition? e.g. BadMethodCallException
return
;
}
$
readBytes
=
0
;
$
bytesRead
=
0
;
while
(
$readBytes
!=
strlen
(
$toWrite
))
{
$addToBuffer
=
substr
(
$toWrite
,
$readBytes
,
$this
->
chunkSize
-
$this
->
bufferLength
);
fwrite
(
$this
->
buffer
,
$addToBuffer
);
$readBytes
+=
strlen
(
$addToBuffer
);
$this
->
bufferLength
+=
strlen
(
$addToBuffer
);
while
(
$bytesRead
!=
strlen
(
$data
))
{
$initialBufferLength
=
strlen
(
$this
->
buffer
);
$this
->
buffer
.=
substr
(
$data
,
$bytesRead
,
$this
->
chunkSize
-
$initialBufferLength
);
$bytesRead
+=
strlen
(
$this
->
buffer
)
-
$initialBufferLength
;
if
(
$this
->
bufferLength
==
$this
->
chunkSize
)
{
rewind
(
$this
->
buffer
);
$this
->
insertChunk
(
stream_get_contents
(
$this
->
buffer
));
ftruncate
(
$this
->
buffer
,
0
);
$this
->
bufferLength
=
0
;
if
(
strlen
(
$this
->
buffer
)
==
$this
->
chunkSize
)
{
$this
->
insertChunkFromBuffer
();
}
}
return
$
readBytes
;
return
$
bytesRead
;
}
private
function
abort
()
...
...
@@ -206,14 +196,21 @@ class WritableStream
return
$this
->
file
[
'_id'
];
}
private
function
insertChunk
(
$data
)
private
function
insertChunk
FromBuffer
(
)
{
if
(
$this
->
isClosed
)
{
// TODO: Should this be an error condition? e.g. BadMethodCallException
return
;
}
$toUpload
=
[
if
(
strlen
(
$this
->
buffer
)
==
0
)
{
return
;
}
$data
=
$this
->
buffer
;
$this
->
buffer
=
''
;
$chunk
=
[
'files_id'
=>
$this
->
file
[
'_id'
],
'n'
=>
$this
->
chunkOffset
,
'data'
=>
new
Binary
(
$data
,
Binary
::
TYPE_GENERIC
),
...
...
@@ -221,7 +218,7 @@ class WritableStream
hash_update
(
$this
->
ctx
,
$data
);
$this
->
collectionWrapper
->
insertChunk
(
$
toUpload
);
$this
->
collectionWrapper
->
insertChunk
(
$
chunk
);
$this
->
length
+=
strlen
(
$data
);
$this
->
chunkOffset
++
;
}
...
...
tests/GridFS/WritableStreamFunctionalTest.php
View file @
6676e7ef
...
...
@@ -55,10 +55,10 @@ class WritableStreamFunctionalTest extends FunctionalTestCase
/**
* @dataProvider provideInputDataAndExpectedMD5
*/
public
function
test
InsertChunk
sCalculatesMD5
(
$input
,
$expectedMD5
)
public
function
test
WriteByte
sCalculatesMD5
(
$input
,
$expectedMD5
)
{
$stream
=
new
WritableStream
(
$this
->
collectionWrapper
,
'filename'
);
$stream
->
insertChunk
s
(
$input
);
$stream
->
writeByte
s
(
$input
);
$stream
->
close
();
$fileDocument
=
$this
->
filesCollection
->
findOne
(
...
...
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