Search in sources :

Example 1 with BsonObjectId

use of org.bson.BsonObjectId in project mongo-java-driver by mongodb.

the class GridFSBucketImpl method uploadFromStream.

@Override
public void uploadFromStream(final String filename, final AsyncInputStream source, final GridFSUploadOptions options, final SingleResultCallback<ObjectId> callback) {
    final BsonObjectId id = new BsonObjectId();
    uploadFromStream(id, filename, source, options, new SingleResultCallback<Void>() {

        @Override
        public void onResult(final Void result, final Throwable t) {
            if (t != null) {
                callback.onResult(null, t);
            } else {
                callback.onResult(id.getValue(), null);
            }
        }
    });
}
Also used : BsonObjectId(org.bson.BsonObjectId)

Example 2 with BsonObjectId

use of org.bson.BsonObjectId 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 3 with BsonObjectId

use of org.bson.BsonObjectId in project core-ng-project by neowu.

the class EntityCodecTest method getDocumentId.

@Test
void getDocumentId() {
    TestEntity entity = new TestEntity();
    entity.id = new ObjectId();
    assertEquals(new BsonObjectId(entity.id), entityCodec.getDocumentId(entity));
}
Also used : BsonObjectId(org.bson.BsonObjectId) ObjectId(org.bson.types.ObjectId) BsonObjectId(org.bson.BsonObjectId) Test(org.junit.jupiter.api.Test)

Example 4 with BsonObjectId

use of org.bson.BsonObjectId in project drill by axbaretto.

the class TestBsonRecordReader method testObjectIdType.

@Test
public void testObjectIdType() throws IOException {
    BsonDocument bsonDoc = new BsonDocument();
    BsonObjectId value = new BsonObjectId(new ObjectId());
    bsonDoc.append("_idKey", value);
    writer.reset();
    bsonReader.write(writer, new BsonDocumentReader(bsonDoc));
    SingleMapReaderImpl mapReader = (SingleMapReaderImpl) writer.getMapVector().getReader();
    byte[] readByteArray = mapReader.reader("_idKey").readByteArray();
    assertTrue(Arrays.equals(value.getValue().toByteArray(), readByteArray));
}
Also used : BsonDocument(org.bson.BsonDocument) SingleMapReaderImpl(org.apache.drill.exec.vector.complex.impl.SingleMapReaderImpl) BsonObjectId(org.bson.BsonObjectId) ObjectId(org.bson.types.ObjectId) BsonDocumentReader(org.bson.BsonDocumentReader) BsonObjectId(org.bson.BsonObjectId) Test(org.junit.Test)

Example 5 with BsonObjectId

use of org.bson.BsonObjectId in project drill by apache.

the class TestBsonRecordReader method testObjectIdType.

@Test
public void testObjectIdType() throws IOException {
    BsonDocument bsonDoc = new BsonDocument();
    BsonObjectId value = new BsonObjectId(new ObjectId());
    bsonDoc.append("_idKey", value);
    writer.reset();
    bsonReader.write(writer, new BsonDocumentReader(bsonDoc));
    SingleMapReaderImpl mapReader = (SingleMapReaderImpl) writer.getMapVector().getReader();
    byte[] readByteArray = mapReader.reader("_idKey").readByteArray();
    assertArrayEquals(value.getValue().toByteArray(), readByteArray);
}
Also used : BsonDocument(org.bson.BsonDocument) SingleMapReaderImpl(org.apache.drill.exec.vector.complex.impl.SingleMapReaderImpl) BsonObjectId(org.bson.BsonObjectId) ObjectId(org.bson.types.ObjectId) BsonDocumentReader(org.bson.BsonDocumentReader) BsonObjectId(org.bson.BsonObjectId) BaseTest(org.apache.drill.test.BaseTest) Test(org.junit.Test)

Aggregations

BsonObjectId (org.bson.BsonObjectId)39 ObjectId (org.bson.types.ObjectId)30 Test (org.junit.Test)18 GridFSFile (com.mongodb.client.gridfs.model.GridFSFile)14 BsonString (org.bson.BsonString)13 ArrayList (java.util.ArrayList)11 Document (org.bson.Document)11 BsonDocument (org.bson.BsonDocument)10 GridFSFindIterable (com.mongodb.client.gridfs.GridFSFindIterable)8 ByteArrayInputStream (java.io.ByteArrayInputStream)8 Test (org.junit.jupiter.api.Test)8 List (java.util.List)7 BsonValue (org.bson.BsonValue)7 MongoGridFSException (com.mongodb.MongoGridFSException)6 Date (java.util.Date)6 Map (java.util.Map)6 BsonArray (org.bson.BsonArray)6 IOException (java.io.IOException)5 Assertions (org.assertj.core.api.Assertions)5 Before (org.junit.Before)5