depage-xmldb v2.3.1
XML documents in a relational database
Loading...
Searching...
No Matches
SubhashUpdater.php
Go to the documentation of this file.
1<?php
2
12
13namespace Depage\XmlDb\Helpers;
14
25{
26 protected $pdo;
27
36 public function __construct(
37 $pdo,
38 protected string $table,
39 protected $docId,
40 protected int $pad,
41 protected int $batchSize = CaseUpdate::DEFAULT_SIZE
42 ) {
43 $this->pdo = $pdo;
44 }
45
51 public function refresh(): void
52 {
53 $query = $this->pdo->prepare(
54 TreeSql::descendants($this->table, "id_doc = :doc_id AND id_parent IS NULL", $this->pad) . "
55 SELECT xml.id, xml.id_parent, xml.type, xml.name, xml.value, xml.subhash
56 FROM tree JOIN {$this->table} AS xml ON tree.id = xml.id
57 ORDER BY tree.lvl DESC, xml.id_parent ASC, xml.pos ASC;",
58 \Depage\Db\Pdo::unbuffered(),
59 );
60 $query->execute([
61 'doc_id' => $this->docId,
62 ]);
63
64 // deepest level first, so a node is only reached after all of its
65 // children contributed their hash
66 $childHashes = [];
67
68 // the recalculated hashes are only written after the cursor is
69 // closed (no statement may run on the connection while the
70 // unbuffered result is open), so they are collected in the update
71 // buffer first
72 $subhashes = new CaseUpdate($this->pdo, $this->table, $this->batchSize);
73
74 while ($row = $query->fetchObject()) {
75 $hash = NodeContent::hash($row->type, $row->name, $row->value, $childHashes[$row->id] ?? []);
76
77 if ($row->subhash !== $hash) {
78 $subhashes->add($row->id, $hash);
79 }
80
81 if ($row->id_parent !== null) {
82 $childHashes[$row->id_parent][] = $hash;
83 }
84 unset($childHashes[$row->id]);
85 }
86 // no statements may run while the unbuffered result is open
87 $query->closeCursor();
88
89 // subhash takes part in no unique index, so the case updates
90 // cannot collide
91 $subhashes->write('subhash', ['id_doc' => $this->docId]);
92 }
93}
94
95/* vim:set ft=php sw=4 sts=4 fdm=marker et : */
const DEFAULT_SIZE
number of rows written with a single statement
static hash($type, $name, $value, array $childHashes)
canonical hash over one node row and the hashes of its children; the "v1" prefix pins the canonicaliz...
refresh()
recalculates the subhash of every node of the document; nodes that already carry the hash they would ...
__construct( $pdo, protected string $table, protected $docId, protected int $pad, protected int $batchSize=CaseUpdate::DEFAULT_SIZE)
static descendants(string $table, string $anchorCondition, int $pad)
builds the recursive common table expression selecting a node matched by $anchor and all of its desce...
Definition TreeSql.php:43