Skip to content

Commit 1ea4f67

Browse files
authored
Update objects.rst
The previous code has a bug, in that it assigns the names of the objects, not the objects themselves. It also has complex looping logic setting and examining $marker which in fact does nothing. I found that both count() and empty() do not work as expected on the array returned by the function - I have not worked out why, just avoided their use.
1 parent ea17eff commit 1ea4f67

File tree

1 file changed

+41
-23
lines changed

1 file changed

+41
-23
lines changed

doc/services/object-store/objects.rst

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -185,30 +185,48 @@ the built-in paging which uses a 'marker' parameter to fetch the next page of da
185185

186186
.. code-block:: php
187187
188-
$containerObjects = array();
189-
$marker = '';
190-
191-
while ($marker !== null) {
192-
$params = array(
193-
'marker' => $marker,
194-
);
195-
196-
$objects = $container->objectList($params);
197-
$total = $objects->count();
198-
$count = 0;
199-
200-
if ($total == 0) {
201-
break;
202-
}
203-
204-
foreach ($objects as $object) {
205-
/** @var $object OpenCloud\ObjectStore\Resource\DataObject **/
206-
$containerObjects[] = $object->getName();
207-
$count++;
208-
209-
$marker = ($count == $total) ? $object->getName() : null;
188+
$objectsInContainer = array();
189+
$marker = '';
190+
191+
/* At the end of the code this is available to compare with the web GUI number.
192+
* it is not functionally required.
193+
*/
194+
$totalInContainer = 0;
195+
196+
while (true)
197+
{
198+
/* The 'marker' entry in the array parameter of objectList
199+
* holds the name of an object
200+
* which lies just before where we want to start in the container,
201+
* and an empty string means start at the beginning.
202+
*/
203+
$moreObjects = $container->objectList(array('marker' => $marker));
204+
205+
/* We exit this loop if the container was exhausted by the previous
206+
* objectList() call, so the current one has nothing more for us.
207+
* Which will also work immediately for a completely empty container.
208+
*/
209+
210+
/* Neither count() nor empty() seem to be a reliable test here. */
211+
if ( ! isset($moreObjects[0]))
212+
{
213+
break; /* The only way out of the loop. */
214+
}
215+
216+
foreach ($moreObjects as $object)
217+
{
218+
/** @var $object OpenCloud\ObjectStore\Resource\DataObject **/
219+
220+
$objectsInContainer[] = $object;
221+
$totalInContainer++;
222+
223+
/* If we wanted to save time, we could count the contents of the array
224+
* and only do this assignment for the final entry.
225+
* However the counting itself takes time, and makes the code more complex.
226+
*/
227+
$marker = $object->getName();
228+
}
210229
}
211-
}
212230
213231
`Get the executable PHP script for this example <https://raw.githubusercontent.com/rackspace/php-opencloud/master/samples/ObjectStore/list-objects-over-10000.php>`_
214232

0 commit comments

Comments
 (0)