Search in sources :

Example 6 with NonNull

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

the class LegacyQuery method iterable.

@NonNull
private <E> FindIterable<E> iterable(FindOptions options, MongoCollection<E> collection) {
    final Document query = this.toDocument();
    if (LOG.isTraceEnabled()) {
        LOG.trace(format("Running query(%s) : %s, options: %s,", getCollectionName(), query.toJson(), options));
    }
    if ((options.getCursorType() != null && options.getCursorType() != NonTailable) && (options.getSort() != null)) {
        LOG.warn("Sorting on tail is not allowed.");
    }
    ClientSession clientSession = datastore.findSession(options);
    FindIterable<E> iterable = clientSession != null ? collection.find(clientSession, query) : collection.find(query);
    return iterable;
}
Also used : ClientSession(com.mongodb.client.ClientSession) Document(org.bson.Document) NonNull(com.mongodb.lang.NonNull)

Example 7 with NonNull

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

the class ReferenceCodec method processId.

/**
 * Decodes an ID value
 *
 * @param datastore      the Datastore to use
 * @param decode         the value to decode
 * @param decoderContext the decoder context
 * @return the decoded value
 */
@NonNull
public static Object processId(Datastore datastore, Object decode, DecoderContext decoderContext) {
    Object id = decode;
    if (id instanceof Iterable) {
        Iterable<?> iterable = (Iterable<?>) id;
        List<Object> ids = new ArrayList<>();
        for (Object o : iterable) {
            ids.add(processId(datastore, o, decoderContext));
        }
        id = ids;
    } else if (id instanceof Document) {
        Document document = (Document) id;
        if (document.containsKey("$ref")) {
            id = processId(datastore, new DBRef(document.getString("$db"), document.getString("$ref"), document.get("$id")), decoderContext);
        } else if (document.containsKey(datastore.getMapper().getOptions().getDiscriminatorKey())) {
            try {
                id = datastore.getCodecRegistry().get(datastore.getMapper().getClass(document)).decode(new DocumentReader(document), decoderContext);
            } catch (CodecConfigurationException e) {
                throw new MappingException(Sofia.cannotFindTypeInDocument(), e);
            }
        }
    } else if (id instanceof DBRef) {
        DBRef ref = (DBRef) id;
        Object refId = ref.getId();
        if (refId instanceof Document) {
            refId = datastore.getCodecRegistry().get(Object.class).decode(new DocumentReader((Document) refId), decoderContext);
        }
        id = new DBRef(ref.getDatabaseName(), ref.getCollectionName(), refId);
    }
    return id;
}
Also used : CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) ArrayList(java.util.ArrayList) DBRef(com.mongodb.DBRef) DocumentReader(dev.morphia.mapping.codec.reader.DocumentReader) Document(org.bson.Document) MappingException(dev.morphia.mapping.MappingException) NonNull(com.mongodb.lang.NonNull)

Aggregations

NonNull (com.mongodb.lang.NonNull)7 Document (org.bson.Document)4 ClientSession (com.mongodb.client.ClientSession)2 DBRef (com.mongodb.DBRef)1 MongoInternalException (com.mongodb.MongoInternalException)1 SubjectProvider (com.mongodb.SubjectProvider)1 MongoServer (de.bwaldvogel.mongo.MongoServer)1 MemoryBackend (de.bwaldvogel.mongo.backend.memory.MemoryBackend)1 MappingException (dev.morphia.mapping.MappingException)1 DocumentReader (dev.morphia.mapping.codec.reader.DocumentReader)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 HttpURLConnection (java.net.HttpURLConnection)1 InetSocketAddress (java.net.InetSocketAddress)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CodecConfigurationException (org.bson.codecs.configuration.CodecConfigurationException)1