public class GridFs extends Object
This implementation uses a ObjectId
as the id when writing and stores
the name of the file in the files collection document's "filename" field. To
unlink(String)
or read(String, OutputStream)
a file from
the collection the _id field may contain any value but the filename field
must be present.
Modifier and Type | Field and Description |
---|---|
static String |
CHUNK_NUMBER_FIELD
The field in the
chunks collection containing the
chunk's number. |
static int |
CHUNK_OVERHEAD
The amount of overhead in a chunk document in bytes: 58
|
static String |
CHUNK_SIZE_FIELD
The field in the
files collection containing the
file's chunk size. |
static String |
CHUNKS_SUFFIX
The suffix for the chunks collection.
|
static String |
DATA_FIELD
The field in the
chunks collection containing the
chunk's data. |
static int |
DEFAULT_CHUNK_SIZE
The default chunk size.
|
static String |
DEFAULT_ROOT
The suffix for the files collection.
|
static String |
FILENAME_FIELD
The field in the
files collection containing the
file's name. |
static String |
FILES_ID_FIELD
The field in the
chunks collection containing the
chunk's related file id. |
static String |
FILES_SUFFIX
The suffix for the files collection.
|
static String |
ID_FIELD
The
_id field name. |
static String |
LENGTH_FIELD
The field in the
files collection containing the
file's length. |
static String |
MD5_FIELD
The field in the
files collection containing the
file's MD5. |
static String |
UPLOAD_DATE_FIELD
The field in the
files collection containing the
file's upload date. |
Constructor and Description |
---|
GridFs(MongoDatabase database)
Creates a new GridFs.
|
GridFs(MongoDatabase database,
String rootName)
Creates a new GridFs.
|
GridFs(String mongoDbUri)
Creates a new GridFs.
|
GridFs(String mongoDbUri,
String rootName)
Creates a new GridFs.
|
Modifier and Type | Method and Description |
---|---|
void |
createIndexes()
Creates the following indexes:
Files Collection:
{ 'filename' : 1, 'uploadDate' : 1 }
Chunks Collection:
{ 'files_id' : 1, 'n' : 1 }
If in a non-sharded environment the indexes will be unique. |
protected void |
doAddFault(Map<Object,List<String>> faults,
Element idObj,
String message)
Adds a fault message to the faults map.
|
protected void |
doRead(Document fileDoc,
OutputStream sink)
Reads a file from the GridFS collections and writes the contents to the
sink |
protected void |
doTryAndRepair(Document fileDoc,
Map<Object,List<String>> faults)
Tries to repair the file.
|
protected boolean |
doUnlink(Document fileDoc)
Unlinks (deletes) the file from the GridFS collections.
|
protected boolean |
doValidate(Document fileDoc)
Validates the file from the GridFS collections using the
filemd5
command. |
protected boolean |
doVerifyFileMd5(Map<Object,List<String>> faults,
Document fileDoc,
Document cmdResult)
Verifies the MD5 result for the filemd5 command.
|
Map<Object,List<String>> |
fsck(boolean repair)
Validates and optionally tries to repair the GridFS collections.
|
int |
getChunkSize()
Returns the size for a chunk written.
|
void |
read(ObjectId id,
OutputStream sink)
Reads a file from the GridFS collections and writes the contents to the
sink |
void |
read(String name,
OutputStream sink)
Reads a file from the GridFS collections and writes the contents to the
sink |
void |
setChunkSize(int chunkSize)
Sets the value of size for a chunk written.
|
boolean |
unlink(ObjectId id)
Unlinks (deletes) the file from the GridFS collections.
|
boolean |
unlink(String name)
Unlinks (deletes) the file from the GridFS collections.
|
boolean |
validate(ObjectId id)
Validates the file from the GridFS collections using the
filemd5
command. |
boolean |
validate(String name)
Validates the file from the GridFS collections using the
filemd5
command. |
ObjectId |
write(String name,
InputStream source)
Attempts to write a file into the GridFS collections using the specified
name for the file and deriving the chunks from the data read from the
source.
|
public static final String CHUNK_NUMBER_FIELD
chunks
collection containing the
chunk's number.public static final int CHUNK_OVERHEAD
public static final String CHUNK_SIZE_FIELD
files
collection containing the
file's chunk size.public static final String CHUNKS_SUFFIX
public static final String DATA_FIELD
chunks
collection containing the
chunk's data.public static final int DEFAULT_CHUNK_SIZE
CHUNK_OVERHEAD
when using the power of two allocator.public static final String DEFAULT_ROOT
public static final String FILENAME_FIELD
files
collection containing the
file's name.public static final String FILES_ID_FIELD
chunks
collection containing the
chunk's related file id.public static final String FILES_SUFFIX
public static final String ID_FIELD
_id
field name.public static final String LENGTH_FIELD
files
collection containing the
file's length.public static final String MD5_FIELD
files
collection containing the
file's MD5.public static final String UPLOAD_DATE_FIELD
files
collection containing the
file's upload date.public GridFs(MongoDatabase database)
The GridFS objects will be stored in the 'fs' collection.
database
- The database containing the GridFS collections.public GridFs(MongoDatabase database, String rootName)
database
- The database containing the GridFS collections.rootName
- The rootName for the collections. The FILES_SUFFIX
and CHUNKS_SUFFIX
will be appended to create the two
collection names.public GridFs(String mongoDbUri)
mongoDbUri
- The configuration for the connection to MongoDB expressed as a
MongoDB URL.IllegalArgumentException
- If the mongoDbUri is not a properly formated MongoDB
style URL.public GridFs(String mongoDbUri, String rootName)
mongoDbUri
- The configuration for the connection to MongoDB expressed as a
MongoDB URL.rootName
- The rootName for the collections. The FILES_SUFFIX
and CHUNKS_SUFFIX
will be appended to create the two
collection names.IllegalArgumentException
- If the mongoDbUri is not a properly formated MongoDB
style URL.public void createIndexes()
{ 'filename' : 1, 'uploadDate' : 1 }
{ 'files_id' : 1, 'n' : 1 }
public Map<Object,List<String>> fsck(boolean repair) throws IOException
{ 'filename' : 1, 'uploadDate' : 1 }
{ 'files_id' : 1, 'n' : 1 }
n
values for the chunks of a file.
If repair
is true then the n
values will be updated to be
sequential based on the ordering { 'n' : 1, '_id' 1 }.Warning: This function iterates over every file in the GridFS collection and can take a considerable amount of time and resources on the client and the server.
Note: Due to a limitation in the MongoDB server this method will
return false positives when used with a sharded cluster when the shard
key for the chunks collection is not one of {files_id:1}
or
{files_id:1, n:1}
. See SERVER-9888.
repair
- If set to true
then the fsck will attempt to
repair common errors.IOException
- On a failure to execute the fsck.public int getChunkSize()
public void read(ObjectId id, OutputStream sink) throws IOException
sink
id
- The id of the file.sink
- The stream to write the data to. This stream will not be
closed by this method.IOException
- On a failure reading the data from MongoDB or writing to the
sink
.public void read(String name, OutputStream sink) throws IOException
sink
name
- The name of the file.sink
- The stream to write the data to. This stream will not be
closed by this method.IOException
- On a failure reading the data from MongoDB or writing to the
sink
.public void setChunkSize(int chunkSize)
chunkSize
- The new value for the size for a chunk written.public boolean unlink(ObjectId id) throws IOException
id
- The id of the file to be deleted.IOException
- On a failure to delete the file.public boolean unlink(String name) throws IOException
name
- The name of the file to be deleted.IOException
- On a failure to validate the file.public boolean validate(ObjectId id) throws IOException
filemd5
command.
Note: Due to a limitation in the MongoDB server this method will
always return false
when used with a sharded cluster when
the shard key for the chunks collection is not one of
{files_id:1}
or {files_id:1, n:1}
. See SERVER-9888.
id
- The id of the file to be validate.IOException
- On a failure to validate the file.public boolean validate(String name) throws IOException
filemd5
command.
Note: Due to a limitation in the MongoDB server this method will
always return false
when used with a sharded cluster when
the shard key for the chunks collection is not one of
{files_id:1}
or {files_id:1, n:1}
. See SERVER-9888.
name
- The name of the file to be validate.IOException
- On a failure to validate the file.public ObjectId write(String name, InputStream source) throws IOException
name
- The name of the file being written.source
- The source of the bits in the file. This stream will not be
closed.ObjectId
associted with the file.IOException
- On a failure writing the documents or reading the file
contents. In the case of a failure an attempt is made to
remove the documents written to the collections.protected void doAddFault(Map<Object,List<String>> faults, Element idObj, String message)
faults
- The map of file ids to the error messages.idObj
- The id for the file.message
- The message to add.protected void doRead(Document fileDoc, OutputStream sink) throws IOException
sink
fileDoc
- The document for the file.sink
- The stream to write the data to. This stream will not be
closed by this method.IOException
- On a failure reading the data from MongoDB or writing to the
sink
.protected void doTryAndRepair(Document fileDoc, Map<Object,List<String>> faults)
Currently the only strategy is to reorder the chunk's into _id order. The operation verifies that the reorder fixes the file prior to modifying anything. it also verifies that the reordering worked after reordering the chunks.
fileDoc
- The document representing the file.faults
- The map to update with the status of the repair.protected boolean doUnlink(Document fileDoc) throws IOException
fileDoc
- The document for the file to delete.IOException
- On a failure to delete the file.protected boolean doValidate(Document fileDoc)
filemd5
command.
Note: Due to a limitation in the MongoDB server this method will
always return false
when used with a sharded cluster when
the shard key for the chunks collection is not one of
{files_id:1}
or {files_id:1, n:1}
. See SERVER-9888.
fileDoc
- The document for the file to delete.protected boolean doVerifyFileMd5(Map<Object,List<String>> faults, Document fileDoc, Document cmdResult)
faults
- The faults for to update if the verify fails.fileDoc
- The document representing the file.cmdResult
- The document returned from the 'filemd5' command.Copyright © 2011–2014 Allanbank Consulting, Inc.. All rights reserved.