Skip to content

Commit

Permalink
Merge pull request #55 from hkulekci/shard-key-operations
Browse files Browse the repository at this point in the history
shard key opreation for collection initalized
  • Loading branch information
hkulekci authored Jun 26, 2024
2 parents 9bae9fe + 4f9da28 commit 7c5371d
Show file tree
Hide file tree
Showing 9 changed files with 255 additions and 97 deletions.
6 changes: 6 additions & 0 deletions src/Endpoints/Collections.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
42 changes: 42 additions & 0 deletions src/Endpoints/Collections/Shards.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Shards
*
* https://qdrant.github.io/qdrant/redoc/index.html#tag/collections/operation/create_shard_key
*
* @since Mar 2023
* @author Haydar KULEKCI <haydarkulekci@gmail.com>
*/
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()
)
);
}
}
56 changes: 0 additions & 56 deletions src/Models/Request/ClusterUpdate/CreateShardingKeyOperation.php

This file was deleted.

34 changes: 34 additions & 0 deletions src/Models/Request/CollectionConfig/CreateShardKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* CreateShardKey
*
* @since Jun 2024
* @author Haydar KULEKCI <haydarkulekci@gmail.com>
*/

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);
});
}
}
26 changes: 26 additions & 0 deletions src/Models/Request/CollectionConfig/DeleteShardKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* DeleteShardKey
*
* @since Jun 2024
* @author Haydar KULEKCI <haydarkulekci@gmail.com>
*/

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
];
}
}
62 changes: 62 additions & 0 deletions tests/Integration/Endpoints/Collections/ShardsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* @since Jun 2024
* @author Haydar KULEKCI <haydarkulekci@gmail.com>
*/

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();
}
}

This file was deleted.

56 changes: 56 additions & 0 deletions tests/Unit/Models/Request/CollectionConfig/CreateShardKeyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* @since Jun 2024
* @author Haydar KULEKCI <haydarkulekci@gmail.com>
*/

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());
}
}
29 changes: 29 additions & 0 deletions tests/Unit/Models/Request/CollectionConfig/DeleteShardKeyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* @since Jun 2024
* @author Haydar KULEKCI <haydarkulekci@gmail.com>
*/

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());
}
}

0 comments on commit 7c5371d

Please sign in to comment.