Skip to content

Commit 2570935

Browse files
committed
Defer server selection until executing operation
1 parent 573951f commit 2570935

File tree

1 file changed

+25
-66
lines changed

1 file changed

+25
-66
lines changed

src/Collection.php

Lines changed: 25 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,8 @@ public function bulkWrite(array $operations, array $options = [])
263263
);
264264

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

268-
return $operation->execute($server);
267+
return $operation->execute(select_server($this->manager, $options));
269268
}
270269

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

289-
$server = select_server($this->manager, $options);
290-
291288
$operation = new Count($this->databaseName, $this->collectionName, $filter, $options);
292289

293-
return $operation->execute($server);
290+
return $operation->execute(select_server($this->manager, $options));
294291
}
295292

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

312-
$server = select_server($this->manager, $options);
313-
314309
$operation = new CountDocuments($this->databaseName, $this->collectionName, $filter, $options);
315310

316-
return $operation->execute($server);
311+
return $operation->execute(select_server($this->manager, $options));
317312
}
318313

319314
/**
@@ -367,13 +362,11 @@ public function createIndex($key, array $options = [])
367362
*/
368363
public function createIndexes(array $indexes, array $options = [])
369364
{
370-
$server = select_server($this->manager, $options);
371-
372365
$options = $this->inheritWriteOptions($options);
373366

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

376-
return $operation->execute($server);
369+
return $operation->execute(select_server($this->manager, $options));
377370
}
378371

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

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

398-
return $operation->execute($server);
390+
return $operation->execute(select_server($this->manager, $options));
399391
}
400392

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

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

420-
return $operation->execute($server);
411+
return $operation->execute(select_server($this->manager, $options));
421412
}
422413

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

442-
$server = select_server($this->manager, $options);
443-
444433
$operation = new Distinct($this->databaseName, $this->collectionName, $fieldName, $filter, $options);
445434

446-
return $operation->execute($server);
435+
return $operation->execute(select_server($this->manager, $options));
447436
}
448437

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

498-
$server = select_server($this->manager, $options);
499-
500487
$options = $this->inheritWriteOptions(
501488
$this->inheritCodec($options),
502489
);
503490

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

506-
return $operation->execute($server);
493+
return $operation->execute(select_server($this->manager, $options));
507494
}
508495

509496
/**
@@ -518,15 +505,13 @@ public function dropIndex($indexName, array $options = [])
518505
*/
519506
public function dropIndexes(array $options = [])
520507
{
521-
$server = select_server($this->manager, $options);
522-
523508
$options = $this->inheritWriteOptions(
524509
$this->inheritCodec($options),
525510
);
526511

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

529-
return $operation->execute($server);
514+
return $operation->execute(select_server($this->manager, $options));
530515
}
531516

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

547-
$server = select_server($this->manager, $options);
548-
549532
$operation = new EstimatedDocumentCount($this->databaseName, $this->collectionName, $options);
550533

551-
return $operation->execute($server);
534+
return $operation->execute(select_server($this->manager, $options));
552535
}
553536

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

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

574-
$server = select_server($this->manager, $options);
575-
576557
$operation = new Explain($this->databaseName, $explainable, $options);
577558

578-
return $operation->execute($server);
559+
return $operation->execute(select_server($this->manager, $options));
579560
}
580561

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

599-
$server = select_server($this->manager, $options);
600-
601580
$operation = new Find($this->databaseName, $this->collectionName, $filter, $options);
602581

603-
return $operation->execute($server);
582+
return $operation->execute(select_server($this->manager, $options));
604583
}
605584

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

624-
$server = select_server($this->manager, $options);
625-
626603
$operation = new FindOne($this->databaseName, $this->collectionName, $filter, $options);
627604

628-
return $operation->execute($server);
605+
return $operation->execute(select_server($this->manager, $options));
629606
}
630607

631608
/**
@@ -645,15 +622,13 @@ public function findOne($filter = [], array $options = [])
645622
*/
646623
public function findOneAndDelete($filter, array $options = [])
647624
{
648-
$server = select_server($this->manager, $options);
649-
650625
$options = $this->inheritWriteOptions(
651626
$this->inheritCodecOrTypeMap($options),
652627
);
653628

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

656-
return $operation->execute($server);
631+
return $operation->execute(select_server($this->manager, $options));
657632
}
658633

659634
/**
@@ -678,15 +653,13 @@ public function findOneAndDelete($filter, array $options = [])
678653
*/
679654
public function findOneAndReplace($filter, $replacement, array $options = [])
680655
{
681-
$server = select_server($this->manager, $options);
682-
683656
$options = $this->inheritWriteOptions(
684657
$this->inheritCodecOrTypeMap($options),
685658
);
686659

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

689-
return $operation->execute($server);
662+
return $operation->execute(select_server($this->manager, $options));
690663
}
691664

692665
/**
@@ -711,15 +684,13 @@ public function findOneAndReplace($filter, $replacement, array $options = [])
711684
*/
712685
public function findOneAndUpdate($filter, $update, array $options = [])
713686
{
714-
$server = select_server($this->manager, $options);
715-
716687
$options = $this->inheritWriteOptions(
717688
$this->inheritCodecOrTypeMap($options),
718689
);
719690

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

722-
return $operation->execute($server);
693+
return $operation->execute(select_server($this->manager, $options));
723694
}
724695

725696
/**
@@ -823,9 +794,8 @@ public function insertMany(array $documents, array $options = [])
823794
);
824795

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

828-
return $operation->execute($server);
798+
return $operation->execute(select_server($this->manager, $options));
829799
}
830800

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

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

851-
return $operation->execute($server);
820+
return $operation->execute(select_server($this->manager, $options));
852821
}
853822

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

867-
return $operation->execute($server);
835+
return $operation->execute(select_server($this->manager, $options));
868836
}
869837

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

898-
$server = select_server($this->manager, $options);
899-
900866
/* A "majority" read concern is not compatible with inline output, so
901867
* avoid providing the Collection's read concern if it would conflict.
902868
*
@@ -912,7 +878,7 @@ public function mapReduce(JavascriptInterface $map, JavascriptInterface $reduce,
912878

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

915-
return $operation->execute($server);
881+
return $operation->execute(select_server($this->manager, $options));
916882
}
917883

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

936-
$server = select_server($this->manager, $options);
937-
938902
$options = $this->inheritWriteOptions(
939903
$this->inheritCodec($options),
940904
);
941905

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

944-
return $operation->execute($server);
908+
return $operation->execute(select_server($this->manager, $options));
945909
}
946910

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

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

969-
return $operation->execute($server);
932+
return $operation->execute(select_server($this->manager, $options));
970933
}
971934

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

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

992-
return $operation->execute($server);
954+
return $operation->execute(select_server($this->manager, $options));
993955
}
994956

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

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

1015-
return $operation->execute($server);
976+
return $operation->execute(select_server($this->manager, $options));
1016977
}
1017978

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

1033-
$server = select_server($this->manager, $options);
1034-
1035994
$operation = new Watch($this->manager, $this->databaseName, $this->collectionName, $pipeline, $options);
1036995

1037-
return $operation->execute($server);
996+
return $operation->execute(select_server($this->manager, $options));
1038997
}
1039998

1040999
/**

0 commit comments

Comments
 (0)