com.mongodb.gridfs
Class GridFS

java.lang.Object
  extended by com.mongodb.gridfs.GridFS

public class GridFS
extends Object

Implementation of GridFS v1.0 GridFS 1.0 spec


MongoDB Doc Links

Field Summary
protected  String _bucketName
           
protected  DBCollection _chunkCollection
           
protected  DB _db
           
protected  DBCollection _filesCollection
           
static String DEFAULT_BUCKET
          bucket to use for the collection namespaces
static int DEFAULT_CHUNKSIZE
          file's chunk size
static long MAX_CHUNKSIZE
          file's max chunk size
 
Constructor Summary
GridFS(DB db)
          Creates a GridFS instance for the default bucket "fs" in the given database.
GridFS(DB db, String bucket)
          Creates a GridFS instance for the specified bucket in the given database.
 
Method Summary
 GridFSInputFile createFile()
          This method creates an empty GridFSInputFile instance.
 GridFSInputFile createFile(byte[] data)
          creates a file entry.
 GridFSInputFile createFile(File f)
          creates a file entry.
 GridFSInputFile createFile(InputStream in)
          creates a file entry.
 GridFSInputFile createFile(InputStream in, boolean closeStreamOnPersist)
          creates a file entry.
 GridFSInputFile createFile(InputStream in, String filename)
          creates a file entry.
 GridFSInputFile createFile(InputStream in, String filename, boolean closeStreamOnPersist)
          creates a file entry.
 GridFSInputFile createFile(String filename)
           
 List<GridFSDBFile> find(DBObject query)
          finds a list of files matching the given query
 GridFSDBFile find(ObjectId id)
          finds one file matching the given id.
 List<GridFSDBFile> find(String filename)
          finds a list of files matching the given filename
 GridFSDBFile findOne(DBObject query)
          finds one file matching the given query
 GridFSDBFile findOne(ObjectId id)
          finds one file matching the given id.
 GridFSDBFile findOne(String filename)
          finds one file matching the given filename
 String getBucketName()
          gets the bucket name used in the collection's namespace
 DB getDB()
          gets the db used
 DBCursor getFileList()
          gets the list of files stored in this gridfs, sorted by filename
 DBCursor getFileList(DBObject query)
          gets a filtered list of files stored in this gridfs, sorted by filename
 void remove(DBObject query)
          removes all files matching the given query
 void remove(ObjectId id)
          removes the file matching the given id
 void remove(String filename)
          removes all files matching the given filename
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_CHUNKSIZE

public static final int DEFAULT_CHUNKSIZE
file's chunk size

See Also:
Constant Field Values

MAX_CHUNKSIZE

public static final long MAX_CHUNKSIZE
file's max chunk size

See Also:
Constant Field Values

DEFAULT_BUCKET

public static final String DEFAULT_BUCKET
bucket to use for the collection namespaces

See Also:
Constant Field Values

_db

protected final DB _db

_bucketName

protected final String _bucketName

_filesCollection

protected final DBCollection _filesCollection

_chunkCollection

protected final DBCollection _chunkCollection
Constructor Detail

GridFS

public GridFS(DB db)
Creates a GridFS instance for the default bucket "fs" in the given database. Set the preferred WriteConcern on the give DB with DB.setWriteConcern

Parameters:
db - database to work with
Throws:
MongoException
See Also:
WriteConcern

GridFS

public GridFS(DB db,
              String bucket)
Creates a GridFS instance for the specified bucket in the given database. Set the preferred WriteConcern on the give DB with DB.setWriteConcern

Parameters:
db - database to work with
bucket - bucket to use in the given database
Throws:
MongoException
See Also:
WriteConcern
Method Detail

getFileList

public DBCursor getFileList()
gets the list of files stored in this gridfs, sorted by filename

Returns:
cursor of file objects

getFileList

public DBCursor getFileList(DBObject query)
gets a filtered list of files stored in this gridfs, sorted by filename

Parameters:
query - filter to apply
Returns:
cursor of file objects

find

public GridFSDBFile find(ObjectId id)
finds one file matching the given id. Equivalent to findOne(id)

Parameters:
id -
Returns:
Throws:
MongoException

findOne

public GridFSDBFile findOne(ObjectId id)
finds one file matching the given id.

Parameters:
id -
Returns:
Throws:
MongoException

findOne

public GridFSDBFile findOne(String filename)
finds one file matching the given filename

Parameters:
filename -
Returns:
Throws:
MongoException

findOne

public GridFSDBFile findOne(DBObject query)
finds one file matching the given query

Parameters:
query -
Returns:
Throws:
MongoException

find

public List<GridFSDBFile> find(String filename)
finds a list of files matching the given filename

Parameters:
filename -
Returns:
Throws:
MongoException

find

public List<GridFSDBFile> find(DBObject query)
finds a list of files matching the given query

Parameters:
query -
Returns:
Throws:
MongoException

remove

public void remove(ObjectId id)
removes the file matching the given id

Parameters:
id -
Throws:
MongoException

remove

public void remove(String filename)
removes all files matching the given filename

Parameters:
filename -
Throws:
MongoException

remove

public void remove(DBObject query)
removes all files matching the given query

Parameters:
query -
Throws:
MongoException

createFile

public GridFSInputFile createFile(byte[] data)
creates a file entry. After calling this method, you have to call save() on the GridFSInputFile file

Parameters:
data - the file's data
Returns:

createFile

public GridFSInputFile createFile(File f)
                           throws IOException
creates a file entry. After calling this method, you have to call save() on the GridFSInputFile file

Parameters:
f - the file object
Returns:
Throws:
IOException

createFile

public GridFSInputFile createFile(InputStream in)
creates a file entry. after calling this method, you have to call save() on the GridFSInputFile file

Parameters:
in - an inputstream containing the file's data
Returns:

createFile

public GridFSInputFile createFile(InputStream in,
                                  boolean closeStreamOnPersist)
creates a file entry. after calling this method, you have to call save() on the GridFSInputFile file

Parameters:
in - an inputstream containing the file's data
closeStreamOnPersist - indicate the passed in input stream should be closed once the data chunk persisted
Returns:

createFile

public GridFSInputFile createFile(InputStream in,
                                  String filename)
creates a file entry. After calling this method, you have to call save() on the GridFSInputFile file

Parameters:
in - an inputstream containing the file's data
filename - the file name as stored in the db
Returns:

createFile

public GridFSInputFile createFile(InputStream in,
                                  String filename,
                                  boolean closeStreamOnPersist)
creates a file entry. After calling this method, you have to call save() on the GridFSInputFile file

Parameters:
in - an inputstream containing the file's data
filename - the file name as stored in the db
closeStreamOnPersist - indicate the passed in input stream should be closed once the data chunk persisted
Returns:

createFile

public GridFSInputFile createFile(String filename)
Parameters:
filename - the file name as stored in the db
Returns:
See Also:
GridFS#createFile()} on how to use this method

createFile

public GridFSInputFile createFile()
This method creates an empty GridFSInputFile instance. On this instance an OutputStream can be obtained using the GridFSInputFile.getOutputStream() method. You can still call GridFSInputFile.setContentType(String) and GridFSInputFile.setFilename(String). The file will be completely written and closed after calling the OutputStream.close() method on the output stream.

Returns:
GridFS file handle instance.

getBucketName

public String getBucketName()
gets the bucket name used in the collection's namespace

Returns:

getDB

public DB getDB()
gets the db used

Returns: