Search in sources :

Example 96 with Document

use of org.bson.Document in project morphia by mongodb.

the class TestDocumentValidation method overwriteValidation.

@Test
public void overwriteValidation() {
    Document validator = Document.parse("{ jelly : { $ne : 'rhubarb' } }");
    MongoDatabase database = addValidation(validator, "validation");
    assertEquals(validator, getValidator());
    Document rhubarb = new Document("jelly", "rhubarb").append("number", 20);
    database.getCollection("validation").insertOne(new Document("jelly", "grape"));
    try {
        database.getCollection("validation").insertOne(rhubarb);
        fail("Document should have failed validation");
    } catch (MongoWriteException e) {
        assertTrue(e.getMessage().contains("Document failed validation"));
    }
    getMorphia().map(DocumentValidation.class);
    getDs().enableDocumentValidation();
    assertEquals(Document.parse(DocumentValidation.class.getAnnotation(Validation.class).value()), getValidator());
    try {
        database.getCollection("validation").insertOne(rhubarb);
    } catch (MongoWriteException e) {
        assertFalse(e.getMessage().contains("Document failed validation"));
    }
    try {
        getDs().save(new DocumentValidation("John", 1, new Date()));
        fail("Document should have failed validation");
    } catch (WriteConcernException e) {
        assertTrue(e.getMessage().contains("Document failed validation"));
    }
}
Also used : DocumentValidation(org.mongodb.morphia.entities.DocumentValidation) Validation(org.mongodb.morphia.annotations.Validation) DocumentValidation(org.mongodb.morphia.entities.DocumentValidation) MongoWriteException(com.mongodb.MongoWriteException) WriteConcernException(com.mongodb.WriteConcernException) Document(org.bson.Document) Date(java.util.Date) MongoDatabase(com.mongodb.client.MongoDatabase) Test(org.junit.Test)

Example 97 with Document

use of org.bson.Document in project morphia by mongodb.

the class TestDocumentValidation method saveToNewCollection.

@Test
public void saveToNewCollection() {
    getMorphia().map(DocumentValidation.class);
    final Document validator = Document.parse("{ number : { $gt : 10 } }");
    String collection = "newdocs";
    addValidation(validator, collection);
    try {
        getAds().save(collection, new DocumentValidation("Harold", 8, new Date()));
        fail("Document validation should have complained");
    } catch (WriteConcernException e) {
    // expected
    }
    getAds().save(collection, new DocumentValidation("Harold", 8, new Date()), new InsertOptions().bypassDocumentValidation(true));
    Query<DocumentValidation> query = getAds().createQuery(collection, DocumentValidation.class).field("number").equal(8);
    Assert.assertNotNull(query.get());
}
Also used : DocumentValidation(org.mongodb.morphia.entities.DocumentValidation) WriteConcernException(com.mongodb.WriteConcernException) Document(org.bson.Document) Date(java.util.Date) Test(org.junit.Test)

Example 98 with Document

use of org.bson.Document in project morphia by mongodb.

the class TestMapreduce method testBypassDocumentValidation.

@Test
public void testBypassDocumentValidation() {
    checkMinServerVersion(3.4);
    getDs().save(asList(new Book("The Banquet", "Dante", 2), new Book("Divine Comedy", "Dante", 1), new Book("Eclogues", "Dante", 2), new Book("The Odyssey", "Homer", 10), new Book("Iliad", "Homer", 10)));
    Document validator = Document.parse("{ count : { $gt : '10' } }");
    ValidationOptions validationOptions = new ValidationOptions().validator(validator).validationLevel(ValidationLevel.STRICT).validationAction(ValidationAction.ERROR);
    MongoDatabase database = getMongoClient().getDatabase(TEST_DB_NAME);
    database.getCollection("counts").drop();
    database.createCollection("counts", new CreateCollectionOptions().validationOptions(validationOptions));
    final String map = "function () { emit(this.author, 1); return; }";
    final String reduce = "function (key, values) { return values.length }";
    MapReduceOptions<CountResult> options = new MapReduceOptions<CountResult>().query(getDs().find(Book.class)).resultType(CountResult.class).outputType(OutputType.REPLACE).map(map).reduce(reduce);
    try {
        getDs().mapReduce(options);
        fail("Document validation should have complained.");
    } catch (MongoCommandException e) {
    // expected
    }
    getDs().mapReduce(options.bypassDocumentValidation(true));
    Assert.assertEquals(2, count(getDs().find(CountResult.class).iterator()));
}
Also used : MongoCommandException(com.mongodb.MongoCommandException) Book(org.mongodb.morphia.aggregation.AggregationTest.Book) CreateCollectionOptions(com.mongodb.client.model.CreateCollectionOptions) Document(org.bson.Document) ValidationOptions(com.mongodb.client.model.ValidationOptions) CountResult(org.mongodb.morphia.aggregation.AggregationTest.CountResult) MongoDatabase(com.mongodb.client.MongoDatabase) Test(org.junit.Test)

Example 99 with Document

use of org.bson.Document in project presto by prestodb.

the class MongoSession method guessFieldType.

private Optional<TypeSignature> guessFieldType(Object value) {
    if (value == null) {
        return Optional.empty();
    }
    TypeSignature typeSignature = null;
    if (value instanceof String) {
        typeSignature = createUnboundedVarcharType().getTypeSignature();
    } else if (value instanceof Integer || value instanceof Long) {
        typeSignature = BIGINT.getTypeSignature();
    } else if (value instanceof Boolean) {
        typeSignature = BOOLEAN.getTypeSignature();
    } else if (value instanceof Float || value instanceof Double) {
        typeSignature = DOUBLE.getTypeSignature();
    } else if (value instanceof Date) {
        typeSignature = TIMESTAMP.getTypeSignature();
    } else if (value instanceof ObjectId) {
        typeSignature = OBJECT_ID.getTypeSignature();
    } else if (value instanceof List) {
        List<Optional<TypeSignature>> subTypes = ((List<?>) value).stream().map(this::guessFieldType).collect(toList());
        if (subTypes.isEmpty() || subTypes.stream().anyMatch(t -> !t.isPresent())) {
            return Optional.empty();
        }
        Set<TypeSignature> signatures = subTypes.stream().map(t -> t.get()).collect(toSet());
        if (signatures.size() == 1) {
            typeSignature = new TypeSignature(StandardTypes.ARRAY, signatures.stream().map(s -> TypeSignatureParameter.of(s)).collect(Collectors.toList()));
        } else {
            // TODO: presto cli doesn't handle empty field name row type yet
            typeSignature = new TypeSignature(StandardTypes.ROW, IntStream.range(0, subTypes.size()).mapToObj(idx -> TypeSignatureParameter.of(new NamedTypeSignature(String.format("%s%d", implicitPrefix, idx + 1), subTypes.get(idx).get()))).collect(toList()));
        }
    } else if (value instanceof Document) {
        List<TypeSignatureParameter> parameters = new ArrayList<>();
        for (String key : ((Document) value).keySet()) {
            Optional<TypeSignature> fieldType = guessFieldType(((Document) value).get(key));
            if (!fieldType.isPresent()) {
                return Optional.empty();
            }
            parameters.add(TypeSignatureParameter.of(new NamedTypeSignature(key, fieldType.get())));
        }
        typeSignature = new TypeSignature(StandardTypes.ROW, parameters);
    }
    return Optional.ofNullable(typeSignature);
}
Also used : Document(org.bson.Document) Arrays(java.util.Arrays) LoadingCache(com.google.common.cache.LoadingCache) TypeManager(com.facebook.presto.spi.type.TypeManager) Date(java.util.Date) MongoDatabase(com.mongodb.client.MongoDatabase) BIGINT(com.facebook.presto.spi.type.BigintType.BIGINT) SchemaTableName(com.facebook.presto.spi.SchemaTableName) BOOLEAN(com.facebook.presto.spi.type.BooleanType.BOOLEAN) SchemaNotFoundException(com.facebook.presto.spi.SchemaNotFoundException) Map(java.util.Map) TypeSignatureParameter(com.facebook.presto.spi.type.TypeSignatureParameter) StandardTypes(com.facebook.presto.spi.type.StandardTypes) Collectors.toSet(java.util.stream.Collectors.toSet) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) Collectors(java.util.stream.Collectors) Preconditions.checkState(com.google.common.base.Preconditions.checkState) CacheLoader(com.google.common.cache.CacheLoader) TupleDomain(com.facebook.presto.spi.predicate.TupleDomain) Domain(com.facebook.presto.spi.predicate.Domain) List(java.util.List) FindIterable(com.mongodb.client.FindIterable) Optional(java.util.Optional) CacheBuilder(com.google.common.cache.CacheBuilder) TypeSignature(com.facebook.presto.spi.type.TypeSignature) IntStream(java.util.stream.IntStream) DOUBLE(com.facebook.presto.spi.type.DoubleType.DOUBLE) MongoCollection(com.mongodb.client.MongoCollection) Slice(io.airlift.slice.Slice) Logger(io.airlift.log.Logger) OBJECT_ID(com.facebook.presto.mongodb.ObjectIdType.OBJECT_ID) MINUTES(java.util.concurrent.TimeUnit.MINUTES) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ImmutableList(com.google.common.collect.ImmutableList) MongoCursor(com.mongodb.client.MongoCursor) Verify.verify(com.google.common.base.Verify.verify) Type(com.facebook.presto.spi.type.Type) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) NamedTypeSignature(com.facebook.presto.spi.type.NamedTypeSignature) Objects.requireNonNull(java.util.Objects.requireNonNull) TIMESTAMP(com.facebook.presto.spi.type.TimestampType.TIMESTAMP) Throwables(com.google.common.base.Throwables) Range(com.facebook.presto.spi.predicate.Range) IndexOptions(com.mongodb.client.model.IndexOptions) ExecutionException(java.util.concurrent.ExecutionException) VarcharType.createUnboundedVarcharType(com.facebook.presto.spi.type.VarcharType.createUnboundedVarcharType) Collectors.toList(java.util.stream.Collectors.toList) TableNotFoundException(com.facebook.presto.spi.TableNotFoundException) ColumnHandle(com.facebook.presto.spi.ColumnHandle) MongoClient(com.mongodb.MongoClient) DeleteResult(com.mongodb.client.result.DeleteResult) ObjectId(org.bson.types.ObjectId) HOURS(java.util.concurrent.TimeUnit.HOURS) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Optional(java.util.Optional) ObjectId(org.bson.types.ObjectId) NamedTypeSignature(com.facebook.presto.spi.type.NamedTypeSignature) Document(org.bson.Document) Date(java.util.Date) TypeSignature(com.facebook.presto.spi.type.TypeSignature) NamedTypeSignature(com.facebook.presto.spi.type.NamedTypeSignature) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) Collectors.toList(java.util.stream.Collectors.toList)

Example 100 with Document

use of org.bson.Document in project presto by prestodb.

the class MongoSession method execute.

public MongoCursor<Document> execute(MongoSplit split, List<MongoColumnHandle> columns) {
    Document output = new Document();
    for (MongoColumnHandle column : columns) {
        output.append(column.getName(), 1);
    }
    MongoCollection<Document> collection = getCollection(split.getSchemaTableName());
    FindIterable<Document> iterable = collection.find(buildQuery(split.getTupleDomain())).projection(output);
    if (cursorBatchSize != 0) {
        iterable.batchSize(cursorBatchSize);
    }
    return iterable.iterator();
}
Also used : Document(org.bson.Document)

Aggregations

Document (org.bson.Document)310 Test (org.junit.Test)193 ArrayList (java.util.ArrayList)39 BsonDocument (org.bson.BsonDocument)36 MongoDatabase (com.mongodb.client.MongoDatabase)26 ObjectId (org.bson.types.ObjectId)22 List (java.util.List)21 HashMap (java.util.HashMap)19 Bson (org.bson.conversions.Bson)19 MongoClient (com.mongodb.MongoClient)17 Exchange (org.apache.camel.Exchange)14 IndexOptions (com.mongodb.client.model.IndexOptions)12 ReturnDocument (com.mongodb.client.model.ReturnDocument)12 CreateCollectionOptions (com.mongodb.client.model.CreateCollectionOptions)11 Date (java.util.Date)11 BasicDBObject (com.mongodb.BasicDBObject)10 UpdateResult (com.mongodb.client.result.UpdateResult)10 DBObject (com.mongodb.DBObject)9 UpdateOptions (com.mongodb.client.model.UpdateOptions)9 Map (java.util.Map)9