Search in sources :

Example 16 with Nullable

use of org.springframework.lang.Nullable in project spring-data-mongodb by spring-projects.

the class GeospatialIndex method getIndexOptions.

@Nullable
public Document getIndexOptions() {
    Document document = new Document();
    if (StringUtils.hasText(name)) {
        document.put("name", name);
    }
    switch(type) {
        case GEO_2D:
            if (min != null) {
                document.put("min", min);
            }
            if (max != null) {
                document.put("max", max);
            }
            if (bits != null) {
                document.put("bits", bits);
            }
            break;
        case GEO_2DSPHERE:
            break;
        case GEO_HAYSTACK:
            document.put("bucketSize", bucketSize);
            break;
    }
    filter.ifPresent(val -> document.put("partialFilterExpression", val.getFilterObject()));
    collation.ifPresent(val -> document.append("collation", val.toDocument()));
    return document;
}
Also used : Document(org.bson.Document) Nullable(org.springframework.lang.Nullable)

Example 17 with Nullable

use of org.springframework.lang.Nullable in project spring-data-mongodb by spring-projects.

the class MongoDbFactoryParser method getMongoUri.

/**
 * Creates a {@link BeanDefinition} for a {@link MongoURI} or {@link MongoClientURI} depending on configured
 * attributes. <br />
 * Errors when configured element contains {@literal uri} or {@literal client-uri} along with other attributes except
 * {@literal write-concern} and/or {@literal id}.
 *
 * @param element must not be {@literal null}.
 * @param parserContext
 * @return {@literal null} in case no client-/uri defined.
 */
@Nullable
private BeanDefinition getMongoUri(Element element, ParserContext parserContext) {
    boolean hasClientUri = element.hasAttribute("client-uri");
    if (!hasClientUri && !element.hasAttribute("uri")) {
        return null;
    }
    int allowedAttributesCount = 1;
    for (String attribute : MONGO_URI_ALLOWED_ADDITIONAL_ATTRIBUTES) {
        if (element.hasAttribute(attribute)) {
            allowedAttributesCount++;
        }
    }
    if (element.getAttributes().getLength() > allowedAttributesCount) {
        parserContext.getReaderContext().error("Configure either " + (hasClientUri ? "Mongo Client URI" : "Mongo URI") + " or details individually!", parserContext.extractSource(element));
    }
    Class<?> type = MongoClientURI.class;
    String uri = hasClientUri ? element.getAttribute("client-uri") : element.getAttribute("uri");
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(type);
    builder.addConstructorArgValue(uri);
    return builder.getBeanDefinition();
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) MongoClientURI(com.mongodb.MongoClientURI) Nullable(org.springframework.lang.Nullable)

Example 18 with Nullable

use of org.springframework.lang.Nullable in project spring-data-mongodb by spring-projects.

the class ServerAddressPropertyEditor method parseServerAddress.

/**
 * Parses the given source into a {@link ServerAddress}.
 *
 * @param source
 * @return the
 */
@Nullable
private ServerAddress parseServerAddress(String source) {
    if (!StringUtils.hasText(source)) {
        LOG.warn(COULD_NOT_PARSE_ADDRESS_MESSAGE, "source", source);
        return null;
    }
    String[] hostAndPort = extractHostAddressAndPort(source.trim());
    if (hostAndPort.length > 2) {
        LOG.warn(COULD_NOT_PARSE_ADDRESS_MESSAGE, "source", source);
        return null;
    }
    try {
        InetAddress hostAddress = InetAddress.getByName(hostAndPort[0]);
        Integer port = hostAndPort.length == 1 ? null : Integer.parseInt(hostAndPort[1]);
        return port == null ? new ServerAddress(hostAddress) : new ServerAddress(hostAddress, port);
    } catch (UnknownHostException e) {
        LOG.warn(COULD_NOT_PARSE_ADDRESS_MESSAGE, "host", hostAndPort[0]);
    } catch (NumberFormatException e) {
        LOG.warn(COULD_NOT_PARSE_ADDRESS_MESSAGE, "port", hostAndPort[1]);
    }
    return null;
}
Also used : UnknownHostException(java.net.UnknownHostException) ServerAddress(com.mongodb.ServerAddress) InetAddress(java.net.InetAddress) Nullable(org.springframework.lang.Nullable)

Aggregations

Nullable (org.springframework.lang.Nullable)18 Document (org.bson.Document)11 BasicDBObject (com.mongodb.BasicDBObject)5 Entry (java.util.Map.Entry)5 BsonValue (org.bson.BsonValue)5 MappingException (org.springframework.data.mapping.MappingException)5 PersistentPropertyAccessor (org.springframework.data.mapping.PersistentPropertyAccessor)5 ConvertingPropertyAccessor (org.springframework.data.mapping.model.ConvertingPropertyAccessor)5 DBObject (com.mongodb.DBObject)4 DeleteResult (com.mongodb.client.result.DeleteResult)4 UpdateResult (com.mongodb.client.result.UpdateResult)4 ClientSession (com.mongodb.session.ClientSession)4 JSONParseException (com.mongodb.util.JSONParseException)4 Bson (org.bson.conversions.Bson)4 BasicDBList (com.mongodb.BasicDBList)3 java.util (java.util)3 Collectors (java.util.stream.Collectors)3 NonNull (lombok.NonNull)3 RequiredArgsConstructor (lombok.RequiredArgsConstructor)3 Codec (org.bson.codecs.Codec)3