1
0

Store the stack in a string instead of nested arrays to save space

when serializing, which gives us a little more headroom to store big
stacks in the context field.
This commit is contained in:
Bharat Mediratta 2009-09-12 17:29:22 -07:00
parent 067b6b5073
commit 1040302794

View File

@ -36,25 +36,25 @@ class rescue_task_Core {
$total = $task->get("total"); $total = $task->get("total");
if (empty($total)) { if (empty($total)) {
$task->set("total", $total = Database::instance()->count_records("items")); $task->set("total", $total = Database::instance()->count_records("items"));
$task->set("stack", array(array(1, self::LEFT))); $task->set("stack", "1:" . self::LEFT);
$task->set("ptr", 1); $task->set("ptr", 1);
$task->set("completed", 0); $task->set("completed", 0);
} }
$ptr = $task->get("ptr"); $ptr = $task->get("ptr");
$stack = $task->get("stack"); $stack = explode(" ", $task->get("stack"));
$completed = $task->get("completed"); $completed = $task->get("completed");
// Implement a depth-first tree walk using a stack. Not the most efficient, but it's simple. // Implement a depth-first tree walk using a stack. Not the most efficient, but it's simple.
while ($stack && microtime(true) - $start < 1.5) { while ($stack && microtime(true) - $start < 1.5) {
list($id, $state) = array_pop($stack); list($id, $state) = explode(":", array_pop($stack));
switch ($state) { switch ($state) {
case self::LEFT: case self::LEFT:
self::set_left($id, $ptr++); self::set_left($id, $ptr++);
$item = ORM::factory("item", $id); $item = ORM::factory("item", $id);
array_push($stack, array($id, self::RIGHT)); array_push($stack, $id . ":" . self::RIGHT);
foreach (self::children($id) as $child) { foreach (self::children($id) as $child) {
array_push($stack, array($child->id, self::LEFT)); array_push($stack, $child->id . ":" . self::LEFT);
} }
break; break;
@ -65,7 +65,7 @@ class rescue_task_Core {
} }
} }
$task->set("stack", $stack); $task->set("stack", implode(" ", $stack));
$task->set("ptr", $ptr); $task->set("ptr", $ptr);
$task->set("completed", $completed); $task->set("completed", $completed);