Skip to content
This repository was archived by the owner on Mar 6, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ before_script:
- composer install

script:
- bin/console index:build
- composer integrate
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"phpstan/phpstan": "^0.12.0",
"phpactor/test-utils": "^1.0.2",
"phpactor/console-extension": "^0.1.2",
"symfony/var-dumper": "^5.1"
"symfony/var-dumper": "^5.1",
"phpbench/phpbench": "^1.0"
},
"autoload": {
"psr-4": {
Expand All @@ -55,7 +56,8 @@
"integrate": [
"vendor/bin/phpstan analyse",
"vendor/bin/php-cs-fixer fix",
"vendor/bin/phpunit"
"vendor/bin/phpunit",
"vendor/bin/phpbench run"
]
}
}
16 changes: 15 additions & 1 deletion lib/Adapter/Php/FileSearchIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,19 @@ public function search(string $query): Generator
}
}

public function remove(Record $record): void
{
if (!isset($this->subjects[$this->recordKey($record)])) {
return;
}

unset($this->subjects[$this->recordKey($record)]);
}

public function write(Record $record): void
{
$this->open();
$this->subjects[$record->recordType().$record->identifier()] = [$record->recordType(), $record->identifier()];
$this->subjects[$this->recordKey($record)] = [$record->recordType(), $record->identifier()];

if (++$this->counter % self::BATCH_SIZE === 0) {
$this->flush();
Expand Down Expand Up @@ -107,4 +116,9 @@ private function open(): void

$this->initialized = true;
}

private function recordKey(Record $record): string
{
return $record->recordType().$record->identifier();
}
}
70 changes: 26 additions & 44 deletions lib/Adapter/Php/InMemory/InMemoryIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,13 @@
use Generator;
use Phpactor\Indexer\Model\Index;
use Phpactor\Indexer\Model\IndexQueryAgent;
use RuntimeException;
use Phpactor\Indexer\Model\Record\FunctionRecord;
use Phpactor\Indexer\Model\Record\ClassRecord;
use Phpactor\Indexer\Model\Record;
use SplFileInfo;

class InMemoryIndex implements Index
{
/**
* @var InMemoryRepository
*/
private $repository;

/**
* @var int
* @var int|null
*/
private $lastUpdate;

Expand All @@ -28,11 +20,19 @@ class InMemoryIndex implements Index
*/
private $searchIndex;

public function __construct(?InMemoryRepository $repository = null)
/**
* @var array<Record>
*/
private $index;

/**
* @param array<Record> $index
*/
public function __construct(array $index = [])
{
$this->repository = $repository ?: new InMemoryRepository();
$this->searchIndex = new InMemorySearchIndex();
$this->lastUpdate = 0;
$this->index = $index;
}

public function lastUpdate(): int
Expand All @@ -47,43 +47,20 @@ public function query(): IndexQueryAgent

public function write(Record $record): void
{
$this->index[$this->recordKey($record)] = $record;
$this->searchIndex->write($record);

if ($record instanceof ClassRecord) {
$this->repository->putClass($record);
return;
}

if ($record instanceof FunctionRecord) {
$this->repository->putFunction($record);
return;
}

throw new RuntimeException(sprintf(
'Do not know how to index "%s"',
get_class($record)
));
}

/**
* {@inheritDoc}
*/
public function get(Record $record): Record
{
if ($record instanceof ClassRecord) {
// @phpstan-ignore-next-line
return $this->repository->getClass($record->fqn()) ?? $record;
}
$key = $this->recordKey($record);

if ($record instanceof FunctionRecord) {
// @phpstan-ignore-next-line
return $this->repository->getFunction($record->fqn()) ?? $record;
if (isset($this->index[$key])) {
/** @phpstan-ignore-next-line */
return $this->index[$key];
}

throw new RuntimeException(sprintf(
'Do not know how to index "%s"',
get_class($record)
));
return $record;
}

public function isFresh(SplFileInfo $fileInfo): bool
Expand All @@ -93,22 +70,22 @@ public function isFresh(SplFileInfo $fileInfo): bool

public function reset(): void
{
$this->repository->reset();
$this->index = [];
}

public function exists(): bool
{
return $this->repository->lastUpdate !== 0;
return $this->lastUpdate !== 0;
}

public function done(): void
{
$this->repository->lastUpdate = time();
$this->lastUpdate = time();
}

public function has(Record $record): bool
{
return false;
return isset($this->index[$this->recordKey($record)]);
}

/**
Expand All @@ -118,4 +95,9 @@ public function search(string $search): Generator
{
yield from $this->searchIndex->search($search);
}

private function recordKey(Record $record): string
{
return $record->recordType().$record->identifier();
}
}
58 changes: 0 additions & 58 deletions lib/Adapter/Php/InMemory/InMemoryRepository.php

This file was deleted.

10 changes: 10 additions & 0 deletions lib/Adapter/Php/InMemory/InMemorySearchIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,17 @@ public function write(Record $record): void
$this->buffer[$record->identifier()] = [$record->recordType(), $record->identifier()];
}

public function has(Record $record): bool
{
return isset($this->buffer[$record->identifier()]);
}

public function flush(): void
{
}

public function remove(Record $record): void
{
unset($this->buffer[$record->identifier()]);
}
}
2 changes: 2 additions & 0 deletions lib/Model/SearchIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ public function search(string $query): Generator;

public function write(Record $record): void;

public function remove(Record $record): void;

public function flush(): void;
}
5 changes: 5 additions & 0 deletions lib/Model/SearchIndex/FilteredSearchIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,9 @@ public function flush(): void
{
$this->innerIndex->flush();
}

public function remove(Record $record): void
{
$this->innerIndex->remove($record);
}
}
70 changes: 70 additions & 0 deletions lib/Model/SearchIndex/ValidatingSearchIndex.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Phpactor\Indexer\Model\SearchIndex;

use Generator;
use Phpactor\Indexer\Model\Index;
use Phpactor\Indexer\Model\Record;
use Phpactor\Indexer\Model\Record\HasPath;
use Phpactor\Indexer\Model\SearchIndex;

class ValidatingSearchIndex implements SearchIndex
{
/**
* @var SearchIndex
*/
private $innerIndex;

/**
* @var Index
*/
private $index;

public function __construct(SearchIndex $innerIndex, Index $index)
{
$this->innerIndex = $innerIndex;
$this->index = $index;
}

/**
* {@inheritDoc}
*/
public function search(string $query): Generator
{
foreach ($this->innerIndex->search($query) as $result) {
if (!$this->index->has($result)) {
$this->innerIndex->remove($result);
continue;
}

$record = $this->index->get($result);

if (!$record instanceof HasPath) {
yield $result;
return;
}

if (!file_exists($record->filePath())) {
$this->innerIndex->remove($record);
continue;
}

yield $result;
}
}

public function write(Record $record): void
{
$this->innerIndex->write($record);
}

public function remove(Record $record): void
{
$this->innerIndex->remove($record);
}

public function flush(): void
{
$this->innerIndex->flush();
}
}
Loading