-
Notifications
You must be signed in to change notification settings - Fork 266
PHPLIB-114: Implement GridFS specification #57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 5 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
56023b0
PHPLIB-146: Implement GridFS upload, plus initial GridFS commit
623a98b
PHPLIB-147: Implement GridFS download
c787545
PHPLIB-148: Implement file deletion
2f76c46
PHPLIB-154: implement downloadByName
22e0480
PHPLIB-149: implement find on files collection
dacd44d
Response to PR comments to mongodb-php-library pull 57
8bcdab2
restructure some aspects to be more inline with SPEC and add addition…
d4fded2
additional tests: WIP
6cc4a58
added many many more tests, added getIdFromStream
1171293
Stream registering is now totally transparent to the user
60d5e88
added 'Abort' to clean up according to spec
21fb2c9
Rename function added to bucket
65fde2b
Straighten out abort method
ece9a00
Added corrupt chunk tests
a81bb85
removed all of the unused 'use' files
ead7cf1
uncommented argument check
e534225
BucketReadWriter and GridFsStream are obsolete
jmikola 73a129e
Apply CS fixes and refactor GridFS classes
jmikola 1706d0a
Replace public $id property with getter method
jmikola 59e7ebd
Replace large GridFS fixture with generated temp file
jmikola 8277ec3
Merge pull request #1 from jmikola/phplib-114-review
williambanfield 4b947a3
PHPLIB-158: delete removes files document first
cb2a7ee
PHPLIB-159: rename uses UpdateOne() per the spec
87d08a6
PHPLIB-160: implement drop() on bucket
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace MongoDB\Exception; | ||
|
||
class GridFSCorruptFileException extends \MongoDB\Driver\Exception\RuntimeException implements Exception | ||
{ | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace MongoDB\Exception; | ||
|
||
class GridFSFileNotFoundException extends \MongoDB\Driver\Exception\RuntimeException implements Exception | ||
{ | ||
public function __construct($fname, $bucketName, $databaseName){ | ||
parent::__construct(sprintf('Unable to find file by: %s in %s.%s', $fname,$databaseName, $bucketName)); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
<?php | ||
namespace MongoDB\GridFS; | ||
|
||
use MongoDB\Collection; | ||
use MongoDB\Database; | ||
use MongoDB\BSON\ObjectId; | ||
use MongoDB\Driver\ReadPreference; | ||
use MongoDB\Driver\WriteConcern; | ||
use MongoDB\Driver\Manager; | ||
use MongoDB\Exception\InvalidArgumentException; | ||
use MongoDB\Exception\InvalidArgumentTypeException; | ||
use MongoDB\Exception\RuntimeException; | ||
use MongoDB\Exception\UnexpectedValueException; | ||
/** | ||
* Bucket abstracts the GridFS files and chunks collections. | ||
* | ||
* @api | ||
*/ | ||
class Bucket | ||
{ | ||
private $databaseName; | ||
private $options; | ||
private $filesCollection; | ||
private $chunksCollection; | ||
private $ensuredIndexes = false; | ||
/** | ||
* Constructs a GridFS bucket. | ||
* | ||
* Supported options: | ||
* | ||
* * bucketName (string): The bucket name, which will be used as a prefix | ||
* for the files and chunks collections. Defaults to "fs". | ||
* | ||
* * chunkSizeBytes (integer): The chunk size in bytes. Defaults to | ||
* 261120 (i.e. 255 KiB). | ||
* | ||
* * readPreference (MongoDB\Driver\ReadPreference): Read preference. | ||
* | ||
* * writeConcern (MongoDB\Driver\WriteConcern): Write concern. | ||
* | ||
* @param Manager $manager Manager instance from the driver | ||
* @param string $databaseName Database name | ||
* @param array $options Bucket options | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function __construct(Manager $manager, $databaseName, array $options = []) | ||
{ | ||
$collectionOptions = []; | ||
$options += [ | ||
'bucketName' => 'fs', | ||
'chunkSizeBytes' => 261120, | ||
]; | ||
if (isset($options['bucketName']) && ! is_string($options['bucketName'])) { | ||
throw new InvalidArgumentTypeException('"bucketName" option', $options['bucketName'], 'string'); | ||
} | ||
if (isset($options['chunkSizeBytes']) && ! is_integer($options['chunkSizeBytes'])) { | ||
throw new InvalidArgumentTypeException('"chunkSizeBytes" option', $options['chunkSizeBytes'], 'integer'); | ||
} | ||
if (isset($options['readPreference'])) { | ||
if (! $options['readPreference'] instanceof ReadPreference) { | ||
throw new InvalidArgumentTypeException('"readPreference" option', $options['readPreference'], 'MongoDB\Driver\ReadPreference'); | ||
} else { | ||
$collectionOptions['readPreference'] = $options['readPreference']; | ||
} | ||
} | ||
if (isset($options['writeConcern'])) { | ||
if (! $options['writeConcern'] instanceof WriteConcern) { | ||
throw new InvalidArgumentTypeException('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern'); | ||
} else { | ||
$collectionOptions['writeConcern'] = $options['writeConcern']; | ||
} | ||
} | ||
$this->databaseName = (string) $databaseName; | ||
$this->options = $options; | ||
|
||
$this->filesCollection = new Collection( | ||
$manager, | ||
sprintf('%s.%s.files', $this->databaseName, $options['bucketName']), | ||
$collectionOptions | ||
); | ||
$this->chunksCollection = new Collection( | ||
$manager, | ||
sprintf('%s.%s.chunks', $this->databaseName, $options['bucketName']), | ||
$collectionOptions | ||
); | ||
} | ||
/** | ||
* Return the chunkSizeBytes option for this Bucket. | ||
* | ||
* @return integer | ||
*/ | ||
public function getChunkSizeBytes() | ||
{ | ||
return $this->options['chunkSizeBytes']; | ||
} | ||
|
||
public function getDatabaseName() | ||
{ | ||
return $this->databaseName; | ||
} | ||
public function getFilesCollection() | ||
{ | ||
return $this->filesCollection; | ||
} | ||
|
||
public function getChunksCollection() | ||
{ | ||
return $this->chunksCollection; | ||
} | ||
public function getBucketName() | ||
{ | ||
return $this->options['bucketName']; | ||
} | ||
public function getReadConcern() | ||
{ | ||
if(isset($this->options['readPreference'])) { | ||
return $this->options['readPreference']; | ||
} else{ | ||
return null; | ||
} | ||
} | ||
public function getWriteConcern() | ||
{ | ||
if(isset($this->options['writeConcern'])) { | ||
return $this->options['writeConcern']; | ||
} else{ | ||
return null; | ||
} | ||
} | ||
|
||
private function ensureIndexes() | ||
{ | ||
if ($this->ensuredIndexes) { | ||
return; | ||
} | ||
if ( ! $this->isFilesCollectionEmpty()) { | ||
return; | ||
} | ||
$this->ensureFilesIndex(); | ||
$this->ensureChunksIndex(); | ||
$this->ensuredIndexes = true; | ||
} | ||
private function ensureChunksIndex() | ||
{ | ||
foreach ($this->chunksCollection->listIndexes() as $index) { | ||
if ($index->isUnique() && $index->getKey() === ['files_id' => 1, 'n' => 1]) { | ||
return; | ||
} | ||
} | ||
$this->chunksCollection->createIndex(['files_id' => 1, 'n' => 1], ['unique' => true]); | ||
} | ||
private function ensureFilesIndex() | ||
{ | ||
foreach ($this->filesCollection->listIndexes() as $index) { | ||
if ($index->getKey() === ['filename' => 1, 'uploadDate' => 1]) { | ||
return; | ||
} | ||
} | ||
$this->filesCollection->createIndex(['filename' => 1, 'uploadDate' => 1]); | ||
} | ||
private function isFilesCollectionEmpty() | ||
{ | ||
return null === $this->filesCollection->findOne([], [ | ||
'readPreference' => new ReadPreference(ReadPreference::RP_PRIMARY), | ||
'projection' => ['_id' => 1], | ||
]); | ||
} | ||
public function findFileRevision($filename, $revision) | ||
{ | ||
if ($revision < 0) { | ||
$skip = abs($revision) -1; | ||
$sortOrder = -1; | ||
} else { | ||
$skip = $revision; | ||
$sortOrder = 1; | ||
} | ||
$file = $this->filesCollection->findOne(["filename"=> $filename], ["sort" => ["uploadDate"=> $sortOrder], "limit"=>1, "skip" => $skip]); | ||
if(is_null($file)) { | ||
throw new \MongoDB\Exception\GridFSFileNotFoundException($filename, $this->getBucketName(), $this->getDatabaseName());; | ||
} | ||
return $file; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
<?php | ||
namespace MongoDB\GridFS; | ||
|
||
class BucketReadWriter | ||
{ | ||
private $bucket; | ||
|
||
public function __construct(Bucket $bucket) | ||
{ | ||
$this->bucket = $bucket; | ||
} | ||
/** | ||
* Opens a Stream for writing the contents of a file. | ||
* | ||
* @param string $filename file to upload | ||
* @param array $options Stream Options | ||
* @return Stream uploadStream | ||
*/ | ||
public function openUploadStream($filename, array $options = []) | ||
{ | ||
$options = [ | ||
'bucket' => $this->bucket, | ||
'uploadOptions' => $options | ||
]; | ||
$context = stream_context_create(['gridfs' => $options]); | ||
return fopen(sprintf('gridfs://%s/%s', $this->bucket->getDatabaseName(), $filename), 'w', false, $context); | ||
} | ||
/** | ||
* Upload a file to this bucket by specifying the source stream file | ||
* | ||
* @param String $filename Filename To Insert | ||
* @param Stream $source Source Stream | ||
* @param array $options Stream Options | ||
* @return ObjectId | ||
*/ | ||
public function uploadFromStream($filename, $source, array $options = []) | ||
{ | ||
$gridFsStream = new GridFsUpload($this->bucket, $filename, $options); | ||
return $gridFsStream->uploadFromStream($source); | ||
} | ||
/** | ||
* Opens a Stream for reading the contents of a file specified by ID. | ||
* | ||
* @param ObjectId $id | ||
* @return Stream | ||
*/ | ||
public function openDownloadStream(\MongoDB\BSON\ObjectId $id) | ||
{ | ||
$options = [ | ||
'bucket' => $this->bucket | ||
]; | ||
$context = stream_context_create(['gridfs' => $options]); | ||
return fopen(sprintf('gridfs://%s/%s', $this->bucket->getDatabaseName(), $id), 'r', false, $context); | ||
} | ||
/** | ||
* Downloads the contents of the stored file specified by id and writes | ||
* the contents to the destination Stream. | ||
* @param ObjectId $id GridFS File Id | ||
* @param Stream $destination Destination Stream | ||
*/ | ||
public function downloadToStream(\MongoDB\BSON\ObjectId $id, $destination) | ||
{ | ||
$gridFsStream = new GridFsDownload($this->bucket, $id); | ||
$gridFsStream->downloadToStream($destination); | ||
} | ||
/** | ||
* Delete a file from the GridFS bucket. If the file collection entry is not found, still attempts to delete orphaned chunks | ||
* | ||
* @param ObjectId $id file id | ||
* @throws GridFSFileNotFoundException | ||
*/ | ||
public function delete(\MongoDB\BSON\ObjectId $id) | ||
{ | ||
$options =[]; | ||
$writeConcern = $this->bucket->getWriteConcern(); | ||
if(!is_null($writeConcern)) { | ||
$options['writeConcern'] = $writeConcern; | ||
} | ||
$file = $this->bucket->getFilesCollection()->findOne(['_id' => $id]); | ||
$this->bucket->getChunksCollection()->deleteMany(['files_id' => $id], $options); | ||
if (is_null($file)) { | ||
throw new \MongoDB\Exception\GridFSFileNotFoundException($id, $this->bucket->getDatabaseName(), $this->bucket->getBucketName()); | ||
} | ||
|
||
$this->bucket->getFilesCollection()->deleteOne(['_id' => $id], $options); | ||
} | ||
/** | ||
* Open a stream to download a file from the GridFS bucket. Searches for the file by the specified name then returns a stream to the specified file | ||
* @param string $filename name of the file to download | ||
* @param int $revision the revision of the file to download | ||
* @throws GridFSFileNotFoundException | ||
*/ | ||
public function openDownloadStreamByName($filename, $revision = -1) | ||
{ | ||
$file = $this->bucket->findFileRevision($filename, $revision); | ||
$options = ['bucket' => $this->bucket, | ||
'file' => $file | ||
]; | ||
$context = stream_context_create(['gridfs' => $options]); | ||
return fopen(sprintf('gridfs://%s/%s', $this->bucket->getDatabaseName(), $filename), 'r', false, $context); | ||
} | ||
/** | ||
* Download a file from the GridFS bucket by name. Searches for the file by the specified name then loads data into the stream | ||
* | ||
* @param string $filename name of the file to download | ||
* @param int $revision the revision of the file to download | ||
* @throws GridFSFileNotFoundException | ||
*/ | ||
public function downloadToStreamByName($filename, $destination, $revision=-1) | ||
{ | ||
$file = $this->bucket->findFileRevision($filename, $revision); | ||
$gridFsStream = new GridFsDownload($this->bucket, null, $file); | ||
$gridFsStream->downloadToStream($destination); | ||
} | ||
/** | ||
* Find files from the GridFS bucket files collection. | ||
* | ||
* @param array $filter filter to find by | ||
* @param array $options options to | ||
*/ | ||
public function find($filter, array $options =[]) | ||
{ | ||
return $this->bucket->getFilesCollection()->find($filter, $options); | ||
} | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's fold this class and its methods into the Bucket class, since that's where the spec expects to find these methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This class still exists, but is no longer called from anywhere. Just keeping it around until we're sure we're done with it.