Skip to content

Commit

Permalink
DataFactory: avoid race conditions with PHP garbage collector
Browse files Browse the repository at this point in the history
  • Loading branch information
zozlak committed Mar 13, 2024
1 parent 77c15c2 commit 43a6969
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/quickRdf/DataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class DataFactory implements iDataFactory {
*
* @var array<string, WeakReference<Literal>>
*/
private static array $literals = [];
private static array $literals = [];
private static DefaultGraph $defaultGraph;

/**
Expand All @@ -75,23 +75,28 @@ class DataFactory implements iDataFactory {

public static function blankNode(string | Stringable | null $iri = null): BlankNode {
$a = &self::$blankNodes;
$iri = $iri === null ? $iri : (string) $iri;
if ($iri === null || !isset($a[$iri]) || $a[$iri]->get() === null) {
$obj = null;
if ($iri !== null) {
$iri = (string) $iri;
$obj = isset($a[$iri]) ? $a[$iri]->get() : null;
}
if ($obj === null) {
$obj = new BlankNode($iri);
$iri = $obj->getValue();
$a[$iri] = WeakReference::create($obj);
}
return $a[$iri]->get() ?? throw new RuntimeException("Object creation failed");
return $obj;
}

public static function namedNode(string | Stringable $iri): NamedNode {
$iri = (string) $iri;
$a = &self::$namedNodes;
if (!isset($a[$iri]) || $a[$iri]->get() === null) {
$obj = isset($a[$iri]) ? $a[$iri]->get() : null;
if ($obj === null) {
$obj = new NamedNode($iri);
$a[$iri] = WeakReference::create($obj);
}
return $a[$iri]->get() ?? throw new RuntimeException("Object creation failed");
return $obj;
}

public static function defaultGraph(): DefaultGraph {
Expand Down Expand Up @@ -128,11 +133,12 @@ public static function literal(int | float | string | bool | Stringable $value,

$hash = self::hashLiteral((string) $value, $lang, $datatype);
$a = &self::$literals;
if (!isset($a[$hash]) || $a[$hash]->get() === null) {
$obj = isset($a[$hash]) ? $a[$hash]->get() : null;
if ($obj === null) {
$obj = new Literal($value, $lang, $datatype);
$a[$hash] = WeakReference::create($obj);
}
return $a[$hash]->get() ?? throw new RuntimeException("Object creation failed");
return $obj;
}

public static function quad(iTerm $subject, iNamedNode $predicate,
Expand All @@ -141,11 +147,12 @@ public static function quad(iTerm $subject, iNamedNode $predicate,
$graph ??= self::defaultGraph();
$hash = self::hashQuad($subject, $predicate, $object, $graph);
$a = &self::$quads;
if (!isset($a[$hash]) || $a[$hash]->get() === null) {
$obj = isset($a[$hash]) ? $a[$hash]->get() : null;
if ($obj === null) {
$obj = new Quad($subject, $predicate, $object, $graph);
$a[$hash] = WeakReference::create($obj);
}
return $a[$hash]->get() ?? throw new RuntimeException("Object creation failed");
return $obj;
}

public static function quadNoSubject(
Expand Down

0 comments on commit 43a6969

Please sign in to comment.