2009-02-02 23:19:55 +01:00
|
|
|
// gridfs.cpp
|
|
|
|
|
2009-02-10 16:48:41 +01:00
|
|
|
#include "../stdafx.h"
|
2009-02-02 23:19:55 +01:00
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include "gridfs.h"
|
|
|
|
|
2009-02-10 16:48:41 +01:00
|
|
|
#if defined(_WIN32)
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
|
|
|
|
2009-02-02 23:19:55 +01:00
|
|
|
#ifndef MIN
|
|
|
|
#define MIN(a,b) ( (a) < (b) ? (a) : (b) )
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace mongo {
|
|
|
|
|
2009-02-09 21:38:26 +01:00
|
|
|
const unsigned DEFAULT_CHUNK_SIZE = 256 * 1024;
|
2009-02-02 23:19:55 +01:00
|
|
|
|
|
|
|
Chunk::Chunk( BSONObj o ){
|
|
|
|
_data = o;
|
|
|
|
}
|
|
|
|
|
2009-02-18 17:26:30 +01:00
|
|
|
Chunk::Chunk( BSONObj fileObject , int chunkNumber , const char * data , int len ){
|
2009-02-02 23:19:55 +01:00
|
|
|
BSONObjBuilder b;
|
2009-02-18 17:26:30 +01:00
|
|
|
b.appendAs( fileObject["_id"] , "files_id" );
|
2009-02-09 21:38:26 +01:00
|
|
|
b.append( "n" , chunkNumber );
|
2009-02-02 23:19:55 +01:00
|
|
|
b.appendBinDataArray( "data" , data , len );
|
2009-02-09 19:04:32 +01:00
|
|
|
_data = b.obj();
|
2009-02-02 23:19:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-14 22:22:00 +02:00
|
|
|
GridFS::GridFS( DBClientBase& client , const string& dbName , const string& prefix ) : _client( client ) , _dbName( dbName ) , _prefix( prefix ){
|
2009-02-02 23:19:55 +01:00
|
|
|
_filesNS = dbName + "." + prefix + ".files";
|
|
|
|
_chunksNS = dbName + "." + prefix + ".chunks";
|
|
|
|
|
|
|
|
|
|
|
|
client.ensureIndex( _filesNS , BSON( "filename" << 1 ) );
|
|
|
|
client.ensureIndex( _chunksNS , BSON( "files_id" << 1 << "n" << 1 ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
GridFS::~GridFS(){
|
2009-08-06 20:12:32 +02:00
|
|
|
|
2009-02-02 23:19:55 +01:00
|
|
|
}
|
2009-08-06 20:12:32 +02:00
|
|
|
|
2009-09-24 22:29:38 +02:00
|
|
|
BSONObj GridFS::storeFile( const string& fileName , const string& remoteName ){
|
2009-06-14 22:22:00 +02:00
|
|
|
uassert( "file doesn't exist" , boost::filesystem::exists( fileName ) );
|
2009-08-06 20:12:32 +02:00
|
|
|
|
2009-06-14 22:22:00 +02:00
|
|
|
gridfs_offset length = boost::filesystem::file_size( fileName );
|
2009-08-06 20:12:32 +02:00
|
|
|
|
2009-02-18 17:26:30 +01:00
|
|
|
BSONObjBuilder fileObject;
|
|
|
|
BSONObj idObj;
|
|
|
|
OID id;
|
2009-02-02 23:19:55 +01:00
|
|
|
{
|
2009-08-06 20:12:32 +02:00
|
|
|
|
2009-02-02 23:19:55 +01:00
|
|
|
id.init();
|
2009-02-18 17:26:30 +01:00
|
|
|
fileObject.appendOID( "_id" , &id );
|
|
|
|
|
2009-09-24 22:29:38 +02:00
|
|
|
fileObject << "filename" << (remoteName.empty() ? fileName : remoteName) ;
|
2009-02-18 17:26:30 +01:00
|
|
|
massert("large files not yet implemented", length <= 0xffffffff);
|
|
|
|
fileObject << "length" << (unsigned) length ;
|
|
|
|
fileObject << "chunkSize" << DEFAULT_CHUNK_SIZE ;
|
|
|
|
|
|
|
|
BSONObjBuilder b;
|
2009-02-02 23:19:55 +01:00
|
|
|
b.appendOID( "_id" , &id );
|
2009-02-18 17:26:30 +01:00
|
|
|
idObj = b.obj();
|
2009-02-02 23:19:55 +01:00
|
|
|
}
|
2009-08-06 20:12:32 +02:00
|
|
|
|
2009-02-02 23:19:55 +01:00
|
|
|
char buf[DEFAULT_CHUNK_SIZE];
|
|
|
|
gridfs_offset pos = 0;
|
2009-06-14 22:22:00 +02:00
|
|
|
FILE* fd = fopen( fileName.c_str() , "rb" );
|
2009-08-06 20:12:32 +02:00
|
|
|
|
2009-02-02 23:19:55 +01:00
|
|
|
int chunkNumber = 0;
|
|
|
|
while ( pos < length ){
|
2009-06-13 23:58:21 +02:00
|
|
|
int l = fread( buf , 1, MIN( DEFAULT_CHUNK_SIZE , length - pos ), fd );
|
2009-08-06 20:12:32 +02:00
|
|
|
|
2009-02-18 17:26:30 +01:00
|
|
|
Chunk c( idObj , chunkNumber , buf , l );
|
2009-02-02 23:19:55 +01:00
|
|
|
_client.insert( _chunksNS.c_str() , c._data );
|
2009-08-06 20:12:32 +02:00
|
|
|
|
2009-02-02 23:19:55 +01:00
|
|
|
pos += l;
|
|
|
|
chunkNumber++;
|
|
|
|
}
|
2009-08-06 20:12:32 +02:00
|
|
|
|
2009-06-13 23:58:21 +02:00
|
|
|
fclose( fd );
|
2009-02-02 23:19:55 +01:00
|
|
|
|
2009-02-18 17:26:30 +01:00
|
|
|
BSONObj res;
|
2009-02-18 19:11:22 +01:00
|
|
|
if ( ! _client.runCommand( _dbName.c_str() , BSON( "filemd5" << id << "root" << _prefix ) , res ) )
|
2009-02-18 17:26:30 +01:00
|
|
|
throw UserException( "filemd5 failed" );
|
2009-08-06 20:12:32 +02:00
|
|
|
|
2009-02-18 17:26:30 +01:00
|
|
|
fileObject.appendAs( res["md5"] , "md5" );
|
|
|
|
|
|
|
|
BSONObj real = fileObject.obj();
|
|
|
|
_client.insert( _filesNS.c_str() , real );
|
2009-02-02 23:19:55 +01:00
|
|
|
|
2009-02-18 17:26:30 +01:00
|
|
|
return real;
|
2009-02-02 23:19:55 +01:00
|
|
|
}
|
|
|
|
|
2009-06-14 22:22:00 +02:00
|
|
|
void GridFS::removeFile( const string& fileName ){
|
|
|
|
BSONObj idObj = _client.findOne( _filesNS , BSON( "filename" << fileName ) );
|
|
|
|
if ( !idObj.isEmpty() ){
|
2009-08-06 20:12:32 +02:00
|
|
|
_client.remove( _filesNS.c_str() , BSON( "filename" << fileName ) );
|
|
|
|
_client.remove( _chunksNS.c_str() , BSON( "files_id" << idObj["_id"] ) );
|
|
|
|
}
|
2009-06-14 22:22:00 +02:00
|
|
|
}
|
|
|
|
|
2009-02-02 23:19:55 +01:00
|
|
|
GridFile::GridFile( GridFS * grid , BSONObj obj ){
|
|
|
|
_grid = grid;
|
|
|
|
_obj = obj;
|
|
|
|
}
|
2009-08-06 20:12:32 +02:00
|
|
|
|
|
|
|
GridFile GridFS::findFile( const string& fileName ){
|
|
|
|
return findFile( BSON( "filename" << fileName ) );
|
2009-02-02 23:19:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
GridFile GridFS::findFile( BSONObj query ){
|
|
|
|
return GridFile( this , _client.findOne( _filesNS.c_str() , query ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
auto_ptr<DBClientCursor> GridFS::list(){
|
2009-03-19 21:23:04 +01:00
|
|
|
return _client.query( _filesNS.c_str() , BSONObj() );
|
2009-02-02 23:19:55 +01:00
|
|
|
}
|
|
|
|
|
2009-02-03 04:16:39 +01:00
|
|
|
auto_ptr<DBClientCursor> GridFS::list( BSONObj o ){
|
|
|
|
return _client.query( _filesNS.c_str() , o );
|
|
|
|
}
|
|
|
|
|
2009-08-20 04:34:13 +02:00
|
|
|
BSONObj GridFile::getMetadata(){
|
|
|
|
BSONElement meta_element = _obj["metadata"];
|
|
|
|
if( meta_element.eoo() ){
|
|
|
|
return BSONObj();
|
|
|
|
}
|
|
|
|
|
|
|
|
return meta_element.embeddedObject();
|
|
|
|
}
|
|
|
|
|
2009-02-02 23:19:55 +01:00
|
|
|
Chunk GridFile::getChunk( int n ){
|
|
|
|
_exists();
|
|
|
|
BSONObjBuilder b;
|
|
|
|
b.appendAs( _obj["_id"] , "files_id" );
|
2009-02-09 21:38:26 +01:00
|
|
|
b.append( "n" , n );
|
2009-02-02 23:19:55 +01:00
|
|
|
|
2009-02-09 19:04:32 +01:00
|
|
|
BSONObj o = _grid->_client.findOne( _grid->_chunksNS.c_str() , b.obj() );
|
2009-09-18 20:20:44 +02:00
|
|
|
uassert( "chunk is empty!" , ! o.isEmpty() );
|
2009-02-02 23:19:55 +01:00
|
|
|
return Chunk(o);
|
|
|
|
}
|
|
|
|
|
|
|
|
gridfs_offset GridFile::write( ostream & out ){
|
|
|
|
_exists();
|
2009-08-06 20:12:32 +02:00
|
|
|
|
2009-02-02 23:19:55 +01:00
|
|
|
const int num = getNumChunks();
|
2009-08-06 20:12:32 +02:00
|
|
|
|
2009-02-02 23:19:55 +01:00
|
|
|
for ( int i=0; i<num; i++ ){
|
|
|
|
Chunk c = getChunk( i );
|
|
|
|
|
|
|
|
int len;
|
|
|
|
const char * data = c.data( len );
|
|
|
|
out.write( data , len );
|
|
|
|
}
|
|
|
|
|
|
|
|
return getContentLength();
|
|
|
|
}
|
2009-08-06 20:12:32 +02:00
|
|
|
|
2009-06-14 22:22:00 +02:00
|
|
|
gridfs_offset GridFile::write( const string& where ){
|
2009-02-02 23:19:55 +01:00
|
|
|
ofstream out(where.c_str() , ios::out | ios::binary );
|
|
|
|
return write( out );
|
|
|
|
}
|
|
|
|
|
|
|
|
void GridFile::_exists(){
|
|
|
|
uassert( "doesn't exists" , exists() );
|
|
|
|
}
|
2009-08-06 20:12:32 +02:00
|
|
|
|
2009-02-02 23:19:55 +01:00
|
|
|
}
|