stephpy/timeline-bundle

prefer using hash in ActionManager::findOrCreateComponent

Closed this issue · 1 comments

Pain point

when the table grows a lot (million of lines), hash is an indexed column while model and identifier are not 😢 .

The sadness resides in this picture:
image

Change proposal

The happiness resides in this picture ⚡️ 🦄 🎉
image (1)

So, to implement that, I suggest to change ActionManager::findOrCreateComponent() implementation to use the hash:

public function findOrCreateComponent($model, $identifier = null, $flush = true)
{
$resolvedComponentData = $this->resolveModelAndIdentifier($model, $identifier);
$component = $this->getComponentRepository()
->createQueryBuilder('c')
->where('c.model = :model')
->andWhere('c.identifier = :identifier')
->setParameter('model', $resolvedComponentData->getModel())
->setParameter('identifier', serialize($resolvedComponentData->getIdentifier()))
->getQuery()
->getOneOrNullResult()
;
if ($component) {
$component->setData($resolvedComponentData->getData());
return $component;
}
return $this->createComponentFromResolvedComponentData($resolvedComponentData, $flush);
}

    public function findOrCreateComponent($model, $identifier = null, $flush = true)
    {
        $resolvedComponentData = $this->resolveModelAndIdentifier($model, $identifier);
        $component = $this->getComponentRepository()
            ->createQueryBuilder('c')
            ->where('c.hash = :hash')
           // Imply to add getHash method into https://github.com/stephpy/timeline/blob/d059ba3c2ffbefc2ec6d8ac68c7d6037b890b487/src/ResolveComponent/ValueObject/ResolveComponentModelIdentifier.php
            ->setParameter('hash', $resolvedComponentData->getHash())
            ->getQuery()
            ->getOneOrNullResult()
        ;
        if ($component) {
            $component->setData($resolvedComponentData->getData());
            return $component;
        }
        return $this->createComponentFromResolvedComponentData($resolvedComponentData, $flush);
    }