Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions driver/src/main/com/mongodb/gridfs/GridFS.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<ObjectId> filesIds = new ArrayList<ObjectId>();
for (GridFSDBFile f : find(query)) {
filesIds.add((ObjectId) f.getId());
}
getFilesCollection().remove(query);
getChunksCollection().remove(new BasicDBObject("files_id", new BasicDBObject("$in", filesIds)));
}
}

Expand Down
37 changes: 37 additions & 0 deletions driver/src/test/functional/com/mongodb/gridfs/GridFSTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down