Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement read and write concerns #40

Merged
merged 5 commits into from
Aug 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/main/php/com/mongodb/Options.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,34 @@ public function readPreference($mode) {
return $this;
}

/**
* Sets read concern (the read isolation level)
*
* @see https://www.mongodb.com/docs/manual/reference/read-concern/
* @param string $level
* @return self
*/
public function readConcern($level) {
$this->pairs['readConcern']= ['level' => $level];
return $this;
}

/**
* Sets write concern (the write acknowledgment)
*
* @see https://www.mongodb.com/docs/manual/reference/write-concern/
* @param string|int $w
* @param ?int $wtimeout
* @param ?bool $journal
* @return self
*/
public function writeConcern($w, $wtimeout= null, $journal= null) {
$this->pairs['writeConcern']= ['w' => $w];
null === $wtimeout || $this->pairs['writeConcern']['wtimeout']= (int)$wtimeout;
null === $journal || $this->pairs['writeConcern']['j']= (bool)$journal;
return $this;
}

/**
* Returns fields to be sent along with the command. Used by Protocol class.
*
Expand Down
37 changes: 37 additions & 0 deletions src/test/php/com/mongodb/unittest/CollectionTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,4 +308,41 @@ public function find_with_read_preference() {
];
Assert::equals($find, $proto->connections()[self::$PRIMARY]->command(-1));
}

#[Test, Values(['local', 'majority'])]
public function find_with_read_concern($level) {
$replies= [self::$PRIMARY => [$this->hello(self::$PRIMARY), $this->cursor([])]];
$proto= $this->protocol($replies, 'primary')->connect();

$coll= new Collection($proto, 'test', 'tests');
$coll->find([], (new Options())->readConcern($level));

$find= [
'find' => 'tests',
'filter' => (object)[],
'$db' => 'test',
'readConcern' => ['level' => $level],
'$readPreference' => ['mode' => 'primary']
];
Assert::equals($find, $proto->connections()[self::$PRIMARY]->command(-1));
}

#[Test, Values([1, 'majority'])]
public function update_with_write_concern($w) {
$replies= [self::$PRIMARY => [$this->hello(self::$PRIMARY), $this->ok()]];
$proto= $this->protocol($replies, 'primary')->connect();

$coll= new Collection($proto, 'test', 'tests');
$coll->update([], [], (new Options())->writeConcern($w));

$find= [
'update' => 'tests',
'updates' => [['u' => [], 'q' => [], 'multi' => true]],
'ordered' => true,
'$db' => 'test',
'writeConcern' => ['w' => $w],
'$readPreference' => ['mode' => 'primary']
];
Assert::equals($find, $proto->connections()[self::$PRIMARY]->command(-1));
}
}
32 changes: 32 additions & 0 deletions src/test/php/com/mongodb/unittest/OptionsTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,36 @@ public function read_preference() {
(new Options())->readPreference('secondary')->send($this->protocol)
);
}

#[Test, Values(['local', 'available'])]
public function read_concern($level) {
Assert::equals(
['readConcern' => ['level' => $level]],
(new Options())->readConcern($level)->send($this->protocol)
);
}

#[Test, Values(['majority', 1])]
public function write_concern($w) {
Assert::equals(
['writeConcern' => ['w' => $w]],
(new Options())->writeConcern($w)->send($this->protocol)
);
}

#[Test]
public function write_concern_with_timeout() {
Assert::equals(
['writeConcern' => ['w' => 2, 'wtimeout' => 10000]],
(new Options())->writeConcern(2, 10000)->send($this->protocol)
);
}

#[Test]
public function write_concern_with_journal() {
Assert::equals(
['writeConcern' => ['w' => 2, 'j' => true]],
(new Options())->writeConcern(2, null, true)->send($this->protocol)
);
}
}
Loading