MongoDB C++ Driver  legacy-1.0.5
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gridfs.h
Go to the documentation of this file.
1 
3 /* Copyright 2009 10gen Inc.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #pragma once
19 
20 #include "boost/scoped_array.hpp"
21 
22 #include "mongo/bson/bsonelement.h"
23 #include "mongo/bson/bsonobj.h"
25 #include "mongo/client/export_macros.h"
26 
27 namespace mongo {
28 
29  typedef unsigned long long gridfs_offset;
30 
31  class GridFS;
32  class GridFile;
33  class GridFileBuilder;
34 
35  class MONGO_CLIENT_API GridFSChunk {
36  public:
37  GridFSChunk( BSONObj data );
38  GridFSChunk( BSONObj fileId , int chunkNumber , const char * data , int len );
39 
40  int len() const {
41  int len;
42  _data["data"].binDataClean( len );
43  return len;
44  }
45 
46  const char * data( int & len ) const {
47  return _data["data"].binDataClean( len );
48  }
49 
50  private:
51  BSONObj _data;
52  friend class GridFS;
53  };
54 
55 
60  class MONGO_CLIENT_API GridFS {
61  public:
67  GridFS( DBClientBase& client , const std::string& dbName , const std::string& prefix="fs" );
68  ~GridFS();
69 
73  void setChunkSize(unsigned int size);
74 
75  unsigned int getChunkSize() const;
76 
86  BSONObj storeFile( const std::string& fileName , const std::string& remoteName="" , const std::string& contentType="");
87 
97  BSONObj storeFile( const char* data , size_t length , const std::string& remoteName , const std::string& contentType="");
98 
104  void removeFile( const std::string& fileName );
105 
109  GridFile findFile( Query query ) const;
110 
114  GridFile findFileByName( const std::string& fileName ) const;
115 
119  std::auto_ptr<DBClientCursor> list() const;
120 
124  std::auto_ptr<DBClientCursor> list( BSONObj query ) const;
125 
126  private:
127  DBClientBase& _client;
128  std::string _dbName;
129  std::string _prefix;
130  std::string _filesNS;
131  std::string _chunksNS;
132  unsigned int _chunkSize;
133 
134  // insert fileobject. All chunks must be in DB.
135  BSONObj insertFile(const std::string& name, const OID& id, gridfs_offset length, const std::string& contentType);
136 
137  // Insert a chunk into DB, this method is intended to be used by
138  // GridFileBuilder to incrementally insert chunks
139  void _insertChunk(const GridFSChunk& chunk);
140 
141  friend class GridFile;
142  friend class GridFileBuilder;
143  };
144 
148  class MONGO_CLIENT_API GridFile {
149  public:
154  bool exists() const {
155  return ! _obj.isEmpty();
156  }
157 
158  std::string getFilename() const {
159  return _obj["filename"].str();
160  }
161 
162  int getChunkSize() const {
163  return (int)(_obj["chunkSize"].number());
164  }
165 
166  gridfs_offset getContentLength() const {
167  return (gridfs_offset)(_obj["length"].number());
168  }
169 
170  std::string getContentType() const {
171  return _obj["contentType"].valuestr();
172  }
173 
174  Date_t getUploadDate() const {
175  return _obj["uploadDate"].date();
176  }
177 
178  std::string getMD5() const {
179  return _obj["md5"].str();
180  }
181 
182  BSONElement getFileField( const std::string& name ) const {
183  return _obj[name];
184  }
185 
186  BSONObj getMetadata() const;
187 
188  int getNumChunks() const {
189  return (int) ceil( (double)getContentLength() / (double)getChunkSize() );
190  }
191 
192  GridFSChunk getChunk( int n ) const;
193 
197  gridfs_offset write( std::ostream & out ) const;
198 
202  gridfs_offset write( const std::string& where ) const;
203 
204  private:
205  GridFile(const GridFS * grid , BSONObj obj );
206 
207  void _exists() const;
208 
209  const GridFS * _grid;
210  BSONObj _obj;
211 
212  friend class GridFS;
213  };
214 
218  class MONGO_CLIENT_API GridFileBuilder {
219  public:
223  GridFileBuilder( GridFS* const grid );
224 
233  void appendChunk( const char* data, size_t length );
234 
244  mongo::BSONObj buildFile( const std::string& remoteName,
245  const std::string& contentType="" );
246 
247  private:
248  GridFS* const _grid;
249  const size_t _chunkSize; // taken from GridFS in the constructor
250  unsigned int _currentChunk;
251  OID _fileId;
252  BSONObj _fileIdObj;
253  boost::scoped_array<char> _pendingData; // pointer with _chunkSize space
254  size_t _pendingDataSize;
255  gridfs_offset _fileLength;
256 
257  const char* _appendChunk( const char* data, size_t length,
258  bool forcePendingInsert );
259 
260  void _appendPendingData();
261  };
262 
263 
264 }
GridFS is for storing large file-style objects in MongoDB.
Definition: gridfs.h:60
the main MongoDB namespace
Definition: bulk_operation_builder.h:24
Object ID type.
Definition: oid.h:60
bool exists() const
Definition: gridfs.h:154
Definition: gridfs.h:35
Core MongoDB C++ driver interfaces are defined here.
class which allow to build GridFiles in a stream fashion way
Definition: gridfs.h:218
wrapper for a file stored in the Mongo database
Definition: gridfs.h:148
Represents a Mongo query expression.
Definition: dbclientinterface.h:387
abstract class that implements the core db operations
Definition: dbclientinterface.h:1330
C++ representation of a "BSON" object – that is, an extended JSON-style object in a binary represent...
Definition: bsonobj.h:78