Skip to content

Commit

Permalink
Add 'comment' to search index commands
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Jun 27, 2023
1 parent 1627399 commit ceaf580
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 28 deletions.
23 changes: 23 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
<code>$mergedDriver['platform']</code>
</MixedAssignment>
</file>
<file src="src/Collection.php">
<ArgumentTypeCoercion occurrences="1">
<code>$options</code>
</ArgumentTypeCoercion>
</file>
<file src="src/Command/ListCollections.php">
<MixedAssignment occurrences="2">
<code>$cmd[$option]</code>
Expand Down Expand Up @@ -446,6 +451,9 @@
<MixedArgumentTypeCoercion occurrences="1">
<code>(array) $index</code>
</MixedArgumentTypeCoercion>
<MixedAssignment occurrences="1">
<code>$cmd[$option]</code>
</MixedAssignment>
</file>
<file src="src/Operation/DatabaseCommand.php">
<MixedArgument occurrences="1">
Expand Down Expand Up @@ -527,6 +535,11 @@
<code>isInTransaction</code>
</MixedMethodCall>
</file>
<file src="src/Operation/DropSearchIndex.php">
<MixedAssignment occurrences="1">
<code>$cmd[$option]</code>
</MixedAssignment>
</file>
<file src="src/Operation/Explain.php">
<MixedArgument occurrences="1">
<code>$this-&gt;options['typeMap']</code>
Expand Down Expand Up @@ -636,6 +649,11 @@
<code>$options['session']</code>
</MixedAssignment>
</file>
<file src="src/Operation/ListSearchIndexes.php">
<DocblockTypeContradiction occurrences="1">
<code>isset($options['name']) &amp;&amp; ! is_string($options['name'])</code>
</DocblockTypeContradiction>
</file>
<file src="src/Operation/MapReduce.php">
<DocblockTypeContradiction occurrences="1">
<code>! is_string($out) &amp;&amp; ! is_array($out) &amp;&amp; ! is_object($out)</code>
Expand Down Expand Up @@ -711,6 +729,11 @@
<code>! is_array($update) &amp;&amp; ! is_object($update)</code>
</DocblockTypeContradiction>
</file>
<file src="src/Operation/UpdateSearchIndex.php">
<MixedAssignment occurrences="1">
<code>$cmd[$option]</code>
</MixedAssignment>
</file>
<file src="src/Operation/Watch.php">
<DocblockTypeContradiction occurrences="1">
<code>isset($resumeToken) &amp;&amp; ! is_array($resumeToken) &amp;&amp; ! is_object($resumeToken)</code>
Expand Down
20 changes: 10 additions & 10 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,8 @@ public function createIndexes(array $indexes, array $options = [])
*
* @see https://www.mongodb.com/docs/manual/reference/command/createSearchIndexes/
* @see https://mongodb.com/docs/manual/reference/method/db.collection.createSearchIndex/
* @param array|object $definition Atlas Search index mapping definition
* @param array{name?: string} $options Command options
* @param array|object $definition Atlas Search index mapping definition
* @param array{name?: string, comment?: mixed} $options Command options
* @return string The name of the created search index
* @throws UnsupportedException if options are not supported by the selected server
* @throws InvalidArgumentException for parameter/option parsing errors
Expand Down Expand Up @@ -440,15 +440,15 @@ public function createSearchIndex($definition, array $options = []): string
* @see https://www.mongodb.com/docs/manual/reference/command/createSearchIndexes/
* @see https://mongodb.com/docs/manual/reference/method/db.collection.createSearchIndex/
* @param list<array{name?: string, definition: array|object}> $indexes List of search index specifications
* @param array $options Command options
* @param array{comment?: string} $options Command options
* @return string[] The names of the created search indexes
* @throws UnsupportedException if options are not supported by the selected server
* @throws InvalidArgumentException for parameter/option parsing errors
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
public function createSearchIndexes(array $indexes, array $options = []): array
{
$operation = new CreateSearchIndexes($this->databaseName, $this->collectionName, $indexes);
$operation = new CreateSearchIndexes($this->databaseName, $this->collectionName, $indexes, $options);
$server = select_server($this->manager, $options);

return $operation->execute($server);
Expand Down Expand Up @@ -635,8 +635,8 @@ public function dropIndexes(array $options = [])
* Drop a single Atlas Search index in the collection.
* Only available when used against a 7.0+ Atlas cluster.
*
* @param string $name Search index name
* @param array $options Additional options
* @param string $name Search index name
* @param array{comment?: mixed} $options Additional options
* @throws UnsupportedException if options are not supported by the selected server
* @throws InvalidArgumentException for parameter/option parsing errors
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
Expand Down Expand Up @@ -1205,16 +1205,16 @@ public function updateOne($filter, $update, array $options = [])
* Update a single Atlas Search index in the collection.
* Only available when used against a 7.0+ Atlas cluster.
*
* @param string $name Search index name
* @param array|object $definition Atlas Search index definition
* @param array $options Command options
* @param string $name Search index name
* @param array|object $definition Atlas Search index definition
* @param array{comment?: mixed} $options Command options
* @throws UnsupportedException if options are not supported by the selected server
* @throws InvalidArgumentException for parameter parsing errors
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
public function updateSearchIndex(string $name, $definition, array $options = []): void
{
$operation = new UpdateSearchIndex($this->databaseName, $this->collectionName, $name, $definition);
$operation = new UpdateSearchIndex($this->databaseName, $this->collectionName, $name, $definition, $options);
$server = select_server($this->manager, $options);

$operation->execute($server);
Expand Down
19 changes: 15 additions & 4 deletions src/Operation/CreateSearchIndexes.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,19 @@ class CreateSearchIndexes implements Executable
/** @var array */
private $indexes = [];

/** @var array */
private $options;

/**
* Constructs a createSearchIndexes command.
*
* @param string $databaseName Database name
* @param string $collectionName Collection name
* @param list<array|object> $indexes List of search index specifications
* @param string $databaseName Database name
* @param string $collectionName Collection name
* @param list<array|object> $indexes List of search index specifications
* @param array{comment?: mixed} $options Command options
* @throws InvalidArgumentException for parameter parsing errors
*/
public function __construct(string $databaseName, string $collectionName, array $indexes)
public function __construct(string $databaseName, string $collectionName, array $indexes, array $options)
{
if (! array_is_list($indexes)) {
throw new InvalidArgumentException('$indexes is not a list');
Expand All @@ -72,6 +76,7 @@ public function __construct(string $databaseName, string $collectionName, array

$this->databaseName = $databaseName;
$this->collectionName = $collectionName;
$this->options = $options;
}

/**
Expand All @@ -89,6 +94,12 @@ public function execute(Server $server): array
'indexes' => $this->indexes,
];

foreach (['comment'] as $option) {
if (isset($this->options[$option])) {
$cmd[$option] = $this->options[$option];
}
}

$cursor = $server->executeCommand($this->databaseName, new Command($cmd));

/** @var object{indexesCreated: list<object{name: string}>} $result */
Expand Down
19 changes: 15 additions & 4 deletions src/Operation/DropSearchIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,19 @@ class DropSearchIndex implements Executable
/** @var string */
private $name;

/** @var array */
private $options;

/**
* Constructs a dropSearchIndex command.
*
* @param string $databaseName Database name
* @param string $collectionName Collection name
* @param string $name Index name
* @param string $databaseName Database name
* @param string $collectionName Collection name
* @param string $name Index name
* @param array{comment?: mixed} $options Command options
* @throws InvalidArgumentException for parameter parsing errors
*/
public function __construct(string $databaseName, string $collectionName, string $name)
public function __construct(string $databaseName, string $collectionName, string $name, array $options = [])
{
if ($name === '') {
throw new InvalidArgumentException('Index name cannot be empty');
Expand All @@ -57,6 +61,7 @@ public function __construct(string $databaseName, string $collectionName, string
$this->databaseName = $databaseName;
$this->collectionName = $collectionName;
$this->name = $name;
$this->options = $options;
}

/**
Expand All @@ -73,6 +78,12 @@ public function execute(Server $server): void
'name' => $this->name,
];

foreach (['comment'] as $option) {
if (isset($this->options[$option])) {
$cmd[$option] = $this->options[$option];
}
}

$server->executeCommand($this->databaseName, new Command($cmd));
}
}
21 changes: 16 additions & 5 deletions src/Operation/UpdateSearchIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,20 @@ class UpdateSearchIndex implements Executable
/** @var array|object */
private $definition;

/** @var array */
private $options = [];

/**
* Constructs a createSearchIndexes command.
*
* @param string $databaseName Database name
* @param string $collectionName Collection name
* @param string $name Search index name
* @param array|object $definition Atlas Search index definition
* @param string $databaseName Database name
* @param string $collectionName Collection name
* @param string $name Search index name
* @param array|object $definition Atlas Search index definition
* @param array{comment?: mixed} $options Command options
* @throws InvalidArgumentException for parameter parsing errors
*/
public function __construct(string $databaseName, string $collectionName, string $name, $definition)
public function __construct(string $databaseName, string $collectionName, string $name, $definition, array $options = [])
{
if ($name === '') {
throw new InvalidArgumentException('Index name cannot be empty');
Expand All @@ -68,6 +72,7 @@ public function __construct(string $databaseName, string $collectionName, string
$this->collectionName = $collectionName;
$this->name = $name;
$this->definition = $definition;
$this->options = $options;
}

/**
Expand All @@ -85,6 +90,12 @@ public function execute(Server $server): void
'definition' => $this->definition,
];

foreach (['comment'] as $option) {
if (isset($this->options[$option])) {
$cmd[$option] = $this->options[$option];
}
}

$server->executeCommand($this->databaseName, new Command($cmd));
}
}
10 changes: 5 additions & 5 deletions tests/Collection/SearchIndexFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ public function testIndexLifecycle(): void
$name = 'search_index';

// Create a search index
$createdName = $collection->createSearchIndex(['mappings' => ['dynamic' => true]], ['name' => $name]);
$createdName = $collection->createSearchIndex(['mappings' => ['dynamic' => true]], ['name' => $name, 'comment' => 'Index creation test']);
$this->assertSame($name, $createdName);

// Wait for the index to be ready
$maxWaitingTime = 120;
do {
sleep(1);
$result = $collection->listSearchIndexes($name);
$result = $collection->listSearchIndexes(['name' => $name]);
$this->assertInstanceOf(CachingIterator::class, $result);
$index = $result->current();

Expand All @@ -74,16 +74,16 @@ public function testIndexLifecycle(): void
// Update the search index
$collection->updateSearchIndex($name, [
'mappings' => ['dynamic' => false],
]);
], ['comment' => 'Index update test']);

// Delete the search index
$collection->dropSearchIndex($name);
$collection->dropSearchIndex($name, ['comment' => 'Index deletion test']);

// Wait for the index to be deleted
$maxWaitingTime = 90;
do {
sleep(1);
$result = $collection->listSearchIndexes($name);
$result = $collection->listSearchIndexes(['name' => $name]);
$this->assertInstanceOf(CachingIterator::class, $result);

if ($maxWaitingTime-- <= 0) {
Expand Down

0 comments on commit ceaf580

Please sign in to comment.