Skip to content

Commit

Permalink
Instance Cache added to DocumentParser::parse() (#788)
Browse files Browse the repository at this point in the history
- I've discovered that this method was being called a lot of times when indexing large volumes of documents with my bulk indexer code.
- Constantly parsing a Reflection class was becoming too costly so I've added an instance cache to store the document information.
  • Loading branch information
carlcasbolt authored and saimaz committed May 17, 2017
1 parent 1bb0c31 commit b638601
Showing 1 changed file with 38 additions and 28 deletions.
66 changes: 38 additions & 28 deletions Mapping/DocumentParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ class DocumentParser
*/
private $properties = [];

/**
* @var array Local cache for documents
*/
private $documents = [];

/**
* @param Reader $reader Used for reading annotations.
* @param DocumentFinder $finder Used for resolving namespaces.
Expand All @@ -86,35 +91,40 @@ public function __construct(Reader $reader, DocumentFinder $finder)
*/
public function parse(\ReflectionClass $class)
{
/** @var Document $document */
$document = $this->reader->getClassAnnotation($class, self::DOCUMENT_ANNOTATION);

if ($document === null) {
throw new MissingDocumentAnnotationException(
sprintf(
'"%s" class cannot be parsed as document because @Document annotation is missing.',
$class->getName()
)
);
}
$className = $class->getName();

$fields = [];

return [
'type' => $document->type ?: Caser::snake($class->getShortName()),
'properties' => $this->getProperties($class),
'fields' => array_filter(
array_merge(
$document->dump(),
$fields
)
),
'aliases' => $this->getAliases($class, $fields),
'analyzers' => $this->getAnalyzers($class),
'objects' => $this->getObjects(),
'namespace' => $class->getName(),
'class' => $class->getShortName(),
];
if (!isset($this->documents[$className])) {
/** @var Document $document */
$document = $this->reader->getClassAnnotation($class, self::DOCUMENT_ANNOTATION);

if ($document === null) {
throw new MissingDocumentAnnotationException(
sprintf(
'"%s" class cannot be parsed as document because @Document annotation is missing.',
$class->getName()
)
);
}

$fields = [];

$this->documents[$className] = [
'type' => $document->type ?: Caser::snake($class->getShortName()),
'properties' => $this->getProperties($class),
'fields' => array_filter(
array_merge(
$document->dump(),
$fields
)
),
'aliases' => $this->getAliases($class, $fields),
'analyzers' => $this->getAnalyzers($class),
'objects' => $this->getObjects(),
'namespace' => $class->getName(),
'class' => $class->getShortName(),
];
}
return $this->documents[$className];
}

/**
Expand Down

0 comments on commit b638601

Please sign in to comment.