From 2ebe38b148da8830f2b4064bb738633c511c97cc Mon Sep 17 00:00:00 2001 From: shadlaws Date: Tue, 14 May 2013 13:35:09 +0200 Subject: #2069 - Change "Fix your Gallery" task go faster and be more comprehensive. - optimize MPTT pointer rebuilding for leaf nodes (i.e. non-albums). - reverse order_by to try and preserve existing tree ordering. - reset item level while we're here. - use "$stack[] = 123" instead of array_push($stack, 123) since it's faster. --HG-- extra : source : 297e4c0eccc5a7940224ff8e908b366e83017354 --- modules/gallery/helpers/gallery_task.php | 50 ++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 16 deletions(-) (limited to 'modules/gallery/helpers') diff --git a/modules/gallery/helpers/gallery_task.php b/modules/gallery/helpers/gallery_task.php index a79cb2d5..ac98831b 100644 --- a/modules/gallery/helpers/gallery_task.php +++ b/modules/gallery/helpers/gallery_task.php @@ -398,42 +398,60 @@ class gallery_task_Core { switch ($state) { case self::FIX_STATE_START_MPTT: $task->set("ptr", $ptr = 1); - $task->set("stack", item::root()->id . ":L"); + $task->set("stack", item::root()->id . ":album:1:L"); $state = self::FIX_STATE_RUN_MPTT; break; case self::FIX_STATE_RUN_MPTT: $ptr = $task->get("ptr"); $stack = explode(" ", $task->get("stack")); - list ($id, $ptr_mode) = explode(":", array_pop($stack)); + list ($id, $type, $level, $ptr_mode) = explode(":", array_pop($stack)); if ($ptr_mode == "L") { - $stack[] = "$id:R"; - db::build() - ->update("items") - ->set("left_ptr", $ptr++) - ->where("id", "=", $id) - ->execute(); + if ($type == "album") { + // Albums could be parent nodes. + $stack[] = "$id:$type:$level:R"; + db::build() + ->update("items") + ->set("left_ptr", $ptr++) + ->where("id", "=", $id) + ->execute(); - foreach (db::build() - ->select(array("id")) - ->from("items") - ->where("parent_id", "=", $id) - ->order_by("left_ptr", "ASC") - ->execute() as $child) { - array_push($stack, "{$child->id}:L"); + $level++; + foreach (db::build() + ->select(array("id", "type")) + ->from("items") + ->where("parent_id", "=", $id) + ->order_by("left_ptr", "DESC") // DESC since array_pop effectively reverses them + ->execute() as $child) { + $stack[] = "{$child->id}:{$child->type}:$level:L"; + } + $completed++; + } else { + // Non-albums must be leaf nodes. + db::build() + ->update("items") + ->set("left_ptr", $ptr++) + ->set("right_ptr", $ptr++) + ->set("level", $level) + ->set("relative_path_cache", null) + ->set("relative_url_cache", null) + ->where("id", "=", $id) + ->execute(); + $completed += 2; // we updated two pointers } } else if ($ptr_mode == "R") { db::build() ->update("items") ->set("right_ptr", $ptr++) + ->set("level", $level) ->set("relative_path_cache", null) ->set("relative_url_cache", null) ->where("id", "=", $id) ->execute(); + $completed++; } $task->set("ptr", $ptr); $task->set("stack", implode(" ", $stack)); - $completed++; if (empty($stack)) { $state = self::FIX_STATE_START_DUPE_SLUGS; -- cgit v1.2.3 From 6f922ca427ff94937904979d02e6fba063effd77 Mon Sep 17 00:00:00 2001 From: shadlaws Date: Wed, 15 May 2013 11:16:46 +0200 Subject: Follow-on to #2069 - Decrease stack size of MPTT rebuild task. This reduces the likelihood that we'll hit a limit with gargantuan galleries. --HG-- extra : source : c4a118d43145a2a4ec9b934d3aebe8f9458bcf07 --- modules/gallery/helpers/gallery_task.php | 70 ++++++++++++++++---------------- 1 file changed, 36 insertions(+), 34 deletions(-) (limited to 'modules/gallery/helpers') diff --git a/modules/gallery/helpers/gallery_task.php b/modules/gallery/helpers/gallery_task.php index ac98831b..618cf8fd 100644 --- a/modules/gallery/helpers/gallery_task.php +++ b/modules/gallery/helpers/gallery_task.php @@ -398,48 +398,50 @@ class gallery_task_Core { switch ($state) { case self::FIX_STATE_START_MPTT: $task->set("ptr", $ptr = 1); - $task->set("stack", item::root()->id . ":album:1:L"); + $task->set("stack", item::root()->id . "L1"); $state = self::FIX_STATE_RUN_MPTT; break; case self::FIX_STATE_RUN_MPTT: $ptr = $task->get("ptr"); $stack = explode(" ", $task->get("stack")); - list ($id, $type, $level, $ptr_mode) = explode(":", array_pop($stack)); - if ($ptr_mode == "L") { - if ($type == "album") { - // Albums could be parent nodes. - $stack[] = "$id:$type:$level:R"; - db::build() - ->update("items") - ->set("left_ptr", $ptr++) - ->where("id", "=", $id) - ->execute(); + preg_match("/([0-9]+)([A-Z])([0-9]+)/", array_pop($stack), $matches); // e.g. "12345L10" + list ( , $id, $ptr_mode, $level) = $matches; // Skip the 0th entry of matches. + switch ($ptr_mode) { + case "L": + // Albums could be parent nodes. + $stack[] = "{$id}R{$level}"; + db::build() + ->update("items") + ->set("left_ptr", $ptr++) + ->where("id", "=", $id) + ->execute(); - $level++; - foreach (db::build() - ->select(array("id", "type")) - ->from("items") - ->where("parent_id", "=", $id) - ->order_by("left_ptr", "DESC") // DESC since array_pop effectively reverses them - ->execute() as $child) { - $stack[] = "{$child->id}:{$child->type}:$level:L"; - } - $completed++; - } else { - // Non-albums must be leaf nodes. - db::build() - ->update("items") - ->set("left_ptr", $ptr++) - ->set("right_ptr", $ptr++) - ->set("level", $level) - ->set("relative_path_cache", null) - ->set("relative_url_cache", null) - ->where("id", "=", $id) - ->execute(); - $completed += 2; // we updated two pointers + $level++; + foreach (db::build() + ->select(array("id", "type")) + ->from("items") + ->where("parent_id", "=", $id) + ->order_by("left_ptr", "DESC") // DESC since array_pop effectively reverses them + ->execute() as $child) { + $stack[] = ($child->type == "album") ? "{$child->id}L{$level}" : "{$child->id}B{$level}"; } - } else if ($ptr_mode == "R") { + $completed++; + break; + case "B": + // Non-albums must be leaf nodes. + db::build() + ->update("items") + ->set("left_ptr", $ptr++) + ->set("right_ptr", $ptr++) + ->set("level", $level) + ->set("relative_path_cache", null) + ->set("relative_url_cache", null) + ->where("id", "=", $id) + ->execute(); + $completed += 2; // we updated two pointers + break; + case "R": db::build() ->update("items") ->set("right_ptr", $ptr++) -- cgit v1.2.3