From 4f9da2848cc27322d19609f68171a84d384de03e Mon Sep 17 00:00:00 2001 From: Haydar Kulekci Date: Tue, 25 Jun 2024 08:49:51 +0300 Subject: [PATCH] shard key opreation for collection initalized integration tests for shards --- src/Endpoints/Collections.php | 6 ++ src/Endpoints/Collections/Shards.php | 42 +++++++++++++ .../CreateShardingKeyOperation.php | 56 ----------------- .../CollectionConfig/CreateShardKey.php | 34 ++++++++++ .../CollectionConfig/DeleteShardKey.php | 26 ++++++++ .../Endpoints/Collections/ShardsTest.php | 62 +++++++++++++++++++ .../CreateShardingKeyOperationTest.php | 41 ------------ .../CollectionConfig/CreateShardKeyTest.php | 56 +++++++++++++++++ .../CollectionConfig/DeleteShardKeyTest.php | 29 +++++++++ 9 files changed, 255 insertions(+), 97 deletions(-) create mode 100644 src/Endpoints/Collections/Shards.php delete mode 100644 src/Models/Request/ClusterUpdate/CreateShardingKeyOperation.php create mode 100644 src/Models/Request/CollectionConfig/CreateShardKey.php create mode 100644 src/Models/Request/CollectionConfig/DeleteShardKey.php create mode 100644 tests/Integration/Endpoints/Collections/ShardsTest.php delete mode 100644 tests/Unit/Models/Request/ClusterUpdate/CreateShardingKeyOperationTest.php create mode 100644 tests/Unit/Models/Request/CollectionConfig/CreateShardKeyTest.php create mode 100644 tests/Unit/Models/Request/CollectionConfig/DeleteShardKeyTest.php diff --git a/src/Endpoints/Collections.php b/src/Endpoints/Collections.php index 8e9d887..4e5f69c 100644 --- a/src/Endpoints/Collections.php +++ b/src/Endpoints/Collections.php @@ -13,6 +13,7 @@ use Qdrant\Endpoints\Collections\Cluster; use Qdrant\Endpoints\Collections\Index; use Qdrant\Endpoints\Collections\Points; +use Qdrant\Endpoints\Collections\Shards; use Qdrant\Endpoints\Collections\Snapshots; use Qdrant\Exception\InvalidArgumentException; use Qdrant\Models\Request\CreateCollection; @@ -134,4 +135,9 @@ public function cluster(): Cluster { return (new Cluster($this->client))->setCollectionName($this->collectionName); } + + public function shards(): Shards + { + return (new Shards($this->client))->setCollectionName($this->collectionName); + } } diff --git a/src/Endpoints/Collections/Shards.php b/src/Endpoints/Collections/Shards.php new file mode 100644 index 0000000..7c1ac38 --- /dev/null +++ b/src/Endpoints/Collections/Shards.php @@ -0,0 +1,42 @@ + + */ +namespace Qdrant\Endpoints\Collections; + +use Qdrant\Endpoints\AbstractEndpoint; +use Qdrant\Exception\InvalidArgumentException; +use Qdrant\Models\Request\CollectionConfig\CreateShardKey; +use Qdrant\Models\Request\CollectionConfig\DeleteShardKey; +use Qdrant\Models\Request\UpdateCollectionCluster; +use Qdrant\Response; + +class Shards extends AbstractEndpoint +{ + public function create(CreateShardKey $params, array $queryParams = []): Response + { + return $this->client->execute( + $this->createRequest( + 'PUT', + '/collections/' . $this->getCollectionName() . '/shards' . $this->queryBuild($queryParams), + $params->toArray() + ) + ); + } + + public function delete(DeleteShardKey $params, array $queryParams = []): Response + { + return $this->client->execute( + $this->createRequest( + 'POST', + '/collections/' . $this->getCollectionName() . '/shards/delete' . $this->queryBuild($queryParams), + $params->toArray() + ) + ); + } +} \ No newline at end of file diff --git a/src/Models/Request/ClusterUpdate/CreateShardingKeyOperation.php b/src/Models/Request/ClusterUpdate/CreateShardingKeyOperation.php deleted file mode 100644 index 899814e..0000000 --- a/src/Models/Request/ClusterUpdate/CreateShardingKeyOperation.php +++ /dev/null @@ -1,56 +0,0 @@ - - */ - -namespace Qdrant\Models\Request\ClusterUpdate; - -class CreateShardingKeyOperation implements Operation -{ - protected string $shardKey; - protected ?int $shardsNumber = null; - protected ?int $replicationFactor = null; - protected ?int $placement = null; - - public function __construct(string $shardKey) - { - $this->shardKey = $shardKey; - } - - public function getKey(): string - { - return 'create_sharding_key'; - } - - public function toArray(): array - { - return array_filter([ - 'shard_key' => $this->shardKey, - 'shards_number' => $this->shardsNumber, - 'replication_factor' => $this->replicationFactor, - 'placement' => $this->placement, - ], static function($v) { return $v !== null; }); - } - - public function setShardsNumber(int $shardsNumber): CreateShardingKeyOperation - { - $this->shardsNumber = $shardsNumber; - - return $this; - } - - public function setPlacement(int $placement): CreateShardingKeyOperation - { - $this->placement = $placement; - - return $this; - } - - public function setReplicationFactor(int $replicationFactor): CreateShardingKeyOperation - { - $this->replicationFactor = $replicationFactor; - - return $this; - } -} \ No newline at end of file diff --git a/src/Models/Request/CollectionConfig/CreateShardKey.php b/src/Models/Request/CollectionConfig/CreateShardKey.php new file mode 100644 index 0000000..d62d3be --- /dev/null +++ b/src/Models/Request/CollectionConfig/CreateShardKey.php @@ -0,0 +1,34 @@ + + */ + +namespace Qdrant\Models\Request\CollectionConfig; + +use Qdrant\Models\Request\RequestModel; + +class CreateShardKey implements RequestModel +{ + public function __construct( + protected int|string $shardKey, + protected ?int $shardNumber = null, + protected ?int $replicationFactor = null, + protected ?array $placement = null + ) { + } + + public function toArray(): array + { + return array_filter([ + 'shard_key' => $this->shardKey, + 'shard_number' => $this->shardNumber, + 'replication_factor' => $this->replicationFactor, + 'placement' => $this->placement, + ], function($val) { + return !is_null($val); + }); + } +} \ No newline at end of file diff --git a/src/Models/Request/CollectionConfig/DeleteShardKey.php b/src/Models/Request/CollectionConfig/DeleteShardKey.php new file mode 100644 index 0000000..bbedcc9 --- /dev/null +++ b/src/Models/Request/CollectionConfig/DeleteShardKey.php @@ -0,0 +1,26 @@ + + */ + +namespace Qdrant\Models\Request\CollectionConfig; + +use Qdrant\Models\Request\RequestModel; + +class DeleteShardKey implements RequestModel +{ + public function __construct( + protected int|string $shardKey + ) { + } + + public function toArray(): array + { + return [ + 'shard_key' => $this->shardKey + ]; + } +} \ No newline at end of file diff --git a/tests/Integration/Endpoints/Collections/ShardsTest.php b/tests/Integration/Endpoints/Collections/ShardsTest.php new file mode 100644 index 0000000..465b67b --- /dev/null +++ b/tests/Integration/Endpoints/Collections/ShardsTest.php @@ -0,0 +1,62 @@ + + */ + +namespace Integration\Endpoints\Collections; + +use Qdrant\Endpoints\Collections; +use Qdrant\Exception\InvalidArgumentException; +use Qdrant\Models\Request\CollectionConfig\CreateShardKey; +use Qdrant\Models\Request\CollectionConfig\DeleteShardKey; +use Qdrant\Models\Request\CreateIndex; +use Qdrant\Tests\Integration\AbstractIntegration; + +class ShardsTest extends AbstractIntegration +{ + /** + * @throws InvalidArgumentException + */ + public function testCollectionCreateShards(): void + { + //TODO: We need to find a way to enable distributed mode in tests? + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Bad request: Distributed mode disabled'); + + $collection = new Collections($this->client); + $this->createCollections('sample-collection'); + $collection->setCollectionName('sample-collection'); + + $shards = $collection->shards(); + $this->assertEquals('sample-collection', $shards->getCollectionName()); + + $shards->create(new CreateShardKey(1)); + } + + /** + * @throws InvalidArgumentException + */ + public function testCollectionDeleteShards(): void + { + //TODO: We need to find a way to enable distributed mode in tests? + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Bad request: Distributed mode disabled'); + + $collection = new Collections($this->client); + $this->createCollections('sample-collection'); + $collection->setCollectionName('sample-collection'); + + $shards = $collection->shards(); + $this->assertEquals('sample-collection', $shards->getCollectionName()); + + $shards->delete(new DeleteShardKey(1)); + } + + protected function tearDown(): void + { + parent::tearDown(); + + $this->getCollections('sample-collection')->delete(); + } +} \ No newline at end of file diff --git a/tests/Unit/Models/Request/ClusterUpdate/CreateShardingKeyOperationTest.php b/tests/Unit/Models/Request/ClusterUpdate/CreateShardingKeyOperationTest.php deleted file mode 100644 index 057bed0..0000000 --- a/tests/Unit/Models/Request/ClusterUpdate/CreateShardingKeyOperationTest.php +++ /dev/null @@ -1,41 +0,0 @@ - - */ - -namespace Qdrant\Tests\Unit\Models\Request\ClusterUpdate; - -use PHPUnit\Framework\TestCase; -use Qdrant\Models\Request\ClusterUpdate\CreateShardingKeyOperation; -use Qdrant\Models\Request\UpdateCollectionCluster; - -class CreateShardingKeyOperationTest extends TestCase -{ - public function testBasic(): void - { - $config = new CreateShardingKeyOperation('foo'); - - $this->assertEquals([ - 'shard_key' => 'foo' - ], $config->toArray()); - } - - public function testWithOtherParameters() - { - $config = new UpdateCollectionCluster( - (new CreateShardingKeyOperation('foo'))->setShardsNumber(1) - ->setReplicationFactor(1) - ->setPlacement(0) - ); - - $this->assertEquals([ - 'create_sharding_key' => [ - 'shard_key' => 'foo', - 'shards_number' => 1, - 'replication_factor' => 1, - 'placement' => 0, - ] - ], $config->toArray()); - } -} diff --git a/tests/Unit/Models/Request/CollectionConfig/CreateShardKeyTest.php b/tests/Unit/Models/Request/CollectionConfig/CreateShardKeyTest.php new file mode 100644 index 0000000..2e9a35f --- /dev/null +++ b/tests/Unit/Models/Request/CollectionConfig/CreateShardKeyTest.php @@ -0,0 +1,56 @@ + + */ + +namespace Qdrant\Tests\Unit\Models\Request\CollectionConfig; + +use PHPUnit\Framework\TestCase; +use Qdrant\Models\Request\CollectionConfig\BinaryQuantization; +use Qdrant\Models\Request\CollectionConfig\CreateShardKey; + +class CreateShardKeyTest extends TestCase +{ + public function testBasic(): void + { + $config = new CreateShardKey(1); + + $this->assertEquals(['shard_key' => 1], $config->toArray()); + } + + public function testWithSameParameters(): void + { + $config = new CreateShardKey(1, 1, 0); + + $this->assertEquals([ + 'shard_key' => 1, + 'shard_number' => 1, + 'replication_factor' => 0 + ], $config->toArray()); + } + + public function testWithAllParameters(): void + { + $config = new CreateShardKey(1, 1, 0, [1, 2, 3]); + + $this->assertEquals([ + 'shard_key' => 1, + 'shard_number' => 1, + 'replication_factor' => 0, + 'placement' => [1, 2, 3] + ], $config->toArray()); + } + + public function testWithAllParametersEmptyArrayOfPlacement(): void + { + $config = new CreateShardKey(1, 1, 0, []); + + $this->assertEquals([ + 'shard_key' => 1, + 'shard_number' => 1, + 'replication_factor' => 0, + 'placement' => [] + ], $config->toArray()); + } +} diff --git a/tests/Unit/Models/Request/CollectionConfig/DeleteShardKeyTest.php b/tests/Unit/Models/Request/CollectionConfig/DeleteShardKeyTest.php new file mode 100644 index 0000000..6251a15 --- /dev/null +++ b/tests/Unit/Models/Request/CollectionConfig/DeleteShardKeyTest.php @@ -0,0 +1,29 @@ + + */ + +namespace Qdrant\Tests\Unit\Models\Request\CollectionConfig; + +use PHPUnit\Framework\TestCase; +use Qdrant\Models\Request\CollectionConfig\BinaryQuantization; +use Qdrant\Models\Request\CollectionConfig\CreateShardKey; +use Qdrant\Models\Request\CollectionConfig\DeleteShardKey; + +class DeleteShardKeyTest extends TestCase +{ + public function testBasic(): void + { + $config = new DeleteShardKey(1); + + $this->assertEquals(['shard_key' => 1], $config->toArray()); + } + + public function testBasic2(): void + { + $config = new DeleteShardKey(0); + + $this->assertEquals(['shard_key' => 0], $config->toArray()); + } +}