Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/infrastructure/storage/lisk/LiskMigrationIterator.php
12241 views
1
<?php
2
3
/**
4
* Iterate over every object of a given type, without holding all of them in
5
* memory. This is useful for performing database migrations.
6
*
7
* $things = new LiskMigrationIterator(new LiskThing());
8
* foreach ($things as $thing) {
9
* // do something
10
* }
11
*
12
* NOTE: This only works on objects with a normal `id` column.
13
*
14
* @task storage
15
*/
16
final class LiskMigrationIterator extends PhutilBufferedIterator {
17
18
private $object;
19
private $cursor;
20
21
public function __construct(LiskDAO $object) {
22
$this->object = $object;
23
}
24
25
protected function didRewind() {
26
$this->cursor = 0;
27
}
28
29
public function key() {
30
return $this->current()->getID();
31
}
32
33
protected function loadPage() {
34
$results = $this->object->loadAllWhere(
35
'id > %d ORDER BY id ASC LIMIT %d',
36
$this->cursor,
37
$this->getPageSize());
38
39
if ($results) {
40
$this->cursor = last($results)->getID();
41
}
42
43
return $results;
44
}
45
46
}
47
48