Skip to content

Commit 3fe7c8d

Browse files
authored
PCBC-915: fix timestamp as expiry in mutation options (#88)
1 parent 3886e66 commit 3fe7c8d

File tree

5 files changed

+39
-6
lines changed

5 files changed

+39
-6
lines changed

Couchbase/InsertOptions.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class InsertOptions
2828
private Transcoder $transcoder;
2929
private ?int $timeoutMilliseconds = null;
3030
private ?int $expirySeconds = null;
31+
private ?int $expiryTimestamp = null;
3132
private ?string $durabilityLevel = null;
3233
private ?int $durabilityTimeoutSeconds = null;
3334

@@ -76,7 +77,7 @@ public function timeout(int $milliseconds): InsertOptions
7677
public function expiry($seconds): InsertOptions
7778
{
7879
if ($seconds instanceof DateTimeInterface) {
79-
$this->expirySeconds = $seconds->getTimestamp();
80+
$this->expiryTimestamp = $seconds->getTimestamp();
8081
} else {
8182
$this->expirySeconds = (int)$seconds;
8283
}
@@ -151,6 +152,7 @@ public static function export(?InsertOptions $options): array
151152
return [
152153
'timeoutMilliseconds' => $options->timeoutMilliseconds,
153154
'expirySeconds' => $options->expirySeconds,
155+
'expiryTimestamp' => $options->expiryTimestamp,
154156
'durabilityLevel' => $options->durabilityLevel,
155157
'durabilityTimeoutSeconds' => $options->durabilityTimeoutSeconds,
156158
];

Couchbase/MutateInOptions.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class MutateInOptions
3131
private ?string $durabilityLevel = null;
3232
private ?string $cas = null;
3333
private ?int $expirySeconds = null;
34+
private ?int $expiryTimestamp = null;
3435
private ?bool $preserveExpiry = null;
3536
private string $storeSemantics;
3637

@@ -94,7 +95,7 @@ public function cas(string $cas): MutateInOptions
9495
public function expiry($seconds): MutateInOptions
9596
{
9697
if ($seconds instanceof DateTimeInterface) {
97-
$this->expirySeconds = $seconds->getTimestamp();
98+
$this->expiryTimestamp = $seconds->getTimestamp();
9899
} else {
99100
$this->expirySeconds = (int)$seconds;
100101
}
@@ -201,6 +202,7 @@ public static function export(?MutateInOptions $options): array
201202
return [
202203
'timeoutMilliseconds' => $options->timeoutMilliseconds,
203204
'expirySeconds' => $options->expirySeconds,
205+
'expiryTimestamp' => $options->expiryTimestamp,
204206
'preserveExpiry' => $options->preserveExpiry,
205207
'cas' => $options->cas,
206208
'durabilityLevel' => $options->durabilityLevel,

Couchbase/ReplaceOptions.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class ReplaceOptions
2828
private Transcoder $transcoder;
2929
private ?int $timeoutMilliseconds = null;
3030
private ?int $expirySeconds = null;
31+
private ?int $expiryTimestamp = null;
3132
private ?bool $preserveExpiry = null;
3233
private ?string $durabilityLevel = null;
3334
private ?string $cas = null;
@@ -77,7 +78,7 @@ public function timeout(int $milliseconds): ReplaceOptions
7778
public function expiry($seconds): ReplaceOptions
7879
{
7980
if ($seconds instanceof DateTimeInterface) {
80-
$this->expirySeconds = $seconds->getTimestamp();
81+
$this->expiryTimestamp = $seconds->getTimestamp();
8182
} else {
8283
$this->expirySeconds = (int)$seconds;
8384
}
@@ -179,6 +180,7 @@ public static function export(?ReplaceOptions $options): array
179180
return [
180181
'timeoutMilliseconds' => $options->timeoutMilliseconds,
181182
'expirySeconds' => $options->expirySeconds,
183+
'expiryTimestamp' => $options->expiryTimestamp,
182184
'preserveExpiry' => $options->preserveExpiry,
183185
'durabilityLevel' => $options->durabilityLevel,
184186
'cas' => $options->cas,

src/wrapper/conversion_utilities.hxx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,15 +158,22 @@ cb_set_expiry(Options& opts, const zval* options)
158158
if (auto [e, value] = cb_get_integer<std::uint64_t>(options, "expirySeconds"); e.ec) {
159159
return e;
160160
} else if (value) {
161-
opts.expiry(std::chrono::seconds{ value.value() });
161+
try {
162+
opts.expiry(std::chrono::seconds{ value.value() });
163+
} catch (const std::system_error& ec) {
164+
return { ec.code(), ERROR_LOCATION, ec.what() };
165+
}
162166
return {};
163167
}
164168

165169
if (auto [e, value] = cb_get_integer<std::uint64_t>(options, "expiryTimestamp"); e.ec) {
166170
return e;
167171
} else if (value) {
168-
opts.expiry(std::chrono::system_clock::time_point{ std::chrono::seconds{ value.value() } });
169-
return {};
172+
try {
173+
opts.expiry(std::chrono::system_clock::time_point{ std::chrono::seconds{ value.value() } });
174+
} catch (const std::system_error& ec) {
175+
return { ec.code(), ERROR_LOCATION, ec.what() };
176+
}
170177
}
171178
return {};
172179
}

tests/KeyValueInsertTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
declare(strict_types=1);
2020

2121
use Couchbase\Exception\DocumentExistsException;
22+
use Couchbase\GetOptions;
23+
use Couchbase\InsertOptions;
2224

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

@@ -34,4 +36,22 @@ public function testInsertFailsIfDocumentExistsAlready()
3436
$this->expectException(DocumentExistsException::class);
3537
$collection->insert($id, ["answer" => "foo"]);
3638
}
39+
40+
public function testCanInsertWithExpiry()
41+
{
42+
$expiry = 300;
43+
44+
$options = new InsertOptions();
45+
$expiryDate = (new \DateTimeImmutable())->modify('+' . $expiry . ' seconds');
46+
$options->expiry($expiryDate);
47+
48+
$collection = $this->defaultCollection();
49+
$id = $this->uniqueId();
50+
51+
$collection->insert($id, ["answer" => 42], $options);
52+
53+
$opts = (GetOptions::build())->withExpiry(true);
54+
$res = $collection->get($id, $opts);
55+
$this->assertGreaterThan((new DateTime())->getTimestamp(), $res->expiryTime()->getTimestamp());
56+
}
3757
}

0 commit comments

Comments
 (0)