mongodb/mongo-php-driver — pull request #10 by crodas (2011-12-14 07:21:19+00:00)
Title: Streaming support

I added add new method (`getResource()`) to `MongoGridFSFile` that basically returns a *file-descriptor* (per say in reality it's a stream resource) so the file function (such as fread, fseek, fclose) can be used with files stored on Mongo.

How to use it
-------------
$conn = new Mongo
$db = $conn->someDatabase;
$grid = $db->getGridFs();

$file = $grid->findOne(array('name' => 'foobar.txt'));
$fp = $file->getResource();

fseek($fp, -1024, SEEK_SET);
echo fread($fp, 1024);
fclose($fp);

Some details
------------

1. The code (`gridfs_stream.c`) looks very similar to `gridfs.c`, perhaps I could refactor both later to reuse more code.
2. It is holding in memory only *one* chunk at a time, it could hold multiple chunks with some changes (i.e, to support read ahead), but I strongly belive that memory is more important than a bit of performance in this particular case.
3. `getResource()` is only returning the resource, no database operation is performed (chunk loading) until fread is called (also fseek just changes the offset position).
4. I wrote tests for what I considered important, fread, fseek+fread and memory consumption (if something is missing let me know).
5. I think would great to make it less verbosed perhaps `findOneFs($query)` method that will an alias for `findOne($query)->getResource()`