public class GridFS
extends java.lang.Object
Implementation of GridFS - a specification for storing and retrieving files that exceed the BSON-document size limit of 16MB.
Instead of storing a file in a single document, GridFS divides a file into parts, or chunks, and stores each of those chunks as a separate document. By default GridFS limits chunk size to 255k. GridFS uses two collections to store files. One collection stores the file chunks, and the other stores file metadata.
When you query a GridFS store for a file, the driver or client will reassemble the chunks as needed. You can perform range queries on files stored through GridFS. You also can access information from arbitrary sections of files, which allows you to "skip" into the middle of a video or audio file.
GridFS is useful not only for storing files that exceed 16MB but also for storing any files for which you want access without having to load the entire file into memory. For more information on the indications of GridFS, see MongoDB official documentation.
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
DEFAULT_BUCKET
Bucket to use for the collection namespaces
|
static int |
DEFAULT_CHUNKSIZE
File's chunk size
|
static long |
MAX_CHUNKSIZE
Deprecated.
You can calculate max chunkSize with a similar formula
Mongo.getMaxBsonObjectSize() - 500*1000.
Please ensure that you left enough space for metadata (500kb is enough). |
Constructor and Description |
---|
GridFS(DB db)
Creates a GridFS instance for the default bucket "fs" in the given database.
|
GridFS(DB db,
java.lang.String bucket)
Creates a GridFS instance for the specified bucket in the given database.
|
Modifier and Type | Method and Description |
---|---|
GridFSInputFile |
createFile()
This method creates an empty
GridFSInputFile instance. |
GridFSInputFile |
createFile(byte[] data)
Creates a file entry.
|
GridFSInputFile |
createFile(java.io.File file)
Creates a file entry.
|
GridFSInputFile |
createFile(java.io.InputStream in)
Creates a file entry.
|
GridFSInputFile |
createFile(java.io.InputStream in,
boolean closeStreamOnPersist)
Creates a file entry.
|
GridFSInputFile |
createFile(java.io.InputStream in,
java.lang.String filename)
Creates a file entry.
|
GridFSInputFile |
createFile(java.io.InputStream in,
java.lang.String filename,
boolean closeStreamOnPersist)
Creates a file entry.
|
GridFSInputFile |
createFile(java.lang.String filename)
Creates a file entry.
|
java.util.List<GridFSDBFile> |
find(DBObject query)
Finds a list of files matching the given query.
|
java.util.List<GridFSDBFile> |
find(DBObject query,
DBObject sort)
Finds a list of files matching the given query.
|
GridFSDBFile |
find(ObjectId objectId)
Finds one file matching the given objectId.
|
java.util.List<GridFSDBFile> |
find(java.lang.String filename)
Finds a list of files matching the given filename.
|
java.util.List<GridFSDBFile> |
find(java.lang.String filename,
DBObject sort)
Finds a list of files matching the given filename.
|
GridFSDBFile |
findOne(DBObject query)
Finds one file matching the given query.
|
GridFSDBFile |
findOne(ObjectId objectId)
Finds one file matching the given objectId.
|
GridFSDBFile |
findOne(java.lang.String filename)
Finds one file matching the given filename.
|
java.lang.String |
getBucketName()
Gets the bucket name used in the collection's namespace.
|
DB |
getDB()
Gets the database 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.
|
DBCursor |
getFileList(DBObject query,
DBObject sort)
Gets a sorted, filtered list of files stored in this gridfs.
|
void |
remove(DBObject query)
Removes all files matching the given query.
|
void |
remove(ObjectId id)
Removes the file matching the given id.
|
void |
remove(java.lang.String filename)
Removes all files matching the given filename.
|
public static final int DEFAULT_CHUNKSIZE
@Deprecated public static final long MAX_CHUNKSIZE
Mongo.getMaxBsonObjectSize()
- 500*1000.
Please ensure that you left enough space for metadata (500kb is enough).public static final java.lang.String DEFAULT_BUCKET
public GridFS(DB db)
db
- database to work withMongoException
- if there's a failureWriteConcern
public GridFS(DB db, java.lang.String bucket)
db
- database to work withbucket
- bucket to use in the given databaseMongoException
- if there's a failureWriteConcern
public DBCursor getFileList()
public DBCursor getFileList(DBObject query)
query
- filter to applypublic DBCursor getFileList(DBObject query, DBObject sort)
query
- filter to applysort
- sorting to applypublic GridFSDBFile find(ObjectId objectId)
objectId
- the objectId of the file stored on a serverMongoException
- if the operation failspublic GridFSDBFile findOne(ObjectId objectId)
objectId
- the objectId of the file stored on a serverMongoException
- if the operation failspublic GridFSDBFile findOne(java.lang.String filename)
filename
- the name of the file stored on a serverMongoException
- if the operation failspublic GridFSDBFile findOne(DBObject query)
query
- filter to applyMongoException
- if the operation failspublic java.util.List<GridFSDBFile> find(java.lang.String filename)
filename
- the filename to look forMongoException
- if the operation failspublic java.util.List<GridFSDBFile> find(java.lang.String filename, DBObject sort)
filename
- the filename to look forsort
- the fields to sort withMongoException
- if the operation failspublic java.util.List<GridFSDBFile> find(DBObject query)
query
- the filter to applyMongoException
- if the operation failspublic java.util.List<GridFSDBFile> find(DBObject query, DBObject sort)
query
- the filter to applysort
- the fields to sort withMongoException
- if the operation failspublic void remove(ObjectId id)
id
- the id of the file to be removedMongoException
- if the operation failspublic void remove(java.lang.String filename)
filename
- the name of the file to be removedMongoException
- if the operation failspublic void remove(DBObject query)
query
- filter to applyMongoException
- if the operation failspublic GridFSInputFile createFile(byte[] data)
GridFSInputFile.save()
.data
- the file's datapublic GridFSInputFile createFile(java.io.File file) throws java.io.IOException
GridFSInputFile.save()
.file
- the file objectjava.io.IOException
- if there are problems reading file
public GridFSInputFile createFile(java.io.InputStream in)
GridFSInputFile.save()
.in
- an inputstream containing the file's datapublic GridFSInputFile createFile(java.io.InputStream in, boolean closeStreamOnPersist)
GridFSInputFile.save()
.in
- an inputstream containing the file's datacloseStreamOnPersist
- indicate the passed in input stream should be closed once the data chunk persistedpublic GridFSInputFile createFile(java.io.InputStream in, java.lang.String filename)
GridFSInputFile.save()
.in
- an inputstream containing the file's datafilename
- the file name as stored in the dbpublic GridFSInputFile createFile(java.io.InputStream in, java.lang.String filename, boolean closeStreamOnPersist)
GridFSInputFile.save()
.in
- an inputstream containing the file's datafilename
- the file name as stored in the dbcloseStreamOnPersist
- indicate the passed in input stream should be closed once the data chunk persistedpublic GridFSInputFile createFile(java.lang.String filename)
filename
- the file name as stored in the dbcreateFile()
public GridFSInputFile createFile()
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.public java.lang.String getBucketName()
public DB getDB()
Copyright © 2010 - 2020 Adobe. All Rights Reserved