MongoDB C++ Driver  legacy-1.1.2
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,
87  const std::string& remoteName = "",
88  const std::string& contentType = "");
89 
99  BSONObj storeFile(const char* data,
100  size_t length,
101  const std::string& remoteName,
102  const std::string& contentType = "");
103 
109  void removeFile(const std::string& fileName);
110 
114  GridFile findFile(Query query) const;
115 
119  GridFile findFileByName(const std::string& fileName) const;
120 
124  std::auto_ptr<DBClientCursor> list() const;
125 
129  std::auto_ptr<DBClientCursor> list(BSONObj query) const;
130 
131 private:
132  DBClientBase& _client;
133  std::string _dbName;
134  std::string _prefix;
135  std::string _filesNS;
136  std::string _chunksNS;
137  unsigned int _chunkSize;
138 
139  // insert fileobject. All chunks must be in DB.
140  BSONObj insertFile(const std::string& name,
141  const OID& id,
142  gridfs_offset length,
143  const std::string& contentType);
144 
145  // Insert a chunk into DB, this method is intended to be used by
146  // GridFileBuilder to incrementally insert chunks
147  void _insertChunk(const GridFSChunk& chunk);
148 
149  friend class GridFile;
150  friend class GridFileBuilder;
151 };
152 
156 class MONGO_CLIENT_API GridFile {
157 public:
162  bool exists() const {
163  return !_obj.isEmpty();
164  }
165 
166  std::string getFilename() const {
167  return _obj["filename"].str();
168  }
169 
170  int getChunkSize() const {
171  return (int)(_obj["chunkSize"].number());
172  }
173 
174  gridfs_offset getContentLength() const {
175  return (gridfs_offset)(_obj["length"].number());
176  }
177 
178  std::string getContentType() const {
179  return _obj["contentType"].valuestr();
180  }
181 
182  Date_t getUploadDate() const {
183  return _obj["uploadDate"].date();
184  }
185 
186  std::string getMD5() const {
187  return _obj["md5"].str();
188  }
189 
190  BSONElement getFileField(const std::string& name) const {
191  return _obj[name];
192  }
193 
194  BSONObj getMetadata() const;
195 
196  int getNumChunks() const {
197  return (int)ceil((double)getContentLength() / (double)getChunkSize());
198  }
199 
200  GridFSChunk getChunk(int n) const;
201 
205  gridfs_offset write(std::ostream& out) const;
206 
210  gridfs_offset write(const std::string& where) const;
211 
212 private:
213  GridFile(const GridFS* grid, BSONObj obj);
214 
215  void _exists() const;
216 
217  const GridFS* _grid;
218  BSONObj _obj;
219 
220  friend class GridFS;
221 };
222 
226 class MONGO_CLIENT_API GridFileBuilder {
227 public:
231  GridFileBuilder(GridFS* const grid);
232 
241  void appendChunk(const char* data, size_t length);
242 
252  mongo::BSONObj buildFile(const std::string& remoteName, const std::string& contentType = "");
253 
254 private:
255  GridFS* const _grid;
256  const size_t _chunkSize; // taken from GridFS in the constructor
257  unsigned int _currentChunk;
258  OID _fileId;
259  BSONObj _fileIdObj;
260  boost::scoped_array<char> _pendingData; // pointer with _chunkSize space
261  size_t _pendingDataSize;
262  gridfs_offset _fileLength;
263 
264  const char* _appendChunk(const char* data, size_t length, bool forcePendingInsert);
265 
266  void _appendPendingData();
267 };
268 }
GridFS is for storing large file-style objects in MongoDB.
Definition: gridfs.h:60
Utility functions for parsing numbers from strings.
Definition: compare_numbers.h:20
Object ID type.
Definition: oid.h:60
bool exists() const
Definition: gridfs.h:162
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:226
wrapper for a file stored in the Mongo database
Definition: gridfs.h:156
Represents a Mongo query expression.
Definition: dbclientinterface.h:403
abstract class that implements the core db operations
Definition: dbclientinterface.h:1422
C++ representation of a "BSON" object – that is, an extended JSON-style object in a binary represent...
Definition: bsonobj.h:78