Commit cc09f79e authored by Jeremy Mikola's avatar Jeremy Mikola

Merge pull request #224

parents 0195906b 748c14f4
...@@ -9,6 +9,7 @@ use MongoDB\Driver\ReadConcern; ...@@ -9,6 +9,7 @@ use MongoDB\Driver\ReadConcern;
use MongoDB\Driver\ReadPreference; use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern; use MongoDB\Driver\WriteConcern;
use MongoDB\Exception\InvalidArgumentException; use MongoDB\Exception\InvalidArgumentException;
use MongoDB\GridFS\Bucket;
use MongoDB\Model\CollectionInfoIterator; use MongoDB\Model\CollectionInfoIterator;
use MongoDB\Operation\CreateCollection; use MongoDB\Operation\CreateCollection;
use MongoDB\Operation\DatabaseCommand; use MongoDB\Operation\DatabaseCommand;
...@@ -262,6 +263,24 @@ class Database ...@@ -262,6 +263,24 @@ class Database
return new Collection($this->manager, $this->databaseName, $collectionName, $options); return new Collection($this->manager, $this->databaseName, $collectionName, $options);
} }
/**
* Select a GridFS bucket within this database.
*
* @see Bucket::__construct() for supported options
* @param array $options Bucket constructor options
* @return Bucket
*/
public function selectGridFSBucket(array $options = [])
{
$options += [
'readConcern' => $this->readConcern,
'readPreference' => $this->readPreference,
'writeConcern' => $this->writeConcern,
];
return new Bucket($this->manager, $this->databaseName, $options);
}
/** /**
* Get a clone of this database with different options. * Get a clone of this database with different options.
* *
......
...@@ -27,8 +27,12 @@ class Bucket ...@@ -27,8 +27,12 @@ class Bucket
private $collectionWrapper; private $collectionWrapper;
private $databaseName; private $databaseName;
private $manager;
private $bucketName; private $bucketName;
private $chunkSizeBytes; private $chunkSizeBytes;
private $readConcern;
private $readPreference;
private $writeConcern;
/** /**
* Constructs a GridFS bucket. * Constructs a GridFS bucket.
...@@ -79,9 +83,13 @@ class Bucket ...@@ -79,9 +83,13 @@ class Bucket
throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern'); throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
} }
$this->manager = $manager;
$this->databaseName = (string) $databaseName; $this->databaseName = (string) $databaseName;
$this->bucketName = $options['bucketName']; $this->bucketName = $options['bucketName'];
$this->chunkSizeBytes = $options['chunkSizeBytes']; $this->chunkSizeBytes = $options['chunkSizeBytes'];
$this->readConcern = isset($options['readConcern']) ? $options['readConcern'] : $this->manager->getReadConcern();
$this->readPreference = isset($options['readPreference']) ? $options['readPreference'] : $this->manager->getReadPreference();
$this->writeConcern = isset($options['writeConcern']) ? $options['writeConcern'] : $this->manager->getWriteConcern();
$collectionOptions = array_intersect_key($options, ['readConcern' => 1, 'readPreference' => 1, 'writeConcern' => 1]); $collectionOptions = array_intersect_key($options, ['readConcern' => 1, 'readPreference' => 1, 'writeConcern' => 1]);
...@@ -100,7 +108,11 @@ class Bucket ...@@ -100,7 +108,11 @@ class Bucket
return [ return [
'bucketName' => $this->bucketName, 'bucketName' => $this->bucketName,
'databaseName' => $this->databaseName, 'databaseName' => $this->databaseName,
'manager' => $this->manager,
'chunkSizeBytes' => $this->chunkSizeBytes, 'chunkSizeBytes' => $this->chunkSizeBytes,
'readConcern' => $this->readConcern,
'readPreference' => $this->readPreference,
'writeConcern' => $this->writeConcern,
]; ];
} }
......
...@@ -190,6 +190,55 @@ class DatabaseFunctionalTest extends FunctionalTestCase ...@@ -190,6 +190,55 @@ class DatabaseFunctionalTest extends FunctionalTestCase
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW()); $this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
} }
public function testSelectGridFSBucketInheritsOptions()
{
$databaseOptions = [
'readConcern' => new ReadConcern(ReadConcern::LOCAL),
'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
];
$database = new Database($this->manager, $this->getDatabaseName(), $databaseOptions);
$bucket = $database->selectGridFSBucket();
$debug = $bucket->__debugInfo();
$this->assertSame($this->manager, $debug['manager']);
$this->assertSame($this->getDatabaseName(), $debug['databaseName']);
$this->assertSame('fs', $debug['bucketName']);
$this->assertSame(261120, $debug['chunkSizeBytes']);
$this->assertInstanceOf('MongoDB\Driver\ReadConcern', $debug['readConcern']);
$this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
$this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
}
public function testSelectGridFSBucketPassesOptions()
{
$bucketOptions = [
'bucketName' => 'custom_fs',
'chunkSizeBytes' => 8192,
'readConcern' => new ReadConcern(ReadConcern::LOCAL),
'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
];
$database = new Database($this->manager, $this->getDatabaseName());
$bucket = $database->selectGridFSBucket($bucketOptions);
$debug = $bucket->__debugInfo();
$this->assertSame($this->getDatabaseName(), $debug['databaseName']);
$this->assertSame('custom_fs', $debug['bucketName']);
$this->assertSame(8192, $debug['chunkSizeBytes']);
$this->assertInstanceOf('MongoDB\Driver\ReadConcern', $debug['readConcern']);
$this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
$this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
}
public function testWithOptionsInheritsOptions() public function testWithOptionsInheritsOptions()
{ {
$databaseOptions = [ $databaseOptions = [
......
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