Search in sources :

Example 1 with Nullable

use of com.mongodb.lang.Nullable in project mongo-java-driver by mongodb.

the class ChangeStreamBatchCursor method convertAndProduceLastId.

/**
 * @param lastIdConsumer Is {@linkplain Consumer#accept(Object) called} iff {@code rawDocuments} is successfully converted
 *                       and the returned {@link List} is neither {@code null} nor {@linkplain List#isEmpty() empty}.
 */
@Nullable
static <T> List<T> convertAndProduceLastId(@Nullable final List<RawBsonDocument> rawDocuments, final Decoder<T> decoder, final Consumer<BsonDocument> lastIdConsumer) {
    List<T> results = null;
    if (rawDocuments != null) {
        results = new ArrayList<T>();
        for (RawBsonDocument rawDocument : rawDocuments) {
            if (!rawDocument.containsKey("_id")) {
                throw new MongoChangeStreamException("Cannot provide resume functionality when the resume token is missing.");
            }
            results.add(rawDocument.decode(decoder));
        }
        lastIdConsumer.accept(rawDocuments.get(rawDocuments.size() - 1).getDocument("_id"));
    }
    return results;
}
Also used : RawBsonDocument(org.bson.RawBsonDocument) MongoChangeStreamException(com.mongodb.MongoChangeStreamException) Nullable(com.mongodb.lang.Nullable)

Example 2 with Nullable

use of com.mongodb.lang.Nullable in project mongo-java-driver by mongodb.

the class AggregatePublisherImpl method getOutNamespace.

@Nullable
private MongoNamespace getOutNamespace() {
    BsonDocument lastPipelineStage = getLastPipelineStage();
    if (lastPipelineStage == null) {
        return null;
    }
    String databaseName = getNamespace().getDatabaseName();
    if (lastPipelineStage.containsKey("$out")) {
        if (lastPipelineStage.get("$out").isString()) {
            return new MongoNamespace(databaseName, lastPipelineStage.getString("$out").getValue());
        } else if (lastPipelineStage.get("$out").isDocument()) {
            BsonDocument outDocument = lastPipelineStage.getDocument("$out");
            if (!outDocument.containsKey("db") || !outDocument.containsKey("coll")) {
                throw new IllegalStateException("Cannot return a cursor when the value for $out stage is not a namespace document");
            }
            return new MongoNamespace(outDocument.getString("db").getValue(), outDocument.getString("coll").getValue());
        } else {
            throw new IllegalStateException("Cannot return a cursor when the value for $out stage " + "is not a string or namespace document");
        }
    } else if (lastPipelineStage.containsKey("$merge")) {
        if (lastPipelineStage.isString("$merge")) {
            return new MongoNamespace(databaseName, lastPipelineStage.getString("$merge").getValue());
        } else if (lastPipelineStage.isDocument("$merge")) {
            BsonDocument mergeDocument = lastPipelineStage.getDocument("$merge");
            if (mergeDocument.isDocument("into")) {
                BsonDocument intoDocument = mergeDocument.getDocument("into");
                return new MongoNamespace(intoDocument.getString("db", new BsonString(databaseName)).getValue(), intoDocument.getString("coll").getValue());
            } else if (mergeDocument.isString("into")) {
                return new MongoNamespace(databaseName, mergeDocument.getString("into").getValue());
            }
        } else {
            throw new IllegalStateException("Cannot return a cursor when the value for $merge stage is not a string or a document");
        }
    }
    return null;
}
Also used : BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonString(org.bson.BsonString) MongoNamespace(com.mongodb.MongoNamespace) Nullable(com.mongodb.lang.Nullable)

Example 3 with Nullable

use of com.mongodb.lang.Nullable in project mongo-java-driver by mongodb.

the class DBObjectCodec method readValue.

@Nullable
private Object readValue(final BsonReader reader, final DecoderContext decoderContext, @Nullable final String fieldName, final List<String> path) {
    Object initialRetVal;
    BsonType bsonType = reader.getCurrentBsonType();
    if (bsonType.isContainer() && fieldName != null) {
        // if we got into some new context like nested document or array
        path.add(fieldName);
    }
    switch(bsonType) {
        case DOCUMENT:
            initialRetVal = verifyForDBRef(readDocument(reader, decoderContext, path));
            break;
        case ARRAY:
            initialRetVal = readArray(reader, decoderContext, path);
            break;
        case // custom for driver-compat types
        JAVASCRIPT_WITH_SCOPE:
            initialRetVal = readCodeWScope(reader, decoderContext, path);
            break;
        case // custom for driver-compat types
        DB_POINTER:
            BsonDbPointer dbPointer = reader.readDBPointer();
            initialRetVal = new DBRef(dbPointer.getNamespace(), dbPointer.getId());
            break;
        case BINARY:
            initialRetVal = readBinary(reader, decoderContext);
            break;
        case NULL:
            reader.readNull();
            initialRetVal = null;
            break;
        default:
            initialRetVal = bsonTypeCodecMap.get(bsonType).decode(reader, decoderContext);
    }
    if (bsonType.isContainer() && fieldName != null) {
        // step out of current context to a parent
        path.remove(fieldName);
    }
    return initialRetVal;
}
Also used : BsonType(org.bson.BsonType) BSONObject(org.bson.BSONObject) BsonDbPointer(org.bson.BsonDbPointer) Nullable(com.mongodb.lang.Nullable)

Example 4 with Nullable

use of com.mongodb.lang.Nullable in project morphia by mongodb.

the class Filter method getValue.

@Nullable
protected Object getValue(Datastore datastore) {
    if (!mapped) {
        PathTarget target = pathTarget(datastore.getMapper());
        OperationTarget operationTarget = new OperationTarget(pathTarget, value);
        this.value = operationTarget.getValue();
        PropertyModel property = target.getTarget();
        if (property != null && property.specializeCodec(datastore) instanceof PropertyHandler) {
            this.value = ((Document) operationTarget.encode(datastore)).get(field);
        }
        mapped = true;
    }
    return value;
}
Also used : PathTarget(dev.morphia.internal.PathTarget) PropertyHandler(dev.morphia.mapping.codec.pojo.PropertyHandler) OperationTarget(dev.morphia.query.OperationTarget) PropertyModel(dev.morphia.mapping.codec.pojo.PropertyModel) Nullable(com.mongodb.lang.Nullable)

Example 5 with Nullable

use of com.mongodb.lang.Nullable in project morphia by mongodb.

the class PathTarget method resolveProperty.

@Nullable
private PropertyModel resolveProperty(String segment) {
    if (context != null) {
        PropertyModel model = context.getProperty(segment);
        if (model == null) {
            Iterator<EntityModel> subTypes = context.getSubtypes().iterator();
            while (model == null && subTypes.hasNext()) {
                context = subTypes.next();
                model = resolveProperty(segment);
            }
        }
        if (model != null) {
            try {
                context = mapper.getEntityModel(model.getNormalizedType());
            } catch (NotMappableException ignored) {
                context = null;
            }
        }
        return model;
    } else {
        return null;
    }
}
Also used : NotMappableException(dev.morphia.mapping.NotMappableException) PropertyModel(dev.morphia.mapping.codec.pojo.PropertyModel) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) Nullable(com.mongodb.lang.Nullable)

Aggregations

Nullable (com.mongodb.lang.Nullable)19 EntityModel (dev.morphia.mapping.codec.pojo.EntityModel)4 BsonDocument (org.bson.BsonDocument)4 Mono (reactor.core.publisher.Mono)4 MongoClientException (com.mongodb.MongoClientException)3 Assertions.notNull (com.mongodb.assertions.Assertions.notNull)3 PropertyModel (dev.morphia.mapping.codec.pojo.PropertyModel)3 BsonString (org.bson.BsonString)3 RawBsonDocument (org.bson.RawBsonDocument)3 MongoException (com.mongodb.MongoException)2 MongoInternalException (com.mongodb.MongoInternalException)2 MongoNamespace (com.mongodb.MongoNamespace)2 ReadPreference (com.mongodb.ReadPreference)2 RequestContext (com.mongodb.RequestContext)2 ClientSession (com.mongodb.reactivestreams.client.ClientSession)2 MongoOperationPublisher.sinkToCallback (com.mongodb.reactivestreams.client.internal.MongoOperationPublisher.sinkToCallback)2 String.format (java.lang.String.format)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2