summaryrefslogtreecommitdiff
path: root/client/gridfs.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'client/gridfs.cpp')
-rw-r--r--client/gridfs.cpp51
1 files changed, 32 insertions, 19 deletions
diff --git a/client/gridfs.cpp b/client/gridfs.cpp
index 892ec6e..b2ae478 100644
--- a/client/gridfs.cpp
+++ b/client/gridfs.cpp
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-#include "../stdafx.h"
+#include "pch.h"
#include <fcntl.h>
#include <utility>
@@ -34,15 +34,15 @@ namespace mongo {
const unsigned DEFAULT_CHUNK_SIZE = 256 * 1024;
- Chunk::Chunk( BSONObj o ){
+ GridFSChunk::GridFSChunk( BSONObj o ){
_data = o;
}
- Chunk::Chunk( BSONObj fileObject , int chunkNumber , const char * data , int len ){
+ GridFSChunk::GridFSChunk( BSONObj fileObject , int chunkNumber , const char * data , int len ){
BSONObjBuilder b;
b.appendAs( fileObject["_id"] , "files_id" );
b.append( "n" , chunkNumber );
- b.appendBinDataArray( "data" , data , len );
+ b.appendBinData( "data" , len, BinDataGeneral, data );
_data = b.obj();
}
@@ -50,7 +50,7 @@ namespace mongo {
GridFS::GridFS( DBClientBase& client , const string& dbName , const string& prefix ) : _client( client ) , _dbName( dbName ) , _prefix( prefix ){
_filesNS = dbName + "." + prefix + ".files";
_chunksNS = dbName + "." + prefix + ".chunks";
-
+ _chunkSize = DEFAULT_CHUNK_SIZE;
client.ensureIndex( _filesNS , BSON( "filename" << 1 ) );
client.ensureIndex( _chunksNS , BSON( "files_id" << 1 << "n" << 1 ) );
@@ -60,6 +60,11 @@ namespace mongo {
}
+ void GridFS::setChunkSize(unsigned int size) {
+ massert( 13296 , "invalid chunk size is specified", (size == 0));
+ _chunkSize = size;
+ }
+
BSONObj GridFS::storeFile( const char* data , size_t length , const string& remoteName , const string& contentType){
massert( 10279 , "large files not yet implemented", length <= 0xffffffff);
char const * const end = data + length;
@@ -70,8 +75,8 @@ namespace mongo {
int chunkNumber = 0;
while (data < end){
- int chunkLen = MIN(DEFAULT_CHUNK_SIZE, (unsigned)(end-data));
- Chunk c(idObj, chunkNumber, data, chunkLen);
+ int chunkLen = MIN(_chunkSize, (unsigned)(end-data));
+ GridFSChunk c(idObj, chunkNumber, data, chunkLen);
_client.insert( _chunksNS.c_str() , c._data );
chunkNumber++;
@@ -99,22 +104,24 @@ namespace mongo {
int chunkNumber = 0;
gridfs_offset length = 0;
while (!feof(fd)){
- boost::scoped_array<char>buf (new char[DEFAULT_CHUNK_SIZE]);
- char* bufPos = buf.get();
+ //boost::scoped_array<char>buf (new char[_chunkSize+1]);
+ char * buf = new char[_chunkSize+1];
+ char* bufPos = buf;//.get();
unsigned int chunkLen = 0; // how much in the chunk now
- while(chunkLen != DEFAULT_CHUNK_SIZE && !feof(fd)){
- int readLen = fread(bufPos, 1, DEFAULT_CHUNK_SIZE - chunkLen, fd);
+ while(chunkLen != _chunkSize && !feof(fd)){
+ int readLen = fread(bufPos, 1, _chunkSize - chunkLen, fd);
chunkLen += readLen;
bufPos += readLen;
- assert(chunkLen <= DEFAULT_CHUNK_SIZE);
+ assert(chunkLen <= _chunkSize);
}
- Chunk c(idObj, chunkNumber, buf.get(), chunkLen);
+ GridFSChunk c(idObj, chunkNumber, buf, chunkLen);
_client.insert( _chunksNS.c_str() , c._data );
length += chunkLen;
chunkNumber++;
+ delete[] buf;
}
if (fd != stdin)
@@ -125,7 +132,7 @@ namespace mongo {
return insertFile((remoteName.empty() ? fileName : remoteName), id, length, contentType);
}
- BSONObj GridFS::insertFile(const string& name, const OID& id, unsigned length, const string& contentType){
+ BSONObj GridFS::insertFile(const string& name, const OID& id, gridfs_offset length, const string& contentType){
BSONObj res;
if ( ! _client.runCommand( _dbName.c_str() , BSON( "filemd5" << id << "root" << _prefix ) , res ) )
@@ -134,12 +141,17 @@ namespace mongo {
BSONObjBuilder file;
file << "_id" << id
<< "filename" << name
- << "length" << (unsigned) length
- << "chunkSize" << DEFAULT_CHUNK_SIZE
+ << "chunkSize" << _chunkSize
<< "uploadDate" << DATENOW
<< "md5" << res["md5"]
;
+ if (length < 1024*1024*1024){ // 2^30
+ file << "length" << (int) length;
+ }else{
+ file << "length" << (long long) length;
+ }
+
if (!contentType.empty())
file << "contentType" << contentType;
@@ -190,7 +202,7 @@ namespace mongo {
return meta_element.embeddedObject();
}
- Chunk GridFile::getChunk( int n ){
+ GridFSChunk GridFile::getChunk( int n ){
_exists();
BSONObjBuilder b;
b.appendAs( _obj["_id"] , "files_id" );
@@ -198,7 +210,7 @@ namespace mongo {
BSONObj o = _grid->_client.findOne( _grid->_chunksNS.c_str() , b.obj() );
uassert( 10014 , "chunk is empty!" , ! o.isEmpty() );
- return Chunk(o);
+ return GridFSChunk(o);
}
gridfs_offset GridFile::write( ostream & out ){
@@ -207,7 +219,7 @@ namespace mongo {
const int num = getNumChunks();
for ( int i=0; i<num; i++ ){
- Chunk c = getChunk( i );
+ GridFSChunk c = getChunk( i );
int len;
const char * data = c.data( len );
@@ -222,6 +234,7 @@ namespace mongo {
return write( cout );
} else {
ofstream out(where.c_str() , ios::out | ios::binary );
+ uassert(13325, "couldn't open file: " + where, out.is_open() );
return write( out );
}
}