Search in sources :

Example 11 with BSONException

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

the class AbstractExplicitUuidCodecUuidRepresentationTest method shouldDecodeDBObjectWithUuidRepresentation.

@Test
public void shouldDecodeDBObjectWithUuidRepresentation() {
    bsonDocumentCollection.insertOne(new BsonDocument("standard", new BsonBinary(uuid, UuidRepresentation.STANDARD)).append("legacy", new BsonBinary(uuid, uuidRepresentationForExplicitEncoding)));
    DBObject document;
    try {
        document = dbObjectCollection.find().first();
        assertNotNull(document);
    } catch (BSONException e) {
        if (uuidCodec.getUuidRepresentation() != STANDARD) {
            throw e;
        }
        return;
    }
    if (uuidRepresentationForClient == UuidRepresentation.STANDARD) {
        assertEquals(UUID.class, document.get("standard").getClass());
        assertEquals(uuid, document.get("standard"));
        assertEquals(Binary.class, document.get("legacy").getClass());
        assertEquals(new Binary(BsonBinarySubType.UUID_LEGACY, encodedValue), document.get("legacy"));
    } else {
        if (uuidRepresentationForClient == UuidRepresentation.JAVA_LEGACY) {
            assertEquals(UUID.class, document.get("standard").getClass());
            assertEquals(uuid, document.get("standard"));
        } else {
            assertEquals(Binary.class, document.get("standard").getClass());
            assertEquals(new Binary(BsonBinarySubType.UUID_STANDARD, standardEncodedValue), document.get("standard"));
        }
        assertEquals(UUID.class, document.get("legacy").getClass());
        assertEquals(uuid, document.get("legacy"));
    }
}
Also used : BsonDocument(org.bson.BsonDocument) BsonBinary(org.bson.BsonBinary) BSONException(org.bson.BSONException) Binary(org.bson.types.Binary) BsonBinary(org.bson.BsonBinary) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Test(org.junit.Test)

Example 12 with BSONException

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

the class AbstractUuidRepresentationTest method shouldDecodeDbObjectWithUuidRepresentation.

@Test
public void shouldDecodeDbObjectWithUuidRepresentation() {
    bsonDocumentCollection.insertOne(new BsonDocument("standard", new BsonBinary(uuid, UuidRepresentation.STANDARD)).append("legacy", new BsonBinary(uuid, uuidRepresentation == UuidRepresentation.UNSPECIFIED || uuidRepresentation == UuidRepresentation.STANDARD ? UuidRepresentation.PYTHON_LEGACY : uuidRepresentation)));
    DBObject document;
    try {
        document = dbObjectCollection.find().first();
        assertNotNull(document);
    } catch (BSONException e) {
        if (uuidRepresentation != STANDARD && uuidRepresentation != JAVA_LEGACY) {
            throw e;
        }
        return;
    }
    if (uuidRepresentation == UuidRepresentation.UNSPECIFIED) {
        assertEquals(Binary.class, document.get("standard").getClass());
        assertEquals(new Binary(BsonBinarySubType.UUID_STANDARD, standardEncodedValue), document.get("standard"));
        assertEquals(Binary.class, document.get("legacy").getClass());
        assertEquals(new Binary(BsonBinarySubType.UUID_LEGACY, standardEncodedValue), document.get("legacy"));
    } else if (uuidRepresentation == UuidRepresentation.STANDARD) {
        assertEquals(UUID.class, document.get("standard").getClass());
        assertEquals(uuid, document.get("standard"));
        assertEquals(Binary.class, document.get("legacy").getClass());
        assertEquals(new Binary(BsonBinarySubType.UUID_LEGACY, standardEncodedValue), document.get("legacy"));
    } else {
        assertEquals(Binary.class, document.get("standard").getClass());
        assertEquals(new Binary(BsonBinarySubType.UUID_STANDARD, standardEncodedValue), document.get("standard"));
        assertEquals(UUID.class, document.get("legacy").getClass());
        assertEquals(uuid, document.get("legacy"));
    }
}
Also used : BsonDocument(org.bson.BsonDocument) BsonBinary(org.bson.BsonBinary) BSONException(org.bson.BSONException) Binary(org.bson.types.Binary) BsonBinary(org.bson.BsonBinary) UUID(java.util.UUID) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Test(org.junit.Test)

Example 13 with BSONException

use of org.bson.BSONException in project pulsar by yahoo.

the class MongoSink method flush.

private void flush() {
    final List<Document> docsToInsert = new ArrayList<>();
    final List<Record<byte[]>> recordsToInsert;
    synchronized (this) {
        if (incomingList.isEmpty()) {
            return;
        }
        recordsToInsert = incomingList;
        incomingList = Lists.newArrayList();
    }
    final Iterator<Record<byte[]>> iter = recordsToInsert.iterator();
    while (iter.hasNext()) {
        final Record<byte[]> record = iter.next();
        try {
            final byte[] docAsBytes = record.getValue();
            final Document doc = Document.parse(new String(docAsBytes, StandardCharsets.UTF_8));
            docsToInsert.add(doc);
        } catch (JsonParseException | BSONException e) {
            log.error("Bad message", e);
            record.fail();
            iter.remove();
        }
    }
    if (docsToInsert.size() > 0) {
        collection.insertMany(docsToInsert).subscribe(new DocsToInsertSubscriber(docsToInsert, recordsToInsert));
    }
}
Also used : ArrayList(java.util.ArrayList) BSONException(org.bson.BSONException) Document(org.bson.Document) JsonParseException(org.bson.json.JsonParseException) Record(org.apache.pulsar.functions.api.Record)

Example 14 with BSONException

use of org.bson.BSONException in project pulsar by apache.

the class MongoSink method flush.

private void flush() {
    final List<Document> docsToInsert = new ArrayList<>();
    final List<Record<byte[]>> recordsToInsert;
    synchronized (this) {
        if (incomingList.isEmpty()) {
            return;
        }
        recordsToInsert = incomingList;
        incomingList = Lists.newArrayList();
    }
    final Iterator<Record<byte[]>> iter = recordsToInsert.iterator();
    while (iter.hasNext()) {
        final Record<byte[]> record = iter.next();
        try {
            final byte[] docAsBytes = record.getValue();
            final Document doc = Document.parse(new String(docAsBytes, StandardCharsets.UTF_8));
            docsToInsert.add(doc);
        } catch (JsonParseException | BSONException e) {
            log.error("Bad message", e);
            record.fail();
            iter.remove();
        }
    }
    if (docsToInsert.size() > 0) {
        collection.insertMany(docsToInsert).subscribe(new DocsToInsertSubscriber(docsToInsert, recordsToInsert));
    }
}
Also used : ArrayList(java.util.ArrayList) BSONException(org.bson.BSONException) Document(org.bson.Document) JsonParseException(org.bson.json.JsonParseException) Record(org.apache.pulsar.functions.api.Record)

Aggregations

BSONException (org.bson.BSONException)14 BsonBinary (org.bson.BsonBinary)9 BsonDocument (org.bson.BsonDocument)8 Test (org.junit.Test)8 Document (org.bson.Document)6 JsonParseException (org.bson.json.JsonParseException)5 Binary (org.bson.types.Binary)4 ArrayList (java.util.ArrayList)3 Record (org.apache.pulsar.functions.api.Record)3 BasicDBObject (com.mongodb.BasicDBObject)2 DBObject (com.mongodb.DBObject)2 MongoSKDocument (fr.romitou.mongosk.elements.MongoSKDocument)2 UUID (java.util.UUID)2 SubscriberHelpers (fr.romitou.mongosk.SubscriberHelpers)1 MongoSKDatabase (fr.romitou.mongosk.elements.MongoSKDatabase)1