Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return fully functional GridFSFile objects from getFileList methods #48

Closed
wants to merge 1 commit into from
Closed

Conversation

chacal
Copy link

@chacal chacal commented Sep 23, 2011

At the moment DBObjects returned by GridFS.getFileList methods can be casted to GridFSFiles, but input/output with them fails as _fs instance variable has not been properly initialized using _fix method in GridFS class.

This effectively prevents one to execute "cursor queries" (with limit, skip etc) and still use the returned objects for input/output. Only way to circumvent this is to use find() instead of getFiles(), but it does not provide any API to e.g. limit the resultset and thus in many cases ends up using unnecessary I/O.

This commit wraps DBCursor returned from getFileList with GridFSCursor that uses _fix to inject GridFS instance into GridFSFiles returned when calling next(). This allows one to cast returned DBObjects into GridFSFiles and make I/O operations with them successfully.

I'm not sure if this kind of approach is an appropriate one and would very much like to hear any better suggestions to solve the same problem!

    
Wraps returned DBCursor with GridFSCursor that uses _fix to inject GridFS instance into GridFSFiles returned by calling next().
@trishagee
Copy link
Contributor

Hi, I'm afraid we're going to close this pull request as it's very old.

@trishagee trishagee closed this Jul 2, 2013
@richardwilly98
Copy link

I need to go through the entire gridfs collection. I am currently using getFileList() but facing the issue above.
I need to call an additional findOne to get it working.
My current working code is:

            GridFS grid = new GridFS(mongo.getDB(definition.getMongoDb()),
                    definition.getMongoCollection());
            cursor = grid.getFileList();
            while (cursor.hasNext()) {
                DBObject object = cursor.next();
                if (object instanceof GridFSDBFile) {
                    GridFSDBFile file = grid.findOne(new ObjectId(object.get(MONGODB_ID_FIELD).toString()));
                    addToStream(OPLOG_INSERT_OPERATION, null, file);
                }
            }

What would be the prefered way to walk through the entire gridfs collection?
Any reason this PR has not been merged?

@trishagee
Copy link
Contributor

The pull request did not work with the current code base, sadly we'd left it too long and the code had moved on. If someone wanted to submit an updated one, we'd look again at merging it.

However, please be aware that the whole driver is undergoing a large change as we implement the 3.0 driver, and we may find a better way to do this later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants