MongoDB C++ Driver legacy-1.1.2
Loading...
Searching...
No Matches
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
27namespace mongo {
28
29typedef unsigned long long gridfs_offset;
30
31class GridFS;
32class GridFile;
33class GridFileBuilder;
34
35class MONGO_CLIENT_API GridFSChunk {
36public:
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
50private:
51 BSONObj _data;
52 friend class GridFS;
53};
54
55
60class MONGO_CLIENT_API GridFS {
61public:
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
131private:
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
156class MONGO_CLIENT_API GridFile {
157public:
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
212private:
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
226class MONGO_CLIENT_API GridFileBuilder {
227public:
232
241 void appendChunk(const char* data, size_t length);
242
252 mongo::BSONObj buildFile(const std::string& remoteName, const std::string& contentType = "");
253
254private:
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}
C++ representation of a "BSON" object – that is, an extended JSON-style object in a binary representa...
Definition bsonobj.h:78
abstract class that implements the core db operations
Definition dbclientinterface.h:1422
Definition gridfs.h:35
GridFS is for storing large file-style objects in MongoDB.
Definition gridfs.h:60
BSONObj storeFile(const std::string &fileName, const std::string &remoteName="", const std::string &contentType="")
puts the file reference by fileName into the db
GridFS(DBClientBase &client, const std::string &dbName, const std::string &prefix="fs")
GridFile findFile(Query query) const
returns a file object matching the query
GridFile findFileByName(const std::string &fileName) const
equiv to findFile( { filename : filename } )
void setChunkSize(unsigned int size)
std::auto_ptr< DBClientCursor > list() const
convenience method to get all the files
void removeFile(const std::string &fileName)
removes file referenced by fileName from the db
BSONObj storeFile(const char *data, size_t length, const std::string &remoteName, const std::string &contentType="")
puts the file represented by data into the db
std::auto_ptr< DBClientCursor > list(BSONObj query) const
convenience method to get all the files with a filter
class which allow to build GridFiles in a stream fashion way
Definition gridfs.h:226
void appendChunk(const char *data, size_t length)
Appends a chunk of data.
GridFileBuilder(GridFS *const grid)
mongo::BSONObj buildFile(const std::string &remoteName, const std::string &contentType="")
Inserts the description of the file in GridFS collection.
wrapper for a file stored in the Mongo database
Definition gridfs.h:156
gridfs_offset write(const std::string &where) const
write the file to this filename
gridfs_offset write(std::ostream &out) const
write the file to the output stream
bool exists() const
Definition gridfs.h:162
Object ID type.
Definition oid.h:60
Represents a Mongo query expression.
Definition dbclientinterface.h:403
Core MongoDB C++ driver interfaces are defined here.
Utility functions for parsing numbers from strings.
Definition compare_numbers.h:20