Skip to content

Commit

Permalink
Merge pull request #4456 from corentin-soriano/improve_tree_rebuild_u…
Browse files Browse the repository at this point in the history
…pdates

Only update changed rows in $tree->rebuild() to reduce load latency.
  • Loading branch information
nilsteampassnet authored Nov 14, 2024
2 parents 5ac7ff1 + d384a54 commit 0ee3c30
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
21 changes: 21 additions & 0 deletions includes/libraries/teampassclasses/nestedtree/src/NestedTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,18 @@ public function rebuild()
// give it an initial nleft value of 0 and an nlevel of 0.
$this->generateTreeData($data, 0, 0, $n_tally);

// Get current nlevel, nright and nleft
$folder_ids_str = implode(',', array_map('intval', array_keys($data)));
$query = "SELECT id, nlevel, nright, nleft
FROM " . $this->table . "
WHERE id IN(" . $folder_ids_str . ")";
$result = mysqli_query($this->link, $query);

// Array with folders current nlevel, nright and nleft values.
$folders_infos = [];
while ($result && $row = mysqli_fetch_assoc($result))
$folders_infos[$row['id']] = $row;

// at this point the the root node will have nleft of 0, nlevel of 0
// and nright of (tree size * 2 + 1)

Expand All @@ -470,6 +482,15 @@ public function rebuild()
continue;
}

// Don't update if no change (better performances)
if (!empty($folders_infos[$folder_id])
&& (int) $row->nlevel === (int) $folders_infos[$folder_id]['nlevel']
&& (int) $row->nleft === (int) $folders_infos[$folder_id]['nleft']
&& (int) $row->nright === (int) $folders_infos[$folder_id]['nright']
) {
continue;
}

$query = sprintf(
'update %s set nlevel = %d, nleft = %d, nright = %d where %s = %d',
$this->table,
Expand Down
21 changes: 21 additions & 0 deletions vendor/teampassclasses/nestedtree/src/NestedTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,18 @@ public function rebuild()
// give it an initial nleft value of 0 and an nlevel of 0.
$this->generateTreeData($data, 0, 0, $n_tally);

// Get current nlevel, nright and nleft
$folder_ids_str = implode(',', array_map('intval', array_keys($data)));
$query = "SELECT id, nlevel, nright, nleft
FROM " . $this->table . "
WHERE id IN(" . $folder_ids_str . ")";
$result = mysqli_query($this->link, $query);

// Array with folders current nlevel, nright and nleft values.
$folders_infos = [];
while ($result && $row = mysqli_fetch_assoc($result))
$folders_infos[$row['id']] = $row;

// at this point the the root node will have nleft of 0, nlevel of 0
// and nright of (tree size * 2 + 1)

Expand All @@ -470,6 +482,15 @@ public function rebuild()
continue;
}

// Don't update if no change (better performances)
if (!empty($folders_infos[$folder_id])
&& (int) $row->nlevel === (int) $folders_infos[$folder_id]['nlevel']
&& (int) $row->nleft === (int) $folders_infos[$folder_id]['nleft']
&& (int) $row->nright === (int) $folders_infos[$folder_id]['nright']
) {
continue;
}

$query = sprintf(
'update %s set nlevel = %d, nleft = %d, nright = %d where %s = %d',
$this->table,
Expand Down

0 comments on commit 0ee3c30

Please sign in to comment.