Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 243 lines (191 sloc) 7.31 kb
e3dca15 @gerald-lindsly License info added
authored
1 /** Copyright 2009-2011 10gen Inc.
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
92ab7e6 @gerald-lindsly GridFS implemented
authored
15 #include "MongoMatlabDriver.h"
16 #include "gridfs.h"
17
18 #include <mex.h>
19
20 EXPORT int mongo_gridfs_create(struct mongo_* conn, char* db, char* prefix, struct gridfs_** gfs) {
21 gridfs* gfs_ = (gridfs*)malloc(sizeof(gridfs));
22 if (gridfs_init((mongo*)conn, db, prefix, gfs_) != MONGO_OK) {
23 free(gfs_);
24 return 0;
25 }
26 *gfs = (struct gridfs_*)gfs_;
27 return 1;
28 }
29
30
31 EXPORT void mongo_gridfs_destroy(struct gridfs_* gfs) {
32 if (gfs)
33 gridfs_destroy((gridfs*) gfs);
34 }
35
36
37 EXPORT int mongo_gridfs_store_file(struct gridfs_* gfs, char* filename, char* remoteName, char* contentType) {
38 return (gridfs_store_file((gridfs*)gfs, filename, remoteName, contentType) == MONGO_OK);
39 }
40
41
42 EXPORT void mongo_gridfs_remove_file(struct gridfs_* gfs, char* remoteName) {
43 gridfs_remove_filename((gridfs*)gfs, remoteName);
44 }
45
46
47 static void* calcSize(mxArray* data, uint64_t* size) {
48
49 uint64_t numel = mxGetNumberOfElements(data);
50 switch (mxGetClassID(data)) {
51 case mxLOGICAL_CLASS: ;
52 case mxINT8_CLASS:
53 case mxUINT8_CLASS:
54 *size = numel;
55 return mxGetData(data);
56 case mxCHAR_CLASS:
57 *size = numel * sizeof(mxChar);
58 return mxGetChars(data);
59 case mxDOUBLE_CLASS:
60 *size = numel * sizeof(double);
61 return mxGetPr(data);
62 case mxSINGLE_CLASS:
63 *size = numel * sizeof(float);
64 return mxGetData(data);
65 case mxINT16_CLASS:
66 case mxUINT16_CLASS:
67 *size = numel * sizeof(short);
68 return mxGetData(data);
69 case mxINT32_CLASS:
70 case mxUINT32_CLASS:
71 *size = numel * sizeof(int);
72 return mxGetData(data);
73 case mxINT64_CLASS:
74 case mxUINT64_CLASS:
75 *size = numel * sizeof(int64_t);
76 return mxGetData(data);
77 default: {
78 char s[256];
79 sprintf(s, "GridFS - Unsupported type (%s)\n", mxGetClassName(data));
80 mexErrMsgTxt(s);
81 return 0;
82 }
83 }
84 }
85
86
87 EXPORT int mongo_gridfs_store(struct gridfs_* gfs, mxArray* data, char* remoteName, char* contentType) {
88 uint64_t size;
89 void* p;
90 if (mxIsComplex(data))
91 mexErrMsgTxt("GridFS:store - Complex values not supported");
92 p = calcSize(data, &size);
93 return (gridfs_store_buffer((gridfs*)gfs, (char*)p, size, remoteName, contentType) == MONGO_OK);
94 }
95
96
97 EXPORT void mongo_gridfile_writer_create(struct gridfs_* gfs, char* remoteName, char* contentType, struct gridfile_** gf) {
98 gridfile* gf_ = (gridfile*)malloc(sizeof(gridfile));
99 gridfile_writer_init(gf_, (gridfs*)gfs, remoteName, contentType);
100 *gf = (struct gridfile_*)gf_;
101 }
102
103
104 EXPORT void mongo_gridfile_writer_write(struct gridfile_* gf, mxArray* data) {
105 uint64_t size;
106 void* p;
107 int cplx = mxIsComplex(data);
108 if (cplx && mxGetClassID(data) != mxDOUBLE_CLASS)
109 mexErrMsgTxt("GridfileWriter:write - only complex values of type double are supported");
110 p = calcSize(data, &size);
111 gridfile_write_buffer((gridfile*)gf, (char*)p, size);
112 if (cplx)
113 gridfile_write_buffer((gridfile*)gf, (char*)mxGetPi(data), size);
114 }
115
116
117 EXPORT int mongo_gridfile_writer_finish(struct gridfile_* gf) {
118 return (gridfile_writer_done((gridfile*)gf) == MONGO_OK);
119 }
120
121
122 EXPORT int mongo_gridfs_find(struct gridfs_* gfs, struct bson_* query, struct gridfile_** gf) {
123 gridfile* gf_ = (gridfile*)malloc(sizeof(gridfile));
124 if (gridfs_find_query((gridfs*)gfs, (bson*)query, gf_) != MONGO_OK) {
125 free(gf_);
126 return 0;
127 }
128 *gf = (struct gridfile_*)gf_;
129 return 1;
130 }
131
132
133 EXPORT void mongo_gridfile_destroy(struct gridfile_* gf) {
134 if (gf) {
135 gridfile_destroy((gridfile*)gf);
136 free(gf);
137 }
138 }
139
140
141 EXPORT const char* mongo_gridfile_get_filename(struct gridfile_* gf) {
142 return gridfile_get_filename((gridfile*)gf);
143 }
144
145
146 EXPORT double mongo_gridfile_get_length(struct gridfile_* gf) {
147 return (double)gridfile_get_contentlength((gridfile*)gf);
148 }
149
150
151 EXPORT int mongo_gridfile_get_chunk_size(struct gridfile_* gf) {
152 return gridfile_get_chunksize((gridfile*)gf);
153 }
154
155
156 EXPORT int mongo_gridfile_get_chunk_count(struct gridfile_* gf) {
157 return gridfile_get_numchunks((gridfile*)gf);
158 }
159
160
161 EXPORT const char* mongo_gridfile_get_content_type(struct gridfile_* gf) {
162 return gridfile_get_contenttype((gridfile*)gf);
163 }
164
165
166 EXPORT double mongo_gridfile_get_upload_date(struct gridfile_* gf) {
167 return 719529 + gridfile_get_uploaddate((gridfile*)gf) / (1000.0 * 60 * 60 * 24);
168 }
169
170
171 EXPORT const char* mongo_gridfile_get_md5(struct gridfile_* gf) {
172 return gridfile_get_md5((gridfile*)gf);
173 }
174
175
176 EXPORT void mongo_gridfile_get_descriptor(struct gridfile_* gf, struct bson_** out) {
177 bson* b = (bson*)malloc(sizeof(bson));
178 bson_copy(b, ((gridfile*)gf)->meta);
179 *out = (struct bson_*) b;
180 }
181
182
183 int mongo_gridfile_get_metadata(struct gridfile_* gf, struct bson_** out) {
f8a6e3e @gerald-lindsly Driver Sync; also mongo.distinct() added
authored
184 bson meta, *b;
185 gridfile_get_metadata((gridfile*)gf, &meta);
92ab7e6 @gerald-lindsly GridFS implemented
authored
186 if (bson_size(&meta) <= 5)
187 return 0;
188 b = (bson*)malloc(sizeof(bson));
189 bson_copy(b, &meta);
190 *out = (struct bson_*) b;
191 return 1;
192 }
193
194
195 int mongo_gridfile_get_chunk(struct gridfile_* gf, int i, struct bson_** out) {
f8a6e3e @gerald-lindsly Driver Sync; also mongo.distinct() added
authored
196 bson chunk, *b;
197 gridfile_get_chunk((gridfile*)gf, i, &chunk);
92ab7e6 @gerald-lindsly GridFS implemented
authored
198 if (bson_size(&chunk) <= 5)
199 return 0;
200 b = (bson*)malloc(sizeof(bson));
201 *b = chunk;
202 *out = (struct bson_*)b;
203 return 1;
204 }
205
206
207 EXPORT int mongo_gridfile_get_chunks(struct gridfile_* gf, int start, int count, struct mongo_cursor_** out) {
208 mongo_cursor* cursor = gridfile_get_chunks((gridfile*)gf, start, count);
209 if (!cursor)
210 return 0;
211 *out = (struct mongo_cursor_*)cursor;
212 return 1;
213 }
214
215
216 EXPORT int mongo_gridfile_read(struct gridfile_* gf, mxArray* data) {
217 gridfile* gf_ = (gridfile*)gf;
218 uint64_t size;
219 void* p;
220 gridfs_offset remaining;
221 int cplx = mxIsComplex(data);
222 if (cplx && mxGetClassID(data) != mxDOUBLE_CLASS)
223 mexErrMsgTxt("Gridfile:read - only complex values of type double are supported");
224 p = calcSize(data, &size);
225 remaining = gridfile_get_contentlength(gf_) - gf_->pos;
226 if (size > remaining || cplx && size*2 > remaining)
227 return 0;
228 if (size) gridfile_read(gf_, size, (char*)p);
229 if (size && cplx)
230 gridfile_read(gf_, size, (char*)mxGetPi(data));
231 return 1;
232 }
233
234
235 EXPORT double mongo_gridfile_get_pos(struct gridfile_* gf) {
236 return (double)((gridfile*)gf)->pos;
237 }
238
239
240 EXPORT double mongo_gridfile_seek(struct gridfile_* gf, double offset) {
241 return (double)gridfile_seek((gridfile*)gf, (gridfs_offset)offset);
242 }
Something went wrong with that request. Please try again.