Skip to content

Instantly share code, notes, and snippets.

@joegreen88
Created October 11, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joegreen88/9fd1cc566b0d7099e574 to your computer and use it in GitHub Desktop.
Save joegreen88/9fd1cc566b0d7099e574 to your computer and use it in GitHub Desktop.
Memory leak from this code
<?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;
@joegreen88
Copy link
Author

In the real environment, ideally, $perPage would probably be in the region of 1000 or so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment