Search in sources :

Example 6 with MongoGridFSException

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;
}
Also used : GridFSFile(com.mongodb.client.gridfs.model.GridFSFile) MongoGridFSException(com.mongodb.MongoGridFSException) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument)

Example 7 with MongoGridFSException

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);
            }
        }
    }
}
Also used : BsonDocument(org.bson.BsonDocument) BsonObjectId(org.bson.BsonObjectId) ObjectId(org.bson.types.ObjectId) BsonString(org.bson.BsonString) MongoGridFSException(com.mongodb.MongoGridFSException) BsonArray(org.bson.BsonArray) BsonString(org.bson.BsonString) BsonObjectId(org.bson.BsonObjectId) BsonValue(org.bson.BsonValue)

Example 8 with MongoGridFSException

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);
            }
        }
    }
}
Also used : BsonObjectId(org.bson.BsonObjectId) ObjectId(org.bson.types.ObjectId) MongoGridFSException(com.mongodb.MongoGridFSException) BsonString(org.bson.BsonString) BsonObjectId(org.bson.BsonObjectId) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) BsonValue(org.bson.BsonValue)

Example 9 with MongoGridFSException

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);
                        }
                    }
                });
            }
        }
    });
}
Also used : BsonDocument(org.bson.BsonDocument) MongoGridFSException(com.mongodb.MongoGridFSException) SingleResultCallback(com.mongodb.async.SingleResultCallback) DeleteResult(com.mongodb.client.result.DeleteResult)

Example 10 with MongoGridFSException

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);
            }
        }
    });
}
Also used : GridFSFile(com.mongodb.client.gridfs.model.GridFSFile) MongoGridFSException(com.mongodb.MongoGridFSException)

Aggregations

MongoGridFSException (com.mongodb.MongoGridFSException)13 BsonDocument (org.bson.BsonDocument)6 SingleResultCallback (com.mongodb.async.SingleResultCallback)3 IOException (java.io.IOException)3 BsonString (org.bson.BsonString)3 GridFSFile (com.mongodb.client.gridfs.model.GridFSFile)2 DeleteResult (com.mongodb.client.result.DeleteResult)2 BsonArray (org.bson.BsonArray)2 BsonObjectId (org.bson.BsonObjectId)2 BsonValue (org.bson.BsonValue)2 Document (org.bson.Document)2 Binary (org.bson.types.Binary)2 ObjectId (org.bson.types.ObjectId)2 UpdateResult (com.mongodb.client.result.UpdateResult)1