Commit 2b93dab1 authored by Jeremy Mikola's avatar Jeremy Mikola

Rename GridFSCollectionsWrapper to CollectionWrapper

parent 400e829c
...@@ -22,7 +22,7 @@ class Bucket ...@@ -22,7 +22,7 @@ class Bucket
private static $streamWrapper; private static $streamWrapper;
private static $defaultChunkSizeBytes = 261120; private static $defaultChunkSizeBytes = 261120;
private $collectionsWrapper; private $collectionWrapper;
private $databaseName; private $databaseName;
private $options; private $options;
...@@ -74,7 +74,7 @@ class Bucket ...@@ -74,7 +74,7 @@ class Bucket
$collectionOptions = array_intersect_key($options, ['readPreference' => 1, 'writeConcern' => 1]); $collectionOptions = array_intersect_key($options, ['readPreference' => 1, 'writeConcern' => 1]);
$this->collectionsWrapper = new GridFSCollectionsWrapper($manager, $databaseName, $options['bucketName'], $collectionOptions); $this->collectionWrapper = new CollectionWrapper($manager, $databaseName, $options['bucketName'], $collectionOptions);
$this->registerStreamWrapper($manager); $this->registerStreamWrapper($manager);
} }
...@@ -89,12 +89,12 @@ class Bucket ...@@ -89,12 +89,12 @@ class Bucket
*/ */
public function delete(ObjectId $id) public function delete(ObjectId $id)
{ {
$file = $this->collectionsWrapper->getFilesCollection()->findOne(['_id' => $id]); $file = $this->collectionWrapper->getFilesCollection()->findOne(['_id' => $id]);
$this->collectionsWrapper->getFilesCollection()->deleteOne(['_id' => $id]); $this->collectionWrapper->getFilesCollection()->deleteOne(['_id' => $id]);
$this->collectionsWrapper->getChunksCollection()->deleteMany(['files_id' => $id]); $this->collectionWrapper->getChunksCollection()->deleteMany(['files_id' => $id]);
if ($file === null) { if ($file === null) {
throw FileNotFoundException::byId($id, $this->collectionsWrapper->getFilesCollection()->getNameSpace()); throw FileNotFoundException::byId($id, $this->collectionWrapper->getFilesCollection()->getNameSpace());
} }
} }
...@@ -108,16 +108,16 @@ class Bucket ...@@ -108,16 +108,16 @@ class Bucket
*/ */
public function downloadToStream(ObjectId $id, $destination) public function downloadToStream(ObjectId $id, $destination)
{ {
$file = $this->collectionsWrapper->getFilesCollection()->findOne( $file = $this->collectionWrapper->getFilesCollection()->findOne(
['_id' => $id], ['_id' => $id],
['typeMap' => ['root' => 'stdClass']] ['typeMap' => ['root' => 'stdClass']]
); );
if ($file === null) { if ($file === null) {
throw FileNotFoundException::byId($id, $this->collectionsWrapper->getFilesCollection()->getNameSpace()); throw FileNotFoundException::byId($id, $this->collectionWrapper->getFilesCollection()->getNameSpace());
} }
$gridFsStream = new GridFSDownload($this->collectionsWrapper, $file); $gridFsStream = new GridFSDownload($this->collectionWrapper, $file);
$gridFsStream->downloadToStream($destination); $gridFsStream->downloadToStream($destination);
} }
...@@ -149,7 +149,7 @@ class Bucket ...@@ -149,7 +149,7 @@ class Bucket
{ {
$options += ['revision' => -1]; $options += ['revision' => -1];
$file = $this->findFileRevision($filename, $options['revision']); $file = $this->findFileRevision($filename, $options['revision']);
$gridFsStream = new GridFSDownload($this->collectionsWrapper, $file); $gridFsStream = new GridFSDownload($this->collectionWrapper, $file);
$gridFsStream->downloadToStream($destination); $gridFsStream->downloadToStream($destination);
} }
...@@ -160,7 +160,7 @@ class Bucket ...@@ -160,7 +160,7 @@ class Bucket
public function drop() public function drop()
{ {
$this->collectionsWrapper->dropCollections(); $this->collectionWrapper->dropCollections();
} }
/** /**
...@@ -173,12 +173,12 @@ class Bucket ...@@ -173,12 +173,12 @@ class Bucket
*/ */
public function find($filter, array $options = []) public function find($filter, array $options = [])
{ {
return $this->collectionsWrapper->getFilesCollection()->find($filter, $options); return $this->collectionWrapper->getFilesCollection()->find($filter, $options);
} }
public function getCollectionsWrapper() public function getCollectionsWrapper()
{ {
return $this->collectionsWrapper; return $this->collectionWrapper;
} }
public function getDatabaseName() public function getDatabaseName()
...@@ -212,13 +212,13 @@ class Bucket ...@@ -212,13 +212,13 @@ class Bucket
*/ */
public function openDownloadStream(ObjectId $id) public function openDownloadStream(ObjectId $id)
{ {
$file = $this->collectionsWrapper->getFilesCollection()->findOne( $file = $this->collectionWrapper->getFilesCollection()->findOne(
['_id' => $id], ['_id' => $id],
['typeMap' => ['root' => 'stdClass']] ['typeMap' => ['root' => 'stdClass']]
); );
if ($file === null) { if ($file === null) {
throw FileNotFoundException::byId($id, $this->collectionsWrapper->getFilesCollection()->getNameSpace()); throw FileNotFoundException::byId($id, $this->collectionWrapper->getFilesCollection()->getNameSpace());
} }
return $this->openDownloadStreamByFile($file); return $this->openDownloadStreamByFile($file);
...@@ -273,7 +273,7 @@ class Bucket ...@@ -273,7 +273,7 @@ class Bucket
$options += ['chunkSizeBytes' => $this->options['chunkSizeBytes']]; $options += ['chunkSizeBytes' => $this->options['chunkSizeBytes']];
$streamOptions = [ $streamOptions = [
'collectionsWrapper' => $this->collectionsWrapper, 'collectionWrapper' => $this->collectionWrapper,
'uploadOptions' => $options, 'uploadOptions' => $options,
]; ];
...@@ -291,10 +291,10 @@ class Bucket ...@@ -291,10 +291,10 @@ class Bucket
*/ */
public function rename(ObjectId $id, $newFilename) public function rename(ObjectId $id, $newFilename)
{ {
$filesCollection = $this->collectionsWrapper->getFilesCollection(); $filesCollection = $this->collectionWrapper->getFilesCollection();
$result = $filesCollection->updateOne(['_id' => $id], ['$set' => ['filename' => $newFilename]]); $result = $filesCollection->updateOne(['_id' => $id], ['$set' => ['filename' => $newFilename]]);
if($result->getModifiedCount() == 0) { if($result->getModifiedCount() == 0) {
throw FileNotFoundException::byId($id, $this->collectionsWrapper->getFilesCollection()->getNameSpace()); throw FileNotFoundException::byId($id, $this->collectionWrapper->getFilesCollection()->getNameSpace());
} }
} }
...@@ -314,7 +314,7 @@ class Bucket ...@@ -314,7 +314,7 @@ class Bucket
public function uploadFromStream($filename, $source, array $options = []) public function uploadFromStream($filename, $source, array $options = [])
{ {
$options += ['chunkSizeBytes' => $this->options['chunkSizeBytes']]; $options += ['chunkSizeBytes' => $this->options['chunkSizeBytes']];
$gridFsStream = new GridFSUpload($this->collectionsWrapper, $filename, $options); $gridFsStream = new GridFSUpload($this->collectionWrapper, $filename, $options);
return $gridFsStream->uploadFromStream($source); return $gridFsStream->uploadFromStream($source);
} }
...@@ -329,7 +329,7 @@ class Bucket ...@@ -329,7 +329,7 @@ class Bucket
$sortOrder = 1; $sortOrder = 1;
} }
$filesCollection = $this->collectionsWrapper->getFilesCollection(); $filesCollection = $this->collectionWrapper->getFilesCollection();
$file = $filesCollection->findOne( $file = $filesCollection->findOne(
['filename' => $filename], ['filename' => $filename],
[ [
...@@ -349,7 +349,7 @@ class Bucket ...@@ -349,7 +349,7 @@ class Bucket
private function openDownloadStreamByFile($file) private function openDownloadStreamByFile($file)
{ {
$options = [ $options = [
'collectionsWrapper' => $this->collectionsWrapper, 'collectionWrapper' => $this->collectionWrapper,
'file' => $file, 'file' => $file,
]; ];
......
...@@ -5,14 +5,13 @@ namespace MongoDB\GridFS; ...@@ -5,14 +5,13 @@ namespace MongoDB\GridFS;
use MongoDB\Collection; use MongoDB\Collection;
use MongoDB\Driver\Manager; use MongoDB\Driver\Manager;
use MongoDB\Driver\ReadPreference; use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern;
/** /**
* GridFSCollectionsWrapper abstracts the GridFS files and chunks collections. * CollectionWrapper abstracts the GridFS files and chunks collections.
* *
* @internal * @internal
*/ */
class GridFSCollectionsWrapper class CollectionWrapper
{ {
private $chunksCollection; private $chunksCollection;
private $ensuredIndexes = false; private $ensuredIndexes = false;
...@@ -34,8 +33,9 @@ class GridFSCollectionsWrapper ...@@ -34,8 +33,9 @@ class GridFSCollectionsWrapper
$this->chunksCollection = new Collection($manager, $databaseName, sprintf('%s.chunks', $bucketName), $collectionOptions); $this->chunksCollection = new Collection($manager, $databaseName, sprintf('%s.chunks', $bucketName), $collectionOptions);
} }
public function dropCollections(){ public function dropCollections()
$this->filesCollection-> drop(); {
$this->filesCollection->drop();
$this->chunksCollection->drop(); $this->chunksCollection->drop();
} }
......
...@@ -19,7 +19,7 @@ class GridFSDownload ...@@ -19,7 +19,7 @@ class GridFSDownload
private $bytesSeen = 0; private $bytesSeen = 0;
private $chunkOffset = 0; private $chunkOffset = 0;
private $chunksIterator; private $chunksIterator;
private $collectionsWrapper; private $collectionWrapper;
private $file; private $file;
private $firstCheck = true; private $firstCheck = true;
private $iteratorEmpty = false; private $iteratorEmpty = false;
...@@ -28,17 +28,17 @@ class GridFSDownload ...@@ -28,17 +28,17 @@ class GridFSDownload
/** /**
* Constructs a GridFS download stream. * Constructs a GridFS download stream.
* *
* @param GridFSCollectionsWrapper $collectionsWrapper GridFS collections wrapper * @param CollectionWrapper $collectionWrapper GridFS collection wrapper
* @param stdClass $file GridFS file document * @param stdClass $file GridFS file document
* @throws CorruptFileException * @throws CorruptFileException
*/ */
public function __construct(GridFSCollectionsWrapper $collectionsWrapper, stdClass $file) public function __construct(CollectionWrapper $collectionWrapper, stdClass $file)
{ {
$this->collectionsWrapper = $collectionsWrapper; $this->collectionWrapper = $collectionWrapper;
$this->file = $file; $this->file = $file;
try { try {
$cursor = $this->collectionsWrapper->getChunksCollection()->find( $cursor = $this->collectionWrapper->getChunksCollection()->find(
['files_id' => $this->file->_id], ['files_id' => $this->file->_id],
['sort' => ['n' => 1]] ['sort' => ['n' => 1]]
); );
......
...@@ -19,7 +19,7 @@ class GridFSUpload ...@@ -19,7 +19,7 @@ class GridFSUpload
private $bufferLength = 0; private $bufferLength = 0;
private $chunkOffset = 0; private $chunkOffset = 0;
private $chunkSize; private $chunkSize;
private $collectionsWrapper; private $collectionWrapper;
private $ctx; private $ctx;
private $file; private $file;
private $indexChecker; private $indexChecker;
...@@ -44,12 +44,12 @@ class GridFSUpload ...@@ -44,12 +44,12 @@ class GridFSUpload
* * metadata (document): User data for the "metadata" field of the files * * metadata (document): User data for the "metadata" field of the files
* collection document. * collection document.
* *
* @param GridFSCollectionsWrapper $collectionsWrapper GridFS collections wrapper * @param CollectionWrapper $collectionWrapper GridFS collection wrapper
* @param string $filename File name * @param string $filename File name
* @param array $options Upload options * @param array $options Upload options
* @throws InvalidArgumentException * @throws InvalidArgumentException
*/ */
public function __construct(GridFSCollectionsWrapper $collectionsWrapper, $filename, array $options = []) public function __construct(CollectionWrapper $collectionWrapper, $filename, array $options = [])
{ {
$options += ['chunkSizeBytes' => 261120]; $options += ['chunkSizeBytes' => 261120];
...@@ -66,7 +66,7 @@ class GridFSUpload ...@@ -66,7 +66,7 @@ class GridFSUpload
} }
$this->chunkSize = $options['chunkSizeBytes']; $this->chunkSize = $options['chunkSizeBytes'];
$this->collectionsWrapper = $collectionsWrapper; $this->collectionWrapper = $collectionWrapper;
$this->buffer = fopen('php://temp', 'w+'); $this->buffer = fopen('php://temp', 'w+');
$this->ctx = hash_init('md5'); $this->ctx = hash_init('md5');
...@@ -189,8 +189,8 @@ class GridFSUpload ...@@ -189,8 +189,8 @@ class GridFSUpload
private function abort() private function abort()
{ {
$this->collectionsWrapper->getChunksCollection()->deleteMany(['files_id' => $this->file['_id']]); $this->collectionWrapper->getChunksCollection()->deleteMany(['files_id' => $this->file['_id']]);
$this->collectionsWrapper->getFilesCollection()->deleteOne(['_id' => $this->file['_id']]); $this->collectionWrapper->getFilesCollection()->deleteOne(['_id' => $this->file['_id']]);
$this->isClosed = true; $this->isClosed = true;
} }
...@@ -215,7 +215,7 @@ class GridFSUpload ...@@ -215,7 +215,7 @@ class GridFSUpload
$this->file['length'] = $this->length; $this->file['length'] = $this->length;
$this->file['md5'] = $md5; $this->file['md5'] = $md5;
$this->collectionsWrapper->insertFile($this->file); $this->collectionWrapper->insertFile($this->file);
return $this->file['_id']; return $this->file['_id'];
} }
...@@ -235,7 +235,7 @@ class GridFSUpload ...@@ -235,7 +235,7 @@ class GridFSUpload
hash_update($this->ctx, $data); hash_update($this->ctx, $data);
$this->collectionsWrapper->insertChunk($toUpload); $this->collectionWrapper->insertChunk($toUpload);
$this->length += strlen($data); $this->length += strlen($data);
$this->chunkOffset++; $this->chunkOffset++;
} }
......
...@@ -26,7 +26,7 @@ class StreamWrapper ...@@ -26,7 +26,7 @@ class StreamWrapper
public function openReadStream() public function openReadStream()
{ {
$context = stream_context_get_options($this->context); $context = stream_context_get_options($this->context);
$this->gridFSStream = new GridFSDownload($this->collectionsWrapper, $context['gridfs']['file']); $this->gridFSStream = new GridFSDownload($this->collectionWrapper, $context['gridfs']['file']);
$this->id = $this->gridFSStream->getId(); $this->id = $this->gridFSStream->getId();
return true; return true;
...@@ -36,7 +36,7 @@ class StreamWrapper ...@@ -36,7 +36,7 @@ class StreamWrapper
{ {
$context = stream_context_get_options($this->context); $context = stream_context_get_options($this->context);
$options = $context['gridfs']['uploadOptions']; $options = $context['gridfs']['uploadOptions'];
$this->gridFSStream = new GridFSUpload($this->collectionsWrapper, $this->identifier, $options); $this->gridFSStream = new GridFSUpload($this->collectionWrapper, $this->identifier, $options);
$this->id = $this->gridFSStream->getId(); $this->id = $this->gridFSStream->getId();
return true; return true;
...@@ -68,7 +68,7 @@ class StreamWrapper ...@@ -68,7 +68,7 @@ class StreamWrapper
{ {
$this->initProtocol($path); $this->initProtocol($path);
$context = stream_context_get_options($this->context); $context = stream_context_get_options($this->context);
$this->collectionsWrapper = $context['gridfs']['collectionsWrapper']; $this->collectionWrapper = $context['gridfs']['collectionWrapper'];
$this->mode = $mode; $this->mode = $mode;
switch ($this->mode) { switch ($this->mode) {
......
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