Skip to content

PCBC-915: fix timestamp as expiry in mutation options #88

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

Merged
merged 1 commit into from
Mar 28, 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
4 changes: 3 additions & 1 deletion Couchbase/InsertOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class InsertOptions
private Transcoder $transcoder;
private ?int $timeoutMilliseconds = null;
private ?int $expirySeconds = null;
private ?int $expiryTimestamp = null;
private ?string $durabilityLevel = null;
private ?int $durabilityTimeoutSeconds = null;

Expand Down Expand Up @@ -76,7 +77,7 @@ public function timeout(int $milliseconds): InsertOptions
public function expiry($seconds): InsertOptions
{
if ($seconds instanceof DateTimeInterface) {
$this->expirySeconds = $seconds->getTimestamp();
$this->expiryTimestamp = $seconds->getTimestamp();
} else {
$this->expirySeconds = (int)$seconds;
}
Expand Down Expand Up @@ -151,6 +152,7 @@ public static function export(?InsertOptions $options): array
return [
'timeoutMilliseconds' => $options->timeoutMilliseconds,
'expirySeconds' => $options->expirySeconds,
'expiryTimestamp' => $options->expiryTimestamp,
'durabilityLevel' => $options->durabilityLevel,
'durabilityTimeoutSeconds' => $options->durabilityTimeoutSeconds,
];
Expand Down
4 changes: 3 additions & 1 deletion Couchbase/MutateInOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class MutateInOptions
private ?string $durabilityLevel = null;
private ?string $cas = null;
private ?int $expirySeconds = null;
private ?int $expiryTimestamp = null;
private ?bool $preserveExpiry = null;
private string $storeSemantics;

Expand Down Expand Up @@ -94,7 +95,7 @@ public function cas(string $cas): MutateInOptions
public function expiry($seconds): MutateInOptions
{
if ($seconds instanceof DateTimeInterface) {
$this->expirySeconds = $seconds->getTimestamp();
$this->expiryTimestamp = $seconds->getTimestamp();
} else {
$this->expirySeconds = (int)$seconds;
}
Expand Down Expand Up @@ -201,6 +202,7 @@ public static function export(?MutateInOptions $options): array
return [
'timeoutMilliseconds' => $options->timeoutMilliseconds,
'expirySeconds' => $options->expirySeconds,
'expiryTimestamp' => $options->expiryTimestamp,
'preserveExpiry' => $options->preserveExpiry,
'cas' => $options->cas,
'durabilityLevel' => $options->durabilityLevel,
Expand Down
4 changes: 3 additions & 1 deletion Couchbase/ReplaceOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class ReplaceOptions
private Transcoder $transcoder;
private ?int $timeoutMilliseconds = null;
private ?int $expirySeconds = null;
private ?int $expiryTimestamp = null;
private ?bool $preserveExpiry = null;
private ?string $durabilityLevel = null;
private ?string $cas = null;
Expand Down Expand Up @@ -77,7 +78,7 @@ public function timeout(int $milliseconds): ReplaceOptions
public function expiry($seconds): ReplaceOptions
{
if ($seconds instanceof DateTimeInterface) {
$this->expirySeconds = $seconds->getTimestamp();
$this->expiryTimestamp = $seconds->getTimestamp();
} else {
$this->expirySeconds = (int)$seconds;
}
Expand Down Expand Up @@ -179,6 +180,7 @@ public static function export(?ReplaceOptions $options): array
return [
'timeoutMilliseconds' => $options->timeoutMilliseconds,
'expirySeconds' => $options->expirySeconds,
'expiryTimestamp' => $options->expiryTimestamp,
'preserveExpiry' => $options->preserveExpiry,
'durabilityLevel' => $options->durabilityLevel,
'cas' => $options->cas,
Expand Down
13 changes: 10 additions & 3 deletions src/wrapper/conversion_utilities.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,22 @@ cb_set_expiry(Options& opts, const zval* options)
if (auto [e, value] = cb_get_integer<std::uint64_t>(options, "expirySeconds"); e.ec) {
return e;
} else if (value) {
opts.expiry(std::chrono::seconds{ value.value() });
try {
opts.expiry(std::chrono::seconds{ value.value() });
} catch (const std::system_error& ec) {
return { ec.code(), ERROR_LOCATION, ec.what() };
}
return {};
}

if (auto [e, value] = cb_get_integer<std::uint64_t>(options, "expiryTimestamp"); e.ec) {
return e;
} else if (value) {
opts.expiry(std::chrono::system_clock::time_point{ std::chrono::seconds{ value.value() } });
return {};
try {
opts.expiry(std::chrono::system_clock::time_point{ std::chrono::seconds{ value.value() } });
} catch (const std::system_error& ec) {
return { ec.code(), ERROR_LOCATION, ec.what() };
}
}
return {};
}
Expand Down
20 changes: 20 additions & 0 deletions tests/KeyValueInsertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
declare(strict_types=1);

use Couchbase\Exception\DocumentExistsException;
use Couchbase\GetOptions;
use Couchbase\InsertOptions;

include_once __DIR__ . "/Helpers/CouchbaseTestCase.php";

Expand All @@ -34,4 +36,22 @@ public function testInsertFailsIfDocumentExistsAlready()
$this->expectException(DocumentExistsException::class);
$collection->insert($id, ["answer" => "foo"]);
}

public function testCanInsertWithExpiry()
{
$expiry = 300;

$options = new InsertOptions();
$expiryDate = (new \DateTimeImmutable())->modify('+' . $expiry . ' seconds');
$options->expiry($expiryDate);

$collection = $this->defaultCollection();
$id = $this->uniqueId();

$collection->insert($id, ["answer" => 42], $options);

$opts = (GetOptions::build())->withExpiry(true);
$res = $collection->get($id, $opts);
$this->assertGreaterThan((new DateTime())->getTimestamp(), $res->expiryTime()->getTimestamp());
}
}