Debian Codegraph @

includes/libs/filebackend/fileiteration/FileBackendStoreShardListIterator.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @ingroup FileBackend
20 */
21
22 namespace Wikimedia\FileBackend\FileIteration;
23
24 use AppendIterator;
25 use FilterIterator;
26 use Iterator;
27 use Wikimedia\FileBackend\FileBackendStore;
28
29 /**
30 * FileBackendStore helper function to handle listings that span container shards.
31 * Do not use this class from places outside of FileBackendStore.
32 *
33 * @ingroup FileBackend
34 */
35 abstract class FileBackendStoreShardListIterator extends FilterIterator {
36 /** @var FileBackendStore */
37 protected $backend;
38
39 /** @var array */
40 protected $params;
41
42 /** @var string Full container name */
43 protected $container;
44
45 /** @var string Resolved relative path */
46 protected $directory;
47
48 /** @var array */
49 protected $multiShardPaths = []; // (rel path => 1)
50
51 /**
52 * @param FileBackendStore $backend
53 * @param string $container Full storage container name
54 * @param string $dir Storage directory relative to container
55 * @param array $suffixes List of container shard suffixes
56 * @param array $params
57 */
58 public function __construct(
59 FileBackendStore $backend, $container, $dir, array $suffixes, array $params
60 ) {
61 $this->backend = $backend;
62 $this->container = $container;
63 $this->directory = $dir;
64 $this->params = $params;
65
66 $iter = new AppendIterator();
67 foreach ( $suffixes as $suffix ) {
68 $iter->append( $this->listFromShard( $this->container . $suffix ) );
69 }
70
71 parent::__construct( $iter );
72 }
73
74 public function accept(): bool {
75 $inner = $this->getInnerIterator();
76 '@phan-var AppendIterator $inner';
77 $rel = $inner->current(); // path relative to given directory
78 $path = $this->params['dir'] . "/{$rel}"; // full storage path
79 if ( $this->backend->isSingleShardPathInternal( $path ) ) {
80 return true; // path is only on one shard; no issue with duplicates
81 } elseif ( isset( $this->multiShardPaths[$rel] ) ) {
82 // Don't keep listing paths that are on multiple shards
83 return false;
84 } else {
85 $this->multiShardPaths[$rel] = 1;
86
87 return true;
88 }
89 }
90
91 public function rewind(): void {
92 parent::rewind();
93 $this->multiShardPaths = [];
94 }
95
96 /**
97 * Get the list for a given container shard
98 *
99 * @param string $container Resolved container name
100 * @return Iterator
101 */
102 abstract protected function listFromShard( $container );
103 }
104
105 /** @deprecated class alias since 1.43 */
106 class_alias( FileBackendStoreShardListIterator::class, 'FileBackendStoreShardListIterator' );