Skip to content
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

refacto: use Weakmap for UnitOfWork internal storage instead of uid i… #90

Merged
merged 1 commit into from
Dec 26, 2024
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
3.10.0 (unreleased):
* Drop PHP 7
* Remove hard dependency to pimple, add it in suggest
* Collection is now JsonSerializable
* Add DateTimeImmutable serialization
* Refacto: use, when possible, Weakmap for UnitOfWork internal storage instead of uuid indexed array
* Fix: Query Generator now handle null values

3.9.1 (2024-07-19):
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"type": "library",
"license": "Apache-2.0",
"require": {
"php": ">=7.1",
"php": ">=8.0",
"aura/sqlquery": "^2.6",
"doctrine/cache": "^1.6"
},
Expand Down
19 changes: 10 additions & 9 deletions src/Ting/Repository/Hydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use CCMBenchmark\Ting\Serializer\UnserializeInterface;
use CCMBenchmark\Ting\UnitOfWork;
use Generator;
use WeakMap;

/**
* @template T
Expand All @@ -45,7 +46,7 @@ class Hydrator implements HydratorInterface
protected $objectDatabase = [];
protected $objectSchema = [];
protected $unserializeAliases = [];
protected $alreadyManaged = [];
protected WeakMap $alreadyManaged;
protected $references = [];
protected $metadataList = [];

Expand All @@ -69,6 +70,11 @@ class Hydrator implements HydratorInterface
*/
protected $identityMap = false;

public function __construct()
{
$this->alreadyManaged = new WeakMap();
}

/**
* @param bool $enable
* @throws Exception
Expand Down Expand Up @@ -243,7 +249,7 @@ private function hasVirtualObject(array $result)
*/
private function unserializeVirtualObjectProperty(\stdClass $virtualObject)
{
foreach ($this->unserializeAliases as $aliasName => list($unserialize, $options)) {
foreach ($this->unserializeAliases as $aliasName => [$unserialize, $options]) {
if (isset($virtualObject->$aliasName) === true) {
$virtualObject->$aliasName = $unserialize->unserialize($virtualObject->$aliasName, $options);
}
Expand Down Expand Up @@ -326,7 +332,6 @@ function (Metadata $metadata) use ($column, &$result) {
if ($this->identityMap === false) {
// If identityMap is disabled, it clones the object and reset UUID
$result[$column['table']] = clone $this->references[$ref];
unset($result[$column['table']]->tingUUID);
} else {
// If identityMap is enabled, it uses the same object
$result[$column['table']] = $this->references[$ref];
Expand Down Expand Up @@ -440,13 +445,9 @@ function (Metadata $metadata) use ($column, &$result) {
*/
private function manageIfYouCan($entity)
{
if (isset($entity->tingUUID) === true && isset($this->alreadyManaged[$entity->tingUUID]) === true) {
return;
}

if (\is_object($entity) === true && ($entity instanceof NotifyPropertyInterface) === true) {
if (\is_object($entity) === true && ($entity instanceof NotifyPropertyInterface) === true && $this->alreadyManaged->offsetExists($entity) === false) {
$this->unitOfWork->manage($entity);
$this->alreadyManaged[$entity->tingUUID] = true;
$this->alreadyManaged[$entity] = true;
}
}
}
1 change: 1 addition & 0 deletions src/Ting/Repository/HydratorRelational.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ final class HydratorRelational extends Hydrator

public function __construct()
{
parent::__construct();
$this->config = new \SplDoublyLinkedList();
}

Expand Down
1 change: 0 additions & 1 deletion src/Ting/Repository/HydratorSingleObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
*/
class HydratorSingleObject extends Hydrator
{

/**
* @return \Generator<int, T>
*/
Expand Down
Loading
Loading