MongoDB  2.0.3
gridfs.h
Go to the documentation of this file.
00001 
00003 /*    Copyright 2009 10gen Inc.
00004  *
00005  *    Licensed under the Apache License, Version 2.0 (the "License");
00006  *    you may not use this file except in compliance with the License.
00007  *    You may obtain a copy of the License at
00008  *
00009  *    http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  *    Unless required by applicable law or agreed to in writing, software
00012  *    distributed under the License is distributed on an "AS IS" BASIS,
00013  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *    See the License for the specific language governing permissions and
00015  *    limitations under the License.
00016  */
00017 
00018 #pragma once
00019 
00020 #include "dbclient.h"
00021 #include "redef_macros.h"
00022 
00023 namespace mongo {
00024 
00025     typedef unsigned long long gridfs_offset;
00026 
00027     class GridFS;
00028     class GridFile;
00029 
00030     class GridFSChunk {
00031     public:
00032         GridFSChunk( BSONObj data );
00033         GridFSChunk( BSONObj fileId , int chunkNumber , const char * data , int len );
00034 
00035         int len() {
00036             int len;
00037             _data["data"].binDataClean( len );
00038             return len;
00039         }
00040 
00041         const char * data( int & len ) {
00042             return _data["data"].binDataClean( len );
00043         }
00044 
00045     private:
00046         BSONObj _data;
00047         friend class GridFS;
00048     };
00049 
00050 
00055     class GridFS {
00056     public:
00062         GridFS( DBClientBase& client , const string& dbName , const string& prefix="fs" );
00063         ~GridFS();
00064 
00068         void setChunkSize(unsigned int size);
00069 
00079         BSONObj storeFile( const string& fileName , const string& remoteName="" , const string& contentType="");
00080 
00091         BSONObj storeFile( const char* data , size_t length , const string& remoteName , const string& contentType="");
00092 
00098         void removeFile( const string& fileName );
00099 
00103         GridFile findFile( BSONObj query );
00104 
00108         GridFile findFile( const string& fileName );
00109 
00113         auto_ptr<DBClientCursor> list();
00114 
00118         auto_ptr<DBClientCursor> list( BSONObj query );
00119 
00120     private:
00121         DBClientBase& _client;
00122         string _dbName;
00123         string _prefix;
00124         string _filesNS;
00125         string _chunksNS;
00126         unsigned int _chunkSize;
00127 
00128         // insert fileobject. All chunks must be in DB.
00129         BSONObj insertFile(const string& name, const OID& id, gridfs_offset length, const string& contentType);
00130 
00131         friend class GridFile;
00132     };
00133 
00137     class GridFile {
00138     public:
00143         bool exists() {
00144             return ! _obj.isEmpty();
00145         }
00146 
00147         string getFilename() {
00148             return _obj["filename"].str();
00149         }
00150 
00151         int getChunkSize() {
00152             return (int)(_obj["chunkSize"].number());
00153         }
00154 
00155         gridfs_offset getContentLength() {
00156             return (gridfs_offset)(_obj["length"].number());
00157         }
00158 
00159         string getContentType() {
00160             return _obj["contentType"].valuestr();
00161         }
00162 
00163         Date_t getUploadDate() {
00164             return _obj["uploadDate"].date();
00165         }
00166 
00167         string getMD5() {
00168             return _obj["md5"].str();
00169         }
00170 
00171         BSONElement getFileField( const string& name ) {
00172             return _obj[name];
00173         }
00174 
00175         BSONObj getMetadata();
00176 
00177         int getNumChunks() {
00178             return (int) ceil( (double)getContentLength() / (double)getChunkSize() );
00179         }
00180 
00181         GridFSChunk getChunk( int n );
00182 
00186         gridfs_offset write( ostream & out );
00187 
00191         gridfs_offset write( const string& where );
00192 
00193     private:
00194         GridFile( GridFS * grid , BSONObj obj );
00195 
00196         void _exists();
00197 
00198         GridFS * _grid;
00199         BSONObj _obj;
00200 
00201         friend class GridFS;
00202     };
00203 }
00204 
00205 #include "undef_macros.h"