Skip to content

Commit

Permalink
Merge pull request #2 from PcComponentes/readings-within-transactions
Browse files Browse the repository at this point in the history
fix: Readings within transactions
  • Loading branch information
zoilomora authored Jul 23, 2020
2 parents b022694 + 54a8003 commit 55788ed
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 35 deletions.
2 changes: 1 addition & 1 deletion phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" ?>
<ruleset name="Project rules">
<file>src</file>
<rule ref="vendor/pccomponentes/coding-standard/PccomponentesCodingStandard/ruleset.xml" />
<rule ref="vendor/pccomponentes/coding-standard/src/ruleset.xml" />
</ruleset>
20 changes: 2 additions & 18 deletions src/Driver/MongoDB/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

namespace PcComponentes\Transaction\Driver\MongoDB;

use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\Session;

final class Client extends \MongoDB\Client
Expand All @@ -13,26 +12,11 @@ final class Client extends \MongoDB\Client
public function __construct(
string $uri = 'mongodb://127.0.0.1/',
array $uriOptions = [],
array $driverOptions = [],
array $transactionOptions = []
array $driverOptions = []
) {
parent::__construct($uri, $uriOptions, $driverOptions);

$this->session = $this->startSession(
[
'defaultTransactionOptions' => \array_merge(
$transactionOptions,
$this->defaultTransactionOptions(),
),
]
);
}

private function defaultTransactionOptions(): array
{
return [
'readPreference' => new ReadPreference(ReadPreference::RP_PRIMARY),
];
$this->session = $this->startSession();
}

public function selectDatabase($databaseName, array $options = []): Database
Expand Down
129 changes: 113 additions & 16 deletions src/Driver/MongoDB/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,47 @@

namespace PcComponentes\Transaction\Driver\MongoDB;

use MongoDB\BSON\JavascriptInterface;
use MongoDB\Driver\Manager;
use MongoDB\Driver\Session;
use MongoDB\Operation\Explainable;

final class Collection extends \MongoDB\Collection
{
private Session $session;

public function __construct(
Manager $manager,
$databaseName,
$collectionName,
Session $session,
array $options = []
) {
public function __construct(Manager $manager, $databaseName, $collectionName, Session $session, array $options = [])
{
parent::__construct(
$manager,
$databaseName,
$collectionName,
$options
$options,
);

$this->session = $session;
}

private function addSession(array $options): array
public function aggregate(array $pipeline, array $options = [])
{
if (false === isset($options['session'])) {
$options['session'] = $this->session;
}

return $options;
return parent::aggregate($pipeline, $this->addSession($options));
}

public function bulkWrite(array $operations, array $options = [])
{
return parent::bulkWrite($operations, $this->addSession($options));
}

public function count($filter = [], array $options = [])
{
return parent::count($filter, $this->addSession($options));
}

public function countDocuments($filter = [], array $options = [])
{
return parent::countDocuments($filter, $this->addSession($options));
}

public function createIndex($key, array $options = [])
{
return parent::createIndex($key, $this->addSession($options));
Expand All @@ -61,9 +64,14 @@ public function deleteOne($filter, array $options = [])
return parent::deleteOne($filter, $this->addSession($options));
}

public function distinct($fieldName, $filter = [], array $options = [])
{
return parent::distinct($fieldName, $filter, $this->addSession($options));
}

public function drop(array $options = [])
{
return parent::drop($this->addSession($options));
return parent::drop($options);
}

public function dropIndex($indexName, array $options = [])
Expand All @@ -73,7 +81,27 @@ public function dropIndex($indexName, array $options = [])

public function dropIndexes(array $options = [])
{
return parent::dropIndexes($this->addSession($options));
return parent::dropIndexes($options);
}

public function estimatedDocumentCount(array $options = [])
{
return parent::estimatedDocumentCount($options);
}

public function explain(Explainable $explainable, array $options = [])
{
return parent::explain($explainable, $this->addSession($options));
}

public function find($filter = [], array $options = [])
{
return parent::find($filter, $this->addSession($options));
}

public function findOne($filter = [], array $options = [])
{
return parent::findOne($filter, $this->addSession($options));
}

public function findOneAndDelete($filter, array $options = [])
Expand All @@ -91,6 +119,46 @@ public function findOneAndUpdate($filter, $update, array $options = [])
return parent::findOneAndUpdate($filter, $update, $this->addSession($options));
}

public function getCollectionName()
{
return parent::getCollectionName();
}

public function getDatabaseName()
{
return parent::getDatabaseName();
}

public function getManager()
{
return parent::getManager();
}

public function getNamespace()
{
return parent::getNamespace();
}

public function getReadConcern()
{
return parent::getReadConcern();
}

public function getReadPreference()
{
return parent::getReadPreference();
}

public function getTypeMap()
{
return parent::getTypeMap();
}

public function getWriteConcern()
{
return parent::getWriteConcern();
}

public function insertMany(array $documents, array $options = [])
{
return parent::insertMany($documents, $this->addSession($options));
Expand All @@ -101,6 +169,16 @@ public function insertOne($document, array $options = [])
return parent::insertOne($document, $this->addSession($options));
}

public function listIndexes(array $options = [])
{
return parent::listIndexes($options);
}

public function mapReduce(JavascriptInterface $map, JavascriptInterface $reduce, $out, array $options = [])
{
return parent::mapReduce($map, $reduce, $out, $this->addSession($options));
}

public function replaceOne($filter, $replacement, array $options = [])
{
return parent::replaceOne($filter, $replacement, $this->addSession($options));
Expand All @@ -115,4 +193,23 @@ public function updateOne($filter, $update, array $options = [])
{
return parent::updateOne($filter, $update, $this->addSession($options));
}

public function watch(array $pipeline = [], array $options = [])
{
return parent::watch($pipeline, $this->addSession($options));
}

public function withOptions(array $options = [])
{
return parent::withOptions($options);
}

private function addSession(array $options): array
{
if (false === isset($options['session'])) {
$options['session'] = $this->session;
}

return $options;
}
}

0 comments on commit 55788ed

Please sign in to comment.