Skip to content

Commit b814e9a

Browse files
authored
Merge pull request #51 from xp-forge/refactor/lazy
Defer connecting the MongoDB cluster until actual I/O is performed
2 parents e0de808 + 5be7d6c commit b814e9a

File tree

4 files changed

+14
-19
lines changed

4 files changed

+14
-19
lines changed

src/main/php/com/mongodb/MongoConnection.class.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ public function connect(): self {
5959
* @throws com.mongodb.Error
6060
*/
6161
public function run($name, array $arguments= [], $semantics= 'write', Options... $options) {
62-
$this->proto->connect();
63-
6462
$commands= Commands::using($this->proto, $semantics);
6563
return new Run(
6664
$commands,
@@ -76,7 +74,6 @@ public function run($name, array $arguments= [], $semantics= 'write', Options...
7674
* @return com.mongodb.Session
7775
*/
7876
public function session($uuid= null) {
79-
$this->proto->connect();
8077

8178
// From the spec: "Drivers SHOULD generate session IDs locally if possible
8279
// instead of running the startSession command, since running the command
@@ -85,19 +82,18 @@ public function session($uuid= null) {
8582
}
8683

8784
/**
88-
* Selects a given database, connecting if necessary
85+
* Selects a given database.
8986
*
9087
* @param string $name The database name
9188
* @return com.mongodb.Database
9289
* @throws com.mongodb.Error
9390
*/
9491
public function database(string $name): Database {
95-
$this->proto->connect();
9692
return new Database($this->proto, $name);
9793
}
9894

9995
/**
100-
* Selects a given collection in a given database, connecting if necessary
96+
* Selects a given collection in a given database.
10197
*
10298
* @param string... $args A string "database.collection" or two arguments
10399
* @return com.mongodb.Collection
@@ -107,7 +103,6 @@ public function database(string $name): Database {
107103
public function collection(... $args): Collection {
108104
$namespace= implode('.', $args);
109105
if (2 === sscanf($namespace, "%[^.].%[^\r]", $database, $collection)) {
110-
$this->proto->connect();
111106
return new Collection($this->proto, $database, $collection);
112107
}
113108

@@ -124,8 +119,6 @@ public function collection(... $args): Collection {
124119
* @throws com.mongodb.Error
125120
*/
126121
public function databases($filter= null, Options... $options) {
127-
$this->proto->connect();
128-
129122
$request= ['listDatabases' => 1, '$db' => 'admin'];
130123
if (null === $filter) {
131124
// NOOP
@@ -155,8 +148,6 @@ public function databases($filter= null, Options... $options) {
155148
* @throws com.mongodb.Error
156149
*/
157150
public function watch(array $pipeline= [], array $params= [], Options... $options): ChangeStream {
158-
$this->proto->connect();
159-
160151
array_unshift($pipeline, ['$changeStream' => ['allChangesForCluster' => true] + $params]);
161152

162153
$commands= Commands::reading($this->proto);

src/main/php/com/mongodb/io/Commands.class.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public function connection() { return $this->conn; }
2929

3030
/** Creates an instance for reading */
3131
public static function reading(Protocol $proto): self {
32+
$proto->nodes || $proto->connect();
3233
return new self($proto, $proto->establish(
3334
$proto->candidates($proto->readPreference),
3435
'reading with '.$proto->readPreference['mode']
@@ -37,6 +38,7 @@ public static function reading(Protocol $proto): self {
3738

3839
/** Creates an instance for writing */
3940
public static function writing(Protocol $proto): self {
41+
$proto->nodes || $proto->connect();
4042
return new self($proto, $proto->establish(
4143
[$proto->nodes['primary']],
4244
'writing'

src/main/php/com/mongodb/io/Protocol.class.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ public function establish($candidates, $intent) {
237237
* @throws com.mongodb.Error
238238
*/
239239
public function read(array $options, $sections) {
240+
$this->nodes || $this->connect();
240241
foreach ($options as $option) {
241242
$sections+= $option->send($this);
242243
}
@@ -259,6 +260,7 @@ public function read(array $options, $sections) {
259260
* @see https://github.com/mongodb/mongo/blob/master/src/mongo/base/error_codes.yml
260261
*/
261262
public function write(array $options, $sections) {
263+
$this->nodes || $this->connect();
262264
foreach ($options as $option) {
263265
$sections+= $option->send($this);
264266
}

src/test/php/com/mongodb/unittest/MongoConnectionTest.class.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,30 +84,30 @@ public function close_unconnected() {
8484
}
8585

8686
#[Test]
87-
public function connects_when_selecting_database() {
87+
public function selecting_database() {
8888
$protocol= $this->protocol([$this->hello(self::$PRIMARY)]);
8989
$database= (new MongoConnection($protocol))->database('test');
9090

9191
Assert::instance(Database::class, $database);
92-
Assert::true($protocol->connections()[self::$PRIMARY]->connected());
92+
Assert::false($protocol->connections()[self::$PRIMARY]->connected());
9393
}
9494

9595
#[Test]
96-
public function connects_when_selecting_collection_via_namespace() {
96+
public function selecting_collection_via_namespace() {
9797
$protocol= $this->protocol([$this->hello(self::$PRIMARY)]);
9898
$collection= (new MongoConnection($protocol))->collection('test.products');
9999

100100
Assert::instance(Collection::class, $collection);
101-
Assert::true($protocol->connections()[self::$PRIMARY]->connected());
101+
Assert::false($protocol->connections()[self::$PRIMARY]->connected());
102102
}
103103

104104
#[Test]
105-
public function connects_when_selecting_collection_via_args() {
105+
public function selecting_collection_via_args() {
106106
$protocol= $this->protocol([$this->hello(self::$PRIMARY)]);
107107
$collection= (new MongoConnection($protocol))->collection('test', 'products');
108108

109109
Assert::instance(Collection::class, $collection);
110-
Assert::true($protocol->connections()[self::$PRIMARY]->connected());
110+
Assert::false($protocol->connections()[self::$PRIMARY]->connected());
111111
}
112112

113113
#[Test, Expect(IllegalArgumentException::class)]
@@ -116,12 +116,12 @@ public function throws_when_given_non_namespace_name() {
116116
}
117117

118118
#[Test]
119-
public function connects_when_creating_session() {
119+
public function creating_session() {
120120
$protocol= $this->protocol([$this->hello(self::$PRIMARY)]);
121121
$session= (new MongoConnection($protocol))->session();
122122

123123
Assert::instance(Session::class, $session);
124-
Assert::true($protocol->connections()[self::$PRIMARY]->connected());
124+
Assert::false($protocol->connections()[self::$PRIMARY]->connected());
125125
}
126126

127127
#[Test, Values([[null, []], ['test', ['filter' => ['name' => 'test']]], [['name' => 'test'], ['filter' => ['name' => 'test']]]])]

0 commit comments

Comments
 (0)