Skip to content

Commit

Permalink
- implemented postbigrequest plugin
Browse files Browse the repository at this point in the history
- added an example script
- added unittests
  • Loading branch information
basdenooijer committed Nov 4, 2011
1 parent 3cf80cd commit fbd0ea7
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 1 deletion.
45 changes: 45 additions & 0 deletions examples/7.2-plugin-postbigrequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
require('init.php');

htmlHeader();

// create a client instance and autoload the postbigrequest plugin
$client = new Solarium_Client($config);
$client->getPlugin('postbigrequest');

// create a basic query to execute
$query = $client->createSelect();

// add a huge filterquery to create a very long query string
// note: normally you would use a range for this, it's just an easy way to create a very big querystring as a test
$fq = '';
for($i=1; $i<=1000; $i++) {
$fq .= ' OR price:'.$i;
}
$fq = substr($fq, 4);
$query->createFilterQuery('fq')->setQuery($fq);

// without the plugin this query would fail as it is bigger than the default servlet container header buffer
$resultset = $client->select($query);

// display the total number of documents found by solr
echo 'NumFound: '.$resultset->getNumFound();

// show documents using the resultset iterator
foreach ($resultset as $document) {

echo '<hr/><table>';

// the documents are also iterable, to get all fields
foreach($document AS $field => $value)
{
// this converts multivalue fields to a comma-separated string
if(is_array($value)) $value = implode(', ', $value);

echo '<tr><th>' . $field . '</th><td>' . $value . '</td></tr>';
}

echo '</table>';
}

htmlFooter();
1 change: 1 addition & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ <h1>Solarium examples</h1>
<li>7. Plugins</li>
<ul style="list-style:none;">
<li><a href="7.1-plugin-loadbalancer.php">7.1 Loadbalancer</a></li>
<li><a href="7.2-plugin-postbigrequest.php">7.2 Post Big Requests</a></li>
</ul>

</ul>
Expand Down
29 changes: 28 additions & 1 deletion library/Solarium/Plugin/PostBigRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,27 @@ class Solarium_Plugin_PostBigRequest extends Solarium_Plugin_Abstract
'maxquerystringlength' => 1024,
);

/**
* Set maxquerystringlength enabled option
*
* @param integer $value
* @return self Provides fluent interface
*/
public function setMaxQueryStringLength($value)
{
return $this->_setOption('maxquerystringlength', $value);
}

/**
* Get maxquerystringlength option
*
* @return integer
*/
public function getMaxQueryStringLength()
{
return $this->getOption('maxquerystringlength');
}

/**
* Event hook to adjust client settings just before query execution
*
Expand All @@ -69,7 +90,13 @@ class Solarium_Plugin_PostBigRequest extends Solarium_Plugin_Abstract
*/
public function postCreateRequest($query, $request)
{

$queryString = $request->getQueryString();
if (strlen($queryString) > $this->getMaxQueryStringLength()) {
$request->setMethod(Solarium_Client_Request::METHOD_POST);
$request->setRawData($queryString);
$request->clearParams();
$request->addHeader('Content-Type: application/x-www-form-urlencoded');
}
}

}
93 changes: 93 additions & 0 deletions tests/Solarium/Plugin/PostBigRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
/**
* Copyright 2011 Bas de Nooijer. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this listof conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of the copyright holder.
*/

class Solarium_Plugin_PostBigRequestTest extends PHPUnit_Framework_TestCase
{
/**
* @var Solarium_Plugin_PostBigRequest
*/
protected $_plugin;

/**
* @var Solarium_Client
*/
protected $_client;

/**
* @var Solarium_Query_Select
*/
protected $query;

public function setUp()
{
$this->_plugin = new Solarium_Plugin_PostBigRequest();

$this->_client = new Solarium_Client();
$this->_query = $this->_client->createSelect();

}

public function testSetAndGetMaxQueryStringLength()
{
$this->_plugin->setMaxQueryStringLength(512);
$this->assertEquals(512, $this->_plugin->getMaxQueryStringLength());
}

public function testPostCreateRequest()
{
// create a very long query
$fq = '';
for($i=1; $i<=1000; $i++) {
$fq .= ' OR price:'.$i;
}
$fq = substr($fq, 4);
$this->_query->createFilterQuery('fq')->setQuery($fq);

$requestOutput = $this->_client->createRequest($this->_query);
$requestInput = clone $requestOutput;
$this->_plugin->postCreateRequest($this->_query, $requestOutput);

$this->assertEquals(Solarium_Client_Request::METHOD_GET, $requestInput->getMethod());
$this->assertEquals(Solarium_Client_Request::METHOD_POST, $requestOutput->getMethod());
$this->assertEquals($requestInput->getQueryString(), $requestOutput->getRawData());
$this->assertEquals('', $requestOutput->getQueryString());
}

public function testPostCreateRequestUnalteredSmallRequest()
{
$requestOutput = $this->_client->createRequest($this->_query);
$requestInput = clone $requestOutput;
$this->_plugin->postCreateRequest($this->_query, $requestOutput);

$this->assertEquals($requestInput, $requestOutput);
}

}

0 comments on commit fbd0ea7

Please sign in to comment.