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

Rename GridFSCollectionsWrapper to CollectionWrapper

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