Skip to content

Commit 7c5371d

Browse files
authored
Merge pull request #55 from hkulekci/shard-key-operations
shard key opreation for collection initalized
2 parents 9bae9fe + 4f9da28 commit 7c5371d

File tree

9 files changed

+255
-97
lines changed

9 files changed

+255
-97
lines changed

src/Endpoints/Collections.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Qdrant\Endpoints\Collections\Cluster;
1414
use Qdrant\Endpoints\Collections\Index;
1515
use Qdrant\Endpoints\Collections\Points;
16+
use Qdrant\Endpoints\Collections\Shards;
1617
use Qdrant\Endpoints\Collections\Snapshots;
1718
use Qdrant\Exception\InvalidArgumentException;
1819
use Qdrant\Models\Request\CreateCollection;
@@ -134,4 +135,9 @@ public function cluster(): Cluster
134135
{
135136
return (new Cluster($this->client))->setCollectionName($this->collectionName);
136137
}
138+
139+
public function shards(): Shards
140+
{
141+
return (new Shards($this->client))->setCollectionName($this->collectionName);
142+
}
137143
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* Shards
4+
*
5+
* https://qdrant.github.io/qdrant/redoc/index.html#tag/collections/operation/create_shard_key
6+
*
7+
* @since Mar 2023
8+
* @author Haydar KULEKCI <haydarkulekci@gmail.com>
9+
*/
10+
namespace Qdrant\Endpoints\Collections;
11+
12+
use Qdrant\Endpoints\AbstractEndpoint;
13+
use Qdrant\Exception\InvalidArgumentException;
14+
use Qdrant\Models\Request\CollectionConfig\CreateShardKey;
15+
use Qdrant\Models\Request\CollectionConfig\DeleteShardKey;
16+
use Qdrant\Models\Request\UpdateCollectionCluster;
17+
use Qdrant\Response;
18+
19+
class Shards extends AbstractEndpoint
20+
{
21+
public function create(CreateShardKey $params, array $queryParams = []): Response
22+
{
23+
return $this->client->execute(
24+
$this->createRequest(
25+
'PUT',
26+
'/collections/' . $this->getCollectionName() . '/shards' . $this->queryBuild($queryParams),
27+
$params->toArray()
28+
)
29+
);
30+
}
31+
32+
public function delete(DeleteShardKey $params, array $queryParams = []): Response
33+
{
34+
return $this->client->execute(
35+
$this->createRequest(
36+
'POST',
37+
'/collections/' . $this->getCollectionName() . '/shards/delete' . $this->queryBuild($queryParams),
38+
$params->toArray()
39+
)
40+
);
41+
}
42+
}

src/Models/Request/ClusterUpdate/CreateShardingKeyOperation.php

Lines changed: 0 additions & 56 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* CreateShardKey
4+
*
5+
* @since Jun 2024
6+
* @author Haydar KULEKCI <haydarkulekci@gmail.com>
7+
*/
8+
9+
namespace Qdrant\Models\Request\CollectionConfig;
10+
11+
use Qdrant\Models\Request\RequestModel;
12+
13+
class CreateShardKey implements RequestModel
14+
{
15+
public function __construct(
16+
protected int|string $shardKey,
17+
protected ?int $shardNumber = null,
18+
protected ?int $replicationFactor = null,
19+
protected ?array $placement = null
20+
) {
21+
}
22+
23+
public function toArray(): array
24+
{
25+
return array_filter([
26+
'shard_key' => $this->shardKey,
27+
'shard_number' => $this->shardNumber,
28+
'replication_factor' => $this->replicationFactor,
29+
'placement' => $this->placement,
30+
], function($val) {
31+
return !is_null($val);
32+
});
33+
}
34+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* DeleteShardKey
4+
*
5+
* @since Jun 2024
6+
* @author Haydar KULEKCI <haydarkulekci@gmail.com>
7+
*/
8+
9+
namespace Qdrant\Models\Request\CollectionConfig;
10+
11+
use Qdrant\Models\Request\RequestModel;
12+
13+
class DeleteShardKey implements RequestModel
14+
{
15+
public function __construct(
16+
protected int|string $shardKey
17+
) {
18+
}
19+
20+
public function toArray(): array
21+
{
22+
return [
23+
'shard_key' => $this->shardKey
24+
];
25+
}
26+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* @since Jun 2024
4+
* @author Haydar KULEKCI <haydarkulekci@gmail.com>
5+
*/
6+
7+
namespace Integration\Endpoints\Collections;
8+
9+
use Qdrant\Endpoints\Collections;
10+
use Qdrant\Exception\InvalidArgumentException;
11+
use Qdrant\Models\Request\CollectionConfig\CreateShardKey;
12+
use Qdrant\Models\Request\CollectionConfig\DeleteShardKey;
13+
use Qdrant\Models\Request\CreateIndex;
14+
use Qdrant\Tests\Integration\AbstractIntegration;
15+
16+
class ShardsTest extends AbstractIntegration
17+
{
18+
/**
19+
* @throws InvalidArgumentException
20+
*/
21+
public function testCollectionCreateShards(): void
22+
{
23+
//TODO: We need to find a way to enable distributed mode in tests?
24+
$this->expectException(InvalidArgumentException::class);
25+
$this->expectExceptionMessage('Bad request: Distributed mode disabled');
26+
27+
$collection = new Collections($this->client);
28+
$this->createCollections('sample-collection');
29+
$collection->setCollectionName('sample-collection');
30+
31+
$shards = $collection->shards();
32+
$this->assertEquals('sample-collection', $shards->getCollectionName());
33+
34+
$shards->create(new CreateShardKey(1));
35+
}
36+
37+
/**
38+
* @throws InvalidArgumentException
39+
*/
40+
public function testCollectionDeleteShards(): void
41+
{
42+
//TODO: We need to find a way to enable distributed mode in tests?
43+
$this->expectException(InvalidArgumentException::class);
44+
$this->expectExceptionMessage('Bad request: Distributed mode disabled');
45+
46+
$collection = new Collections($this->client);
47+
$this->createCollections('sample-collection');
48+
$collection->setCollectionName('sample-collection');
49+
50+
$shards = $collection->shards();
51+
$this->assertEquals('sample-collection', $shards->getCollectionName());
52+
53+
$shards->delete(new DeleteShardKey(1));
54+
}
55+
56+
protected function tearDown(): void
57+
{
58+
parent::tearDown();
59+
60+
$this->getCollections('sample-collection')->delete();
61+
}
62+
}

tests/Unit/Models/Request/ClusterUpdate/CreateShardingKeyOperationTest.php

Lines changed: 0 additions & 41 deletions
This file was deleted.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* @since Jun 2024
4+
* @author Haydar KULEKCI <haydarkulekci@gmail.com>
5+
*/
6+
7+
namespace Qdrant\Tests\Unit\Models\Request\CollectionConfig;
8+
9+
use PHPUnit\Framework\TestCase;
10+
use Qdrant\Models\Request\CollectionConfig\BinaryQuantization;
11+
use Qdrant\Models\Request\CollectionConfig\CreateShardKey;
12+
13+
class CreateShardKeyTest extends TestCase
14+
{
15+
public function testBasic(): void
16+
{
17+
$config = new CreateShardKey(1);
18+
19+
$this->assertEquals(['shard_key' => 1], $config->toArray());
20+
}
21+
22+
public function testWithSameParameters(): void
23+
{
24+
$config = new CreateShardKey(1, 1, 0);
25+
26+
$this->assertEquals([
27+
'shard_key' => 1,
28+
'shard_number' => 1,
29+
'replication_factor' => 0
30+
], $config->toArray());
31+
}
32+
33+
public function testWithAllParameters(): void
34+
{
35+
$config = new CreateShardKey(1, 1, 0, [1, 2, 3]);
36+
37+
$this->assertEquals([
38+
'shard_key' => 1,
39+
'shard_number' => 1,
40+
'replication_factor' => 0,
41+
'placement' => [1, 2, 3]
42+
], $config->toArray());
43+
}
44+
45+
public function testWithAllParametersEmptyArrayOfPlacement(): void
46+
{
47+
$config = new CreateShardKey(1, 1, 0, []);
48+
49+
$this->assertEquals([
50+
'shard_key' => 1,
51+
'shard_number' => 1,
52+
'replication_factor' => 0,
53+
'placement' => []
54+
], $config->toArray());
55+
}
56+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* @since Jun 2024
4+
* @author Haydar KULEKCI <haydarkulekci@gmail.com>
5+
*/
6+
7+
namespace Qdrant\Tests\Unit\Models\Request\CollectionConfig;
8+
9+
use PHPUnit\Framework\TestCase;
10+
use Qdrant\Models\Request\CollectionConfig\BinaryQuantization;
11+
use Qdrant\Models\Request\CollectionConfig\CreateShardKey;
12+
use Qdrant\Models\Request\CollectionConfig\DeleteShardKey;
13+
14+
class DeleteShardKeyTest extends TestCase
15+
{
16+
public function testBasic(): void
17+
{
18+
$config = new DeleteShardKey(1);
19+
20+
$this->assertEquals(['shard_key' => 1], $config->toArray());
21+
}
22+
23+
public function testBasic2(): void
24+
{
25+
$config = new DeleteShardKey(0);
26+
27+
$this->assertEquals(['shard_key' => 0], $config->toArray());
28+
}
29+
}

0 commit comments

Comments
 (0)