Skip to content

Commit d37e58b

Browse files
committed
TASK: Some code cleanup
1 parent 98dfdd0 commit d37e58b

9 files changed

Lines changed: 77 additions & 34 deletions

File tree

Classes/Command/NodeIndexCommandController.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use Doctrine\Common\Collections\ArrayCollection;
1717
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Driver\NodeTypeMappingBuilderInterface;
18+
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\EsProfiler;
1819
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception\RuntimeException;
1920
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Indexer\Error\ErrorInterface;
2021
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Indexer\NodeIndexer;
@@ -226,15 +227,15 @@ public function buildCommand(int $limit = null, bool $update = false, string $wo
226227
$postfix = (string)($postfix ?: time());
227228
$this->nodeIndexer->setIndexNamePostfix((string)$postfix);
228229

229-
$create = function (array $dimensionsValues) use ($update, $postfix) {
230+
$createMapping = function (array $dimensionsValues) use ($update, $postfix) {
230231
$this->executeInternalCommand('createInternal', [
231232
'dimensionsValues' => json_encode($dimensionsValues),
232233
'update' => $update,
233234
'postfix' => $postfix,
234235
]);
235236
};
236237

237-
$build = function (array $dimensionsValues) use ($workspace, $limit, $update, $postfix) {
238+
$buildIndex = function (array $dimensionsValues) use ($workspace, $limit, $update, $postfix) {
238239
$this->build($dimensionsValues, $workspace, $postfix, $limit);
239240
};
240241

@@ -262,8 +263,10 @@ public function buildCommand(int $limit = null, bool $update = false, string $wo
262263
$this->outputLine('<success>Done</success> (took %s seconds)', [number_format(microtime(true) - $timeStart, 2)]);
263264
};
264265

265-
$runAndLog($create, 'Create indicies');
266-
$runAndLog($build, 'Indexing nodes');
266+
$runAndLog($createMapping, 'Create indicies');
267+
$runAndLog($buildIndex, 'Indexing nodes');
268+
$this->outputLine('Batchtime: ' . EsProfiler::getAndReset('estime'));
269+
267270
$runAndLog($refresh, 'Refresh indicies');
268271
$runAndLog($updateAliases, 'Update aliases');
269272

@@ -334,6 +337,7 @@ private function build(array $dimensionsValues, ?string $workspace = null, ?stri
334337
* @throws \Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception
335338
* @throws \Flowpack\ElasticSearch\Exception
336339
* @throws \Neos\Flow\Http\Exception
340+
* @throws \Exception
337341
* @Flow\Internal
338342
*/
339343
public function createInternalCommand(string $dimensionsValues, bool $update = false, ?string $postfix = null): void
@@ -375,7 +379,7 @@ public function buildWorkspaceInternalCommand(string $workspace, string $dimensi
375379
$this->outputLine($message);
376380
};
377381

378-
$count = $this->workspaceIndexer->indexWithDimensions($workspace, $dimensionsValuesArray, $limit, $workspaceLogger);
382+
$this->workspaceIndexer->indexWithDimensions($workspace, $dimensionsValuesArray, $limit, $workspaceLogger);
379383

380384
$this->outputErrorHandling();
381385
}

Classes/Driver/RequestDriverInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ interface RequestDriverInterface
2525
*
2626
* @param Index $index
2727
* @param array|string $request an array or a raw JSON request payload
28-
* @return array
28+
* @return array An array of respones per batch entry.
2929
*/
3030
public function bulk(Index $index, $request): array;
3131
}

Classes/Driver/Version5/IndexerDriver.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
declare(strict_types=1);
43

54
namespace Flowpack\ElasticSearch\ContentRepositoryAdaptor\Driver\Version5;

Classes/EsProfiler.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Flowpack\ElasticSearch\ContentRepositoryAdaptor;
5+
6+
/*
7+
* (c) 2019 punkt.de GmbH - Karlsruhe, Germany - http://punkt.de
8+
* All rights reserved.
9+
*/
10+
11+
class EsProfiler
12+
{
13+
protected static $time = 0;
14+
15+
protected static $nodes = 0;
16+
17+
public static function add(int $data, string $type = 'time'): void
18+
{
19+
self::$$type += $data;
20+
}
21+
22+
public static function get( string $type = 'time'): int
23+
{
24+
return self::$$type;
25+
}
26+
27+
public static function getAndReset( string $type = 'time'): int
28+
{
29+
$$type = self::$$type;
30+
self::$$type = 0;
31+
return $$type;
32+
}
33+
}

Classes/Indexer/BulkRequestPart.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
<?php
2-
32
declare(strict_types=1);
43

4+
/*
5+
* This file is part of the Flowpack.ElasticSearch.ContentRepositoryAdaptor package.
6+
*
7+
* (c) Contributors of the Neos Project - www.neos.io
8+
*
9+
* This package is Open Source Software. For the full copyright and license
10+
* information, please view the LICENSE file which was distributed with this
11+
* source code.
12+
*/
13+
514
namespace Flowpack\ElasticSearch\ContentRepositoryAdaptor\Indexer;
615

716
use Generator;

Classes/Indexer/Error/BulkIndexingError.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
declare(strict_types=1);
43

54
namespace Flowpack\ElasticSearch\ContentRepositoryAdaptor\Indexer\Error;

Classes/Indexer/NodeIndexer.php

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,9 @@
3636
use Neos\ContentRepository\Domain\Service\ContextFactory;
3737
use Neos\ContentRepository\Search\Indexer\AbstractNodeIndexer;
3838
use Neos\ContentRepository\Search\Indexer\BulkNodeIndexerInterface;
39-
use Neos\ContentRepository\Utility;
4039
use Neos\Flow\Annotations as Flow;
4140
use Neos\Flow\Persistence\Exception\IllegalObjectTypeException;
42-
use Neos\Utility\Files;
41+
use Neos\Utility\Exception\FilesException;
4342
use Neos\Flow\Log\Utility\LogEnvironment;
4443
use Psr\Log\LoggerInterface;
4544

@@ -208,6 +207,7 @@ public function getIndex(): Index
208207
* @param NodeInterface $node
209208
* @param string $targetWorkspaceName In case indexing is triggered during publishing, a target workspace name will be passed in
210209
* @return void
210+
* @throws Exception
211211
*/
212212
public function indexNode(NodeInterface $node, $targetWorkspaceName = null): void
213213
{
@@ -271,7 +271,7 @@ public function indexNode(NodeInterface $node, $targetWorkspaceName = null): voi
271271
$handleNode = function (NodeInterface $node, Context $context) use ($targetWorkspaceName, $indexer) {
272272
$nodeFromContext = $context->getNodeByIdentifier($node->getIdentifier());
273273
if ($nodeFromContext instanceof NodeInterface) {
274-
$this->searchClient->withDimensions(function () use ($indexer, $nodeFromContext, $targetWorkspaceName) {
274+
$this->searchClient->withDimensions(static function () use ($indexer, $nodeFromContext, $targetWorkspaceName) {
275275
$indexer($nodeFromContext, $targetWorkspaceName);
276276
}, $nodeFromContext->getContext()->getTargetDimensions());
277277
} else {
@@ -315,15 +315,18 @@ protected function createContentContext(string $workspaceName, array $dimensions
315315

316316
/**
317317
* @param NodeInterface $node
318-
* @param array|null $tuple
318+
* @param array|null $requests
319+
* @throws Exception
320+
* @throws \Flowpack\ElasticSearch\Exception
321+
* @throws FilesException
319322
*/
320-
protected function toBulkRequest(NodeInterface $node, array $tuple = null)
323+
protected function toBulkRequest(NodeInterface $node, array $requests = null): void
321324
{
322-
if ($tuple === null) {
325+
if ($requests === null) {
323326
return;
324327
}
325328

326-
$this->currentBulkRequest[] = new BulkRequestPart($this->dimensionService->hashByNode($node), $tuple);
329+
$this->currentBulkRequest[] = new BulkRequestPart($this->dimensionService->hashByNode($node), $requests);
327330
$this->flushIfNeeded();
328331
}
329332

@@ -352,7 +355,10 @@ protected function calculateDocumentIdentifier(NodeInterface $node, $targetWorks
352355
* @param NodeInterface $node
353356
* @param string $targetWorkspaceName
354357
* @return void
358+
* @throws Exception
359+
* @throws FilesException
355360
* @throws IllegalObjectTypeException
361+
* @throws \Flowpack\ElasticSearch\Exception
356362
*/
357363
public function removeNode(NodeInterface $node, string $targetWorkspaceName = null): void
358364
{
@@ -380,7 +386,7 @@ public function removeNode(NodeInterface $node, string $targetWorkspaceName = nu
380386
/**
381387
* @throws Exception
382388
* @throws \Flowpack\ElasticSearch\Exception
383-
* @throws \Neos\Utility\Exception\FilesException
389+
* @throws FilesException
384390
*/
385391
protected function flushIfNeeded(): void
386392
{
@@ -391,7 +397,7 @@ protected function flushIfNeeded(): void
391397

392398
protected function bulkRequestSize(): int
393399
{
394-
return array_reduce($this->currentBulkRequest, function ($sum, BulkRequestPart $request) {
400+
return array_reduce($this->currentBulkRequest, static function ($sum, BulkRequestPart $request) {
395401
return $sum + $request->getSize();
396402
}, 0);
397403
}
@@ -407,7 +413,7 @@ protected function bulkRequestLenght(): int
407413
* @return void
408414
* @throws Exception
409415
* @throws \Flowpack\ElasticSearch\Exception
410-
* @throws \Neos\Utility\Exception\FilesException
416+
* @throws FilesException
411417
*/
412418
public function flush(): void
413419
{
@@ -450,32 +456,23 @@ public function flush(): void
450456
return;
451457
}
452458

453-
$logDirectory = FLOW_PATH_DATA . 'Logs/ElasticSearch/';
454-
if (!@is_dir($logDirectory)) {
455-
Files::createDirectoryRecursively($logDirectory);
456-
}
457-
458-
// TODO: Remove fileystem logging
459459
foreach ($this->dimensionService->getDimensionsRegistry() as $hash => $dimensions) {
460460
if (!isset($payload[$hash])) {
461461
continue;
462462
}
463+
463464
$this->searchClient->setDimensions($dimensions);
465+
464466
$response = $this->requestDriver->bulk($this->getIndex(), implode(chr(10), $payload[$hash]));
467+
468+
465469
foreach ($response as $responseLine) {
466470
if (isset($response['errors']) && $response['errors'] !== false) {
467-
$this->errorHandlingService->log(
468-
new BulkIndexingError($this->currentBulkRequest, $responseLine)
469-
);
470-
file_put_contents($logDirectory . 'BulkIndexing_Error_' . time() . '.json', json_encode([
471-
'request' => $payload[$hash],
472-
'response' => $responseLine
473-
], JSON_PRETTY_PRINT));
471+
$this->errorHandlingService->log(new BulkIndexingError($this->currentBulkRequest, $responseLine));
474472
}
475473
}
476474
}
477475

478-
479476
$this->reset();
480477
}
481478

Classes/Indexer/WorkspaceIndexer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,10 @@ public function indexWithDimensions(string $workspaceName, array $dimensions = [
9393
if ($limit !== null && $indexedNodes > $limit) {
9494
return;
9595
}
96+
9697
$this->nodeIndexingManager->indexNode($currentNode);
9798
$indexedNodes++;
99+
98100
array_map(function (NodeInterface $childNode) use ($traverseNodes, &$indexedNodes) {
99101
$traverseNodes($childNode, $indexedNodes);
100102
}, $currentNode->getChildNodes());

Classes/Service/DimensionsService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function hash(array $dimensionValues): ?string
3737
if ($dimensionValues === []) {
3838
return null;
3939
}
40-
$this->lastTargetDimensions = array_map(function ($dimensionValues) {
40+
$this->lastTargetDimensions = array_map(static function ($dimensionValues) {
4141
return [\is_array($dimensionValues) ? array_shift($dimensionValues) : $dimensionValues];
4242
}, $dimensionValues);
4343

0 commit comments

Comments
 (0)