diff --git a/driver/src/main/com/mongodb/gridfs/GridFS.java b/driver/src/main/com/mongodb/gridfs/GridFS.java index e4fa2748205..123eb90d4f7 100644 --- a/driver/src/main/com/mongodb/gridfs/GridFS.java +++ b/driver/src/main/com/mongodb/gridfs/GridFS.java @@ -303,12 +303,32 @@ public void remove(final String filename) { * @throws com.mongodb.MongoException if the operation fails */ public void remove(final DBObject query) { + remove(query, true); + } + + /** + * Removes all files matching the given query. + * + * @param query filter to apply + * @param fileByFile if true : for each file remove the file and it's chunks + * if false select all files remove them all, then remove all chunks + * @throws com.mongodb.MongoException if the operation fails + */ + public void remove(final DBObject query, final boolean fileByFile) { if (query == null) { throw new IllegalArgumentException("query can not be null"); } - - for (final GridFSDBFile f : find(query)) { - f.remove(); + if (fileByFile){ + for (final GridFSDBFile f : find(query)) { + f.remove(); + } + } else { + List filesIds = new ArrayList(); + for (GridFSDBFile f : find(query)) { + filesIds.add((ObjectId) f.getId()); + } + getFilesCollection().remove(query); + getChunksCollection().remove(new BasicDBObject("files_id", new BasicDBObject("$in", filesIds))); } } diff --git a/driver/src/test/functional/com/mongodb/gridfs/GridFSTest.java b/driver/src/test/functional/com/mongodb/gridfs/GridFSTest.java index 1e023c0f8a1..f5bede855cc 100644 --- a/driver/src/test/functional/com/mongodb/gridfs/GridFSTest.java +++ b/driver/src/test/functional/com/mongodb/gridfs/GridFSTest.java @@ -275,6 +275,43 @@ public void testCustomFileID() throws IOException { gridFS.remove(new BasicDBObject("_id", id)); } + @Test + public void testRemove() throws Exception { + int target = GridFS.DEFAULT_CHUNKSIZE * 3; + StringBuilder buf = new StringBuilder(target); + while (buf.length() < target) { + buf.append("asdasdkjasldkjasldjlasjdlajsdljasldjlasjdlkasjdlaskjdlaskjdlsakjdlaskjdasldjsad"); + } + String s = buf.toString(); + + int nbFiles = 100; + for (int idx = 0; idx < nbFiles; ++idx) { + GridFSInputFile in = gridFS.createFile(s.getBytes(defaultCharset())); + in.save(); + } + long start = System.currentTimeMillis(); + gridFS.remove(new BasicDBObject()); + //System.out.println("Legacy remove took : " + (System.currentTimeMillis() - start) + " ms"); + } + + @Test + public void testBulkRemove() throws Exception { + int target = GridFS.DEFAULT_CHUNKSIZE * 3; + StringBuilder buf = new StringBuilder(target); + while (buf.length() < target) { + buf.append("asdasdkjasldkjasldjlasjdlajsdljasldjlasjdlkasjdlaskjdlaskjdlsakjdlaskjdasldjsad"); + } + String s = buf.toString(); + int nbFiles = 100; + for (int idx = 0; idx < nbFiles; ++idx) { + GridFSInputFile in = gridFS.createFile(s.getBytes(defaultCharset())); + in.save(); + } + long start = System.currentTimeMillis(); + gridFS.remove(new BasicDBObject(), false); + //System.out.println("Bulk remove took : " + (System.currentTimeMillis() - start) + " ms"); + } + void testInOut(final String s) throws Exception { int[] start = getCurrentCollectionCounts();