Search in sources :

Example 1 with GridFSBucket

use of com.mongodb.client.gridfs.GridFSBucket in project georocket by georocket.

the class MongoDBChunkReadStreamTest method getChunkSize.

/**
 * Connect to MongoDB and get the GridFS chunk size
 * @param vertx the Vert.x instance
 * @param handler a handler that will be called with the chunk size
 */
private void getChunkSize(Vertx vertx, Handler<AsyncResult<Integer>> handler) {
    vertx.<Integer>executeBlocking(f -> {
        try (MongoClient client = new MongoClient(mongoConnector.serverAddress)) {
            MongoDatabase db = client.getDatabase(MongoDBTestConnector.MONGODB_DBNAME);
            GridFSBucket gridFS = GridFSBuckets.create(db);
            f.complete(gridFS.getChunkSizeBytes());
        }
    }, handler);
}
Also used : GridFSBucket(com.mongodb.client.gridfs.GridFSBucket) MongoClient(com.mongodb.MongoClient) MongoDatabase(com.mongodb.client.MongoDatabase)

Example 2 with GridFSBucket

use of com.mongodb.client.gridfs.GridFSBucket in project georocket by georocket.

the class MongoDBChunkReadStreamTest method prepareData.

/**
 * Create a file in GridFS with the given filename and write
 * some random data to it.
 * @param filename the name of the file to create
 * @param size the number of random bytes to write
 * @param vertx the Vert.x instance
 * @param handler a handler that will be called when the file
 * has been written
 */
private void prepareData(String filename, int size, Vertx vertx, Handler<AsyncResult<String>> handler) {
    vertx.<String>executeBlocking(f -> {
        try (MongoClient client = new MongoClient(mongoConnector.serverAddress)) {
            MongoDatabase db = client.getDatabase(MongoDBTestConnector.MONGODB_DBNAME);
            GridFSBucket gridFS = GridFSBuckets.create(db);
            try (GridFSUploadStream os = gridFS.openUploadStream(filename)) {
                for (int i = 0; i < size; ++i) {
                    os.write((byte) (i & 0xFF));
                }
            }
        }
        f.complete(filename);
    }, handler);
}
Also used : GridFSBucket(com.mongodb.client.gridfs.GridFSBucket) MongoClient(com.mongodb.MongoClient) GridFSUploadStream(com.mongodb.client.gridfs.GridFSUploadStream) MongoDatabase(com.mongodb.client.MongoDatabase)

Example 3 with GridFSBucket

use of com.mongodb.client.gridfs.GridFSBucket in project georocket by georocket.

the class MongoDBStoreTest method validateAfterStoreDelete.

@Override
protected void validateAfterStoreDelete(TestContext context, Vertx vertx, String path, Handler<AsyncResult<Void>> handler) {
    vertx.executeBlocking(f -> {
        try (MongoClient client = new MongoClient(mongoConnector.serverAddress)) {
            MongoDatabase db = client.getDatabase(MongoDBTestConnector.MONGODB_DBNAME);
            GridFSBucket gridFS = GridFSBuckets.create(db);
            GridFSFindIterable files = gridFS.find();
            context.assertTrue(Iterables.isEmpty(files));
        }
        f.complete();
    }, handler);
}
Also used : GridFSBucket(com.mongodb.client.gridfs.GridFSBucket) MongoClient(com.mongodb.MongoClient) GridFSFindIterable(com.mongodb.client.gridfs.GridFSFindIterable) MongoDatabase(com.mongodb.client.MongoDatabase)

Example 4 with GridFSBucket

use of com.mongodb.client.gridfs.GridFSBucket in project mongo-java-driver by mongodb.

the class UnifiedGridFSHelper method executeDelete.

OperationResult executeDelete(final BsonDocument operation) {
    GridFSBucket bucket = entities.getBucket(operation.getString("object").getValue());
    BsonDocument arguments = operation.getDocument("arguments");
    BsonValue id = arguments.get("id");
    if (arguments.size() > 1) {
        throw new UnsupportedOperationException("Unexpected arguments");
    }
    requireNonNull(id);
    try {
        bucket.delete(id);
        return OperationResult.NONE;
    } catch (Exception e) {
        return OperationResult.of(e);
    }
}
Also used : GridFSBucket(com.mongodb.client.gridfs.GridFSBucket) BsonDocument(org.bson.BsonDocument) BsonValue(org.bson.BsonValue)

Example 5 with GridFSBucket

use of com.mongodb.client.gridfs.GridFSBucket in project mongo-java-driver by mongodb.

the class UnifiedGridFSHelper method executeDownload.

public OperationResult executeDownload(final BsonDocument operation) {
    GridFSBucket bucket = entities.getBucket(operation.getString("object").getValue());
    BsonDocument arguments = operation.getDocument("arguments");
    BsonValue id = arguments.get("id");
    if (arguments.size() > 1) {
        throw new UnsupportedOperationException("Unexpected arguments");
    }
    requireNonNull(id);
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bucket.downloadToStream(id, baos);
        return OperationResult.of(new BsonString(HexUtils.toHex(baos.toByteArray())));
    } catch (Exception e) {
        return OperationResult.of(e);
    }
}
Also used : GridFSBucket(com.mongodb.client.gridfs.GridFSBucket) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BsonValue(org.bson.BsonValue)

Aggregations

GridFSBucket (com.mongodb.client.gridfs.GridFSBucket)14 MongoDatabase (com.mongodb.client.MongoDatabase)9 MongoClient (com.mongodb.MongoClient)8 ByteArrayInputStream (java.io.ByteArrayInputStream)5 BsonDocument (org.bson.BsonDocument)5 GridFSUploadOptions (com.mongodb.client.gridfs.model.GridFSUploadOptions)4 BsonString (org.bson.BsonString)4 MongoException (com.mongodb.MongoException)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 BsonValue (org.bson.BsonValue)3 ObjectId (org.bson.types.ObjectId)3 GridFSFindIterable (com.mongodb.client.gridfs.GridFSFindIterable)2 GridFSUploadStream (com.mongodb.client.gridfs.GridFSUploadStream)2 GridFSDownloadOptions (com.mongodb.client.gridfs.model.GridFSDownloadOptions)2 GridFSFile (com.mongodb.client.gridfs.model.GridFSFile)2 InputStream (java.io.InputStream)2 BsonObjectId (org.bson.BsonObjectId)2 Document (org.bson.Document)2 MongoClient (com.mongodb.client.MongoClient)1 GridFSDownloadStream (com.mongodb.client.gridfs.GridFSDownloadStream)1