MongoDB
2.5.0
Main Page
Related Pages
Namespaces
Classes
Files
File List
File Members
src
mongo
client
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"
22
#include "
mongo/client/dbclientinterface.h
"
23
24
namespace
mongo {
25
26
typedef
unsigned
long
long
gridfs_offset;
27
28
class
GridFS;
29
class
GridFile;
30
31
class
GridFSChunk
{
32
public
:
33
GridFSChunk
(
BSONObj
data );
34
GridFSChunk
(
BSONObj
fileId ,
int
chunkNumber ,
const
char
* data ,
int
len );
35
36
int
len()
const
{
37
int
len;
38
_data[
"data"
].binDataClean( len );
39
return
len;
40
}
41
42
const
char
* data(
int
& len )
const
{
43
return
_data[
"data"
].binDataClean( len );
44
}
45
46
private
:
47
BSONObj
_data;
48
friend
class
GridFS
;
49
};
50
51
56
class
GridFS
{
57
public
:
63
GridFS
(
DBClientBase
& client ,
const
string
& dbName ,
const
string
& prefix=
"fs"
);
64
~
GridFS
();
65
69
void
setChunkSize
(
unsigned
int
size);
70
71
unsigned
int
getChunkSize()
const
;
72
82
BSONObj
storeFile
(
const
string
& fileName ,
const
string
& remoteName=
""
,
const
string
& contentType=
""
);
83
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
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
}
Generated on Wed May 22 2013 14:16:16 for MongoDB by
1.8.3.1