FileBackendStoreShardListIterator.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. * FileBackendStore helper function to handle listings that span container shards.
  23. * Do not use this class from places outside of FileBackendStore.
  24. *
  25. * @ingroup FileBackend
  26. */
  27. abstract class FileBackendStoreShardListIterator extends FilterIterator {
  28. /** @var FileBackendStore */
  29. protected $backend;
  30. /** @var array */
  31. protected $params;
  32. /** @var string Full container name */
  33. protected $container;
  34. /** @var string Resolved relative path */
  35. protected $directory;
  36. /** @var array */
  37. protected $multiShardPaths = []; // (rel path => 1)
  38. /**
  39. * @param FileBackendStore $backend
  40. * @param string $container Full storage container name
  41. * @param string $dir Storage directory relative to container
  42. * @param array $suffixes List of container shard suffixes
  43. * @param array $params
  44. */
  45. public function __construct(
  46. FileBackendStore $backend, $container, $dir, array $suffixes, array $params
  47. ) {
  48. $this->backend = $backend;
  49. $this->container = $container;
  50. $this->directory = $dir;
  51. $this->params = $params;
  52. $iter = new AppendIterator();
  53. foreach ( $suffixes as $suffix ) {
  54. $iter->append( $this->listFromShard( $this->container . $suffix ) );
  55. }
  56. parent::__construct( $iter );
  57. }
  58. public function accept() {
  59. $rel = $this->getInnerIterator()->current(); // path relative to given directory
  60. $path = $this->params['dir'] . "/{$rel}"; // full storage path
  61. if ( $this->backend->isSingleShardPathInternal( $path ) ) {
  62. return true; // path is only on one shard; no issue with duplicates
  63. } elseif ( isset( $this->multiShardPaths[$rel] ) ) {
  64. // Don't keep listing paths that are on multiple shards
  65. return false;
  66. } else {
  67. $this->multiShardPaths[$rel] = 1;
  68. return true;
  69. }
  70. }
  71. public function rewind() {
  72. parent::rewind();
  73. $this->multiShardPaths = [];
  74. }
  75. /**
  76. * Get the list for a given container shard
  77. *
  78. * @param string $container Resolved container name
  79. * @return Iterator
  80. */
  81. abstract protected function listFromShard( $container );
  82. }