use of com.mongodb.MongoGridFSException in project mongo-java-driver by mongodb.
the class GridFSBucketImpl method getFileByName.
private GridFSFile getFileByName(final String filename, final GridFSDownloadOptions options) {
int revision = options.getRevision();
int skip;
int sort;
if (revision >= 0) {
skip = revision;
sort = 1;
} else {
skip = (-revision) - 1;
sort = -1;
}
GridFSFile fileInfo = find(new Document("filename", filename)).skip(skip).sort(new Document("uploadDate", sort)).first();
if (fileInfo == null) {
throw new MongoGridFSException(format("No file found with the filename: %s and revision: %s", filename, revision));
}
return fileInfo;
}
use of com.mongodb.MongoGridFSException in project mongo-java-driver by mongodb.
the class GridFSTest method doDelete.
private void doDelete(final BsonDocument arguments, final BsonDocument assertion) {
Throwable error = null;
try {
gridFSBucket.delete(arguments.getObjectId("id").getValue());
} catch (MongoGridFSException e) {
error = e;
}
if (assertion.containsKey("error")) {
assertNotNull("Should have thrown an exception", error);
} else {
assertNull("Should not have thrown an exception", error);
for (BsonValue rawDataItem : assertion.getArray("data")) {
BsonDocument dataItem = rawDataItem.asDocument();
for (BsonValue deletedItem : dataItem.getArray("deletes", new BsonArray())) {
String delete = dataItem.getString("delete", new BsonString("none")).getValue();
BsonObjectId id = new BsonObjectId(new ObjectId());
if (delete.equals("expected.files")) {
id = deletedItem.asDocument().getDocument("q").getObjectId("_id");
} else if (delete.equals("expected.chunks")) {
id = deletedItem.asDocument().getDocument("q").getObjectId("files_id");
}
assertEquals(filesCollection.count(new BsonDocument("_id", id)), 0);
assertEquals(chunksCollection.count(new BsonDocument("files_id", id)), 0);
}
}
}
}
use of com.mongodb.MongoGridFSException in project mongo-java-driver by mongodb.
the class GridFSTest method doDelete.
private void doDelete(final BsonDocument arguments, final BsonDocument assertion) {
Throwable error = null;
try {
new MongoOperation<Void>() {
@Override
public void execute() {
gridFSBucket.delete(arguments.getObjectId("id").getValue(), getCallback());
}
}.get();
} catch (MongoGridFSException e) {
error = e;
}
if (assertion.containsKey("error")) {
assertNotNull("Should have thrown an exception", error);
} else {
assertNull("Should not have thrown an exception", error);
for (BsonValue rawDataItem : assertion.getArray("data")) {
BsonDocument dataItem = rawDataItem.asDocument();
for (BsonValue deletedItem : dataItem.getArray("deletes", new BsonArray())) {
String delete = dataItem.getString("delete", new BsonString("none")).getValue();
BsonObjectId id = new BsonObjectId(new ObjectId());
if (delete.equals("expected.files")) {
id = deletedItem.asDocument().getDocument("q").getObjectId("_id");
} else if (delete.equals("expected.chunks")) {
id = deletedItem.asDocument().getDocument("q").getObjectId("files_id");
}
long filesCount = getFilesCount(new BsonDocument("_id", id));
long chunksCount = getChunksCount(new BsonDocument("files_id", id));
assertEquals(filesCount, 0);
assertEquals(chunksCount, 0);
}
}
}
}
use of com.mongodb.MongoGridFSException in project mongo-java-driver by mongodb.
the class GridFSBucketImpl method delete.
@Override
public void delete(final BsonValue id, final SingleResultCallback<Void> callback) {
notNull("id", id);
notNull("callback", callback);
final SingleResultCallback<Void> errHandlingCallback = errorHandlingCallback(callback, LOGGER);
filesCollection.deleteOne(new BsonDocument("_id", id), new SingleResultCallback<DeleteResult>() {
@Override
public void onResult(final DeleteResult filesResult, final Throwable t) {
if (t != null) {
errHandlingCallback.onResult(null, t);
} else {
chunksCollection.deleteMany(new BsonDocument("files_id", id), new SingleResultCallback<DeleteResult>() {
@Override
public void onResult(final DeleteResult chunksResult, final Throwable t) {
if (t != null) {
errHandlingCallback.onResult(null, t);
} else if (filesResult.wasAcknowledged() && filesResult.getDeletedCount() == 0) {
errHandlingCallback.onResult(null, new MongoGridFSException(format("No file found with the ObjectId: %s", id)));
} else {
errHandlingCallback.onResult(null, null);
}
}
});
}
}
});
}
use of com.mongodb.MongoGridFSException in project mongo-java-driver by mongodb.
the class GridFSDownloadStreamImpl method getGridFSFile.
@Override
public void getGridFSFile(final SingleResultCallback<GridFSFile> callback) {
notNull("callback", callback);
final SingleResultCallback<GridFSFile> errHandlingCallback = errorHandlingCallback(callback, LOGGER);
if (hasFileInfo()) {
errHandlingCallback.onResult(fileInfo, null);
return;
}
if (!tryGetReadingLock(errHandlingCallback)) {
return;
}
fileInfoIterable.first(new SingleResultCallback<GridFSFile>() {
@Override
public void onResult(final GridFSFile result, final Throwable t) {
releaseReadingLock();
if (t != null) {
errHandlingCallback.onResult(null, t);
} else if (result == null) {
errHandlingCallback.onResult(null, new MongoGridFSException("File not found"));
} else {
fileInfo = result;
numberOfChunks = (int) Math.ceil((double) fileInfo.getLength() / fileInfo.getChunkSize());
errHandlingCallback.onResult(result, null);
}
}
});
}
Aggregations