[JAVA-1911] java.lang.IllegalStateException: No GridFS instance defined! Created: 03/Aug/15  Updated: 27/Nov/16  Resolved: 07/Aug/15

Status: Closed
Project: Java Driver
Component/s: GridFS
Affects Version/s: 3.0.3
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Pavan [X] Assignee: Ross Lawley
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows 7 64 bit



 Description   

GridFS is failing to write the file. After creating a GridFS class and storing few files to mongodb. I am trying to read it back.

DBCursor cursor = fs.getFileList();
int count=0;
while(cursor.hasNext()){

GridFSDBFile object = (GridFSDBFile)cursor.next();
final InputStream is = object.getInputStream();
System.out.println("is to "+is.toString());
try

{ //destination = Paths.get("C:\\drls\\test"+count+".drl"); //Files.copy(is, destination); object.writeTo("C:\\drls\\test"+count+".drl"); }

catch (IOException e)

{ // TODO Auto-generated catch block e.printStackTrace(); }

count++;
}



 Comments   
Comment by Ross Lawley [ 07/Aug/15 ]

Marking as "Won't Fix" as there are no plans to change the existing com.mongodb.gridfs version of GridFS.

However, there are improvements are being made to the forthcoming com.mongodb.client.gridfs that will meet the new GridFS specification.

Comment by Ross Lawley [ 07/Aug/15 ]

Hi PavanKua,

The reason for the error is due to the casting of a DBObject into a GridFSDBFile, this fails because the DBObject doesn't have access to the GridFS instance.

There are two possible approaches:

1. Use fs.find(new DBObject()) to return a List<GridFSDBFile> and then iterate through. eg:

List<GridFSDBFile> files = gridFS.find(new BasicDBObject());
for (GridFSDBFile file : files) {
    file.writeTo("/tmp/" + file.getFilename());
}

2. Inside the while loop call the gridFS.findOne method for each file eg:

while(cursor.hasNext()) {
    GridFSDBFile file = gridFS.findOne(cursor.next());
    file.writeTo("/tmp/" + file.getFilename());
}

Unfortunately, there are downsides to both, the size in memory for 1. versus the number of queries in 2.
This deficiency is being addressed in the new version of GridFS coming in the upcoming 3.1 version of the driver.

Ross

Generated at Thu Feb 08 08:55:50 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.