Skip to content

PCBC-950: Support bucket settings for no dedup feature #131

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 2 commits into from
Sep 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
93 changes: 93 additions & 0 deletions Couchbase/Management/BucketSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class BucketSettings
private ?string $compressionMode = null;
private ?string $minimumDurabilityLevel = null;
private ?string $conflictResolutionType = null;
private ?bool $historyRetentionCollectionDefault = null;
private ?int $historyRetentionBytes = null;
private ?int $historyRetentionDuration = null;

/**
* @param string $name the name of the bucket
Expand Down Expand Up @@ -225,6 +228,38 @@ public function conflictResolutionType(): ?string
return $this->conflictResolutionType;
}

/**
* Get the default history retention on all collections in this bucket
*
* @return bool|null
* @since 4.1.6
*/
public function historyRetentionCollectionDefault(): ?bool
{
return $this->historyRetentionCollectionDefault;
}

/**
* Get the maximum history retention in bytes on all collections in this bucket
*
* @return int|null
* @since 4.1.6
*/
public function historyRetentionBytes(): ?int
{
return $this->historyRetentionBytes;
}

/**
* Get the maximum duration in seconds to be covered by the change history that is written to disk for all collections in this bucket
* @return int|null
* @since 4.1.6
*/
public function historyRetentionDuration(): ?int
{
return $this->historyRetentionDuration;
}

/**
* Sets the name of the bucket.
*
Expand Down Expand Up @@ -475,6 +510,52 @@ public function setConflictResolutionType(string $resolutionType): BucketSetting
return $this;
}

/**
* Sets whether to enable history on collections by default
*
* @param bool $historyRetentionCollectionDefault
*
* @return BucketSettings
*
* @since 4.1.6
*/
public function enableHistoryRetentionCollectionDefault(bool $historyRetentionCollectionDefault): BucketSettings
{
$this->historyRetentionCollectionDefault = $historyRetentionCollectionDefault;
return $this;
}

/**
* Sets the maximum size, in bytes, o the change history that is written to disk for all collections in this bucket
*
* @param int $historyRetentionBytes
*
* @return BucketSettings
*
* @since 4.1.6
*/
public function setHistoryRetentionBytes(int $historyRetentionBytes): BucketSettings
{
$this->historyRetentionBytes = $historyRetentionBytes;
return $this;
}

/**
* Sets teh maximum number of seconds to be covered by the change history that is written to disk for all collections
* in this bucket
*
* @param int $historyRetentionDuration duration in seconds
*
* @return BucketSettings
*
* @since 4.1.6
*/
public function setHistoryRetentionDuration(int $historyRetentionDuration): BucketSettings
{
$this->historyRetentionDuration = $historyRetentionDuration;
return $this;
}

/**
* @internal
* @since 4.0.0
Expand All @@ -494,6 +575,9 @@ public static function export(BucketSettings $bucket): array
'compressionMode' => $bucket->compressionMode,
'minimumDurabilityLevel' => $bucket->minimumDurabilityLevel,
'conflictResolutionType' => $bucket->conflictResolutionType,
'historyRetentionCollectionDefault' => $bucket->historyRetentionCollectionDefault,
'historyRetentionBytes' => $bucket->historyRetentionBytes,
'historyRetentionDuration' => $bucket->historyRetentionDuration,
];
}

Expand Down Expand Up @@ -537,6 +621,15 @@ public static function import(array $bucket): BucketSettings
if (array_key_exists('storageBackend', $bucket)) {
$settings->setStorageBackend($bucket['storageBackend']);
}
if (array_key_exists('historyRetentionCollectionDefault', $bucket)) {
$settings->enableHistoryRetentionCollectionDefault($bucket['historyRetentionCollectionDefault']);
}
if (array_key_exists('historyRetentionBytes', $bucket)) {
$settings->setHistoryRetentionBytes($bucket['historyRetentionBytes']);
}
if (array_key_exists('historyRetentionDuration', $bucket)) {
$settings->setHistoryRetentionDuration($bucket['historyRetentionDuration']);
}

return $settings;
}
Expand Down
62 changes: 54 additions & 8 deletions Couchbase/Management/CollectionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

namespace Couchbase\Management;

use Couchbase\Exception\InvalidArgumentException;
use Couchbase\Extension;

class CollectionManager
Expand Down Expand Up @@ -80,24 +81,69 @@ public function dropScope(string $name, DropScopeOptions $options = null)
/**
* Creates a new collection
*
* @param CollectionSpec $collection The spec of the collection
* Note: The (CollectionSpec, CreateCollectionOptions) API is now deprecated.
*
* @param string|CollectionSpec $scopeName The name of the scope on which to create the collection. Deprecated: CollectionSpec
* @param string|CreateCollectionOptions $collectionName The name of the collection. Deprecated: CreateCollectionOptions
* @param CreateCollectionSettings|null $settings The settings to apply on the collection
* @param CreateCollectionOptions|null $options The options to use when creating a collection
* @since 4.1.3
*
* @throws InvalidArgumentException
* @since 4.1.6
*/
public function createCollection(CollectionSpec $collection, CreateCollectionOptions $options = null)
public function createCollection($scopeName, $collectionName = null, $settings = null, $options = null)
{
Extension\collectionCreate($this->core, $this->bucketName, CollectionSpec::export($collection), CreateCollectionOptions::export($options));
if (is_string($scopeName) && is_null($collectionName)) {
throw new InvalidArgumentException("Collection name cannot be null if using the (scopeName, collectionName, settings, options) API");
}
// Deprecated usage conversion for (CollectionSpec, CreateCollectionOptions)
if ($scopeName instanceof CollectionSpec) {
$options = $collectionName;
$collectionName = $scopeName->name();
$settings = new CreateCollectionSettings($scopeName->maxExpiry(), $scopeName->history());
$scopeName = $scopeName->scopeName();
}

Extension\collectionCreate($this->core, $this->bucketName, $scopeName, $collectionName, CreateCollectionSettings::export($settings), CreateCollectionOptions::export($options));
}

/**
* Drops an existing collection
*
* @param CollectionSpec $collection The spec of the collection to drop
* @param DropCollectionOptions|null $options The options to use when dropping a collection
* Note: The (CollectionSpec, DropCollectionOptions) API is now deprecated.
*
* @param string|CollectionSpec $scopeName The name of the scope on which the collection exists.
* @param string|DropCollectionOptions|null $collectionName The name of the collection. Only nullable to support the deprecated API.
* @param DropcollectionOptions|null $options The options to use when dropping a collection
*
* @throws InvalidArgumentException
* @since 4.1.3
*/
public function dropCollection(CollectionSpec $collection, DropCollectionOptions $options = null)
public function dropCollection($scopeName, $collectionName = null, $options = null)
{
if (is_string($scopeName) && is_null($collectionName)) {
throw new InvalidArgumentException("Collection name cannot be null if using the (scopeName, collectionName, options) API");
}
// Deprecated usage conversion for (CollectionSpec, DropCollectionOptions)
if ($scopeName instanceof CollectionSpec) {
$options = $collectionName;
$collectionName = $scopeName->name();
$scopeName = $scopeName->scopeName();
}
Extension\collectionDrop($this->core, $this->bucketName, $scopeName, $collectionName, DropCollectionOptions::export($options));
}

/**
* Updates an existing collection
*
* @param string $scopeName name of the scope on which the collection exists
* @param string $collectionName collection name
* @param UpdateCollectionSettings $settings Settings to update on the collection
* @param UpdateCollectionOptions|null $options The options to use when updating the collection
* @since 4.1.6
*/
public function updateCollection(string $scopeName, string $collectionName, UpdateCollectionSettings $settings, UpdateCollectionOptions $options = null)
{
Extension\collectionDrop($this->core, $this->bucketName, CollectionSpec::export($collection), DropCollectionOptions::export($options));
Extension\collectionUpdate($this->core, $this->bucketName, $scopeName, $collectionName, UpdateCollectionSettings::export($settings), UpdateBucketOptions::export($options));
}
}
40 changes: 36 additions & 4 deletions Couchbase/Management/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,20 @@ class CollectionSpec
private string $name;
private string $scopeName;
private ?int $maxExpiry;
private ?bool $history;

/**
* @param string $name
* @param string $scopeName
* @param int|null $maxExpiry
* @since 4.1.3
*/
public function __construct(string $name, string $scopeName, int $maxExpiry = null)
public function __construct(string $name, string $scopeName, int $maxExpiry = null, bool $history = null)
{
$this->name = $name;
$this->scopeName = $scopeName;
$this->maxExpiry = $maxExpiry;
$this->history = $history;
}

/**
Expand All @@ -48,9 +50,9 @@ public function __construct(string $name, string $scopeName, int $maxExpiry = nu
* @return CollectionSpec
* @since 4.1.3
*/
public static function build(string $name, string $scopeName, int $maxExpiry = null): CollectionSpec
public static function build(string $name, string $scopeName, int $maxExpiry = null, bool $history = null): CollectionSpec
{
return new CollectionSpec($name, $scopeName, $maxExpiry);
return new CollectionSpec($name, $scopeName, $maxExpiry, $history);
}

/**
Expand Down Expand Up @@ -86,6 +88,18 @@ public function maxExpiry(): ?int
return $this->maxExpiry;
}

/**
* Gets the history retention override setting on this collection.
* Only supported for Magma buckets
*
* @return bool|null
* @since 4.1.6
*/
public function history(): ?bool
{
return $this->history;
}

/**
* Set the name of the collection
*
Expand Down Expand Up @@ -124,6 +138,20 @@ public function setMaxExpiry(int $seconds): CollectionSpec
return $this;
}

/**
* Sets the history retention override setting for this collection.
* Only supported for Magma buckets.
*
* @param bool $history
* @return CollectionSpec
* @since 4.1.6
*/
public function setHistory(bool $history): CollectionSpec
{
$this->history = $history;
return $this;
}

/**
* @param CollectionSpec $spec
* @return array
Expand All @@ -134,7 +162,8 @@ public static function export(CollectionSpec $spec): array
return [
'name' => $spec->name,
'scopeName' => $spec->scopeName,
'maxExpiry' => $spec->maxExpiry
'maxExpiry' => $spec->maxExpiry,
'history' => $spec->history,
];
}

Expand All @@ -149,6 +178,9 @@ public static function import(array $collection): CollectionSpec
if (array_key_exists('maxExpiry', $collection)) {
$collectionSpec->setMaxExpiry($collection['maxExpiry']);
}
if (array_key_exists('history', $collection)) {
$collectionSpec->setHistory($collection['history']);
}
return $collectionSpec;
}
}
65 changes: 65 additions & 0 deletions Couchbase/Management/CreateCollectionSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

/**
* Copyright 2014-Present Couchbase, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

declare(strict_types=1);

namespace Couchbase\Management;

class CreateCollectionSettings
{
private ?int $maxExpiry;
private ?bool $history;

public function __construct(int $maxExpiry = null, bool $history = null)
{
$this->maxExpiry = $maxExpiry;
$this->history = $history;
}

public static function build(int $maxExpiry = null, bool $history = null): CreateCollectionSettings
{
return new CreateCollectionSettings($maxExpiry, $history);
}

/**
* @return int|null
*/
public function maxExpiry(): ?int
{
return $this->maxExpiry;
}

/**
* @return bool|null
*/
public function history(): ?bool
{
return $this->history;
}

public static function export(?CreateCollectionSettings $settings): array
{
if ($settings == null) {
return [];
}
return [
'maxExpiry' => $settings->maxExpiry,
'history' => $settings->history
];
}
}
3 changes: 3 additions & 0 deletions Couchbase/Management/ScopeSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ public static function import(array $scope): ScopeSpec
foreach ($scope['collections'] as $collection) {
$newColl = new CollectionSpec($collection['name'], $scope['name']);
$newColl->setMaxExpiry($collection['max_expiry']);
if (array_key_exists("history", $collection)) {
$newColl->setHistory($collection['history']);
}
$collections[] = $newColl;
}
return new ScopeSpec($scope['name'], $collections);
Expand Down
Loading