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

PCBC-828 implement collection management #89

Merged
merged 3 commits into from Mar 29, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions Couchbase/Bucket.php
Expand Up @@ -137,12 +137,11 @@ public function viewQuery(string $designDoc, string $viewName, ViewOptions $opti
* Creates a new CollectionManager object for managing collections and scopes.
*
* @return CollectionManager
* @throws UnsupportedOperationException
* @since 4.0.0
*/
public function collections(): CollectionManager
{
throw new UnsupportedOperationException();
return new CollectionManager($this->core, $this->name);
}

/**
Expand Down
67 changes: 61 additions & 6 deletions Couchbase/Management/CollectionManager.php
Expand Up @@ -20,29 +20,84 @@

namespace Couchbase\Management;

use Couchbase\Extension;

class CollectionManager
{
public function getScope(string $name): ScopeSpec
/**
* @var resource
*/
private $core;
private string $bucketName;
public function __construct($core, string $bucketName)
{
$this->core = $core;
$this->bucketName = $bucketName;
}

public function getAllScopes(): array
/**
* Retrieves all scopes within the bucket
*
* @param GetAllScopesOptions|null $options The options to use when retrieving the scopes
*
* @see ScopeSpec
* @return array array of scopes within the bucket
*/
public function getAllScopes(GetAllScopesOptions $options = null): array
{
$result = Extension\scopeGetAll($this->core, $this->bucketName, GetAllScopesOptions::export($options));
$scopes = [];
foreach ($result['scopes'] as $scope) {
$scopes[] = ScopeSpec::import($scope);
}
return $scopes;
}

public function createScope(string $name)
/**
* Create a new scope
*
* @param string $name name of the scope
* @param CreateScopeOptions|null $options the options to use when creating a scope
* @since 4.1.3
*/
public function createScope(string $name, CreateScopeOptions $options = null)
{
Extension\scopeCreate($this->core, $this->bucketName, $name, CreateScopeOptions::export($options));
}

public function dropScope(string $name)
/**
* Drops an existing scope
*
* @param string $name of the scope to drop
* @param DropScopeOptions|null $options the options to use when dropping a scope
* @since 4.1.3
*/
public function dropScope(string $name, DropScopeOptions $options = null)
{
Extension\scopeDrop($this->core, $this->bucketName, $name, DropScopeOptions::export($options));
}

public function createCollection(CollectionSpec $collection)
/**
* Creates a new collection
*
* @param CollectionSpec $collection The spec of the collection
* @param CreateCollectionOptions|null $options The options to use when creating a collection
* @since 4.1.3
*/
public function createCollection(CollectionSpec $collection, CreateCollectionOptions $options = null)
{
Extension\collectionCreate($this->core, $this->bucketName, CollectionSpec::export($collection), CreateCollectionOptions::export($options));
}

public function dropCollection(CollectionSpec $collection)
/**
* 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
* @since 4.1.3
*/
public function dropCollection(CollectionSpec $collection, DropCollectionOptions $options = null)
{
Extension\collectionDrop($this->core, $this->bucketName, CollectionSpec::export($collection), DropCollectionOptions::export($options));
}
}
114 changes: 112 additions & 2 deletions Couchbase/Management/CollectionSpec.php
Expand Up @@ -22,23 +22,133 @@

class CollectionSpec
{
private string $name;
private string $scopeName;
private ?int $maxExpiry;

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

/**
* Static helper to keep code more readable
*
* @param string $name
* @param string $scopeName
* @param int|null $maxExpiry
* @return CollectionSpec
* @since 4.1.3
*/
public static function build(string $name, string $scopeName, int $maxExpiry = null): CollectionSpec
{
return new CollectionSpec($name, $scopeName, $maxExpiry);
}

/**
* Get the name of the collection
*
* @return string collection name
* @since 4.1.3
*/
public function name(): string
{
return $this->name;
}

/**
* Get the name of the scope which the collection belongs to
*
* @return string scope name
* @since 4.1.3
*/
public function scopeName(): string
{
return $this->scopeName;
}

/**
* Get the max expiry of the collection
*
* @return int|null
* @since 4.1.3
*/
public function maxExpiry(): ?int
{
return $this->maxExpiry;
}

/**
* Set the name of the collection
*
* @param string $name collection name
* @return CollectionSpec
* @since 4.1.3
*/
public function setName(string $name): CollectionSpec
{
$this->name = $name;
return $this;
}

/**
* Sets the name of the scope which the collection belongs to
* @param string $scopeName scope name
* @return CollectionSpec
* @since 4.1.3
*/
public function setScopeName(string $scopeName): CollectionSpec
{
$this->scopeName = $scopeName;
return $this;
}

/**
* Sets the max expiry of the collection
*
* @param int $seconds max expiry in seconds
* @return CollectionSpec
* @since 4.1.3
*/
public function setMaxExpiry(int $seconds): CollectionSpec
{
$this->maxExpiry = $seconds;
return $this;
}

public function setScopeName(string $name): CollectionSpec
/**
* @param CollectionSpec $spec
* @return array
* @since 4.1.3
*/
public static function export(CollectionSpec $spec): array
{
return [
'name' => $spec->name,
'scopeName' => $spec->scopeName,
'maxExpiry' => $spec->maxExpiry
];
}

public function setMaxExpiry(int $ms): CollectionSpec
/**
* @param array $collection
* @return CollectionSpec
* @since 4.1.3
*/
public static function import(array $collection): CollectionSpec
{
$collectionSpec = new CollectionSpec($collection['name'], $collection['scopeName']);
if (array_key_exists('maxExpiry', $collection)) {
$collectionSpec->setMaxExpiry($collection['maxExpiry']);
}
return $collectionSpec;
}
}
68 changes: 68 additions & 0 deletions Couchbase/Management/CreateCollectionOptions.php
@@ -0,0 +1,68 @@
<?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 CreateCollectionOptions
{
private ?int $timeoutMilliseconds;

/**
* Static helper to keep code more readable
*
* @return CreateCollectionOptions
* @since 4.1.3
*/
public static function build(): CreateCollectionOptions
{
return new CreateCollectionOptions();
}

/**
* Sets the operation timeout in milliseconds
*
* @param int $milliseconds the operation timeout to apply
*
* @return CreateCollectionOptions
* @since 4.1.3
*/
public function timeout(int $milliseconds): CreateCollectionOptions
{
$this->timeoutMilliseconds = $milliseconds;
return $this;
}

/**
* @param CreateCollectionOptions|null $options
* @return array
* @internal
* @since 4.1.3
*/

public static function export(?CreateCollectionOptions $options): array
{
if ($options == null) {
return [];
}
return [
'timeoutMilliseconds' => $options->timeoutMilliseconds,
];
}
}
66 changes: 66 additions & 0 deletions Couchbase/Management/CreateScopeOptions.php
@@ -0,0 +1,66 @@
<?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 CreateScopeOptions
{
private ?int $timeoutMilliseconds;

/**
* Static helper to make code more readable
*
* @return CreateScopeOptions
* @since 4.1.3
*/
public static function build(): CreateScopeOptions
{
return new CreateScopeOptions();
}
/**
* Sets the operation timeout in milliseconds
*
* @param int $milliseconds the operation timeout to apply
*
* @return CreateScopeOptions
* @since 4.1.3
*/
public function timeout(int $milliseconds): CreateScopeOptions
{
$this->timeoutMilliseconds = $milliseconds;
return $this;
}

/**
* @param CreateScopeOptions|null $options
* @return array
* @internal
* @since 4.1.3
*/
public static function export(?CreateScopeOptions $options): array
{
if ($options == null) {
return [];
}
return [
'timeoutMilliseconds' => $options->timeoutMilliseconds,
];
}
}