From 47aaca49a11997ed2d0d6a92df730f42df77d88c Mon Sep 17 00:00:00 2001
From: Thomas Tanghus <thomas@tanghus.net>
Date: Mon, 9 Jul 2012 00:16:14 +0200
Subject: [PATCH] Rescan categories in small batches to reduce memory usage.

---
 apps/contacts/lib/app.php | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)

diff --git a/apps/contacts/lib/app.php b/apps/contacts/lib/app.php
index 25461877e62..f125ec0293c 100644
--- a/apps/contacts/lib/app.php
+++ b/apps/contacts/lib/app.php
@@ -196,16 +196,19 @@ class OC_Contacts_App {
 				foreach($vcaddressbooks as $vcaddressbook) {
 					$vcaddressbookids[] = $vcaddressbook['id'];
 				}
-				$vccontacts = OC_Contacts_VCard::all($vcaddressbookids);
-			}
-		}
-		if(is_array($vccontacts) && count($vccontacts) > 0) {
-			$cards = array();
-			foreach($vccontacts as $vccontact) {
-				$cards[] = $vccontact['carddata'];
+				$start = 0;
+				$batchsize = 10;
+				while($vccontacts = OC_Contacts_VCard::all($vcaddressbookids, $start, $batchsize)){
+					$cards = array();
+					foreach($vccontacts as $vccontact) {
+						$cards[] = $vccontact['carddata'];
+					}
+					OCP\Util::writeLog('contacts',__CLASS__.'::'.__METHOD__.', scanning: '.$batchsize.' starting from '.$start,OCP\Util::DEBUG);
+					// only reset on first batch.
+					self::getVCategories()->rescan($cards, true, ($start==0?true:false));
+					$start += $batchsize;
+				}
 			}
-
-			self::$categories->rescan($cards);
 		}
 	}
 
-- 
GitLab