-
-
Save joegreen88/9fd1cc566b0d7099e574 to your computer and use it in GitHub Desktop.
Memory leak from this code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$outStream = fopen("php://output", "r+"); | |
fputcsv($outStream, $columnHeadings); | |
// Create a loop so that we can do paginated couchbase view queries | |
$finished = false; | |
$startKeyDocId = null; | |
$perPage = 5; | |
while (!$finished) { | |
// Set up the couchbase view query | |
$query = \CouchbaseViewQuery::from('webforms', $cbView); | |
$query | |
->order($query::ORDER_ASCENDING) | |
->stale((null === $startKeyDocId ? $query::UPDATE_BEFORE : $query::UPDATE_NONE)) | |
->reduce(true) | |
->group(3) | |
->range( | |
[$webForm->getType(), $ID, $startKeyDocId], | |
[$webForm->getType(), $ID+1, null], | |
false | |
) | |
->limit($perPage + 1) | |
; | |
// Fetch query results | |
$result = $bucket->query($query); | |
$countRows = count($result['rows']); | |
// No results means we are done here | |
if (0 === $countRows) { | |
break; | |
} | |
// We can try to calculate if this is the last page based on the number of results | |
if ($countRows <= $perPage) { | |
$finished = true; | |
} | |
// Loop through rows in this paginated result set | |
$numLoops = min($countRows, $perPage); | |
for ($i = 0; $i < $numLoops; $i++) { | |
$entryResult = $bucket->get($result['rows'][$i]['key'][2]); | |
$doc = $entryResult->value; | |
// ... | |
// here I process the data from $doc into an array $data for csv export | |
// ... | |
fputcsv($outStream, $data); | |
} | |
// Limit is $perPage + 1 so on 0-indexed array a key of $perPage means we fulfilled that limit | |
if (array_key_exists($perPage, $result['rows'])) { | |
$startKeyDocId = $result['rows'][$perPage]['key'][2]; | |
} | |
flush(); | |
} | |
fclose($outStream); | |
exit; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In the real environment, ideally,
$perPage
would probably be in the region of 1000 or so.