Skip to content

Commit

Permalink
Defer server selection until executing operation
Browse files Browse the repository at this point in the history
  • Loading branch information
alcaeus committed Jul 27, 2023
1 parent 573951f commit 2570935
Showing 1 changed file with 25 additions and 66 deletions.
91 changes: 25 additions & 66 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,8 @@ public function bulkWrite(array $operations, array $options = [])
);

$operation = new BulkWrite($this->databaseName, $this->collectionName, $operations, $options);
$server = select_server($this->manager, $options);

return $operation->execute($server);
return $operation->execute(select_server($this->manager, $options));
}

/**
Expand All @@ -286,11 +285,9 @@ public function count($filter = [], array $options = [])
{
$options = $this->inheritReadOptions($options);

$server = select_server($this->manager, $options);

$operation = new Count($this->databaseName, $this->collectionName, $filter, $options);

return $operation->execute($server);
return $operation->execute(select_server($this->manager, $options));
}

/**
Expand All @@ -309,11 +306,9 @@ public function countDocuments($filter = [], array $options = [])
{
$options = $this->inheritReadOptions($options);

$server = select_server($this->manager, $options);

$operation = new CountDocuments($this->databaseName, $this->collectionName, $filter, $options);

return $operation->execute($server);
return $operation->execute(select_server($this->manager, $options));
}

/**
Expand Down Expand Up @@ -367,13 +362,11 @@ public function createIndex($key, array $options = [])
*/
public function createIndexes(array $indexes, array $options = [])
{
$server = select_server($this->manager, $options);

$options = $this->inheritWriteOptions($options);

$operation = new CreateIndexes($this->databaseName, $this->collectionName, $indexes, $options);

return $operation->execute($server);
return $operation->execute(select_server($this->manager, $options));
}

/**
Expand All @@ -393,9 +386,8 @@ public function deleteMany($filter, array $options = [])
$options = $this->inheritWriteOptions($options);

$operation = new DeleteMany($this->databaseName, $this->collectionName, $filter, $options);
$server = select_server($this->manager, $options);

return $operation->execute($server);
return $operation->execute(select_server($this->manager, $options));
}

/**
Expand All @@ -415,9 +407,8 @@ public function deleteOne($filter, array $options = [])
$options = $this->inheritWriteOptions($options);

$operation = new DeleteOne($this->databaseName, $this->collectionName, $filter, $options);
$server = select_server($this->manager, $options);

return $operation->execute($server);
return $operation->execute(select_server($this->manager, $options));
}

/**
Expand All @@ -439,11 +430,9 @@ public function distinct(string $fieldName, $filter = [], array $options = [])
$this->inheritCodec($options),
);

$server = select_server($this->manager, $options);

$operation = new Distinct($this->databaseName, $this->collectionName, $fieldName, $filter, $options);

return $operation->execute($server);
return $operation->execute(select_server($this->manager, $options));
}

/**
Expand Down Expand Up @@ -495,15 +484,13 @@ public function dropIndex($indexName, array $options = [])
throw new InvalidArgumentException('dropIndexes() must be used to drop multiple indexes');
}

$server = select_server($this->manager, $options);

$options = $this->inheritWriteOptions(
$this->inheritCodec($options),
);

$operation = new DropIndexes($this->databaseName, $this->collectionName, $indexName, $options);

return $operation->execute($server);
return $operation->execute(select_server($this->manager, $options));
}

/**
Expand All @@ -518,15 +505,13 @@ public function dropIndex($indexName, array $options = [])
*/
public function dropIndexes(array $options = [])
{
$server = select_server($this->manager, $options);

$options = $this->inheritWriteOptions(
$this->inheritCodec($options),
);

$operation = new DropIndexes($this->databaseName, $this->collectionName, '*', $options);

return $operation->execute($server);
return $operation->execute(select_server($this->manager, $options));
}

/**
Expand All @@ -544,11 +529,9 @@ public function estimatedDocumentCount(array $options = [])
{
$options = $this->inheritReadOptions($options);

$server = select_server($this->manager, $options);

$operation = new EstimatedDocumentCount($this->databaseName, $this->collectionName, $options);

return $operation->execute($server);
return $operation->execute(select_server($this->manager, $options));
}

/**
Expand All @@ -571,11 +554,9 @@ public function explain(Explainable $explainable, array $options = [])

$options = $this->inheritCodec($options);

$server = select_server($this->manager, $options);

$operation = new Explain($this->databaseName, $explainable, $options);

return $operation->execute($server);
return $operation->execute(select_server($this->manager, $options));
}

/**
Expand All @@ -596,11 +577,9 @@ public function find($filter = [], array $options = [])
$this->inheritCodecOrTypeMap($options),
);

$server = select_server($this->manager, $options);

$operation = new Find($this->databaseName, $this->collectionName, $filter, $options);

return $operation->execute($server);
return $operation->execute(select_server($this->manager, $options));
}

/**
Expand All @@ -621,11 +600,9 @@ public function findOne($filter = [], array $options = [])
$this->inheritCodecOrTypeMap($options),
);

$server = select_server($this->manager, $options);

$operation = new FindOne($this->databaseName, $this->collectionName, $filter, $options);

return $operation->execute($server);
return $operation->execute(select_server($this->manager, $options));
}

/**
Expand All @@ -645,15 +622,13 @@ public function findOne($filter = [], array $options = [])
*/
public function findOneAndDelete($filter, array $options = [])
{
$server = select_server($this->manager, $options);

$options = $this->inheritWriteOptions(
$this->inheritCodecOrTypeMap($options),
);

$operation = new FindOneAndDelete($this->databaseName, $this->collectionName, $filter, $options);

return $operation->execute($server);
return $operation->execute(select_server($this->manager, $options));
}

/**
Expand All @@ -678,15 +653,13 @@ public function findOneAndDelete($filter, array $options = [])
*/
public function findOneAndReplace($filter, $replacement, array $options = [])
{
$server = select_server($this->manager, $options);

$options = $this->inheritWriteOptions(
$this->inheritCodecOrTypeMap($options),
);

$operation = new FindOneAndReplace($this->databaseName, $this->collectionName, $filter, $replacement, $options);

return $operation->execute($server);
return $operation->execute(select_server($this->manager, $options));
}

/**
Expand All @@ -711,15 +684,13 @@ public function findOneAndReplace($filter, $replacement, array $options = [])
*/
public function findOneAndUpdate($filter, $update, array $options = [])
{
$server = select_server($this->manager, $options);

$options = $this->inheritWriteOptions(
$this->inheritCodecOrTypeMap($options),
);

$operation = new FindOneAndUpdate($this->databaseName, $this->collectionName, $filter, $update, $options);

return $operation->execute($server);
return $operation->execute(select_server($this->manager, $options));
}

/**
Expand Down Expand Up @@ -823,9 +794,8 @@ public function insertMany(array $documents, array $options = [])
);

$operation = new InsertMany($this->databaseName, $this->collectionName, $documents, $options);
$server = select_server($this->manager, $options);

return $operation->execute($server);
return $operation->execute(select_server($this->manager, $options));
}

/**
Expand All @@ -846,9 +816,8 @@ public function insertOne($document, array $options = [])
);

$operation = new InsertOne($this->databaseName, $this->collectionName, $document, $options);
$server = select_server($this->manager, $options);

return $operation->execute($server);
return $operation->execute(select_server($this->manager, $options));
}

/**
Expand All @@ -862,9 +831,8 @@ public function insertOne($document, array $options = [])
public function listIndexes(array $options = [])
{
$operation = new ListIndexes($this->databaseName, $this->collectionName, $options);
$server = select_server($this->manager, $options);

return $operation->execute($server);
return $operation->execute(select_server($this->manager, $options));
}

/**
Expand Down Expand Up @@ -895,8 +863,6 @@ public function mapReduce(JavascriptInterface $map, JavascriptInterface $reduce,
$options['readPreference'] = new ReadPreference(ReadPreference::PRIMARY);
}

$server = select_server($this->manager, $options);

/* A "majority" read concern is not compatible with inline output, so
* avoid providing the Collection's read concern if it would conflict.
*
Expand All @@ -912,7 +878,7 @@ public function mapReduce(JavascriptInterface $map, JavascriptInterface $reduce,

$operation = new MapReduce($this->databaseName, $this->collectionName, $map, $reduce, $out, $options);

return $operation->execute($server);
return $operation->execute(select_server($this->manager, $options));
}

/**
Expand All @@ -933,15 +899,13 @@ public function rename(string $toCollectionName, ?string $toDatabaseName = null,
$toDatabaseName = $this->databaseName;
}

$server = select_server($this->manager, $options);

$options = $this->inheritWriteOptions(
$this->inheritCodec($options),
);

$operation = new RenameCollection($this->databaseName, $this->collectionName, $toDatabaseName, $toCollectionName, $options);

return $operation->execute($server);
return $operation->execute(select_server($this->manager, $options));
}

/**
Expand All @@ -964,9 +928,8 @@ public function replaceOne($filter, $replacement, array $options = [])
);

$operation = new ReplaceOne($this->databaseName, $this->collectionName, $filter, $replacement, $options);
$server = select_server($this->manager, $options);

return $operation->execute($server);
return $operation->execute(select_server($this->manager, $options));
}

/**
Expand All @@ -987,9 +950,8 @@ public function updateMany($filter, $update, array $options = [])
$options = $this->inheritWriteOptions($options);

$operation = new UpdateMany($this->databaseName, $this->collectionName, $filter, $update, $options);
$server = select_server($this->manager, $options);

return $operation->execute($server);
return $operation->execute(select_server($this->manager, $options));
}

/**
Expand All @@ -1010,9 +972,8 @@ public function updateOne($filter, $update, array $options = [])
$options = $this->inheritWriteOptions($options);

$operation = new UpdateOne($this->databaseName, $this->collectionName, $filter, $update, $options);
$server = select_server($this->manager, $options);

return $operation->execute($server);
return $operation->execute(select_server($this->manager, $options));
}

/**
Expand All @@ -1030,11 +991,9 @@ public function watch(array $pipeline = [], array $options = [])
$this->inheritCodecOrTypeMap($options),
);

$server = select_server($this->manager, $options);

$operation = new Watch($this->manager, $this->databaseName, $this->collectionName, $pipeline, $options);

return $operation->execute($server);
return $operation->execute(select_server($this->manager, $options));
}

/**
Expand Down

0 comments on commit 2570935

Please sign in to comment.