3 * OpenStack Swift based file backend.
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
21 * @ingroup FileBackend
26 * SwiftFileBackend helper class to page through listings.
27 * Swift also has a listing limit of 10,000 objects for sanity.
28 * Do not use this class from places outside SwiftFileBackend.
30 * @ingroup FileBackend
32 abstract class SwiftFileBackendList
implements Iterator
{
33 /** @var array List of path or (path,stat array) entries */
34 protected $bufferIter = [];
36 /** @var string List items *after* this path */
37 protected $bufferAfter = null;
43 protected $params = [];
45 /** @var SwiftFileBackend */
48 /** @var string Container name */
51 /** @var string Storage directory */
55 protected $suffixStart;
57 const PAGE_SIZE
= 9000; // file listing buffer size
60 * @param SwiftFileBackend $backend
61 * @param string $fullCont Resolved container name
62 * @param string $dir Resolved directory relative to container
63 * @param array $params
65 public function __construct( SwiftFileBackend
$backend, $fullCont, $dir, array $params ) {
66 $this->backend
= $backend;
67 $this->container
= $fullCont;
69 if ( substr( $this->dir
, -1 ) === '/' ) {
70 $this->dir
= substr( $this->dir
, 0, -1 ); // remove trailing slash
72 if ( $this->dir
== '' ) { // whole container
73 $this->suffixStart
= 0;
74 } else { // dir within container
75 $this->suffixStart
= strlen( $this->dir
) +
1; // size of "path/to/dir/"
77 $this->params
= $params;
81 * @see Iterator::key()
84 public function key() {
89 * @see Iterator::next()
91 public function next() {
92 // Advance to the next file in the page
93 next( $this->bufferIter
);
95 // Check if there are no files left in this page and
96 // advance to the next page if this page was not empty.
97 if ( !$this->valid() && count( $this->bufferIter
) ) {
98 $this->bufferIter
= $this->pageFromList(
99 $this->container
, $this->dir
, $this->bufferAfter
, self
::PAGE_SIZE
, $this->params
100 ); // updates $this->bufferAfter
105 * @see Iterator::rewind()
107 public function rewind() {
109 $this->bufferAfter
= null;
110 $this->bufferIter
= $this->pageFromList(
111 $this->container
, $this->dir
, $this->bufferAfter
, self
::PAGE_SIZE
, $this->params
112 ); // updates $this->bufferAfter
116 * @see Iterator::valid()
119 public function valid() {
120 if ( $this->bufferIter
=== null ) {
121 return false; // some failure?
123 return ( current( $this->bufferIter
) !== false ); // no paths can have this value
128 * Get the given list portion (page)
130 * @param string $container Resolved container name
131 * @param string $dir Resolved path relative to container
132 * @param string &$after
134 * @param array $params
135 * @return Traversable|array
137 abstract protected function pageFromList( $container, $dir, &$after, $limit, array $params );