- add : cache on es "tags"

- removed : variable globalstructure construction (WITH_FIELDS, WITH_FLAGS...) (was unused)
- cleanup (stopwatches etc)
This commit is contained in:
jygaulier
2020-11-05 19:53:00 +01:00
parent 98e0d6ee72
commit cfd385498d
7 changed files with 74 additions and 148 deletions

View File

@@ -9,8 +9,7 @@ use Alchemy\Phrasea\SearchEngine\Elastic\Structure\Field;
use Alchemy\Phrasea\SearchEngine\Elastic\Structure\Flag;
use Alchemy\Phrasea\SearchEngine\Elastic\Structure\GlobalStructure;
use Alchemy\Phrasea\SearchEngine\Elastic\Structure\MetadataHelper;
use Alchemy\Phrasea\SearchEngine\Elastic\Structure\Structure;
use Alchemy\Phrasea\Utilities\Stopwatch;
// use Alchemy\Phrasea\Utilities\Stopwatch;
use databox;
class SearchEngineStructure
@@ -30,20 +29,18 @@ class SearchEngineStructure
/**
* @param databox[] $databoxes
* @param int $what bitmask of what should be included in this structure, in fields, ...
*
* @return GlobalStructure
*/
public function getGlobalStructureFromDataboxes(array $databoxes, $what = Structure::WITH_EVERYTHING)
public function getGlobalStructureFromDataboxes(array $databoxes)
{
$fields = [];
$flags = [];
$stopwatch = new Stopwatch("getGlobalStructureFromDataboxes");
// $stopwatch = new Stopwatch("getGlobalStructureFromDataboxes");
foreach ($databoxes as $databox) {
// we will cache both FIELDS and FLAGS in the same entry :
// it's small data and $what seems always WITH_EVERYTHING
// we will cache both FIELDS and FLAGS in the same entry : it's small data
$k = $this->getCacheKey("FieldsAndFlags", $databox);
try {
$data = $this->cache->get($k);
@@ -57,7 +54,7 @@ class SearchEngineStructure
'flags' => []
];
foreach ($databox->get_meta_structure() as $fieldStructure) {
$data['fields'][] = Field::createFromLegacyField($fieldStructure, $what);
$data['fields'][] = Field::createFromLegacyField($fieldStructure);
}
foreach ($databox->getStatusStructure() as $status) {
$data['flags'][] = Flag::createFromLegacyStatus($status);
@@ -65,20 +62,29 @@ class SearchEngineStructure
$this->cache->save($k, $data);
}
if($what & Structure::STRUCTURE_WITH_FIELDS) {
$fields = array_merge($fields, $data['fields']);
}
if($what & Structure::STRUCTURE_WITH_FLAGS) {
$flags = array_merge($flags, $data['flags']);
}
$fields = array_merge($fields, $data['fields']);
$flags = array_merge($flags, $data['flags']);
}
$stopwatch->lap('loop0');
$r = new GlobalStructure($fields, $flags, MetadataHelper::createTags());
// $stopwatch->lap('loop0');
$stopwatch->log();
// tags does not depends on db
$k = $this->getCacheKey("Tags");
try {
$tags = $this->cache->get($k);
}
catch(\Exception $e) {
$tags = false;
}
if($tags === false) {
$this->cache->save($k, ($tags = MetadataHelper::createTags()));
// nb : tags is a hardcoded list, we don't need to clear this cache
}
return $r;
// $r = new GlobalStructure($fields, $flags, $tags);
// $stopwatch->log();
// return $r;
return new GlobalStructure($fields, $flags, $tags);
}
public function deleteFromCache($databox)
@@ -87,8 +93,15 @@ class SearchEngineStructure
$this->cache->delete($k);
}
private function getCacheKey($what, databox $db)
/**
* build a cache key to store es data, related to a db or not (if db is null)
*
* @param $what
* @param databox|null $db
* @return string
*/
private function getCacheKey($what, databox $db = null)
{
return "es_db-" . $db->get_sbas_id() . '_' . $what;
return 'es' . ($db ? ('_db-'.$db->get_sbas_id()) : '') . '_' . $what;
}
}