-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from hkulekci/shard-key-operations
shard key opreation for collection initalized
- Loading branch information
Showing
9 changed files
with
255 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
56
src/Models/Request/ClusterUpdate/CreateShardingKeyOperation.php
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
41 changes: 0 additions & 41 deletions
41
tests/Unit/Models/Request/ClusterUpdate/CreateShardingKeyOperationTest.php
This file was deleted.
Oops, something went wrong.
56 changes: 56 additions & 0 deletions
56
tests/Unit/Models/Request/CollectionConfig/CreateShardKeyTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
29
tests/Unit/Models/Request/CollectionConfig/DeleteShardKeyTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |