Move Blob class to Rdbms namespaces
[mediawiki.git] / includes / jobqueue / JobQueueMemory.php
blob2866c7f2fa1e2420d5a0f55fc41533c9840ca397
1 <?php
2 /**
3 * PHP memory-backed job queue code.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @author Aaron Schulz
24 /**
25 * Class to handle job queues stored in PHP memory for testing
27 * JobQueueGroup does not remember every queue instance, so statically track it here
29 * @ingroup JobQueue
30 * @since 1.27
32 class JobQueueMemory extends JobQueue {
33 /** @var array[] */
34 protected static $data = [];
36 /**
37 * @see JobQueue::doBatchPush
39 * @param IJobSpecification[] $jobs
40 * @param int $flags
42 protected function doBatchPush( array $jobs, $flags ) {
43 $unclaimed =& $this->getQueueData( 'unclaimed', [] );
45 foreach ( $jobs as $job ) {
46 if ( $job->ignoreDuplicates() ) {
47 $sha1 = Wikimedia\base_convert(
48 sha1( serialize( $job->getDeduplicationInfo() ) ),
49 16, 36, 31
51 if ( !isset( $unclaimed[$sha1] ) ) {
52 $unclaimed[$sha1] = $job;
54 } else {
55 $unclaimed[] = $job;
60 /**
61 * @see JobQueue::supportedOrders
63 * @return string[]
65 protected function supportedOrders() {
66 return [ 'random', 'timestamp', 'fifo' ];
69 /**
70 * @see JobQueue::optimalOrder
72 * @return string
74 protected function optimalOrder() {
75 return 'fifo';
78 /**
79 * @see JobQueue::doIsEmpty
81 * @return bool
83 protected function doIsEmpty() {
84 return ( $this->doGetSize() == 0 );
87 /**
88 * @see JobQueue::doGetSize
90 * @return int
92 protected function doGetSize() {
93 $unclaimed = $this->getQueueData( 'unclaimed' );
95 return $unclaimed ? count( $unclaimed ) : 0;
98 /**
99 * @see JobQueue::doGetAcquiredCount
101 * @return int
103 protected function doGetAcquiredCount() {
104 $claimed = $this->getQueueData( 'claimed' );
106 return $claimed ? count( $claimed ) : 0;
110 * @see JobQueue::doPop
112 * @return Job|bool
114 protected function doPop() {
115 if ( $this->doGetSize() == 0 ) {
116 return false;
119 $unclaimed =& $this->getQueueData( 'unclaimed' );
120 $claimed =& $this->getQueueData( 'claimed', [] );
122 if ( $this->order === 'random' ) {
123 $key = array_rand( $unclaimed );
124 } else {
125 reset( $unclaimed );
126 $key = key( $unclaimed );
129 $spec = $unclaimed[$key];
130 unset( $unclaimed[$key] );
131 $claimed[] = $spec;
133 $job = $this->jobFromSpecInternal( $spec );
135 end( $claimed );
136 $job->metadata['claimId'] = key( $claimed );
138 return $job;
142 * @see JobQueue::doAck
144 * @param Job $job
146 protected function doAck( Job $job ) {
147 if ( $this->getAcquiredCount() == 0 ) {
148 return;
151 $claimed =& $this->getQueueData( 'claimed' );
152 unset( $claimed[$job->metadata['claimId']] );
156 * @see JobQueue::doDelete
158 protected function doDelete() {
159 if ( isset( self::$data[$this->type][$this->wiki] ) ) {
160 unset( self::$data[$this->type][$this->wiki] );
161 if ( !self::$data[$this->type] ) {
162 unset( self::$data[$this->type] );
168 * @see JobQueue::getAllQueuedJobs
170 * @return Iterator of Job objects.
172 public function getAllQueuedJobs() {
173 $unclaimed = $this->getQueueData( 'unclaimed' );
174 if ( !$unclaimed ) {
175 return new ArrayIterator( [] );
178 return new MappedIterator(
179 $unclaimed,
180 function ( $value ) {
181 $this->jobFromSpecInternal( $value );
187 * @see JobQueue::getAllAcquiredJobs
189 * @return Iterator of Job objects.
191 public function getAllAcquiredJobs() {
192 $claimed = $this->getQueueData( 'claimed' );
193 if ( !$claimed ) {
194 return new ArrayIterator( [] );
197 return new MappedIterator(
198 $claimed,
199 function ( $value ) {
200 $this->jobFromSpecInternal( $value );
206 * @param IJobSpecification $spec
208 * @return Job
210 public function jobFromSpecInternal( IJobSpecification $spec ) {
211 return Job::factory( $spec->getType(), $spec->getTitle(), $spec->getParams() );
215 * @param string $field
216 * @param mixed $init
218 * @return mixed
220 private function &getQueueData( $field, $init = null ) {
221 if ( !isset( self::$data[$this->type][$this->wiki][$field] ) ) {
222 if ( $init !== null ) {
223 self::$data[$this->type][$this->wiki][$field] = $init;
224 } else {
225 return $init;
229 return self::$data[$this->type][$this->wiki][$field];