Skip to content

Commit 07b2c77

Browse files
Support CBOR format for Update queries
1 parent c9ba8b5 commit 07b2c77

22 files changed

+1386
-36
lines changed

composer.json

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"require-dev": {
2525
"ext-curl": "*",
2626
"ext-iconv": "*",
27+
"2tvenom/cborencode": "^1.0",
2728
"escapestudios/symfony2-coding-standard": "^3.11",
2829
"nyholm/psr7": "^1.8",
2930
"php-http/guzzle7-adapter": "^1.0",
@@ -34,8 +35,12 @@
3435
"phpunit/phpunit": "^9.6",
3536
"rawr/phpunit-data-provider": "^3.3",
3637
"roave/security-advisories": "dev-master",
38+
"spomky-labs/cbor-php": "^3.0",
3739
"symfony/event-dispatcher": "^5.0 || ^6.0"
3840
},
41+
"suggest": {
42+
"spomky-labs/cbor-php": "Needed to use CBOR formatted requests with Solr 9.3+"
43+
},
3944
"prefer-stable": true,
4045
"config": {
4146
"sort-packages": true,

examples/7.5.3-plugin-bufferedupdate-benchmarks.php

+10-7
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,19 @@
1212

1313
htmlHeader();
1414

15-
if (!isset($weight) || !isset($requestFormat)) {
15+
if (!isset($weight) || !isset($addRequestFormat) || !isset($delRequestFormat)) {
1616
echo <<<'EOT'
1717
<h1>Usage</h1>
1818
19-
<p>This file is intended to be included by a script that sets two variables:</p>
19+
<p>This file is intended to be included by a script that sets three variables:</p>
2020
2121
<dl>
2222
<dt><code>$weight</code></dt>
2323
<dd>Either <code>''</code> for the regular plugins or <code>'lite'</code> for the lite versions.</dd>
24-
<dt><code>$requestFormat</code></dt>
24+
<dt><code>$addRequestFormat</code></dt>
2525
<dd>Any of the <code>Solarium\QueryType\Update\Query\Query::REQUEST_FORMAT_*</code> constants.</dd>
26+
<dt><code>$delRequestFormat</code></dt>
27+
<dd><code>Solarium\QueryType\Update\Query\Query::REQUEST_FORMAT_JSON</code> or <code>REQUEST_FORMAT_XML</code>.</dd>
2628
</dl>
2729
2830
<h2>Example</h2>
@@ -35,7 +37,8 @@
3537
use Solarium\QueryType\Update\Query\Query;
3638
3739
$weight = '';
38-
$requestFormat = Query::REQUEST_FORMAT_JSON;
40+
$addRequestFormat = Query::REQUEST_FORMAT_JSON;
41+
$delRequestFormat = Query::REQUEST_FORMAT_JSON;
3942
4043
require(__DIR__.'/7.5.3-plugin-bufferedupdate-benchmarks.php');
4144
</pre>
@@ -76,10 +79,10 @@
7679
$addBuffer = $client->getPlugin($addPlugin = 'bufferedadd'.$weight);
7780
$delBuffer = $client->getPlugin($delPlugin = 'buffereddelete'.$weight);
7881

79-
$addBuffer->setRequestFormat($requestFormat);
80-
$delBuffer->setRequestFormat($requestFormat);
82+
$addBuffer->setRequestFormat($addRequestFormat);
83+
$delBuffer->setRequestFormat($delRequestFormat);
8184

82-
echo '<h3>'.$addPlugin.' / '.$delPlugin.' ('.strtoupper($requestFormat).')</h3>';
85+
echo '<h3>'.$addPlugin.' ('.strtoupper($addRequestFormat).') / '.$delPlugin.' ('.strtoupper($delRequestFormat).')</h3>';
8386
echo '<table><thead>';
8487
echo '<tr><th>add buffer size</th><th>add time</th>';
8588
echo '<th>delete buffer size</th><th>delete time</th>';

examples/7.5.3.1-plugin-bufferedupdate-benchmarks-xml.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Solarium\QueryType\Update\Query\Query;
66

77
$weight = '';
8-
$requestFormat = Query::REQUEST_FORMAT_XML;
8+
$addRequestFormat = Query::REQUEST_FORMAT_XML;
9+
$delRequestFormat = Query::REQUEST_FORMAT_XML;
910

1011
require(__DIR__.'/7.5.3-plugin-bufferedupdate-benchmarks.php');

examples/7.5.3.2-plugin-bufferedupdate-lite-benchmarks-xml.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Solarium\QueryType\Update\Query\Query;
66

77
$weight = 'lite';
8-
$requestFormat = Query::REQUEST_FORMAT_XML;
8+
$addRequestFormat = Query::REQUEST_FORMAT_XML;
9+
$delRequestFormat = Query::REQUEST_FORMAT_XML;
910

1011
require(__DIR__.'/7.5.3-plugin-bufferedupdate-benchmarks.php');

examples/7.5.3.3-plugin-bufferedupdate-benchmarks-json.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Solarium\QueryType\Update\Query\Query;
66

77
$weight = '';
8-
$requestFormat = Query::REQUEST_FORMAT_JSON;
8+
$addRequestFormat = Query::REQUEST_FORMAT_JSON;
9+
$delRequestFormat = Query::REQUEST_FORMAT_JSON;
910

1011
require(__DIR__.'/7.5.3-plugin-bufferedupdate-benchmarks.php');

examples/7.5.3.4-plugin-bufferedupdate-lite-benchmarks-json.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Solarium\QueryType\Update\Query\Query;
66

77
$weight = 'lite';
8-
$requestFormat = Query::REQUEST_FORMAT_JSON;
8+
$addRequestFormat = Query::REQUEST_FORMAT_JSON;
9+
$delRequestFormat = Query::REQUEST_FORMAT_JSON;
910

1011
require(__DIR__.'/7.5.3-plugin-bufferedupdate-benchmarks.php');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
require_once(__DIR__.'/init.php');
4+
5+
use Solarium\QueryType\Update\Query\Query;
6+
7+
$weight = '';
8+
$addRequestFormat = Query::REQUEST_FORMAT_CBOR;
9+
// CBOR can only be used to add documents
10+
$delRequestFormat = Query::REQUEST_FORMAT_JSON;
11+
12+
require(__DIR__.'/7.5.3-plugin-bufferedupdate-benchmarks.php');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
require_once(__DIR__.'/init.php');
4+
5+
use Solarium\QueryType\Update\Query\Query;
6+
7+
$weight = 'lite';
8+
$addRequestFormat = Query::REQUEST_FORMAT_CBOR;
9+
// CBOR can only be used to add documents
10+
$delRequestFormat = Query::REQUEST_FORMAT_JSON;
11+
12+
require(__DIR__.'/7.5.3-plugin-bufferedupdate-benchmarks.php');

examples/execute_all.php

+2
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@
163163
'7.5.3.2-plugin-bufferedupdate-lite-benchmarks-xml.php', // takes too long for a workflow, can be run manually
164164
'7.5.3.3-plugin-bufferedupdate-benchmarks-json.php', // takes too long for a workflow, can be run manually
165165
'7.5.3.4-plugin-bufferedupdate-lite-benchmarks-json.php', // takes too long for a workflow, can be run manually
166+
'7.5.3.5-plugin-bufferedupdate-benchmarks-cbor.php', // takes too long for a workflow, can be run manually
167+
'7.5.3.6-plugin-bufferedupdate-lite-benchmarks-cbor.php', // takes too long for a workflow, can be run manually
166168
'7.9-plugin-nowaitforresponserequest.php', // there is no default suggester included with techproducts
167169
];
168170

examples/index.html

+2
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ <h2>Examples</h2>
205205
<li><a href="7.5.3.2-plugin-bufferedupdate-lite-benchmarks-xml.php">7.5.3.2 Lite plugins with XML requests</a></li>
206206
<li><a href="7.5.3.3-plugin-bufferedupdate-benchmarks-json.php">7.5.3.3 Regular plugins with JSON requests</a></li>
207207
<li><a href="7.5.3.4-plugin-bufferedupdate-lite-benchmarks-json.php">7.5.3.4 Lite plugins with JSON requests</a></li>
208+
<li><a href="7.5.3.5-plugin-bufferedupdate-benchmarks-cbor.php">7.5.3.5 Regular plugins with CBOR requests</a></li>
209+
<li><a href="7.5.3.6-plugin-bufferedupdate-lite-benchmarks-cbor.php">7.5.3.6 Lite plugins with CBOR requests</a></li>
208210
</ul>
209211
</ul>
210212
<li><a href="7.6-plugin-prefetchiterator.php">7.6 Prefetch iterator for select queries</a></li>

src/Core/Client/Request.php

+5
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ class Request extends Configurable implements RequestParamsInterface
4646
*/
4747
const METHOD_PUT = 'PUT';
4848

49+
/**
50+
* Content-Type for CBOR payloads.
51+
*/
52+
const CONTENT_TYPE_APPLICATION_CBOR = 'application/cbor';
53+
4954
/**
5055
* Content-Type for JSON payloads.
5156
*/

src/Plugin/BufferedAdd/BufferedAdd.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,16 @@ public function commit(?bool $overwrite = null, ?bool $softCommit = null, ?bool
118118
}
119119

120120
$this->updateQuery->add(null, $command);
121-
$this->updateQuery->addCommit($event->getSoftCommit(), $event->getWaitSearcher(), $event->getExpungeDeletes());
121+
122+
if ($this->updateQuery::REQUEST_FORMAT_CBOR === $this->getRequestFormat()) {
123+
$this->updateQuery->addParam('commit', true);
124+
$this->updateQuery->addParam('softCommit', $event->getSoftCommit());
125+
$this->updateQuery->addParam('waitSearcher', $event->getWaitSearcher());
126+
$this->updateQuery->addParam('expungeDeletes', $event->getExpungeDeletes());
127+
} else {
128+
$this->updateQuery->addCommit($event->getSoftCommit(), $event->getWaitSearcher(), $event->getExpungeDeletes());
129+
}
130+
122131
$result = $this->client->update($this->updateQuery, $this->getEndpoint());
123132
$this->clear();
124133

src/Plugin/BufferedAdd/BufferedAddLite.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,16 @@ public function commit(?bool $overwrite = null, ?bool $softCommit = null, ?bool
201201
}
202202

203203
$this->updateQuery->add(null, $command);
204-
$this->updateQuery->addCommit($softCommit, $waitSearcher, $expungeDeletes);
204+
205+
if ($this->updateQuery::REQUEST_FORMAT_CBOR === $this->getRequestFormat()) {
206+
$this->updateQuery->addParam('commit', true);
207+
$this->updateQuery->addParam('softCommit', $softCommit);
208+
$this->updateQuery->addParam('waitSearcher', $waitSearcher);
209+
$this->updateQuery->addParam('expungeDeletes', $expungeDeletes);
210+
} else {
211+
$this->updateQuery->addCommit($softCommit, $waitSearcher, $expungeDeletes);
212+
}
213+
205214
$result = $this->client->update($this->updateQuery, $this->getEndpoint());
206215
$this->clear();
207216

src/Plugin/BufferedDelete/BufferedDeleteLite.php

+21
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace Solarium\Plugin\BufferedDelete;
1111

12+
use Solarium\Exception\InvalidArgumentException;
1213
use Solarium\Exception\RuntimeException;
1314
use Solarium\Plugin\AbstractBufferedUpdate\AbstractBufferedUpdate;
1415
use Solarium\Plugin\BufferedDelete\Delete\Id as DeleteById;
@@ -113,6 +114,26 @@ public function getDeletes(): array
113114
return $this->buffer;
114115
}
115116

117+
/**
118+
* Set the request format for the updates.
119+
*
120+
* Use UpdateQuery::REQUEST_FORMAT_JSON or UpdateQuery::REQUEST_FORMAT_XML as value.
121+
*
122+
* @param string $requestFormat
123+
*
124+
* @throws InvalidArgumentException
125+
*
126+
* @return self Provides fluent interface
127+
*/
128+
public function setRequestFormat(string $requestFormat): self
129+
{
130+
if ($this->updateQuery::REQUEST_FORMAT_CBOR === strtolower($requestFormat)) {
131+
throw new InvalidArgumentException('Unsupported request format: CBOR can only be used to add documents');
132+
}
133+
134+
return parent::setRequestFormat($requestFormat);
135+
}
136+
116137
/**
117138
* Flush any buffered deletes to Solr.
118139
*

src/QueryType/Update/Query/Query.php

+7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Solarium\QueryType\Update\Query\Command\Optimize as OptimizeCommand;
2424
use Solarium\QueryType\Update\Query\Command\RawXml as RawXmlCommand;
2525
use Solarium\QueryType\Update\Query\Command\Rollback as RollbackCommand;
26+
use Solarium\QueryType\Update\RequestBuilder\Cbor as CborRequestBuilder;
2627
use Solarium\QueryType\Update\RequestBuilder\Json as JsonRequestBuilder;
2728
use Solarium\QueryType\Update\RequestBuilder\Xml as XmlRequestBuilder;
2829
use Solarium\QueryType\Update\ResponseParser;
@@ -67,6 +68,11 @@ class Query extends BaseQuery
6768
*/
6869
const COMMAND_ROLLBACK = 'rollback';
6970

71+
/**
72+
* CBOR request format.
73+
*/
74+
const REQUEST_FORMAT_CBOR = 'cbor';
75+
7076
/**
7177
* JSON request format.
7278
*/
@@ -97,6 +103,7 @@ class Query extends BaseQuery
97103
* @var array
98104
*/
99105
protected $requestFormats = [
106+
self::REQUEST_FORMAT_CBOR => CborRequestBuilder::class,
100107
self::REQUEST_FORMAT_JSON => JsonRequestBuilder::class,
101108
self::REQUEST_FORMAT_XML => XmlRequestBuilder::class,
102109
];

0 commit comments

Comments
 (0)