]> gitweb.fluxo.info Git - lorea/elgg.git/commitdiff
Improved algorithm by tracking total incomplete entities
authorSteve Clay <steve@mrclay.org>
Sun, 9 Jun 2013 01:34:11 +0000 (21:34 -0400)
committerSteve Clay <steve@mrclay.org>
Sun, 9 Jun 2013 01:34:11 +0000 (21:34 -0400)
engine/classes/ElggBatch.php
engine/tests/api/helpers.php

index ac79cf0841a4c23c837b2f3b2a87254b70f60dd5..d810ea066340e3765923cc5d0ea4be322811a7bb 100644 (file)
@@ -156,6 +156,13 @@ class ElggBatch
         */
        private $incompleteEntities = array();
 
+       /**
+        * Total number of incomplete entities fetched
+        *
+        * @var int
+        */
+       private $totalIncompletes = 0;
+
        /**
         * Batches operations on any elgg_get_*() or compatible function that supports
         * an options array.
@@ -242,12 +249,9 @@ class ElggBatch
        /**
         * Fetches the next chunk of results
         *
-        * @param int $num_incompletes_last_fetch When called recursively, this is the number of
-        *                                        incomplete entities returned in the last fetch.
-        *
         * @return bool
         */
-       private function getNextResultsChunk($num_incompletes_last_fetch = 0) {
+       private function getNextResultsChunk() {
 
                // always reset results.
                $this->results = array();
@@ -281,7 +285,7 @@ class ElggBatch
                if ($this->incrementOffset) {
                        $offset = $this->offset + $this->retrievedResults;
                } else {
-                       $offset = $this->offset + $num_incompletes_last_fetch;
+                       $offset = $this->offset + $this->totalIncompletes;
                }
 
                $current_options = array(
@@ -298,6 +302,8 @@ class ElggBatch
                $num_results = count($this->results);
                $num_incomplete = count($this->incompleteEntities);
 
+               $this->totalIncompletes += $num_incomplete;
+
                if ($this->incompleteEntities) {
                        // pad the front of the results with nulls representing the incompletes
                        array_splice($this->results, 0, 0, array_pad(array(), $num_incomplete, null));
@@ -318,7 +324,7 @@ class ElggBatch
                        if ($num_results == 0) {
                                // This fetch was *all* incompletes! We need to fetch until we can either
                                // offer at least one row to iterate over, or give up.
-                               return $this->getNextResultsChunk($num_incomplete);
+                               return $this->getNextResultsChunk();
                        }
                        return true;
                } else {
index 06ef55138f070ada55ff01af027ab1ce7af645dc..10216140fd1bbe73ea791e8f66a1e0559b149eaa 100644 (file)
@@ -579,7 +579,7 @@ class ElggCoreHelpersTest extends ElggCoreUnitTest {
        }
 
        public function testElggBatchReadHandlesBrokenEntities() {
-               $num_test_entities = 6;
+               $num_test_entities = 8;
                $guids = array();
                for ($i = $num_test_entities; $i > 0; $i--) {
                        $entity = new ElggObject();
@@ -592,11 +592,11 @@ class ElggCoreHelpersTest extends ElggCoreUnitTest {
                }
 
                // break entities such that the first fetch has one incomplete
-               // and the second fetch has only incompletes!
+               // and the second and third fetches have only incompletes!
                $db_prefix = elgg_get_config('dbprefix');
                delete_data("
                        DELETE FROM {$db_prefix}objects_entity
-                       WHERE guid IN ({$guids[1]}, {$guids[2]}, {$guids[3]})
+                       WHERE guid IN ({$guids[1]}, {$guids[2]}, {$guids[3]}, {$guids[4]}, {$guids[5]})
                ");
 
                $options = array(
@@ -613,7 +613,7 @@ class ElggCoreHelpersTest extends ElggCoreUnitTest {
                }
 
                // The broken entities should not have been visited
-               $this->assertEqual($entities_visited, array($guids[0], $guids[4], $guids[5]));
+               $this->assertEqual($entities_visited, array($guids[0], $guids[6], $guids[7]));
 
                // cleanup (including leftovers from previous tests)
                $entity_rows = elgg_get_entities(array_merge($options, array(
@@ -629,7 +629,7 @@ class ElggCoreHelpersTest extends ElggCoreUnitTest {
        }
 
        public function testElggBatchDeleteHandlesBrokenEntities() {
-               $num_test_entities = 6;
+               $num_test_entities = 8;
                $guids = array();
                for ($i = $num_test_entities; $i > 0; $i--) {
                        $entity = new ElggObject();
@@ -642,11 +642,11 @@ class ElggCoreHelpersTest extends ElggCoreUnitTest {
                }
 
                // break entities such that the first fetch has one incomplete
-               // and the second fetch has only incompletes!
+               // and the second and third fetches have only incompletes!
                $db_prefix = elgg_get_config('dbprefix');
                delete_data("
                        DELETE FROM {$db_prefix}objects_entity
-                       WHERE guid IN ({$guids[1]}, {$guids[2]}, {$guids[3]})
+                       WHERE guid IN ({$guids[1]}, {$guids[2]}, {$guids[3]}, {$guids[4]}, {$guids[5]})
                ");
 
                $options = array(
@@ -664,7 +664,7 @@ class ElggCoreHelpersTest extends ElggCoreUnitTest {
                }
 
                // The broken entities should not have been visited
-               $this->assertEqual($entities_visited, array($guids[0], $guids[4], $guids[5]));
+               $this->assertEqual($entities_visited, array($guids[0], $guids[6], $guids[7]));
 
                // cleanup (including leftovers from previous tests)
                $entity_rows = elgg_get_entities(array_merge($options, array(