Skip to content

Smart eager loading #172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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
52 changes: 44 additions & 8 deletions src/AbstractTDBMObject.php
Original file line number Diff line number Diff line change
@@ -22,8 +22,11 @@
*/

use JsonSerializable;
use TheCodingMachine\TDBM\QueryFactory\SmartEagerLoad\Query\PartialQuery;
use TheCodingMachine\TDBM\QueryFactory\SmartEagerLoad\StorageNode;
use TheCodingMachine\TDBM\Schema\ForeignKeys;
use TheCodingMachine\TDBM\Utils\ManyToManyRelationshipPathDescriptor;
use function array_combine;

/**
* Instances of this class represent a "bean". Usually, a bean is mapped to a row of one table.
@@ -77,6 +80,13 @@ abstract class AbstractTDBMObject implements JsonSerializable
*/
private $manyToOneRelationships = [];

/**
* If this bean originates from a ResultArray, this points back to the result array to build smart eager load queries.
*
* @var PartialQuery|null
*/
private $partialQuery;

/**
* Used with $primaryKeys when we want to retrieve an existing object
* and $primaryKeys=[] if we want a new object.
@@ -113,12 +123,13 @@ public function __construct(?string $tableName = null, array $primaryKeys = [],
* @param array[] $beanData array<table, array<column, value>>
* @param TDBMService $tdbmService
*/
public function _constructFromData(array $beanData, TDBMService $tdbmService): void
public function _constructFromData(array $beanData, TDBMService $tdbmService, ?PartialQuery $partialQuery): void
{
$this->tdbmService = $tdbmService;
$this->partialQuery = $partialQuery;

foreach ($beanData as $table => $columns) {
$this->dbRows[$table] = new DbRow($this, $table, static::getForeignKeys($table), $tdbmService->_getPrimaryKeysFromObjectData($table, $columns), $tdbmService, $columns);
$this->dbRows[$table] = new DbRow($this, $table, static::getForeignKeys($table), $tdbmService->_getPrimaryKeysFromObjectData($table, $columns), $tdbmService, $columns, $partialQuery);
}

$this->status = TDBMObjectStateEnum::STATE_LOADED;
@@ -131,11 +142,12 @@ public function _constructFromData(array $beanData, TDBMService $tdbmService): v
* @param mixed[] $primaryKeys
* @param TDBMService $tdbmService
*/
public function _constructLazy(string $tableName, array $primaryKeys, TDBMService $tdbmService): void
public function _constructLazy(string $tableName, array $primaryKeys, TDBMService $tdbmService, ?PartialQuery $partialQuery): void
{
$this->tdbmService = $tdbmService;
$this->partialQuery = $partialQuery;

$this->dbRows[$tableName] = new DbRow($this, $tableName, static::getForeignKeys($tableName), $primaryKeys, $tdbmService);
$this->dbRows[$tableName] = new DbRow($this, $tableName, static::getForeignKeys($tableName), $primaryKeys, $tdbmService, [], $partialQuery);

$this->status = TDBMObjectStateEnum::STATE_NOT_LOADED;
}
@@ -179,7 +191,7 @@ public function _setStatus(string $state): void
{
$this->status = $state;

// The dirty state comes form the db_row itself so there is no need to set it from the called.
// The dirty state comes from the db_row itself so there is no need to set it from the called.
if ($state !== TDBMObjectStateEnum::STATE_DIRTY) {
foreach ($this->dbRows as $dbRow) {
$dbRow->_setStatus($state);
@@ -520,19 +532,29 @@ private function removeManyToOneRelationship(string $tableName, string $foreignK
*
* @param string $tableName
* @param string $foreignKeyName
* @param mixed[] $searchFilter
* @param string $orderString The ORDER BY part of the query. All columns must be prefixed by the table name (in the form: table.column). WARNING : This parameter is not kept when there is an additionnal or removal object !
* @param array<int, string> $localColumns
* @param array<int, string> $foreignColumns
* @param string $foreignTableName
* @param string $orderString The ORDER BY part of the query. All columns must be prefixed by the table name (in the form: table.column). WARNING : This parameter is not kept when there is an additional or removal object !
*
* @return AlterableResultIterator
* @throws TDBMException
*/
protected function retrieveManyToOneRelationshipsStorage(string $tableName, string $foreignKeyName, array $searchFilter, string $orderString = null) : AlterableResultIterator
protected function retrieveManyToOneRelationshipsStorage(string $tableName, string $foreignKeyName, array $localColumns, array $foreignColumns, string $foreignTableName, string $orderString = null) : AlterableResultIterator
{
$key = $tableName.'___'.$foreignKeyName;
$alterableResultIterator = $this->getManyToOneAlterableResultIterator($tableName, $foreignKeyName);
if ($this->status === TDBMObjectStateEnum::STATE_DETACHED || $this->status === TDBMObjectStateEnum::STATE_NEW || (isset($this->manyToOneRelationships[$key]) && $this->manyToOneRelationships[$key]->getUnderlyingResultIterator() !== null)) {
return $alterableResultIterator;
}

$ids = [];
foreach ($foreignColumns as $foreignColumn) {
$ids[] = $this->get($foreignColumn, $foreignTableName);
}

$searchFilter = array_combine($localColumns, $ids);

$unalteredResultIterator = $this->tdbmService->findObjects($tableName, $searchFilter, [], $orderString);

$alterableResultIterator->setResultIterator($unalteredResultIterator->getIterator());
@@ -558,6 +580,20 @@ public function discardChanges(): void
}

$this->_setStatus(TDBMObjectStateEnum::STATE_NOT_LOADED);
foreach ($this->dbRows as $row) {
$row->disableSmartEagerLoad();
}
$this->partialQuery = null;
}

/**
* Prevents smart eager loading of related entities.
* If this bean was loaded through a result iterator, smart eager loading loads all entities of related beans at once.
* You can disable it with this function.
*/
public function disableSmartEagerLoad(): void
{
$this->partialQuery = null;
}

/**
72 changes: 59 additions & 13 deletions src/DbRow.php
Original file line number Diff line number Diff line change
@@ -21,7 +21,13 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

use TheCodingMachine\TDBM\QueryFactory\SmartEagerLoad\Query\ManyToOnePartialQuery;
use TheCodingMachine\TDBM\QueryFactory\SmartEagerLoad\Query\PartialQuery;
use TheCodingMachine\TDBM\QueryFactory\SmartEagerLoad\StorageNode;
use TheCodingMachine\TDBM\Schema\ForeignKeys;
use function array_pop;
use function count;
use function var_export;

/**
* Instances of this class represent a row in a database.
@@ -77,7 +83,7 @@ class DbRow

/**
* The values of the primary key.
* This is set when the object is in "loaded" state.
* This is set when the object is in "loaded" or "not loaded" state.
*
* @var array An array of column => value
*/
@@ -100,6 +106,10 @@ class DbRow
* @var ForeignKeys
*/
private $foreignKeys;
/**
* @var PartialQuery|null
*/
private $partialQuery;

/**
* You should never call the constructor directly. Instead, you should use the
@@ -115,11 +125,12 @@ class DbRow
* @param mixed[] $dbRow
* @throws TDBMException
*/
public function __construct(AbstractTDBMObject $object, string $tableName, ForeignKeys $foreignKeys, array $primaryKeys = array(), TDBMService $tdbmService = null, array $dbRow = [])
public function __construct(AbstractTDBMObject $object, string $tableName, ForeignKeys $foreignKeys, array $primaryKeys = array(), TDBMService $tdbmService = null, array $dbRow = [], ?PartialQuery $partialQuery = null)
{
$this->object = $object;
$this->dbTableName = $tableName;
$this->foreignKeys = $foreignKeys;
$this->partialQuery = $partialQuery;

$this->status = TDBMObjectStateEnum::STATE_DETACHED;

@@ -175,6 +186,15 @@ public function _setStatus(string $state) : void
}
}

/**
* When discarding a bean, we expect to reload data from the DB, not the cache.
* Hence, we must disable smart eager load.
*/
public function disableSmartEagerLoad(): void
{
$this->partialQuery = null;
}

/**
* This is an internal method. You should not call this method yourself. The TDBM library will do it for you.
* If the object is in state 'not loaded', this method performs a query in database to load the object.
@@ -190,26 +210,43 @@ public function _dbLoadIfNotLoaded(): void
}
$connection = $this->tdbmService->getConnection();

list($sql_where, $parameters) = $this->tdbmService->buildFilterFromFilterBag($this->primaryKeys, $connection->getDatabasePlatform());
if ($this->partialQuery !== null) {
$this->partialQuery->registerDataLoader($connection);

// Let's get the data loader.
$dataLoader = $this->partialQuery->getStorageNode()->getManyToOneDataLoader($this->partialQuery->getKey());

if (count($this->primaryKeys) !== 1) {
throw new \RuntimeException('Data-loader patterns only supports primary keys on one column. Table "'.$this->dbTableName.'" has a PK on '.count($this->primaryKeys). ' columns'); // @codeCoverageIgnore
}
$pks = $this->primaryKeys;
$pkId = array_pop($pks);

$row = $dataLoader->get((string) $pkId);
} else {
list($sql_where, $parameters) = $this->tdbmService->buildFilterFromFilterBag($this->primaryKeys, $connection->getDatabasePlatform());

$sql = 'SELECT * FROM '.$connection->quoteIdentifier($this->dbTableName).' WHERE '.$sql_where;
$result = $connection->executeQuery($sql, $parameters);
$sql = 'SELECT * FROM '.$connection->quoteIdentifier($this->dbTableName).' WHERE '.$sql_where;
$result = $connection->executeQuery($sql, $parameters);

$row = $result->fetch(\PDO::FETCH_ASSOC);
$row = $result->fetch(\PDO::FETCH_ASSOC);

if ($row === false) {
throw new TDBMException("Could not retrieve object from table \"$this->dbTableName\" using filter \".$sql_where.\" with data \"".var_export($parameters, true)."\".");
$result->closeCursor();

if ($row === false) {
throw new NoBeanFoundException("Could not retrieve object from table \"$this->dbTableName\" using filter \"$sql_where\" with data \"".var_export($parameters, true). '".');
}
}



$this->dbRow = [];
$types = $this->tdbmService->_getColumnTypesForTable($this->dbTableName);

foreach ($row as $key => $value) {
$this->dbRow[$key] = $types[$key]->convertToPHPValue($value, $connection->getDatabasePlatform());
}

$result->closeCursor();

$this->status = TDBMObjectStateEnum::STATE_LOADED;
}
}
@@ -289,7 +326,8 @@ public function getRef(string $foreignKeyName) : ?AbstractTDBMObject
$fk = $this->foreignKeys->getForeignKey($foreignKeyName);

$values = [];
foreach ($fk->getUnquotedLocalColumns() as $column) {
$localColumns = $fk->getUnquotedLocalColumns();
foreach ($localColumns as $column) {
if (!isset($this->dbRow[$column])) {
return null;
}
@@ -303,10 +341,18 @@ public function getRef(string $foreignKeyName) : ?AbstractTDBMObject

// If the foreign key points to the primary key, let's use findObjectByPk
if ($this->tdbmService->getPrimaryKeyColumns($foreignTableName) === $foreignColumns) {
return $this->tdbmService->findObjectByPk($foreignTableName, $filter, [], true);
if ($this->partialQuery !== null && count($foreignColumns) === 1) {
// Optimisation: let's build the smart eager load query we need to fetch more than one object at once.
$newPartialQuery = new ManyToOnePartialQuery($this->partialQuery, $this->dbTableName, $fk->getForeignTableName(), $foreignColumns[0], $localColumns[0]);
} else {
$newPartialQuery = null;
}
$ref = $this->tdbmService->findObjectByPk($foreignTableName, $filter, [], true, null, $newPartialQuery);
} else {
return $this->tdbmService->findObject($foreignTableName, $filter);
$ref = $this->tdbmService->findObject($foreignTableName, $filter);
}
$this->references[$foreignKeyName] = $ref;
return $ref;
}
}

8 changes: 6 additions & 2 deletions src/InnerResultArray.php
Original file line number Diff line number Diff line change
@@ -3,7 +3,9 @@

namespace TheCodingMachine\TDBM;

use Doctrine\DBAL\Statement;
use TheCodingMachine\TDBM\QueryFactory\SmartEagerLoad\ManyToOneDataLoader;
use TheCodingMachine\TDBM\QueryFactory\SmartEagerLoad\StorageNode;
use TheCodingMachine\TDBM\QueryFactory\SmartEagerLoad\StorageNodeTrait;

/*
Copyright (C) 2006-2017 David Négrier - THE CODING MACHINE
@@ -26,8 +28,10 @@
/**
* Iterator used to retrieve results. It behaves like an array.
*/
class InnerResultArray extends InnerResultIterator
class InnerResultArray extends InnerResultIterator implements StorageNode
{
use StorageNodeTrait;

/**
* The list of results already fetched.
*
23 changes: 19 additions & 4 deletions src/InnerResultIterator.php
Original file line number Diff line number Diff line change
@@ -8,6 +8,9 @@
use Mouf\Database\MagicQuery;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use TheCodingMachine\TDBM\QueryFactory\SmartEagerLoad\PartialQueryFactory;
use TheCodingMachine\TDBM\QueryFactory\SmartEagerLoad\Query\PartialQuery;
use TheCodingMachine\TDBM\QueryFactory\SmartEagerLoad\StorageNode;
use TheCodingMachine\TDBM\Utils\DbalUtils;

/*
@@ -65,8 +68,14 @@ class InnerResultIterator implements \Iterator, \Countable, \ArrayAccess
* @var LoggerInterface
*/
private $logger;

protected $count = null;
/**
* @var PartialQuery|null
*/
private $partialQuery;
/**
* @var int|null
*/
protected $count;

private function __construct()
{
@@ -76,7 +85,7 @@ private function __construct()
* @param mixed[] $parameters
* @param array[] $columnDescriptors
*/
public static function createInnerResultIterator(string $magicSql, array $parameters, ?int $limit, ?int $offset, array $columnDescriptors, ObjectStorageInterface $objectStorage, ?string $className, TDBMService $tdbmService, MagicQuery $magicQuery, LoggerInterface $logger): self
public static function createInnerResultIterator(string $magicSql, array $parameters, ?int $limit, ?int $offset, array $columnDescriptors, ObjectStorageInterface $objectStorage, ?string $className, TDBMService $tdbmService, MagicQuery $magicQuery, LoggerInterface $logger, ?PartialQueryFactory $partialQueryFactory): self
{
$iterator = new static();
$iterator->magicSql = $magicSql;
@@ -90,6 +99,11 @@ public static function createInnerResultIterator(string $magicSql, array $parame
$iterator->magicQuery = $magicQuery;
$iterator->databasePlatform = $iterator->tdbmService->getConnection()->getDatabasePlatform();
$iterator->logger = $logger;
$partialQuery = null;
if ($iterator instanceof StorageNode && $partialQueryFactory !== null) {
$iterator->partialQuery = $partialQueryFactory->getPartialQuery($iterator, $magicQuery, $parameters);
}

return $iterator;
}

@@ -236,8 +250,9 @@ public function next()
$reflectionClassCache[$actualClassName] = new \ReflectionClass($actualClassName);
}
// Let's bypass the constructor when creating the bean!
/** @var AbstractTDBMObject $bean */
$bean = $reflectionClassCache[$actualClassName]->newInstanceWithoutConstructor();
$bean->_constructFromData($beanData, $this->tdbmService);
$bean->_constructFromData($beanData, $this->tdbmService, $this->partialQuery);
}

// The first bean is the one containing the main table.
12 changes: 9 additions & 3 deletions src/PageIterator.php
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@
use Porpaginas\Page;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use TheCodingMachine\TDBM\QueryFactory\SmartEagerLoad\PartialQueryFactory;

/*
Copyright (C) 2006-2017 David Négrier - THE CODING MACHINE
@@ -49,6 +50,10 @@ class PageIterator implements Page, \ArrayAccess, \JsonSerializable
private $offset;
private $columnDescriptors;
private $magicQuery;
/**
* @var PartialQueryFactory|null
*/
private $partialQueryFactory;

/**
* The key of the current retrieved object.
@@ -76,7 +81,7 @@ private function __construct()
* @param mixed[] $parameters
* @param array[] $columnDescriptors
*/
public static function createResultIterator(ResultIterator $parentResult, string $magicSql, array $parameters, int $limit, int $offset, array $columnDescriptors, ObjectStorageInterface $objectStorage, ?string $className, TDBMService $tdbmService, MagicQuery $magicQuery, int $mode, LoggerInterface $logger): self
public static function createResultIterator(ResultIterator $parentResult, string $magicSql, array $parameters, int $limit, int $offset, array $columnDescriptors, ObjectStorageInterface $objectStorage, ?string $className, TDBMService $tdbmService, MagicQuery $magicQuery, int $mode, LoggerInterface $logger, ?PartialQueryFactory $partialQueryFactory): self
{
$iterator = new self();
$iterator->parentResult = $parentResult;
@@ -91,6 +96,7 @@ public static function createResultIterator(ResultIterator $parentResult, string
$iterator->magicQuery = $magicQuery;
$iterator->mode = $mode;
$iterator->logger = $logger;
$iterator->partialQueryFactory = $partialQueryFactory;
return $iterator;
}

@@ -118,9 +124,9 @@ public function getIterator()
if ($this->parentResult->count() === 0) {
$this->innerResultIterator = InnerResultIterator::createEmpyIterator();
} elseif ($this->mode === TDBMService::MODE_CURSOR) {
$this->innerResultIterator = InnerResultIterator::createInnerResultIterator($this->magicSql, $this->parameters, $this->limit, $this->offset, $this->columnDescriptors, $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->logger);
$this->innerResultIterator = InnerResultIterator::createInnerResultIterator($this->magicSql, $this->parameters, $this->limit, $this->offset, $this->columnDescriptors, $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->logger, $this->partialQueryFactory);
} else {
$this->innerResultIterator = InnerResultArray::createInnerResultIterator($this->magicSql, $this->parameters, $this->limit, $this->offset, $this->columnDescriptors, $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->logger);
$this->innerResultIterator = InnerResultArray::createInnerResultIterator($this->magicSql, $this->parameters, $this->limit, $this->offset, $this->columnDescriptors, $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->logger, $this->partialQueryFactory);
}
}

Loading