Search in sources :

Example 76 with MappedClass

use of org.mongodb.morphia.mapping.MappedClass 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)

Example 77 with MappedClass

use of org.mongodb.morphia.mapping.MappedClass in project morphia by mongodb.

the class TestIndexInheritance method testClassIndexInherit.

@Test
public void testClassIndexInherit() throws Exception {
    getMorphia().map(Circle.class).map(Shape.class);
    final MappedClass mc = getMorphia().getMapper().getMappedClass(Circle.class);
    assertNotNull(mc);
    assertEquals(2, mc.getAnnotations(Indexes.class).size());
    getDs().ensureIndexes();
    final DBCollection coll = getDs().getCollection(Circle.class);
    assertEquals(4, coll.getIndexInfo().size());
}
Also used : DBCollection(com.mongodb.DBCollection) MappedClass(org.mongodb.morphia.mapping.MappedClass) Test(org.junit.Test)

Example 78 with MappedClass

use of org.mongodb.morphia.mapping.MappedClass in project morphia by mongodb.

the class TestIndexed method testMultipleIndexedFields.

@Test
@SuppressWarnings("deprecation")
public void testMultipleIndexedFields() {
    final MappedClass mc = getMorphia().getMapper().getMappedClass(Ad.class);
    getMorphia().map(Ad.class);
    assertThat(getDb().getCollection(mc.getCollectionName()).getIndexInfo(), doesNotHaveIndexNamed("lastMod_1_active_-1"));
    getDs().ensureIndex(Ad.class, "lastMod, -active");
    assertThat(getDb().getCollection(mc.getCollectionName()).getIndexInfo(), hasIndexNamed("lastMod_1_active_-1"));
}
Also used : MappedClass(org.mongodb.morphia.mapping.MappedClass) Test(org.junit.Test)

Example 79 with MappedClass

use of org.mongodb.morphia.mapping.MappedClass in project morphia by mongodb.

the class TestIndexed method testIndexes.

@Test
public void testIndexes() {
    final MappedClass mc = getMorphia().getMapper().addMappedClass(Ad2.class);
    assertThat(getDb().getCollection(mc.getCollectionName()).getIndexInfo(), doesNotHaveIndexNamed("active_1_lastMod_-1"));
    getDs().ensureIndexes(Ad2.class);
    assertThat(getDb().getCollection(mc.getCollectionName()).getIndexInfo(), hasIndexNamed("active_1_lastMod_-1"));
}
Also used : MappedClass(org.mongodb.morphia.mapping.MappedClass) Test(org.junit.Test)

Example 80 with MappedClass

use of org.mongodb.morphia.mapping.MappedClass in project morphia by mongodb.

the class TestIndexed method testEmbeddedIndex.

@Test
public void testEmbeddedIndex() {
    final MappedClass mc = getMorphia().getMapper().addMappedClass(ContainsIndexedEmbed.class);
    assertThat(getDb().getCollection(mc.getCollectionName()).getIndexInfo(), doesNotHaveIndexNamed("e.name_-1"));
    getDs().ensureIndexes(ContainsIndexedEmbed.class);
    assertThat(getDb().getCollection(mc.getCollectionName()).getIndexInfo(), hasIndexNamed("e.name_-1"));
}
Also used : MappedClass(org.mongodb.morphia.mapping.MappedClass) Test(org.junit.Test)

Aggregations

MappedClass (org.mongodb.morphia.mapping.MappedClass)92 Test (org.junit.Test)73 MappedField (org.mongodb.morphia.mapping.MappedField)56 Mapper (org.mongodb.morphia.mapping.Mapper)53 ArrayList (java.util.ArrayList)48 BasicDBObject (com.mongodb.BasicDBObject)18 DBObject (com.mongodb.DBObject)12 BsonDocument (org.bson.BsonDocument)8 Document (org.bson.Document)8 MappingException (org.mongodb.morphia.mapping.MappingException)6 DBCollection (com.mongodb.DBCollection)5 List (java.util.List)5 SimpleEntity (org.mongodb.morphia.entities.SimpleEntity)5 ObjectId (org.bson.types.ObjectId)4 Key (org.mongodb.morphia.Key)4 Index (org.mongodb.morphia.annotations.Index)4 LinkedHashMap (java.util.LinkedHashMap)3 NotSaved (org.mongodb.morphia.annotations.NotSaved)3 UpdateResults (org.mongodb.morphia.query.UpdateResults)3 WriteResult (com.mongodb.WriteResult)2