Skip to content

Commit d1bd39a

Browse files
authored
PCBC-950: Support bucket settings for no dedup feature (#131)
* Implement Bucket no deduplication * Fix server version helper function
1 parent d5f9631 commit d1bd39a

14 files changed

+692
-41
lines changed

Couchbase/Management/BucketSettings.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ class BucketSettings
3434
private ?string $compressionMode = null;
3535
private ?string $minimumDurabilityLevel = null;
3636
private ?string $conflictResolutionType = null;
37+
private ?bool $historyRetentionCollectionDefault = null;
38+
private ?int $historyRetentionBytes = null;
39+
private ?int $historyRetentionDuration = null;
3740

3841
/**
3942
* @param string $name the name of the bucket
@@ -225,6 +228,38 @@ public function conflictResolutionType(): ?string
225228
return $this->conflictResolutionType;
226229
}
227230

231+
/**
232+
* Get the default history retention on all collections in this bucket
233+
*
234+
* @return bool|null
235+
* @since 4.1.6
236+
*/
237+
public function historyRetentionCollectionDefault(): ?bool
238+
{
239+
return $this->historyRetentionCollectionDefault;
240+
}
241+
242+
/**
243+
* Get the maximum history retention in bytes on all collections in this bucket
244+
*
245+
* @return int|null
246+
* @since 4.1.6
247+
*/
248+
public function historyRetentionBytes(): ?int
249+
{
250+
return $this->historyRetentionBytes;
251+
}
252+
253+
/**
254+
* Get the maximum duration in seconds to be covered by the change history that is written to disk for all collections in this bucket
255+
* @return int|null
256+
* @since 4.1.6
257+
*/
258+
public function historyRetentionDuration(): ?int
259+
{
260+
return $this->historyRetentionDuration;
261+
}
262+
228263
/**
229264
* Sets the name of the bucket.
230265
*
@@ -475,6 +510,52 @@ public function setConflictResolutionType(string $resolutionType): BucketSetting
475510
return $this;
476511
}
477512

513+
/**
514+
* Sets whether to enable history on collections by default
515+
*
516+
* @param bool $historyRetentionCollectionDefault
517+
*
518+
* @return BucketSettings
519+
*
520+
* @since 4.1.6
521+
*/
522+
public function enableHistoryRetentionCollectionDefault(bool $historyRetentionCollectionDefault): BucketSettings
523+
{
524+
$this->historyRetentionCollectionDefault = $historyRetentionCollectionDefault;
525+
return $this;
526+
}
527+
528+
/**
529+
* Sets the maximum size, in bytes, o the change history that is written to disk for all collections in this bucket
530+
*
531+
* @param int $historyRetentionBytes
532+
*
533+
* @return BucketSettings
534+
*
535+
* @since 4.1.6
536+
*/
537+
public function setHistoryRetentionBytes(int $historyRetentionBytes): BucketSettings
538+
{
539+
$this->historyRetentionBytes = $historyRetentionBytes;
540+
return $this;
541+
}
542+
543+
/**
544+
* Sets teh maximum number of seconds to be covered by the change history that is written to disk for all collections
545+
* in this bucket
546+
*
547+
* @param int $historyRetentionDuration duration in seconds
548+
*
549+
* @return BucketSettings
550+
*
551+
* @since 4.1.6
552+
*/
553+
public function setHistoryRetentionDuration(int $historyRetentionDuration): BucketSettings
554+
{
555+
$this->historyRetentionDuration = $historyRetentionDuration;
556+
return $this;
557+
}
558+
478559
/**
479560
* @internal
480561
* @since 4.0.0
@@ -494,6 +575,9 @@ public static function export(BucketSettings $bucket): array
494575
'compressionMode' => $bucket->compressionMode,
495576
'minimumDurabilityLevel' => $bucket->minimumDurabilityLevel,
496577
'conflictResolutionType' => $bucket->conflictResolutionType,
578+
'historyRetentionCollectionDefault' => $bucket->historyRetentionCollectionDefault,
579+
'historyRetentionBytes' => $bucket->historyRetentionBytes,
580+
'historyRetentionDuration' => $bucket->historyRetentionDuration,
497581
];
498582
}
499583

@@ -537,6 +621,15 @@ public static function import(array $bucket): BucketSettings
537621
if (array_key_exists('storageBackend', $bucket)) {
538622
$settings->setStorageBackend($bucket['storageBackend']);
539623
}
624+
if (array_key_exists('historyRetentionCollectionDefault', $bucket)) {
625+
$settings->enableHistoryRetentionCollectionDefault($bucket['historyRetentionCollectionDefault']);
626+
}
627+
if (array_key_exists('historyRetentionBytes', $bucket)) {
628+
$settings->setHistoryRetentionBytes($bucket['historyRetentionBytes']);
629+
}
630+
if (array_key_exists('historyRetentionDuration', $bucket)) {
631+
$settings->setHistoryRetentionDuration($bucket['historyRetentionDuration']);
632+
}
540633

541634
return $settings;
542635
}

Couchbase/Management/CollectionManager.php

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
namespace Couchbase\Management;
2222

23+
use Couchbase\Exception\InvalidArgumentException;
2324
use Couchbase\Extension;
2425

2526
class CollectionManager
@@ -80,24 +81,69 @@ public function dropScope(string $name, DropScopeOptions $options = null)
8081
/**
8182
* Creates a new collection
8283
*
83-
* @param CollectionSpec $collection The spec of the collection
84+
* Note: The (CollectionSpec, CreateCollectionOptions) API is now deprecated.
85+
*
86+
* @param string|CollectionSpec $scopeName The name of the scope on which to create the collection. Deprecated: CollectionSpec
87+
* @param string|CreateCollectionOptions $collectionName The name of the collection. Deprecated: CreateCollectionOptions
88+
* @param CreateCollectionSettings|null $settings The settings to apply on the collection
8489
* @param CreateCollectionOptions|null $options The options to use when creating a collection
85-
* @since 4.1.3
90+
*
91+
* @throws InvalidArgumentException
92+
* @since 4.1.6
8693
*/
87-
public function createCollection(CollectionSpec $collection, CreateCollectionOptions $options = null)
94+
public function createCollection($scopeName, $collectionName = null, $settings = null, $options = null)
8895
{
89-
Extension\collectionCreate($this->core, $this->bucketName, CollectionSpec::export($collection), CreateCollectionOptions::export($options));
96+
if (is_string($scopeName) && is_null($collectionName)) {
97+
throw new InvalidArgumentException("Collection name cannot be null if using the (scopeName, collectionName, settings, options) API");
98+
}
99+
// Deprecated usage conversion for (CollectionSpec, CreateCollectionOptions)
100+
if ($scopeName instanceof CollectionSpec) {
101+
$options = $collectionName;
102+
$collectionName = $scopeName->name();
103+
$settings = new CreateCollectionSettings($scopeName->maxExpiry(), $scopeName->history());
104+
$scopeName = $scopeName->scopeName();
105+
}
106+
107+
Extension\collectionCreate($this->core, $this->bucketName, $scopeName, $collectionName, CreateCollectionSettings::export($settings), CreateCollectionOptions::export($options));
90108
}
91109

92110
/**
93111
* Drops an existing collection
94112
*
95-
* @param CollectionSpec $collection The spec of the collection to drop
96-
* @param DropCollectionOptions|null $options The options to use when dropping a collection
113+
* Note: The (CollectionSpec, DropCollectionOptions) API is now deprecated.
114+
*
115+
* @param string|CollectionSpec $scopeName The name of the scope on which the collection exists.
116+
* @param string|DropCollectionOptions|null $collectionName The name of the collection. Only nullable to support the deprecated API.
117+
* @param DropcollectionOptions|null $options The options to use when dropping a collection
118+
*
119+
* @throws InvalidArgumentException
97120
* @since 4.1.3
98121
*/
99-
public function dropCollection(CollectionSpec $collection, DropCollectionOptions $options = null)
122+
public function dropCollection($scopeName, $collectionName = null, $options = null)
123+
{
124+
if (is_string($scopeName) && is_null($collectionName)) {
125+
throw new InvalidArgumentException("Collection name cannot be null if using the (scopeName, collectionName, options) API");
126+
}
127+
// Deprecated usage conversion for (CollectionSpec, DropCollectionOptions)
128+
if ($scopeName instanceof CollectionSpec) {
129+
$options = $collectionName;
130+
$collectionName = $scopeName->name();
131+
$scopeName = $scopeName->scopeName();
132+
}
133+
Extension\collectionDrop($this->core, $this->bucketName, $scopeName, $collectionName, DropCollectionOptions::export($options));
134+
}
135+
136+
/**
137+
* Updates an existing collection
138+
*
139+
* @param string $scopeName name of the scope on which the collection exists
140+
* @param string $collectionName collection name
141+
* @param UpdateCollectionSettings $settings Settings to update on the collection
142+
* @param UpdateCollectionOptions|null $options The options to use when updating the collection
143+
* @since 4.1.6
144+
*/
145+
public function updateCollection(string $scopeName, string $collectionName, UpdateCollectionSettings $settings, UpdateCollectionOptions $options = null)
100146
{
101-
Extension\collectionDrop($this->core, $this->bucketName, CollectionSpec::export($collection), DropCollectionOptions::export($options));
147+
Extension\collectionUpdate($this->core, $this->bucketName, $scopeName, $collectionName, UpdateCollectionSettings::export($settings), UpdateBucketOptions::export($options));
102148
}
103149
}

Couchbase/Management/CollectionSpec.php

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,20 @@ class CollectionSpec
2525
private string $name;
2626
private string $scopeName;
2727
private ?int $maxExpiry;
28+
private ?bool $history;
2829

2930
/**
3031
* @param string $name
3132
* @param string $scopeName
3233
* @param int|null $maxExpiry
3334
* @since 4.1.3
3435
*/
35-
public function __construct(string $name, string $scopeName, int $maxExpiry = null)
36+
public function __construct(string $name, string $scopeName, int $maxExpiry = null, bool $history = null)
3637
{
3738
$this->name = $name;
3839
$this->scopeName = $scopeName;
3940
$this->maxExpiry = $maxExpiry;
41+
$this->history = $history;
4042
}
4143

4244
/**
@@ -48,9 +50,9 @@ public function __construct(string $name, string $scopeName, int $maxExpiry = nu
4850
* @return CollectionSpec
4951
* @since 4.1.3
5052
*/
51-
public static function build(string $name, string $scopeName, int $maxExpiry = null): CollectionSpec
53+
public static function build(string $name, string $scopeName, int $maxExpiry = null, bool $history = null): CollectionSpec
5254
{
53-
return new CollectionSpec($name, $scopeName, $maxExpiry);
55+
return new CollectionSpec($name, $scopeName, $maxExpiry, $history);
5456
}
5557

5658
/**
@@ -86,6 +88,18 @@ public function maxExpiry(): ?int
8688
return $this->maxExpiry;
8789
}
8890

91+
/**
92+
* Gets the history retention override setting on this collection.
93+
* Only supported for Magma buckets
94+
*
95+
* @return bool|null
96+
* @since 4.1.6
97+
*/
98+
public function history(): ?bool
99+
{
100+
return $this->history;
101+
}
102+
89103
/**
90104
* Set the name of the collection
91105
*
@@ -124,6 +138,20 @@ public function setMaxExpiry(int $seconds): CollectionSpec
124138
return $this;
125139
}
126140

141+
/**
142+
* Sets the history retention override setting for this collection.
143+
* Only supported for Magma buckets.
144+
*
145+
* @param bool $history
146+
* @return CollectionSpec
147+
* @since 4.1.6
148+
*/
149+
public function setHistory(bool $history): CollectionSpec
150+
{
151+
$this->history = $history;
152+
return $this;
153+
}
154+
127155
/**
128156
* @param CollectionSpec $spec
129157
* @return array
@@ -134,7 +162,8 @@ public static function export(CollectionSpec $spec): array
134162
return [
135163
'name' => $spec->name,
136164
'scopeName' => $spec->scopeName,
137-
'maxExpiry' => $spec->maxExpiry
165+
'maxExpiry' => $spec->maxExpiry,
166+
'history' => $spec->history,
138167
];
139168
}
140169

@@ -149,6 +178,9 @@ public static function import(array $collection): CollectionSpec
149178
if (array_key_exists('maxExpiry', $collection)) {
150179
$collectionSpec->setMaxExpiry($collection['maxExpiry']);
151180
}
181+
if (array_key_exists('history', $collection)) {
182+
$collectionSpec->setHistory($collection['history']);
183+
}
152184
return $collectionSpec;
153185
}
154186
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2014-Present Couchbase, Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
declare(strict_types=1);
20+
21+
namespace Couchbase\Management;
22+
23+
class CreateCollectionSettings
24+
{
25+
private ?int $maxExpiry;
26+
private ?bool $history;
27+
28+
public function __construct(int $maxExpiry = null, bool $history = null)
29+
{
30+
$this->maxExpiry = $maxExpiry;
31+
$this->history = $history;
32+
}
33+
34+
public static function build(int $maxExpiry = null, bool $history = null): CreateCollectionSettings
35+
{
36+
return new CreateCollectionSettings($maxExpiry, $history);
37+
}
38+
39+
/**
40+
* @return int|null
41+
*/
42+
public function maxExpiry(): ?int
43+
{
44+
return $this->maxExpiry;
45+
}
46+
47+
/**
48+
* @return bool|null
49+
*/
50+
public function history(): ?bool
51+
{
52+
return $this->history;
53+
}
54+
55+
public static function export(?CreateCollectionSettings $settings): array
56+
{
57+
if ($settings == null) {
58+
return [];
59+
}
60+
return [
61+
'maxExpiry' => $settings->maxExpiry,
62+
'history' => $settings->history
63+
];
64+
}
65+
}

Couchbase/Management/ScopeSpec.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ public static function import(array $scope): ScopeSpec
118118
foreach ($scope['collections'] as $collection) {
119119
$newColl = new CollectionSpec($collection['name'], $scope['name']);
120120
$newColl->setMaxExpiry($collection['max_expiry']);
121+
if (array_key_exists("history", $collection)) {
122+
$newColl->setHistory($collection['history']);
123+
}
121124
$collections[] = $newColl;
122125
}
123126
return new ScopeSpec($scope['name'], $collections);

0 commit comments

Comments
 (0)