Skip to content

Commit

Permalink
Support CBOR format for Update queries
Browse files Browse the repository at this point in the history
  • Loading branch information
thomascorthals committed Apr 3, 2024
1 parent c9ba8b5 commit 29a9c1f
Show file tree
Hide file tree
Showing 23 changed files with 1,446 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ jobs:
cd -
mv solr/server/solr/configsets/sample_techproducts_configs/conf/techproducts.zip ../tests/Integration/Fixtures/
cd ../tests/Integration/Fixtures/docker/solr${{ matrix.solr }}_${{ matrix.mode }}
docker-compose up -d
docker compose up -d
- name: Install dependencies
run: |
Expand Down
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"require-dev": {
"ext-curl": "*",
"ext-iconv": "*",
"2tvenom/cborencode": "^1.0",
"escapestudios/symfony2-coding-standard": "^3.11",
"nyholm/psr7": "^1.8",
"php-http/guzzle7-adapter": "^1.0",
Expand All @@ -34,8 +35,12 @@
"phpunit/phpunit": "^9.6",
"rawr/phpunit-data-provider": "^3.3",
"roave/security-advisories": "dev-master",
"spomky-labs/cbor-php": "^3.0",
"symfony/event-dispatcher": "^5.0 || ^6.0"
},
"suggest": {
"spomky-labs/cbor-php": "Needed to use CBOR formatted requests with Solr 9.3+"
},
"prefer-stable": true,
"config": {
"sort-packages": true,
Expand Down
17 changes: 10 additions & 7 deletions examples/7.5.3-plugin-bufferedupdate-benchmarks.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@

htmlHeader();

if (!isset($weight) || !isset($requestFormat)) {
if (!isset($weight) || !isset($addRequestFormat) || !isset($delRequestFormat)) {
echo <<<'EOT'
<h1>Usage</h1>
<p>This file is intended to be included by a script that sets two variables:</p>
<p>This file is intended to be included by a script that sets three variables:</p>
<dl>
<dt><code>$weight</code></dt>
<dd>Either <code>''</code> for the regular plugins or <code>'lite'</code> for the lite versions.</dd>
<dt><code>$requestFormat</code></dt>
<dt><code>$addRequestFormat</code></dt>
<dd>Any of the <code>Solarium\QueryType\Update\Query\Query::REQUEST_FORMAT_*</code> constants.</dd>
<dt><code>$delRequestFormat</code></dt>
<dd><code>Solarium\QueryType\Update\Query\Query::REQUEST_FORMAT_JSON</code> or <code>REQUEST_FORMAT_XML</code>.</dd>
</dl>
<h2>Example</h2>
Expand All @@ -35,7 +37,8 @@
use Solarium\QueryType\Update\Query\Query;
$weight = '';
$requestFormat = Query::REQUEST_FORMAT_JSON;
$addRequestFormat = Query::REQUEST_FORMAT_JSON;
$delRequestFormat = Query::REQUEST_FORMAT_JSON;
require(__DIR__.'/7.5.3-plugin-bufferedupdate-benchmarks.php');
</pre>
Expand Down Expand Up @@ -76,10 +79,10 @@
$addBuffer = $client->getPlugin($addPlugin = 'bufferedadd'.$weight);
$delBuffer = $client->getPlugin($delPlugin = 'buffereddelete'.$weight);

$addBuffer->setRequestFormat($requestFormat);
$delBuffer->setRequestFormat($requestFormat);
$addBuffer->setRequestFormat($addRequestFormat);
$delBuffer->setRequestFormat($delRequestFormat);

echo '<h3>'.$addPlugin.' / '.$delPlugin.' ('.strtoupper($requestFormat).')</h3>';
echo '<h3>'.$addPlugin.' ('.strtoupper($addRequestFormat).') / '.$delPlugin.' ('.strtoupper($delRequestFormat).')</h3>';
echo '<table><thead>';
echo '<tr><th>add buffer size</th><th>add time</th>';
echo '<th>delete buffer size</th><th>delete time</th>';
Expand Down
3 changes: 2 additions & 1 deletion examples/7.5.3.1-plugin-bufferedupdate-benchmarks-xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Solarium\QueryType\Update\Query\Query;

$weight = '';
$requestFormat = Query::REQUEST_FORMAT_XML;
$addRequestFormat = Query::REQUEST_FORMAT_XML;
$delRequestFormat = Query::REQUEST_FORMAT_XML;

require(__DIR__.'/7.5.3-plugin-bufferedupdate-benchmarks.php');
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Solarium\QueryType\Update\Query\Query;

$weight = 'lite';
$requestFormat = Query::REQUEST_FORMAT_XML;
$addRequestFormat = Query::REQUEST_FORMAT_XML;
$delRequestFormat = Query::REQUEST_FORMAT_XML;

require(__DIR__.'/7.5.3-plugin-bufferedupdate-benchmarks.php');
3 changes: 2 additions & 1 deletion examples/7.5.3.3-plugin-bufferedupdate-benchmarks-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Solarium\QueryType\Update\Query\Query;

$weight = '';
$requestFormat = Query::REQUEST_FORMAT_JSON;
$addRequestFormat = Query::REQUEST_FORMAT_JSON;
$delRequestFormat = Query::REQUEST_FORMAT_JSON;

require(__DIR__.'/7.5.3-plugin-bufferedupdate-benchmarks.php');
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Solarium\QueryType\Update\Query\Query;

$weight = 'lite';
$requestFormat = Query::REQUEST_FORMAT_JSON;
$addRequestFormat = Query::REQUEST_FORMAT_JSON;
$delRequestFormat = Query::REQUEST_FORMAT_JSON;

require(__DIR__.'/7.5.3-plugin-bufferedupdate-benchmarks.php');
12 changes: 12 additions & 0 deletions examples/7.5.3.5-plugin-bufferedupdate-benchmarks-cbor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

require_once(__DIR__.'/init.php');

use Solarium\QueryType\Update\Query\Query;

$weight = '';
$addRequestFormat = Query::REQUEST_FORMAT_CBOR;
// CBOR can only be used to add documents
$delRequestFormat = Query::REQUEST_FORMAT_JSON;

require(__DIR__.'/7.5.3-plugin-bufferedupdate-benchmarks.php');
12 changes: 12 additions & 0 deletions examples/7.5.3.6-plugin-bufferedupdate-lite-benchmarks-cbor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

require_once(__DIR__.'/init.php');

use Solarium\QueryType\Update\Query\Query;

$weight = 'lite';
$addRequestFormat = Query::REQUEST_FORMAT_CBOR;
// CBOR can only be used to add documents
$delRequestFormat = Query::REQUEST_FORMAT_JSON;

require(__DIR__.'/7.5.3-plugin-bufferedupdate-benchmarks.php');
2 changes: 2 additions & 0 deletions examples/execute_all.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@
'7.5.3.2-plugin-bufferedupdate-lite-benchmarks-xml.php', // takes too long for a workflow, can be run manually
'7.5.3.3-plugin-bufferedupdate-benchmarks-json.php', // takes too long for a workflow, can be run manually
'7.5.3.4-plugin-bufferedupdate-lite-benchmarks-json.php', // takes too long for a workflow, can be run manually
'7.5.3.5-plugin-bufferedupdate-benchmarks-cbor.php', // takes too long for a workflow, can be run manually
'7.5.3.6-plugin-bufferedupdate-lite-benchmarks-cbor.php', // takes too long for a workflow, can be run manually
'7.9-plugin-nowaitforresponserequest.php', // there is no default suggester included with techproducts
];

Expand Down
2 changes: 2 additions & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ <h2>Examples</h2>
<li><a href="7.5.3.2-plugin-bufferedupdate-lite-benchmarks-xml.php">7.5.3.2 Lite plugins with XML requests</a></li>
<li><a href="7.5.3.3-plugin-bufferedupdate-benchmarks-json.php">7.5.3.3 Regular plugins with JSON requests</a></li>
<li><a href="7.5.3.4-plugin-bufferedupdate-lite-benchmarks-json.php">7.5.3.4 Lite plugins with JSON requests</a></li>
<li><a href="7.5.3.5-plugin-bufferedupdate-benchmarks-cbor.php">7.5.3.5 Regular plugins with CBOR requests</a></li>
<li><a href="7.5.3.6-plugin-bufferedupdate-lite-benchmarks-cbor.php">7.5.3.6 Lite plugins with CBOR requests</a></li>
</ul>
</ul>
<li><a href="7.6-plugin-prefetchiterator.php">7.6 Prefetch iterator for select queries</a></li>
Expand Down
5 changes: 5 additions & 0 deletions src/Core/Client/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ class Request extends Configurable implements RequestParamsInterface
*/
const METHOD_PUT = 'PUT';

/**
* Content-Type for CBOR payloads.
*/
const CONTENT_TYPE_APPLICATION_CBOR = 'application/cbor';

/**
* Content-Type for JSON payloads.
*/
Expand Down
11 changes: 10 additions & 1 deletion src/Plugin/BufferedAdd/BufferedAdd.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,16 @@ public function commit(?bool $overwrite = null, ?bool $softCommit = null, ?bool
}

$this->updateQuery->add(null, $command);
$this->updateQuery->addCommit($event->getSoftCommit(), $event->getWaitSearcher(), $event->getExpungeDeletes());

if ($this->updateQuery::REQUEST_FORMAT_CBOR === $this->getRequestFormat()) {
$this->updateQuery->addParam('commit', true);
$this->updateQuery->addParam('softCommit', $event->getSoftCommit());
$this->updateQuery->addParam('waitSearcher', $event->getWaitSearcher());
$this->updateQuery->addParam('expungeDeletes', $event->getExpungeDeletes());
} else {
$this->updateQuery->addCommit($event->getSoftCommit(), $event->getWaitSearcher(), $event->getExpungeDeletes());
}

$result = $this->client->update($this->updateQuery, $this->getEndpoint());
$this->clear();

Expand Down
11 changes: 10 additions & 1 deletion src/Plugin/BufferedAdd/BufferedAddLite.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,16 @@ public function commit(?bool $overwrite = null, ?bool $softCommit = null, ?bool
}

$this->updateQuery->add(null, $command);
$this->updateQuery->addCommit($softCommit, $waitSearcher, $expungeDeletes);

if ($this->updateQuery::REQUEST_FORMAT_CBOR === $this->getRequestFormat()) {
$this->updateQuery->addParam('commit', true);
$this->updateQuery->addParam('softCommit', $softCommit);
$this->updateQuery->addParam('waitSearcher', $waitSearcher);
$this->updateQuery->addParam('expungeDeletes', $expungeDeletes);
} else {
$this->updateQuery->addCommit($softCommit, $waitSearcher, $expungeDeletes);
}

$result = $this->client->update($this->updateQuery, $this->getEndpoint());
$this->clear();

Expand Down
21 changes: 21 additions & 0 deletions src/Plugin/BufferedDelete/BufferedDeleteLite.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Solarium\Plugin\BufferedDelete;

use Solarium\Exception\InvalidArgumentException;
use Solarium\Exception\RuntimeException;
use Solarium\Plugin\AbstractBufferedUpdate\AbstractBufferedUpdate;
use Solarium\Plugin\BufferedDelete\Delete\Id as DeleteById;
Expand Down Expand Up @@ -113,6 +114,26 @@ public function getDeletes(): array
return $this->buffer;
}

/**
* Set the request format for the updates.
*
* Use UpdateQuery::REQUEST_FORMAT_JSON or UpdateQuery::REQUEST_FORMAT_XML as value.
*
* @param string $requestFormat
*
* @throws InvalidArgumentException
*
* @return self Provides fluent interface
*/
public function setRequestFormat(string $requestFormat): self
{
if ($this->updateQuery::REQUEST_FORMAT_CBOR === strtolower($requestFormat)) {
throw new InvalidArgumentException('Unsupported request format: CBOR can only be used to add documents');
}

return parent::setRequestFormat($requestFormat);
}

/**
* Flush any buffered deletes to Solr.
*
Expand Down
7 changes: 7 additions & 0 deletions src/QueryType/Update/Query/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Solarium\QueryType\Update\Query\Command\Optimize as OptimizeCommand;
use Solarium\QueryType\Update\Query\Command\RawXml as RawXmlCommand;
use Solarium\QueryType\Update\Query\Command\Rollback as RollbackCommand;
use Solarium\QueryType\Update\RequestBuilder\Cbor as CborRequestBuilder;
use Solarium\QueryType\Update\RequestBuilder\Json as JsonRequestBuilder;
use Solarium\QueryType\Update\RequestBuilder\Xml as XmlRequestBuilder;
use Solarium\QueryType\Update\ResponseParser;
Expand Down Expand Up @@ -67,6 +68,11 @@ class Query extends BaseQuery
*/
const COMMAND_ROLLBACK = 'rollback';

/**
* CBOR request format.
*/
const REQUEST_FORMAT_CBOR = 'cbor';

/**
* JSON request format.
*/
Expand Down Expand Up @@ -97,6 +103,7 @@ class Query extends BaseQuery
* @var array
*/
protected $requestFormats = [
self::REQUEST_FORMAT_CBOR => CborRequestBuilder::class,
self::REQUEST_FORMAT_JSON => JsonRequestBuilder::class,
self::REQUEST_FORMAT_XML => XmlRequestBuilder::class,
];
Expand Down
Loading

0 comments on commit 29a9c1f

Please sign in to comment.