Skip to content
This repository has been archived by the owner on Feb 1, 2022. It is now read-only.

Streaming support #10

Merged
merged 0 commits into from
Dec 14, 2011
Merged

Streaming support #10

merged 0 commits into from
Dec 14, 2011

Conversation

crodas
Copy link
Contributor

@crodas crodas commented Dec 14, 2011

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()

@crodas
Copy link
Contributor Author

crodas commented Dec 14, 2011

Fixed a couple of memory leaks.

@crodas crodas merged commit f275adf into mongodb:master Dec 14, 2011
bjori referenced this pull request in bjori/mongo-php-driver-legacy Aug 10, 2012
Fixed PHP-433 (GridFS safe option overwritten)
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant