Search in sources :

Example 1 with Field

use of org.mongodb.morphia.annotations.Field in project morphia by mongodb.

the class IndexHelper method calculateKeys.

BsonDocument calculateKeys(final MappedClass mc, final Index index) {
    BsonDocument keys = new BsonDocument();
    for (Field field : index.fields()) {
        String path;
        try {
            path = findField(mc, index.options(), new ArrayList<String>(asList(field.value().split("\\."))));
        } catch (Exception e) {
            path = field.value();
            String message = format("The path '%s' can not be validated against '%s' and may represent an invalid index", path, mc.getClazz().getName());
            if (!index.options().disableValidation()) {
                throw new MappingException(message);
            }
            LOG.warning(message);
        }
        keys.putAll(toBsonDocument(path, field.type().toIndexValue()));
    }
    return keys;
}
Also used : MappedField(org.mongodb.morphia.mapping.MappedField) Field(org.mongodb.morphia.annotations.Field) BsonDocument(org.bson.BsonDocument) ArrayList(java.util.ArrayList) MappingException(org.mongodb.morphia.mapping.MappingException) MappingException(org.mongodb.morphia.mapping.MappingException)

Example 2 with Field

use of org.mongodb.morphia.annotations.Field in project morphia by mongodb.

the class IndexHelper method collectTopLevelIndexes.

private List<Index> collectTopLevelIndexes(final MappedClass mc) {
    List<Index> list = new ArrayList<Index>();
    if (mc != null) {
        final List<Indexes> annotations = mc.getAnnotations(Indexes.class);
        if (annotations != null) {
            for (final Indexes indexes : annotations) {
                for (final Index index : indexes.value()) {
                    Index updated = index;
                    if (index.fields().length == 0) {
                        LOG.warning(format("This index on '%s' is using deprecated configuration options.  Please update to use the " + "fields value on @Index: %s", mc.getClazz().getName(), index.toString()));
                        updated = new IndexBuilder().migrate(index);
                    }
                    List<Field> fields = new ArrayList<Field>();
                    for (Field field : updated.fields()) {
                        fields.add(new FieldBuilder().value(findField(mc, index.options(), asList(field.value().split("\\.")))).type(field.type()).weight(field.weight()));
                    }
                    list.add(replaceFields(updated, fields));
                }
            }
        }
        list.addAll(collectTopLevelIndexes(mc.getSuperClass()));
    }
    return list;
}
Also used : MappedField(org.mongodb.morphia.mapping.MappedField) Field(org.mongodb.morphia.annotations.Field) ArrayList(java.util.ArrayList) Index(org.mongodb.morphia.annotations.Index) Indexes(org.mongodb.morphia.annotations.Indexes)

Example 3 with Field

use of org.mongodb.morphia.annotations.Field in project morphia by mongodb.

the class IndexBuilder method parseFieldsString.

private List<Field> parseFieldsString(final String str) {
    List<Field> fields = new ArrayList<Field>();
    final String[] parts = str.split(",");
    for (String s : parts) {
        s = s.trim();
        IndexType dir = IndexType.ASC;
        if (s.startsWith("-")) {
            dir = IndexType.DESC;
            s = s.substring(1).trim();
        }
        fields.add(new FieldBuilder().value(s).type(dir));
    }
    return fields;
}
Also used : Field(org.mongodb.morphia.annotations.Field) ArrayList(java.util.ArrayList) IndexType(org.mongodb.morphia.utils.IndexType)

Example 4 with Field

use of org.mongodb.morphia.annotations.Field in project morphia by mongodb.

the class IndexHelper method collectNestedIndexes.

private List<Index> collectNestedIndexes(final MappedClass mc, final List<MappedClass> parentMCs) {
    List<Index> list = new ArrayList<Index>();
    for (final MappedField mf : mc.getPersistenceFields()) {
        if (!mf.isTypeMongoCompatible() && !mf.hasAnnotation(Reference.class) && !mf.hasAnnotation(Serialized.class) && !mf.hasAnnotation(NotSaved.class) && !mf.isTransient()) {
            final List<MappedClass> parents = new ArrayList<MappedClass>(parentMCs);
            parents.add(mc);
            List<MappedClass> classes = new ArrayList<MappedClass>();
            MappedClass mappedClass = mapper.getMappedClass(mf.isSingleValue() ? mf.getType() : mf.getSubClass());
            classes.add(mappedClass);
            classes.addAll(mapper.getSubTypes(mappedClass));
            for (MappedClass aClass : classes) {
                for (Index index : collectIndexes(aClass, parents)) {
                    List<Field> fields = new ArrayList<Field>();
                    for (Field field : index.fields()) {
                        fields.add(new FieldBuilder().value(field.value().equals("$**") ? field.value() : mf.getNameToStore() + "." + field.value()).type(field.type()).weight(field.weight()));
                    }
                    list.add(new IndexBuilder(index).fields(fields));
                }
            }
        }
    }
    return list;
}
Also used : MappedField(org.mongodb.morphia.mapping.MappedField) MappedField(org.mongodb.morphia.mapping.MappedField) Field(org.mongodb.morphia.annotations.Field) NotSaved(org.mongodb.morphia.annotations.NotSaved) Reference(org.mongodb.morphia.annotations.Reference) ArrayList(java.util.ArrayList) Index(org.mongodb.morphia.annotations.Index) MappedClass(org.mongodb.morphia.mapping.MappedClass)

Aggregations

ArrayList (java.util.ArrayList)4 Field (org.mongodb.morphia.annotations.Field)4 MappedField (org.mongodb.morphia.mapping.MappedField)3 Index (org.mongodb.morphia.annotations.Index)2 BsonDocument (org.bson.BsonDocument)1 Indexes (org.mongodb.morphia.annotations.Indexes)1 NotSaved (org.mongodb.morphia.annotations.NotSaved)1 Reference (org.mongodb.morphia.annotations.Reference)1 MappedClass (org.mongodb.morphia.mapping.MappedClass)1 MappingException (org.mongodb.morphia.mapping.MappingException)1 IndexType (org.mongodb.morphia.utils.IndexType)1