Commit fc12c77c authored by Jeremy Mikola's avatar Jeremy Mikola

Add stream debug methods, update docs, and remove unused methods

Classes using CollectionWrapper should rely on its public API rather than work with its files and chunks collections directly.
parent 6b9e7b05
...@@ -17,7 +17,9 @@ use stdClass; ...@@ -17,7 +17,9 @@ use stdClass;
*/ */
class CollectionWrapper class CollectionWrapper
{ {
private $bucketName;
private $chunksCollection; private $chunksCollection;
private $databaseName;
private $checkedIndexes = false; private $checkedIndexes = false;
private $filesCollection; private $filesCollection;
...@@ -33,6 +35,9 @@ class CollectionWrapper ...@@ -33,6 +35,9 @@ class CollectionWrapper
*/ */
public function __construct(Manager $manager, $databaseName, $bucketName, array $collectionOptions = []) public function __construct(Manager $manager, $databaseName, $bucketName, array $collectionOptions = [])
{ {
$this->databaseName = (string) $databaseName;
$this->bucketName = (string) $bucketName;
$this->filesCollection = new Collection($manager, $databaseName, sprintf('%s.files', $bucketName), $collectionOptions); $this->filesCollection = new Collection($manager, $databaseName, sprintf('%s.files', $bucketName), $collectionOptions);
$this->chunksCollection = new Collection($manager, $databaseName, sprintf('%s.chunks', $bucketName), $collectionOptions); $this->chunksCollection = new Collection($manager, $databaseName, sprintf('%s.chunks', $bucketName), $collectionOptions);
} }
...@@ -135,10 +140,14 @@ class CollectionWrapper ...@@ -135,10 +140,14 @@ class CollectionWrapper
return $this->filesCollection->find($filter, $options); return $this->filesCollection->find($filter, $options);
} }
// TODO: Remove this /**
public function getChunksCollection() * Return the bucket name.
*
* @return string
*/
public function getBucketName()
{ {
return $this->chunksCollection; return $this->bucketName;
} }
/** /**
...@@ -160,10 +169,14 @@ class CollectionWrapper ...@@ -160,10 +169,14 @@ class CollectionWrapper
return new IteratorIterator($cursor); return new IteratorIterator($cursor);
} }
// TODO: Remove this /**
public function getFilesCollection() * Return the database name.
*
* @return string
*/
public function getDatabaseName()
{ {
return $this->filesCollection; return $this->databaseName;
} }
/** /**
......
...@@ -20,6 +20,7 @@ class ReadableStream ...@@ -20,6 +20,7 @@ class ReadableStream
private $chunkSize; private $chunkSize;
private $chunkOffset = 0; private $chunkOffset = 0;
private $chunksIterator; private $chunksIterator;
private $collectionWrapper;
private $firstCheck = true; private $firstCheck = true;
private $id; private $id;
private $iteratorEmpty = false; private $iteratorEmpty = false;
...@@ -52,10 +53,28 @@ class ReadableStream ...@@ -52,10 +53,28 @@ class ReadableStream
$this->length = $file->length; $this->length = $file->length;
$this->chunksIterator = $collectionWrapper->getChunksIteratorByFilesId($this->id); $this->chunksIterator = $collectionWrapper->getChunksIteratorByFilesId($this->id);
$this->collectionWrapper = $collectionWrapper;
$this->numChunks = ceil($this->length / $this->chunkSize); $this->numChunks = ceil($this->length / $this->chunkSize);
$this->initEmptyBuffer(); $this->initEmptyBuffer();
} }
/**
* Return internal properties for debugging purposes.
*
* @see http://php.net/manual/en/language.oop5.magic.php#language.oop5.magic.debuginfo
* @return array
*/
public function __debugInfo()
{
return [
'bucketName' => $this->collectionWrapper->getBucketName(),
'databaseName' => $this->collectionWrapper->getDatabaseName(),
'id' => $this->id,
'chunkSize' => $this->chunkSize,
'length' => $this->length,
];
}
public function close() public function close()
{ {
fclose($this->buffer); fclose($this->buffer);
...@@ -129,7 +148,7 @@ class ReadableStream ...@@ -129,7 +148,7 @@ class ReadableStream
} }
/** /**
* Return the ID of the GridFS file document for this stream. * Return the stream's ID (i.e. file document identifier).
* *
* @return integer * @return integer
*/ */
...@@ -139,7 +158,7 @@ class ReadableStream ...@@ -139,7 +158,7 @@ class ReadableStream
} }
/** /**
* Return the stream size in bytes. * Return the stream's size in bytes.
* *
* @return integer * @return integer
*/ */
......
...@@ -89,6 +89,21 @@ class WritableStream ...@@ -89,6 +89,21 @@ class WritableStream
] + array_intersect_key($options, ['aliases' => 1, 'contentType' => 1, 'metadata' => 1]); ] + array_intersect_key($options, ['aliases' => 1, 'contentType' => 1, 'metadata' => 1]);
} }
/**
* Return internal properties for debugging purposes.
*
* @see http://php.net/manual/en/language.oop5.magic.php#language.oop5.magic.debuginfo
* @return array
*/
public function __debugInfo()
{
return [
'bucketName' => $this->collectionWrapper->getBucketName(),
'databaseName' => $this->collectionWrapper->getDatabaseName(),
'file' => $this->file,
];
}
/** /**
* Closes an active stream and flushes all buffered data to GridFS. * Closes an active stream and flushes all buffered data to GridFS.
*/ */
...@@ -111,26 +126,23 @@ class WritableStream ...@@ -111,26 +126,23 @@ class WritableStream
$this->isClosed = true; $this->isClosed = true;
} }
public function getChunkSize() /**
{ * Return the stream's ID (i.e. file document identifier).
return $this->chunkSize; *
} * @return integer
*/
public function getFile()
{
return $this->file;
}
public function getId() public function getId()
{ {
return $this->file['_id']; return $this->file['_id'];
} }
public function getLength() /**
{ * Return the stream's size in bytes.
return $this->length; *
} * Note: this value will increase as more data is written to the stream.
*
* @return integer
*/
public function getSize() public function getSize()
{ {
return $this->length; return $this->length;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment