]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/filteringnoticestream.php
Don't fetch more than needed if exhausted
[quix0rs-gnu-social.git] / lib / filteringnoticestream.php
index f41221107418857b2f4fddd40e4f45b909ac3152..eb51076b7c217980768f66db37d19d56aae21382 100644 (file)
@@ -70,9 +70,12 @@ abstract class FilteringNoticeStream extends NoticeStream
         // or we get nothing from upstream.
 
         $results = null;
+        $round = 0;
 
         do {
 
+            common_debug(get_class($this) . ": ($offset, $limit) Round $round: fetching $askFor notices starting at $startAt");
+
             $raw = $this->upstream->getNotices($startAt, $askFor, $sinceId, $maxId);
 
             $results = $raw->N;
@@ -81,9 +84,13 @@ abstract class FilteringNoticeStream extends NoticeStream
                 break;
             }
 
-            while ($raw->fetch()) {
-                if ($this->filter($raw)) {
-                    $filtered[] = clone($raw);
+            $notices = $raw->fetchAll();
+            
+            $this->prefill($notices);
+
+            foreach ($notices as $notice) {
+                if ($this->filter($notice)) {
+                    $filtered[] = $notice;
                     if (count($filtered) >= $total) {
                         break;
                     }
@@ -93,9 +100,20 @@ abstract class FilteringNoticeStream extends NoticeStream
             // XXX: make these smarter; factor hit rate into $askFor
 
             $startAt += $askFor;
-            $askFor   = max($total - count($filtered), NOTICES_PER_PAGE);
+            
+            $hits = count($filtered);
+
+            $lastAsk = $askFor;
+
+            if ($hits === 0) {
+                $askFor = max(min(2 * $askFor, NOTICES_PER_PAGE * 50), NOTICES_PER_PAGE);
+            } else {
+                $askFor = max(min((($total - $hits)*$startAt)/$hits, NOTICES_PER_PAGE * 50), NOTICES_PER_PAGE);
+            }
 
-        } while (count($filtered) < $total && $results !== 0);
+            common_debug(get_class($this) . ": ($offset, $limit) Round $round hits is $hits, results = $results.");
+
+        } while (count($filtered) < $total && $results >= $lastAsk);
 
         return new ArrayWrapper(array_slice($filtered, $offset, $limit));
     }
@@ -112,4 +130,9 @@ abstract class FilteringNoticeStream extends NoticeStream
 
         return $ids;
     }
+
+    function prefill($notices)
+    {
+        return;
+    }
 }