Skip to content

Commit 4f906ea

Browse files
authored
Merge pull request #46 from hkulekci/update-collection-cluster
Update collection cluster
2 parents 9c82e07 + 5ff54d8 commit 4f906ea

16 files changed

+536
-9
lines changed

src/Endpoints/Collections/Cluster.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use Qdrant\Endpoints\AbstractEndpoint;
1313
use Qdrant\Exception\InvalidArgumentException;
14+
use Qdrant\Models\Request\UpdateCollectionCluster;
1415
use Qdrant\Response;
1516

1617
class Cluster extends AbstractEndpoint
@@ -33,13 +34,14 @@ public function info(): Response
3334
*
3435
* @throws InvalidArgumentException
3536
*/
36-
public function update(array $params, array $queryParams = []): Response
37+
public function update(UpdateCollectionCluster $params, array $queryParams = []): Response
3738
{
3839
return $this->client->execute(
3940
$this->createRequest(
4041
'POST',
4142
'/collections/' . $this->getCollectionName() . '/cluster' . $this->queryBuild($queryParams),
42-
$params)
43+
$params->toArray()
44+
)
4345
);
4446
}
4547
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* @since Dec 2023
4+
* @author Haydar KULEKCI <haydarkulekci@gmail.com>
5+
*/
6+
7+
namespace Qdrant\Models\Request\ClusterUpdate;
8+
9+
class AbortTransferOperation extends MoveShardOperation
10+
{
11+
public function getKey(): string
12+
{
13+
return 'abort_transfer';
14+
}
15+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* @since Dec 2023
4+
* @author Haydar KULEKCI <haydarkulekci@gmail.com>
5+
*/
6+
7+
namespace Qdrant\Models\Request\ClusterUpdate;
8+
9+
class CreateShardingKeyOperation implements Operation
10+
{
11+
protected string $shardKey;
12+
protected ?int $shardsNumber = null;
13+
protected ?int $replicationFactor = null;
14+
protected ?int $placement = null;
15+
16+
public function __construct(string $shardKey)
17+
{
18+
$this->shardKey = $shardKey;
19+
}
20+
21+
public function getKey(): string
22+
{
23+
return 'create_sharding_key';
24+
}
25+
26+
public function toArray(): array
27+
{
28+
return array_filter([
29+
'shard_key' => $this->shardKey,
30+
'shards_number' => $this->shardsNumber,
31+
'replication_factor' => $this->replicationFactor,
32+
'placement' => $this->placement,
33+
], static function($v) { return $v !== null; });
34+
}
35+
36+
public function setShardsNumber(int $shardsNumber): CreateShardingKeyOperation
37+
{
38+
$this->shardsNumber = $shardsNumber;
39+
40+
return $this;
41+
}
42+
43+
public function setPlacement(int $placement): CreateShardingKeyOperation
44+
{
45+
$this->placement = $placement;
46+
47+
return $this;
48+
}
49+
50+
public function setReplicationFactor(int $replicationFactor): CreateShardingKeyOperation
51+
{
52+
$this->replicationFactor = $replicationFactor;
53+
54+
return $this;
55+
}
56+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* @since Dec 2023
4+
* @author Haydar KULEKCI <haydarkulekci@gmail.com>
5+
*/
6+
7+
namespace Qdrant\Models\Request\ClusterUpdate;
8+
9+
class DropReplicaOperation implements Operation
10+
{
11+
public function __construct(
12+
protected int $shardId,
13+
protected int $peerId
14+
) {}
15+
16+
public function toArray(): array
17+
{
18+
return [
19+
'shard_id' => $this->shardId,
20+
'peer_id' => $this->peerId,
21+
];
22+
}
23+
24+
public function getKey(): string
25+
{
26+
return 'drop_replica';
27+
}
28+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* @since Dec 2023
4+
* @author Haydar KULEKCI <haydarkulekci@gmail.com>
5+
*/
6+
7+
namespace Qdrant\Models\Request\ClusterUpdate;
8+
9+
class DropShardingKeyOperation implements Operation
10+
{
11+
public function __construct(protected string $shardKey)
12+
{}
13+
14+
public function getKey(): string
15+
{
16+
return 'drop_sharding_key';
17+
}
18+
19+
public function toArray(): array
20+
{
21+
return [
22+
'shard_key' => $this->shardKey,
23+
];
24+
}
25+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* @since Dec 2023
4+
* @author Haydar KULEKCI <haydarkulekci@gmail.com>
5+
*/
6+
7+
namespace Qdrant\Models\Request\ClusterUpdate;
8+
9+
use Qdrant\Exception\InvalidArgumentException;
10+
11+
class MoveShardOperation implements Operation
12+
{
13+
protected ?string $method = null;
14+
15+
public function __construct(
16+
protected int $shardId,
17+
protected int $toPeerId,
18+
protected int $fromPeerId,
19+
) {}
20+
21+
public function getKey(): string
22+
{
23+
return 'move_shard';
24+
}
25+
26+
public function toArray(): array
27+
{
28+
return array_filter([
29+
'shard_id' => $this->shardId,
30+
'to_peer_id' => $this->toPeerId,
31+
'from_peer_id' => $this->fromPeerId,
32+
'method' => $this->method,
33+
], static function($v) { return $v !== null; });
34+
}
35+
36+
public function setMethod(string $method): MoveShardOperation
37+
{
38+
if (!in_array($method, ['snapshot', 'stream_records'])) {
39+
throw new InvalidArgumentException('Method could be snapshot or stream_record for operations');
40+
}
41+
42+
$this->method = $method;
43+
44+
return $this;
45+
}
46+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
/**
3+
* @since Dec 2023
4+
* @author Haydar KULEKCI <haydarkulekci@gmail.com>
5+
*/
6+
7+
namespace Qdrant\Models\Request\ClusterUpdate;
8+
9+
interface Operation
10+
{
11+
public function getKey(): string;
12+
public function toArray(): array;
13+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* @since Dec 2023
4+
* @author Haydar KULEKCI <haydarkulekci@gmail.com>
5+
*/
6+
7+
namespace Qdrant\Models\Request\ClusterUpdate;
8+
9+
class ReplicateShardOperation extends MoveShardOperation
10+
{
11+
public function getKey(): string
12+
{
13+
return 'replicate_shard';
14+
}
15+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* UpdateCollectionCluster
4+
*
5+
* @since Mar 2023
6+
* @author Haydar KULEKCI <haydarkulekci@gmail.com>
7+
*/
8+
9+
namespace Qdrant\Models\Request;
10+
11+
use Qdrant\Models\Request\ClusterUpdate\Operation;
12+
13+
class UpdateCollectionCluster implements RequestModel
14+
{
15+
/**
16+
* @param Operation $operation
17+
*/
18+
public function __construct(protected Operation $operation)
19+
{
20+
}
21+
22+
public function toArray(): array
23+
{
24+
return [
25+
$this->operation->getKey() => $this->operation->toArray(),
26+
];
27+
}
28+
}

tests/Integration/Endpoints/Collections/ClusterTest.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
use Qdrant\Endpoints\Collections;
99
use Qdrant\Exception\InvalidArgumentException;
10+
use Qdrant\Models\Request\ClusterUpdate\MoveShardOperation;
11+
use Qdrant\Models\Request\UpdateCollectionCluster;
1012
use Qdrant\Tests\Integration\AbstractIntegration;
1113

1214
class ClusterTest extends AbstractIntegration
@@ -40,13 +42,9 @@ public function testClusterUpdate(): void
4042
$this->createCollections('sample-collection');
4143
$cluster->setCollectionName('sample-collection');
4244

43-
$response = $cluster->update([
44-
"move_shard" => [
45-
"shard_id" => 0,
46-
"to_peer_id" => 1,
47-
"from_peer_id" => 0
48-
]
49-
]);
45+
$operation = new UpdateCollectionCluster(new MoveShardOperation(0, 1, 0));
46+
47+
$response = $cluster->update($operation);
5048
}
5149

5250
protected function tearDown(): void

0 commit comments

Comments
 (0)