Differential D16906 Diff 40708 src/infrastructure/daemon/workers/query/PhabricatorWorkerArchiveTaskQuery.php
Changeset View
Changeset View
Standalone View
Standalone View
src/infrastructure/daemon/workers/query/PhabricatorWorkerArchiveTaskQuery.php
- This file was copied to src/infrastructure/daemon/workers/query/PhabricatorWorkerTaskQuery.php.
<?php | <?php | ||||
final class PhabricatorWorkerArchiveTaskQuery | final class PhabricatorWorkerArchiveTaskQuery | ||||
extends PhabricatorQuery { | extends PhabricatorWorkerTaskQuery { | ||||
epriestley: For YAGNI-future-proofing, maybe: `withFailureCountBetween($min, $max)`
Where both are… | |||||
private $ids; | |||||
private $dateModifiedSince; | |||||
private $dateCreatedBefore; | |||||
private $objectPHIDs; | |||||
private $classNames; | |||||
private $limit; | |||||
public function withIDs(array $ids) { | |||||
$this->ids = $ids; | |||||
return $this; | |||||
} | |||||
public function withDateModifiedSince($timestamp) { | |||||
$this->dateModifiedSince = $timestamp; | |||||
return $this; | |||||
} | |||||
public function withDateCreatedBefore($timestamp) { | |||||
$this->dateCreatedBefore = $timestamp; | |||||
return $this; | |||||
} | |||||
public function withObjectPHIDs(array $phids) { | |||||
$this->objectPHIDs = $phids; | |||||
return $this; | |||||
} | |||||
public function withClassNames(array $names) { | |||||
$this->classNames = $names; | |||||
return $this; | |||||
} | |||||
public function setLimit($limit) { | |||||
$this->limit = $limit; | |||||
return $this; | |||||
} | |||||
public function execute() { | public function execute() { | ||||
Not Done Inline Actions4-space indents? THE WORK OF THE DEVIL epriestley: 4-space indents? THE WORK OF THE DEVIL | |||||
$task_table = new PhabricatorWorkerArchiveTask(); | $task_table = new PhabricatorWorkerArchiveTask(); | ||||
$conn_r = $task_table->establishConnection('r'); | $conn_r = $task_table->establishConnection('r'); | ||||
$rows = queryfx_all( | $rows = queryfx_all( | ||||
$conn_r, | $conn_r, | ||||
'SELECT * FROM %T %Q %Q %Q', | 'SELECT * FROM %T %Q %Q %Q', | ||||
$task_table->getTableName(), | $task_table->getTableName(), | ||||
$this->buildWhereClause($conn_r), | $this->buildWhereClause($conn_r), | ||||
$this->buildOrderClause($conn_r), | $this->buildOrderClause($conn_r), | ||||
$this->buildLimitClause($conn_r)); | $this->buildLimitClause($conn_r)); | ||||
return $task_table->loadAllFromArray($rows); | return $task_table->loadAllFromArray($rows); | ||||
} | } | ||||
protected function buildWhereClause(AphrontDatabaseConnection $conn_r) { | |||||
$where = array(); | |||||
if ($this->ids !== null) { | |||||
$where[] = qsprintf( | |||||
$conn_r, | |||||
'id in (%Ld)', | |||||
$this->ids); | |||||
} | |||||
if ($this->objectPHIDs !== null) { | |||||
$where[] = qsprintf( | |||||
$conn_r, | |||||
'objectPHID IN (%Ls)', | |||||
$this->objectPHIDs); | |||||
} | |||||
if ($this->dateModifiedSince !== null) { | |||||
$where[] = qsprintf( | |||||
$conn_r, | |||||
'dateModified > %d', | |||||
$this->dateModifiedSince); | |||||
} | |||||
if ($this->dateCreatedBefore !== null) { | |||||
$where[] = qsprintf( | |||||
$conn_r, | |||||
'dateCreated < %d', | |||||
$this->dateCreatedBefore); | |||||
} | |||||
if ($this->classNames !== null) { | |||||
$where[] = qsprintf( | |||||
$conn_r, | |||||
'taskClass IN (%Ls)', | |||||
$this->classNames); | |||||
} | |||||
return $this->formatWhereClause($where); | |||||
} | |||||
private function buildOrderClause(AphrontDatabaseConnection $conn_r) { | |||||
// NOTE: The garbage collector executes this query with a date constraint, | |||||
// and the query is inefficient if we don't use the same key for ordering. | |||||
// See T9808 for discussion. | |||||
if ($this->dateCreatedBefore) { | |||||
return qsprintf($conn_r, 'ORDER BY dateCreated DESC, id DESC'); | |||||
} else if ($this->dateModifiedSince) { | |||||
return qsprintf($conn_r, 'ORDER BY dateModified DESC, id DESC'); | |||||
} else { | |||||
return qsprintf($conn_r, 'ORDER BY id DESC'); | |||||
} | |||||
} | |||||
private function buildLimitClause(AphrontDatabaseConnection $conn_r) { | |||||
$clause = ''; | |||||
if ($this->limit) { | |||||
$clause = qsprintf($conn_r, 'LIMIT %d', $this->limit); | |||||
} | |||||
return $clause; | |||||
} | |||||
} | } |
For YAGNI-future-proofing, maybe: withFailureCountBetween($min, $max)
Where both are nullable, and null means "no boundary limit".
Similar to existing withVersionBetween(), withNextEventBetween(), etc.