Differential D17384 Diff 42244 src/applications/search/fulltextstorage/PhabricatorFulltextStorageEngine.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/search/fulltextstorage/PhabricatorFulltextStorageEngine.php
<?php | <?php | ||||
/** | /** | ||||
* Base class for Phabricator search engine providers. Each engine must offer | * Base class for Phabricator search engine providers. Each engine must offer | ||||
* three capabilities: indexing, searching, and reconstruction (this can be | * three capabilities: indexing, searching, and reconstruction (this can be | ||||
* stubbed out if an engine can't reasonably do it, it is used for debugging). | * stubbed out if an engine can't reasonably do it, it is used for debugging). | ||||
*/ | */ | ||||
abstract class PhabricatorFulltextStorageEngine extends Phobject { | abstract class PhabricatorFulltextStorageEngine extends Phobject { | ||||
/* -( Engine Metadata )---------------------------------------------------- */ | protected $service; | ||||
public function getHosts() { | |||||
return $this->service->getHosts(); | |||||
} | |||||
public function setService(PhabricatorSearchService $service) { | |||||
$this->service = $service; | |||||
return $this; | |||||
} | |||||
/** | /** | ||||
* Return a unique, nonempty string which identifies this storage engine. | * @return PhabricatorSearchService | ||||
* | |||||
* @return string Unique string for this engine, max length 32. | |||||
* @task meta | |||||
*/ | */ | ||||
abstract public function getEngineIdentifier(); | public function getService() { | ||||
return $this->service; | |||||
} | |||||
/** | /** | ||||
* Prioritize this engine relative to other engines. | * Implementations must return a prototype host instance which is cloned | ||||
* | * by the PhabricatorSearchService infrastructure to configure each engine. | ||||
* Engines with a smaller priority number get an opportunity to write files | * @return PhabricatorSearchHost | ||||
* first. Generally, lower-latency filestores should have lower priority | |||||
* numbers, and higher-latency filestores should have higher priority | |||||
* numbers. Setting priority to approximately the number of milliseconds of | |||||
* read latency will generally produce reasonable results. | |||||
* | |||||
* In conjunction with filesize limits, the goal is to store small files like | |||||
* profile images, thumbnails, and text snippets in lower-latency engines, | |||||
* and store large files in higher-capacity engines. | |||||
* | |||||
* @return float Engine priority. | |||||
* @task meta | |||||
*/ | */ | ||||
abstract public function getEnginePriority(); | abstract public function getHostType(); | ||||
/* -( Engine Metadata )---------------------------------------------------- */ | |||||
/** | /** | ||||
* Return `true` if the engine is currently writable. | * Return a unique, nonempty string which identifies this storage engine. | ||||
* | |||||
* Engines that are disabled or missing configuration should return `false` | |||||
* to prevent new writes. If writes were made with this engine in the past, | |||||
* the application may still try to perform reads. | |||||
* | * | ||||
* @return bool True if this engine can support new writes. | * @return string Unique string for this engine, max length 32. | ||||
* @task meta | * @task meta | ||||
*/ | */ | ||||
abstract public function isEnabled(); | abstract public function getEngineIdentifier(); | ||||
/* -( Managing Documents )------------------------------------------------- */ | /* -( Managing Documents )------------------------------------------------- */ | ||||
/** | /** | ||||
* Update the index for an abstract document. | * Update the index for an abstract document. | ||||
* | * | ||||
* @param PhabricatorSearchAbstractDocument Document to update. | * @param PhabricatorSearchAbstractDocument Document to update. | ||||
* @return void | * @return void | ||||
Show All 21 Lines | /* -( Managing Documents )------------------------------------------------- */ | ||||
/** | /** | ||||
* Does the search index exist? | * Does the search index exist? | ||||
* | * | ||||
* @return bool | * @return bool | ||||
*/ | */ | ||||
abstract public function indexExists(); | abstract public function indexExists(); | ||||
/** | /** | ||||
* Implementations should override this method to return a dictionary of | |||||
* stats which are suitable for display in the admin UI. | |||||
*/ | |||||
abstract public function getIndexStats(); | |||||
/** | |||||
* Is the index in a usable state? | * Is the index in a usable state? | ||||
* | * | ||||
* @return bool | * @return bool | ||||
*/ | */ | ||||
public function indexIsSane() { | public function indexIsSane() { | ||||
return $this->indexExists(); | return $this->indexExists(); | ||||
} | } | ||||
/** | /** | ||||
* Do any sort of setup for the search index. | * Do any sort of setup for the search index. | ||||
* | * | ||||
* @return void | * @return void | ||||
*/ | */ | ||||
public function initIndex() {} | public function initIndex() {} | ||||
/* -( Loading Storage Engines )-------------------------------------------- */ | |||||
/** | |||||
* @task load | |||||
*/ | |||||
public static function loadAllEngines() { | |||||
return id(new PhutilClassMapQuery()) | |||||
->setAncestorClass(__CLASS__) | |||||
->setUniqueMethod('getEngineIdentifier') | |||||
->setSortMethod('getEnginePriority') | |||||
->execute(); | |||||
} | |||||
/** | |||||
* @task load | |||||
*/ | |||||
public static function loadActiveEngines() { | |||||
$engines = self::loadAllEngines(); | |||||
$active = array(); | |||||
foreach ($engines as $key => $engine) { | |||||
if (!$engine->isEnabled()) { | |||||
continue; | |||||
} | |||||
$active[$key] = $engine; | |||||
} | |||||
return $active; | |||||
} | |||||
public static function loadEngine() { | |||||
return head(self::loadActiveEngines()); | |||||
} | |||||
} | } |