Merge pull request #184 from creative-commoners/pulls/3.0/fix-postgres

API Remove bind_manipulation_capture RequestFilter in favour of Injection overrides
This commit is contained in:
Dylan Wagstaff 2017-12-11 10:41:33 +13:00 committed by GitHub
commit 0fd7bd4730
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
24 changed files with 130 additions and 168 deletions

View File

@ -9,7 +9,7 @@ matrix:
- php: 5.6
env: DB=MYSQL PHPCS_TEST=1 PHPUNIT_TEST=1
- php: 7.0
env: DB=MYSQL PHPUNIT_TEST=1
env: DB=PGSQL PHPUNIT_TEST=1
- php: 7.1
env: DB=MYSQL PHPUNIT_COVERAGE_TEST=1
- php: 7.1
@ -24,6 +24,7 @@ before_script:
- composer validate
- composer require --no-update symbiote/silverstripe-queuedjobs ^4.0
- if [[ $SUBSITES ]]; then composer require --no-update silverstripe/subsites 2.0.x-dev; fi
- if [[ $DB == "PGSQL" ]]; then composer require --no-update silverstripe/postgresql 2.0.x-dev; fi
- composer require --no-update silverstripe/installer 4.0.x-dev
- composer install --prefer-dist --no-interaction --no-progress --no-suggest --optimize-autoloader --verbose --profile

View File

@ -5,6 +5,9 @@ mappings:
SearchUpdateProcessor: SilverStripe\FullTextSearch\Search\Processors\SearchUpdateProcessor
SearchUpdateQueuedJobProcessor: SilverStripe\FullTextSearch\Search\Processors\SearchUpdateQueuedJobProcessor
FullTextSearch: SilverStripe\FullTextSearch\Search\FullTextSearch
SearchManipulateCapture_MySQLDatabase: SilverStripe\FullTextSearch\Search\Captures\SearchManipulateCapture_MySQLDatabase
SearchManipulateCapture_PostgreSQLDatabase: SilverStripe\FullTextSearch\Search\Captures\SearchManipulateCapture_PostgreSQLDatabase
SearchManipulateCapture_SQLite3Database: SilverStripe\FullTextSearch\Search\Captures\SearchManipulateCapture_SQLite3Database
SearchIndex: SilverStripe\FullTextSearch\Search\Indexes\SearchIndex
SearchIndex_Null: SilverStripe\FullTextSearch\Search\Indexes\SearchIndex_Null
SearchIndex_Recording: SilverStripe\FullTextSearch\Search\Indexes\SearchIndex_Recording
@ -12,8 +15,7 @@ mappings:
SearchQuery: SilverStripe\FullTextSearch\Search\Queries\SearchQuery
SearchQuery_Range: SilverStripe\FullTextSearch\Search\Queries\SearchQuery_Range
SearchUpdater: SilverStripe\FullTextSearch\Search\Updaters\SearchUpdater
SearchUpdater_BindManipulationCaptureFilter: SilverStripe\FullTextSearch\Search\Updaters\SearchUpdater_BindManipulationCaptureFilter
SearchUpdater_ObjectHandler: SilverStripe\FullTextSearch\Search\Updaters\SearchUpdater_ObjectHandler
SearchUpdater_ObjectHandler: SilverStripe\FullTextSearch\Search\Extensions\SearchUpdater_ObjectHandler
SearchVariant: SilverStripe\FullTextSearch\Search\Variants\SearchVariant
SearchVariant_Caller: SilverStripe\FullTextSearch\Search\Variants\SearchVariant_Caller
SearchVariantSubsites: SilverStripe\FullTextSearch\Search\Variants\SearchVariantSubsites

View File

@ -1,7 +0,0 @@
<?php
use SilverStripe\FullTextSearch\Search\Updaters\SearchUpdater;
if (isset($databaseConfig['type'])) {
SearchUpdater::bind_manipulation_capture();
}

View File

@ -1,5 +0,0 @@
SilverStripe\Core\Injector\Injector:
SilverStripe\Control\RequestProcessor:
properties:
filters:
- '%$SilverStripe\FullTextSearch\Search\Updaters\SearchUpdater_BindManipulationCaptureFilter'

View File

@ -1,3 +1,6 @@
---
Name: fulltextsearchconfig
---
SilverStripe\ORM\DataObject:
extensions:
- 'SilverStripe\FullTextSearch\Search\Updaters\SearchUpdater_ObjectHandler'
- SilverStripe\FullTextSearch\Search\Extensions\SearchUpdater_ObjectHandler

32
_config/database.yml Normal file
View File

@ -0,0 +1,32 @@
---
Name: fulltextsearchmysql
After:
- 'databaseconnectors'
---
SilverStripe\Core\Injector\Injector:
MySQLDatabase:
class: SilverStripe\FullTextSearch\Search\Captures\SearchManipulateCapture_MySQLDatabase
MySQLPDODatabase:
class: SilverStripe\FullTextSearch\Search\Captures\SearchManipulateCapture_MySQLDatabase
---
Name: fulltextsearchpostgresql
After:
- 'postgresqlconnectors'
---
SilverStripe\Core\Injector\Injector:
PostgrePDODatabase:
class: SilverStripe\FullTextSearch\Search\Captures\SearchManipulateCapture_PostgreSQLDatabase
PostgreSQLDatabase:
class: SilverStripe\FullTextSearch\Search\Captures\SearchManipulateCapture_PostgreSQLDatabase
---
Name: fulltextsearchsqlite
After:
- 'sqlite3connectors'
---
SilverStripe\Core\Injector\Injector:
SQLite3PDODatabase:
class: SilverStripe\FullTextSearch\Search\Captures\SearchManipulateCapture_SQLite3Database
SQLite3Database:
class: SilverStripe\FullTextSearch\Search\Captures\SearchManipulateCapture_SQLite3Database

View File

@ -1,6 +1,6 @@
<?php
namespace SilverStripe\FullTextSearch\Captures;
namespace SilverStripe\FullTextSearch\Search\Captures;
use SilverStripe\ORM\Connect\MySQLDatabase;
use SilverStripe\FullTextSearch\Search\Updaters\SearchUpdater;

View File

@ -1,17 +1,16 @@
<?php
namespace SilverStripe\FullTextSearch\Captures;
namespace SilverStripe\FullTextSearch\Search\Captures;
use SilverStripe\PostgreSQL\PostgreSQLDatabase;
use SilverStripe\FullTextSearch\Search\Updaters\SearchUpdater;
if (!class_exists('PostgreSQLDatabase')) {
if (!class_exists(PostgreSQLDatabase::class)) {
return;
}
class SearchManipulateCapture_PostgreSQLDatabase extends PostgreSQLDatabase
{
public $isManipulationCapture = true;
public function manipulate($manipulation)

View File

@ -1,11 +1,11 @@
<?php
namespace SilverStripe\FullTextSearch\Captures;
namespace SilverStripe\FullTextSearch\Search\Captures;
use SilverStripe\SQLite\SQLite3Database;
use SilverStripe\FullTextSearch\Search\Updaters\SearchUpdater;
use SilverStripe\SQLite\SQLite3Database;
if (!class_exists('SQLite3Database')) {
if (!class_exists(SQLite3Database::class)) {
return;
}

View File

@ -1,12 +1,12 @@
<?php
namespace SilverStripe\FullTextSearch\Search\Updaters;
namespace SilverStripe\FullTextSearch\Search\Extensions;
use SilverStripe\ORM\DataExtension;
use SilverStripe\FullTextSearch\Search\Variants\SearchVariant;
use SilverStripe\FullTextSearch\Search\Updaters\SearchUpdater;
use SilverStripe\ORM\DataObject;
use SilverStripe\Core\ClassInfo;
use SilverStripe\FullTextSearch\Search\Updaters\SearchUpdater;
use SilverStripe\FullTextSearch\Search\Variants\SearchVariant;
use SilverStripe\ORM\DataExtension;
use SilverStripe\ORM\DataObject;
/**
* Delete operations do not use database manipulations.

View File

@ -2,20 +2,22 @@
namespace SilverStripe\FullTextSearch\Search\Processors;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Config\Configurable;
/**
* Provides batching of search updates
*/
abstract class SearchUpdateBatchedProcessor extends SearchUpdateProcessor
{
use Configurable;
/**
* List of batches to be processed
*
* @var array
*/
protected $batches;
/**
* Pointer to index of $batches assigned to $current.
* Set to 0 (first index) if not started, or count + 1 if completed.
@ -23,14 +25,14 @@ abstract class SearchUpdateBatchedProcessor extends SearchUpdateProcessor
* @var int
*/
protected $currentBatch;
/**
* List of indexes successfully comitted in the current batch
*
* @var array
*/
protected $completedIndexes;
/**
* Maximum number of record-states to process in one batch.
* Set to zero to process all records in a single batch
@ -39,7 +41,7 @@ abstract class SearchUpdateBatchedProcessor extends SearchUpdateProcessor
* @var int
*/
private static $batch_size = 100;
/**
* Up to this number of additional ids can be added to any batch in order to reduce the number
* of batches
@ -48,15 +50,15 @@ abstract class SearchUpdateBatchedProcessor extends SearchUpdateProcessor
* @var int
*/
private static $batch_soft_cap = 10;
public function __construct()
{
parent::__construct();
$this->batches = array();
$this->setBatch(0);
}
/**
* Set the current batch index
*
@ -66,14 +68,14 @@ abstract class SearchUpdateBatchedProcessor extends SearchUpdateProcessor
{
$this->currentBatch = $batch;
}
protected function getSource()
{
if (isset($this->batches[$this->currentBatch])) {
return $this->batches[$this->currentBatch];
}
}
/**
* Process the current queue
*
@ -85,20 +87,20 @@ abstract class SearchUpdateBatchedProcessor extends SearchUpdateProcessor
if (empty($this->batches)) {
return true;
}
// Don't re-process completed queue
if ($this->currentBatch >= count($this->batches)) {
return true;
}
// Send current patch to indexes
$this->prepareIndexes();
// Advance to next batch if successful
$this->setBatch($this->currentBatch + 1);
return true;
}
/**
* Segments batches acording to the specified rules
*
@ -108,12 +110,12 @@ abstract class SearchUpdateBatchedProcessor extends SearchUpdateProcessor
protected function segmentBatches($source)
{
// Measure batch_size
$batchSize = Config::inst()->get(get_class(), 'batch_size');
$batchSize = static::config()->get('batch_size');
if ($batchSize === 0) {
return array($source);
}
$softCap = Config::inst()->get(get_class(), 'batch_soft_cap');
$softCap = static::config()->get('batch_soft_cap');
// Clear batches
$batches = array();
$current = array();
@ -131,7 +133,7 @@ abstract class SearchUpdateBatchedProcessor extends SearchUpdateProcessor
if (!$ids) {
continue;
}
// Extract items from $ids until empty
while ($ids) {
// Estimate maximum number of items to take for this iteration, allowing for the soft cap
@ -141,7 +143,7 @@ abstract class SearchUpdateBatchedProcessor extends SearchUpdateProcessor
}
$items = array_slice($ids, 0, $take, true);
$ids = array_slice($ids, count($items), null, true);
// Update batch
$currentSize += count($items);
$merge = array(
@ -165,16 +167,16 @@ abstract class SearchUpdateBatchedProcessor extends SearchUpdateProcessor
if ($currentSize) {
$batches[] = $current;
}
return $batches;
}
public function batchData()
{
$this->batches = $this->segmentBatches($this->dirty);
$this->setBatch(0);
}
public function triggerProcessing()
{
$this->batchData();

View File

@ -22,9 +22,9 @@ class SearchUpdateCommitJobProcessor implements QueuedJob
* The QueuedJob queue to use when processing commits
*
* @config
* @var int
* @var string
*/
private static $commit_queue = 2; // QueuedJob::QUEUED;
private static $commit_queue = QueuedJob::QUEUED;
/**
* List of indexes to commit
@ -109,9 +109,7 @@ class SearchUpdateCommitJobProcessor implements QueuedJob
public function getSignature()
{
// There is only ever one commit job on the queue so the signature is consistent
// See QueuedJobService::queueJob() for the code that prevents duplication
return __CLASS__;
return sha1(get_class($this) . time() . mt_rand(0, 100000));
}
public function getTitle()

View File

@ -16,9 +16,9 @@ class SearchUpdateQueuedJobProcessor extends SearchUpdateBatchedProcessor implem
/**
* The QueuedJob queue to use when processing updates
* @config
* @var int
* @var string
*/
private static $reindex_queue = 2; // QueuedJob::QUEUED;
private static $reindex_queue = QueuedJob::QUEUED;
protected $messages = array();

View File

@ -5,6 +5,7 @@ namespace SilverStripe\FullTextSearch\Search\Updaters;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\ORM\Connect\Database;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DB;
use SilverStripe\FullTextSearch\Search\FullTextSearch;
@ -17,13 +18,11 @@ use ReflectionClass;
* This class is responsible for capturing changes to DataObjects and triggering index updates of the resulting dirty
* index items.
*
* Attached automatically by _config calling SearchUpdater#bind_manipulation_capture. Overloads the current database
* connector's manipulate method - basically we need to capture a manipulation _after_ all the augmentManipulation code
* (for instance Version's) is run
* Attached automatically by Injector configuration that overloads your flavour of Database class. The
* SearchManipulateCapture_[type] classes overload the manipulate method - basically we need to capture a
* manipulation _after_ all the augmentManipulation code (for instance Version's) is run
*
* Pretty closely tied to the field structure of SearchIndex.
*
* TODO: The way we bind in is awful hacky.
*/
class SearchUpdater
@ -38,39 +37,6 @@ class SearchUpdater
*/
private static $flush_on_shutdown = true;
/**
* Replace the database object with a subclass that captures all manipulations and passes them to us
*/
public static function bind_manipulation_capture()
{
global $databaseConfig;
$current = DB::get_conn();
if (!$current || !$current->getSelectedDatabase() || @$current->isManipulationCapture) {
return;
} // If not yet set, or its already captured, just return
$type = (new ReflectionClass($current))->getShortName();
$dbClass = 'SilverStripe\FullTextSearch\Captures\SearchManipulateCapture_' . $type;
// Check if Capture class exists.
if (!class_exists($dbClass)) {
return;
}
/** @var SS_Database $captured */
$captured = new $dbClass($databaseConfig);
$captured->setConnector($current->getConnector());
$captured->setQueryBuilder($current->getQueryBuilder());
$captured->setSchemaManager($current->getSchemaManager());
// The connection might have had it's name changed (like if we're currently in a test)
$captured->selectDatabase($current->getSelectedDatabase());
DB::set_conn($captured);
}
public static $registered = false;
/** @var SearchUpdateProcessor */
public static $processor = null;

View File

@ -1,21 +0,0 @@
<?php
namespace SilverStripe\FullTextSearch\Search\Updaters;
use SilverStripe\Control\RequestFilter;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
class SearchUpdater_BindManipulationCaptureFilter implements RequestFilter
{
public function preRequest(HTTPRequest $request)
{
SearchUpdater::bind_manipulation_capture();
}
public function postRequest(HTTPRequest $request, HTTPResponse $response)
{
/* NOP */
}
}

View File

@ -74,7 +74,7 @@ class SolrReindexGroupQueuedJob extends SolrReindexQueuedJobBase
$data->jobData->class = $this->class;
$data->jobData->groups = $this->groups;
$data->jobData->group = $this->group;
return $data;
}
@ -90,11 +90,6 @@ class SolrReindexGroupQueuedJob extends SolrReindexQueuedJobBase
$this->group = $jobData->group;
}
public function getSignature()
{
return md5(get_class($this) . time() . mt_rand(0, 100000));
}
public function getTitle()
{
return sprintf(

View File

@ -51,7 +51,7 @@ class SolrReindexQueuedJob extends SolrReindexQueuedJobBase
$data->jobData->batchSize = $this->batchSize;
$data->jobData->taskName = $this->taskName;
$data->jobData->classes = $this->classes;
return $data;
}
@ -65,11 +65,6 @@ class SolrReindexQueuedJob extends SolrReindexQueuedJobBase
$this->classes = $jobData->classes;
}
public function getSignature()
{
return __CLASS__;
}
public function getTitle()
{
return 'Solr Reindex Job';

View File

@ -138,6 +138,11 @@ abstract class SolrReindexQueuedJobBase implements QueuedJob
return QueuedJob::QUEUED;
}
public function getSignature()
{
return sha1(get_class($this) . time() . mt_rand(0, 100000));
}
public function addMessage($message)
{
$this->messages[] = $message;

View File

@ -15,21 +15,25 @@ use SilverStripe\FullTextSearch\Search\Processors\SearchUpdateQueuedJobProcessor
use SilverStripe\FullTextSearch\Search\Processors\SearchUpdateBatchedProcessor;
use SilverStripe\FullTextSearch\Search\Updaters\SearchUpdater;
use SilverStripe\FullTextSearch\Search\Variants\SearchVariantVersioned;
use SilverStripe\Subsites\Extensions\SiteTreeSubsites;
use SilverStripe\Subsites\Model\Subsite;
use SilverStripe\ORM\FieldType\DBDatetime;
use SilverStripe\Versioned\Versioned;
use Symbiote\QueuedJobs\Services\QueuedJob;
use Symbiote\QueuedJobs\Services\QueuedJobService;
use Symbiote\QueuedJobs\Services\QueuedJob;
/**
* Tests {@see SearchUpdateQueuedJobProcessor}
*/
class BatchedProcessorTest extends SapphireTest
{
protected $usesDatabase = true;
protected $oldProcessor;
protected static $extra_dataobjects = array(
BatchedProcessorTest_Object::class
);
protected static $extra_dataobjects = [
BatchedProcessorTest_Object::class,
];
protected static $illegal_extensions = [
SiteTree::class => [
@ -77,7 +81,7 @@ class BatchedProcessorTest extends SapphireTest
SearchUpdater::$processor = new SearchUpdateQueuedJobProcessor();
}
public function tearDown()
protected function tearDown()
{
if ($this->oldProcessor) {
SearchUpdater::$processor = $this->oldProcessor;
@ -196,21 +200,24 @@ class BatchedProcessorTest extends SapphireTest
);
}
/**
* Tests that the batch_soft_cap setting is properly respected
*/
public function testSoftCap()
{
$this->markTestIncomplete(
'@todo PostgreSQL: This test passes in isolation, but not in conjunction with the previous test'
);
$index = singleton(BatchedProcessorTest_Index::class);
$index->reset();
$processor = $this->generateDirtyIds();
// Test that increasing the soft cap to 2 will reduce the number of batches
Config::modify()->set(SearchUpdateBatchedProcessor::class, 'batch_soft_cap', 2);
$processor->batchData();
$data = $processor->getJobData();
//Debug::dump($data);die;
$this->assertEquals(8, $data->totalSteps);
// A soft cap of 1 should not fit in the hanging two items

View File

@ -30,8 +30,6 @@ class SearchUpdaterTest extends SapphireTest
self::$index->reset();
}
SearchUpdater::bind_manipulation_capture();
Config::modify()->set(Injector::class, SearchUpdateProcessor::class, array(
'class' => SearchUpdateImmediateProcessor::class
));
@ -72,17 +70,17 @@ class SearchUpdaterTest extends SapphireTest
// Check the default "writing a document updates the document"
SearchUpdater::flush_dirty_indexes();
$added = self::$index->getAdded(array('ID'));
$added = self::$index->getAdded(['ID']);
// Some databases don't output $added in a consistent order; that's okay
usort($added, function ($a, $b) {
return $a['ID']-$b['ID'];
});
$this->assertEquals($added, array(
array('ID' => $container1->ID),
array('ID' => $container2->ID),
array('ID' => $container3->ID)
));
$this->assertEquals([
['ID' => $container1->ID],
['ID' => $container2->ID],
['ID' => $container3->ID],
], $added);
// Check writing a has_one tracks back to the origin documents
@ -92,17 +90,17 @@ class SearchUpdaterTest extends SapphireTest
$hasOne->write();
SearchUpdater::flush_dirty_indexes();
$added = self::$index->getAdded(array('ID'));
$added = self::$index->getAdded(['ID']);
// Some databases don't output $added in a consistent order; that's okay
usort($added, function ($a, $b) {
return $a['ID']-$b['ID'];
});
$this->assertEquals($added, array(
array('ID' => $container1->ID),
array('ID' => $container2->ID)
));
$this->assertEquals([
['ID' => $container1->ID],
['ID' => $container2->ID],
], $added);
// Check updating an unrelated field doesn't track back
@ -112,7 +110,7 @@ class SearchUpdaterTest extends SapphireTest
$hasOne->write();
SearchUpdater::flush_dirty_indexes();
$this->assertEquals(self::$index->getAdded(array('ID')), array());
$this->assertEquals([], self::$index->getAdded(['ID']));
// Check writing a has_one tracks back to the origin documents
@ -122,9 +120,9 @@ class SearchUpdaterTest extends SapphireTest
$alternateHasOne->write();
SearchUpdater::flush_dirty_indexes();
$this->assertEquals(self::$index->getAdded(array('ID')), array(
array('ID' => $container3->ID)
));
$this->assertEquals([
['ID' => $container3->ID],
], self::$index->getAdded(['ID']));
}
public function testHasManyHook()
@ -148,10 +146,10 @@ class SearchUpdaterTest extends SapphireTest
SearchUpdater::flush_dirty_indexes();
$this->assertEquals(self::$index->getAdded(array('ID')), array(
array('ID' => $container1->ID),
array('ID' => $container2->ID)
));
$this->assertEquals([
['ID' => $container1->ID],
['ID' => $container2->ID],
], self::$index->getAdded(['ID']));
self::$index->reset();
@ -162,8 +160,8 @@ class SearchUpdaterTest extends SapphireTest
$hasMany2->write();
SearchUpdater::flush_dirty_indexes();
$this->assertEquals(self::$index->getAdded(array('ID')), array(
array('ID' => $container1->ID)
));
$this->assertEquals([
['ID' => $container1->ID],
], self::$index->getAdded(['ID']));
}
}

View File

@ -31,9 +31,7 @@ class SearchVariantSubsiteTest extends SapphireTest
self::$index = singleton(static::class);
}
SearchUpdater::bind_manipulation_capture();
Config::modify()->set(Injector::class, SearchUpdateProcessor::class, [
Config::inst()->update(Injector::class, SearchUpdateProcessor::class, [
'class' => SearchUpdateImmediateProcessor::class
]);

View File

@ -34,8 +34,6 @@ class SearchVariantVersionedTest extends SapphireTest
self::$index = singleton(SearchVariantVersionedTest_Index::class);
}
SearchUpdater::bind_manipulation_capture();
Config::modify()->set(Injector::class, SearchUpdateProcessor::class, array(
'class' => SearchUpdateImmediateProcessor::class
));

View File

@ -55,8 +55,6 @@ class SolrIndexSubsitesTest extends SapphireTest
self::$index = singleton(SolrIndexSubsitesTest_Index::class);
}
SearchUpdater::bind_manipulation_capture();
Config::modify()->set(Injector::class, SearchUpdateProcessor::class, [
'class' => SearchUpdateImmediateProcessor::class,
]);

View File

@ -46,8 +46,6 @@ class SolrIndexVersionedTest extends SapphireTest
self::$index = singleton(SolrVersionedTest_Index::class);
}
SearchUpdater::bind_manipulation_capture();
Config::modify()->set(Injector::class, SearchUpdateProcessor::class, [
'class' => SearchUpdateImmediateProcessor::class
]);