MongoDB  2.7.0
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 "mongo/bson/bsonelement.h"
21 #include "mongo/bson/bsonobj.h"
23 #include "mongo/client/export_macros.h"
24 
25 namespace mongo {
26 
27  typedef unsigned long long gridfs_offset;
28 
29  class GridFS;
30  class GridFile;
31 
32  class MONGO_CLIENT_API GridFSChunk {
33  public:
34  GridFSChunk( BSONObj data );
35  GridFSChunk( BSONObj fileId , int chunkNumber , const char * data , int len );
36 
37  int len() const {
38  int len;
39  _data["data"].binDataClean( len );
40  return len;
41  }
42 
43  const char * data( int & len ) const {
44  return _data["data"].binDataClean( len );
45  }
46 
47  private:
48  BSONObj _data;
49  friend class GridFS;
50  };
51 
52 
57  class MONGO_CLIENT_API GridFS {
58  public:
64  GridFS( DBClientBase& client , const string& dbName , const string& prefix="fs" );
65  ~GridFS();
66 
70  void setChunkSize(unsigned int size);
71 
72  unsigned int getChunkSize() const;
73 
83  BSONObj storeFile( const string& fileName , const string& remoteName="" , const string& contentType="");
84 
94  BSONObj storeFile( const char* data , size_t length , const string& remoteName , const string& contentType="");
95 
101  void removeFile( const string& fileName );
102 
106  GridFile findFile( BSONObj query ) const;
107 
111  GridFile findFile( const string& fileName ) const;
112 
116  auto_ptr<DBClientCursor> list() const;
117 
121  auto_ptr<DBClientCursor> list( BSONObj query ) const;
122 
123  private:
124  DBClientBase& _client;
125  string _dbName;
126  string _prefix;
127  string _filesNS;
128  string _chunksNS;
129  unsigned int _chunkSize;
130 
131  // insert fileobject. All chunks must be in DB.
132  BSONObj insertFile(const string& name, const OID& id, gridfs_offset length, const string& contentType);
133 
134  friend class GridFile;
135  };
136 
140  class MONGO_CLIENT_API GridFile {
141  public:
146  bool exists() const {
147  return ! _obj.isEmpty();
148  }
149 
150  string getFilename() const {
151  return _obj["filename"].str();
152  }
153 
154  int getChunkSize() const {
155  return (int)(_obj["chunkSize"].number());
156  }
157 
158  gridfs_offset getContentLength() const {
159  return (gridfs_offset)(_obj["length"].number());
160  }
161 
162  string getContentType() const {
163  return _obj["contentType"].valuestr();
164  }
165 
166  Date_t getUploadDate() const {
167  return _obj["uploadDate"].date();
168  }
169 
170  string getMD5() const {
171  return _obj["md5"].str();
172  }
173 
174  BSONElement getFileField( const string& name ) const {
175  return _obj[name];
176  }
177 
178  BSONObj getMetadata() const;
179 
180  int getNumChunks() const {
181  return (int) ceil( (double)getContentLength() / (double)getChunkSize() );
182  }
183 
184  GridFSChunk getChunk( int n ) const;
185 
189  gridfs_offset write( ostream & out ) const;
190 
194  gridfs_offset write( const string& where ) const;
195 
196  private:
197  GridFile(const GridFS * grid , BSONObj obj );
198 
199  void _exists() const;
200 
201  const GridFS * _grid;
202  BSONObj _obj;
203 
204  friend class GridFS;
205  };
206 }
LogstreamBuilder out()
Synonym for log().
Definition: log.h:79
Definition: gridfs.h:32
abstract class that implements the core db operations
Definition: dbclientinterface.h:1058
Core MongoDB C++ driver interfaces are defined here.
C++ representation of a "BSON" object – that is, an extended JSON-style object in a binary representa...
Definition: bsonobj.h:77
Object ID type.
Definition: oid.h:42
bool exists() const
Definition: gridfs.h:146
GridFS is for storing large file-style objects in MongoDB.
Definition: gridfs.h:57
wrapper for a file stored in the Mongo database
Definition: gridfs.h:140