|
21 | 21 | #include "mongoc-client-private.h"
|
22 | 22 | #include "mongoc-collection.h"
|
23 | 23 | #include "mongoc-collection-private.h"
|
| 24 | +#include "mongoc-error.h" |
24 | 25 | #include "mongoc-index.h"
|
25 | 26 | #include "mongoc-gridfs.h"
|
26 | 27 | #include "mongoc-gridfs-private.h"
|
@@ -301,3 +302,108 @@ mongoc_gridfs_get_chunks (mongoc_gridfs_t *gridfs)
|
301 | 302 |
|
302 | 303 | return gridfs->chunks;
|
303 | 304 | }
|
| 305 | + |
| 306 | + |
| 307 | +bool |
| 308 | +mongoc_gridfs_remove_by_filename (mongoc_gridfs_t *gridfs, |
| 309 | + const char *filename, |
| 310 | + bson_error_t *error) |
| 311 | +{ |
| 312 | + mongoc_bulk_operation_t *bulk_files = NULL; |
| 313 | + mongoc_bulk_operation_t *bulk_chunks = NULL; |
| 314 | + mongoc_cursor_t *cursor = NULL; |
| 315 | + bson_error_t files_error; |
| 316 | + bson_error_t chunks_error; |
| 317 | + const bson_t *doc; |
| 318 | + const char *key; |
| 319 | + char keybuf[16]; |
| 320 | + int count = 0; |
| 321 | + bool chunks_ret; |
| 322 | + bool files_ret; |
| 323 | + bool ret = false; |
| 324 | + bson_iter_t iter; |
| 325 | + bson_t *files_q; |
| 326 | + bson_t *chunks_q; |
| 327 | + bson_t q = BSON_INITIALIZER; |
| 328 | + bson_t fields = BSON_INITIALIZER; |
| 329 | + bson_t ar = BSON_INITIALIZER; |
| 330 | + |
| 331 | + bson_return_val_if_fail (gridfs, false); |
| 332 | + |
| 333 | + if (!filename) { |
| 334 | + bson_set_error (error, |
| 335 | + MONGOC_ERROR_GRIDFS, |
| 336 | + MONGOC_ERROR_GRIDFS_INVALID_FILENAME, |
| 337 | + "A non-NULL filename must be specified."); |
| 338 | + return false; |
| 339 | + } |
| 340 | + |
| 341 | + /* |
| 342 | + * Find all files matching this filename. Hopefully just one, but not |
| 343 | + * strictly required! |
| 344 | + */ |
| 345 | + |
| 346 | + BSON_APPEND_UTF8 (&q, "filename", filename); |
| 347 | + BSON_APPEND_INT32 (&fields, "_id", 1); |
| 348 | + |
| 349 | + cursor = mongoc_collection_find (gridfs->files, MONGOC_QUERY_NONE, 0, 0, 0, |
| 350 | + &q, &fields, NULL); |
| 351 | + BSON_ASSERT (cursor); |
| 352 | + |
| 353 | + while (mongoc_cursor_next (cursor, &doc)) { |
| 354 | + if (bson_iter_init_find (&iter, doc, "_id")) { |
| 355 | + const bson_value_t *value = bson_iter_value (&iter); |
| 356 | + |
| 357 | + bson_uint32_to_string (count, &key, keybuf, sizeof keybuf); |
| 358 | + BSON_APPEND_VALUE (&ar, key, value); |
| 359 | + } |
| 360 | + } |
| 361 | + |
| 362 | + if (mongoc_cursor_error (cursor, error)) { |
| 363 | + goto failure; |
| 364 | + } |
| 365 | + |
| 366 | + bulk_files = mongoc_collection_create_bulk_operation (gridfs->files, false, NULL); |
| 367 | + bulk_chunks = mongoc_collection_create_bulk_operation (gridfs->chunks, false, NULL); |
| 368 | + |
| 369 | + files_q = BCON_NEW ("_id", "{", "$in", BCON_ARRAY (&ar), "}"); |
| 370 | + chunks_q = BCON_NEW ("files_id", "{", "$in", BCON_ARRAY (&ar), "}"); |
| 371 | + |
| 372 | + mongoc_bulk_operation_remove (bulk_files, files_q); |
| 373 | + mongoc_bulk_operation_remove (bulk_chunks, chunks_q); |
| 374 | + |
| 375 | + files_ret = mongoc_bulk_operation_execute (bulk_files, NULL, &files_error); |
| 376 | + chunks_ret = mongoc_bulk_operation_execute (bulk_chunks, NULL, &chunks_error); |
| 377 | + |
| 378 | + if (error) { |
| 379 | + if (!files_ret) { |
| 380 | + memcpy (error, &files_error, sizeof *error); |
| 381 | + } else if (!chunks_ret) { |
| 382 | + memcpy (error, &chunks_error, sizeof *error); |
| 383 | + } |
| 384 | + } |
| 385 | + |
| 386 | + ret = (files_ret && chunks_ret); |
| 387 | + |
| 388 | +failure: |
| 389 | + if (cursor) { |
| 390 | + mongoc_cursor_destroy (cursor); |
| 391 | + } |
| 392 | + if (bulk_files) { |
| 393 | + mongoc_bulk_operation_destroy (bulk_files); |
| 394 | + } |
| 395 | + if (bulk_chunks) { |
| 396 | + mongoc_bulk_operation_destroy (bulk_chunks); |
| 397 | + } |
| 398 | + bson_destroy (&q); |
| 399 | + bson_destroy (&fields); |
| 400 | + bson_destroy (&ar); |
| 401 | + if (files_q) { |
| 402 | + bson_destroy (files_q); |
| 403 | + } |
| 404 | + if (chunks_q) { |
| 405 | + bson_destroy (chunks_q); |
| 406 | + } |
| 407 | + |
| 408 | + return ret; |
| 409 | +} |
0 commit comments