@ -18,8 +18,6 @@ package org.springframework.data.document.mongodb.mapping;
import java.lang.reflect.Field ;
import java.lang.reflect.Field ;
import java.util.Collections ;
import java.util.Collections ;
import java.util.HashMap ;
import java.util.Map ;
import java.util.Set ;
import java.util.Set ;
import java.util.concurrent.ConcurrentHashMap ;
import java.util.concurrent.ConcurrentHashMap ;
@ -50,12 +48,10 @@ import org.springframework.util.Assert;
* @author Jon Brisbin < jbrisbin @vmware.com >
* @author Jon Brisbin < jbrisbin @vmware.com >
* @author Oliver Gierke
* @author Oliver Gierke
* /
* /
public class MongoPersistentEntityIndexCreator implements ApplicationListener < MappingContextEvent > {
public class MongoPersistentEntityIndexCreator implements ApplicationListener < MappingContextEvent > {
private static final Logger log = LoggerFactory . getLogger ( MongoPersistentEntityIndexCreator . class ) ;
private static final Logger log = LoggerFactory . getLogger ( MongoPersistentEntityIndexCreator . class ) ;
private Map < String , CompoundIndex > compoundIndexes = new HashMap < String , CompoundIndex > ( ) ;
private Map < String , Indexed > fieldIndexes = new HashMap < String , Indexed > ( ) ;
private Set < Class < ? > > classesSeen = Collections . newSetFromMap ( new ConcurrentHashMap < Class < ? > , Boolean > ( ) ) ;
private Set < Class < ? > > classesSeen = Collections . newSetFromMap ( new ConcurrentHashMap < Class < ? > , Boolean > ( ) ) ;
private final MongoTemplate mongoTemplate ;
private final MongoTemplate mongoTemplate ;
@ -79,7 +75,7 @@ public class MongoPersistentEntityIndexCreator implements ApplicationListener<Ma
}
}
protected void checkForIndexes ( MongoPersistentEntity < ? > entity ) {
protected void checkForIndexes ( MongoPersistentEntity < ? > entity ) {
Class < ? > type = entity . getType ( ) ;
final Class < ? > type = entity . getType ( ) ;
if ( ! classesSeen . contains ( type ) ) {
if ( ! classesSeen . contains ( type ) ) {
if ( log . isDebugEnabled ( ) ) {
if ( log . isDebugEnabled ( ) ) {
log . debug ( "Analyzing class " + type + " for index information." ) ;
log . debug ( "Analyzing class " + type + " for index information." ) ;
@ -102,13 +98,10 @@ public class MongoPersistentEntityIndexCreator implements ApplicationListener<Ma
if ( "" . equals ( indexColl ) ) {
if ( "" . equals ( indexColl ) ) {
indexColl = type . getSimpleName ( ) . toLowerCase ( ) ;
indexColl = type . getSimpleName ( ) . toLowerCase ( ) ;
}
}
if ( ! compoundIndexes . containsKey ( indexColl ) ) {
ensureIndex ( indexColl , index . name ( ) , index . def ( ) , index . direction ( ) , index . unique ( ) , index . dropDups ( ) , index . sparse ( ) ) ;
ensureIndex ( indexColl , index . name ( ) , index . def ( ) , index . direction ( ) , index . unique ( ) , index . dropDups ( ) , index . sparse ( ) ) ;
if ( log . isDebugEnabled ( ) ) {
if ( log . isDebugEnabled ( ) ) {
log . debug ( "Created compound index " + index ) ;
log . debug ( "Created compound index " + index ) ;
}
}
compoundIndexes . put ( indexColl , index ) ;
}
}
}
}
}
@ -117,17 +110,18 @@ public class MongoPersistentEntityIndexCreator implements ApplicationListener<Ma
Field field = persistentProperty . getField ( ) ;
Field field = persistentProperty . getField ( ) ;
if ( field . isAnnotationPresent ( Indexed . class ) ) {
if ( field . isAnnotationPresent ( Indexed . class ) ) {
Indexed index = field . getAnnotation ( Indexed . class ) ;
Indexed index = field . getAnnotation ( Indexed . class ) ;
String name = index . name ( ) ;
if ( "" . equals ( name ) ) {
name = field . getName ( ) ;
}
String collection = index . collection ( ) ;
String collection = index . collection ( ) ;
if ( "" . equals ( collection ) ) {
if ( "" . equals ( collection ) ) {
collection = field . getName ( ) ;
collection = type . getSimple Name ( ) . toLowerCas e ( ) ;
}
}
if ( ! fieldIndexes . containsKey ( collection ) ) {
ensureIndex ( collection , name , null , index . direction ( ) , index . unique ( ) , index . dropDups ( ) , index . sparse ( ) ) ;
ensureIndex ( collection , index . name ( ) , null , index . direction ( ) , index . unique ( ) , index . dropDups ( ) , index . sparse ( ) ) ;
if ( log . isDebugEnabled ( ) ) {
if ( log . isDebugEnabled ( ) ) {
log . debug ( "Created property index " + index ) ;
log . debug ( "Created property index " + index ) ;
}
}
fieldIndexes . put ( collection , index ) ;
}
}
}
}
}
} ) ;
} ) ;
@ -135,6 +129,7 @@ public class MongoPersistentEntityIndexCreator implements ApplicationListener<Ma
classesSeen . add ( type ) ;
classesSeen . add ( type ) ;
}
}
}
}
protected void ensureIndex ( String collection ,
protected void ensureIndex ( String collection ,
@ -154,9 +149,7 @@ public class MongoPersistentEntityIndexCreator implements ApplicationListener<Ma
defObj . put ( name , ( direction = = IndexDirection . ASCENDING ? 1 : - 1 ) ) ;
defObj . put ( name , ( direction = = IndexDirection . ASCENDING ? 1 : - 1 ) ) ;
}
}
DBObject opts = new BasicDBObject ( ) ;
DBObject opts = new BasicDBObject ( ) ;
if ( ! "" . equals ( name ) ) {
//opts.put("name", name + "_idx");
opts . put ( "name" , name ) ;
}
opts . put ( "dropDups" , dropDups ) ;
opts . put ( "dropDups" , dropDups ) ;
opts . put ( "sparse" , sparse ) ;
opts . put ( "sparse" , sparse ) ;
opts . put ( "unique" , unique ) ;
opts . put ( "unique" , unique ) ;