Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>3.0.0.BUILD-SNAPSHOT</version>
<version>3.0.0.DATAMONGO-2477-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>3.0.0.BUILD-SNAPSHOT</version>
<version>3.0.0.DATAMONGO-2477-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>3.0.0.BUILD-SNAPSHOT</version>
<version>3.0.0.DATAMONGO-2477-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>3.0.0.BUILD-SNAPSHOT</version>
<version>3.0.0.DATAMONGO-2477-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ public BeanDefinition parse(Element element, ParserContext parserContext) {
String id = element.getAttribute(AbstractBeanDefinitionParser.ID_ATTRIBUTE);
id = StringUtils.hasText(id) ? id : DEFAULT_CONVERTER_BEAN_NAME;

String autoIndexCreation = element.getAttribute("auto-index-creation");
boolean autoIndexCreationEnabled = StringUtils.hasText(autoIndexCreation) && Boolean.valueOf(autoIndexCreation);

parserContext.pushContainingComponent(new CompositeComponentDefinition("Mapping Mongo Converter", element));

BeanDefinition conversionsDefinition = getCustomConversions(element, parserContext);
Expand Down Expand Up @@ -199,6 +202,11 @@ private RuntimeBeanReference getValidator(Object source, ParserContext parserCon

public static String potentiallyCreateMappingContext(Element element, ParserContext parserContext,
@Nullable BeanDefinition conversionsDefinition, @Nullable String converterId) {
return potentiallyCreateMappingContext(element, parserContext, conversionsDefinition, converterId, false);
}

public static String potentiallyCreateMappingContext(Element element, ParserContext parserContext,
@Nullable BeanDefinition conversionsDefinition, @Nullable String converterId, boolean autoIndexCreation) {

String ctxRef = element.getAttribute("mapping-context-ref");

Expand Down Expand Up @@ -226,6 +234,8 @@ public static String potentiallyCreateMappingContext(Element element, ParserCont
mappingContextBuilder.addPropertyValue("simpleTypeHolder", simpleTypesDefinition);
}

mappingContextBuilder.addPropertyValue("autoIndexCreation", autoIndexCreation);

parseFieldNamingStrategy(element, parserContext.getReaderContext(), mappingContextBuilder);

ctxRef = converterId == null || DEFAULT_CONVERTER_BEAN_NAME.equals(converterId) ? MAPPING_CONTEXT_BEAN_NAME
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,12 @@ protected FieldNamingStrategy fieldNamingStrategy() {
* Configure whether to automatically create indices for domain types by deriving the
* {@link org.springframework.data.mongodb.core.index.IndexDefinition} from the entity or not.
*
* @return {@literal true} by default. <br />
* <strong>INFO</strong>: As of 3.x the default will be set to {@literal false}.
* @return {@literal false} by default. <br />
* <strong>INFO</strong>: As of 3.x the default is set to {@literal false}; In 2.x it was {@literal true}.
* @since 2.2
*/
protected boolean autoIndexCreation() {
return true;
return false;
}

/**
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,6 @@ private void checkForAndCreateIndexes(MongoPersistentEntity<?> entity) {

for (IndexDefinition indexDefinition : indexResolver.resolveIndexFor(entity.getTypeInformation())) {

JustOnceLogger.logWarnIndexCreationConfigurationChange(this.getClass().getName());

IndexDefinitionHolder indexToCreate = indexDefinition instanceof IndexDefinitionHolder
? (IndexDefinitionHolder) indexDefinition
: new IndexDefinitionHolder("", indexDefinition, collection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,6 @@ private Mono<Void> checkForAndCreateIndexes(MongoPersistentEntity<?> entity) {

Mono<String> createIndex(IndexDefinitionHolder indexDefinition) {

JustOnceLogger.logWarnIndexCreationConfigurationChange(this.getClass().getName());

return operationsProvider.indexOps(indexDefinition.getCollection()).ensureIndex(indexDefinition) //
.onErrorResume(ReactiveMongoPersistentEntityIndexCreator::isDataIntegrityViolation,
e -> translateException(e, indexDefinition));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ public class MongoMappingContext extends AbstractMappingContext<BasicMongoPersis
private static final FieldNamingStrategy DEFAULT_NAMING_STRATEGY = PropertyNameFieldNamingStrategy.INSTANCE;

private FieldNamingStrategy fieldNamingStrategy = DEFAULT_NAMING_STRATEGY;
private @Nullable ApplicationContext context;
private boolean autoIndexCreation = true;
private boolean autoIndexCreation = false;

/**
* Creates a new {@link MongoMappingContext}.
Expand Down Expand Up @@ -99,16 +98,15 @@ protected <T> BasicMongoPersistentEntity<T> createPersistentEntity(TypeInformati
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

super.setApplicationContext(applicationContext);

this.context = applicationContext;
}

/**
* Returns whether auto-index creation is enabled or disabled. <br />
* <strong>NOTE:</strong>Index creation should happen at a well-defined time that is ideally controlled by the
* application itself.
*
* @return {@literal true} when auto-index creation is enabled; {@literal false} otherwise.
* @return {@literal true} when auto-index creation is enabled; {@literal false} otherwise. <br />
* <strong>INFO</strong>: As of 3.x the default will is set to {@literal false} was {@literal true} in 2.x.
* @since 2.2
* @see org.springframework.data.mongodb.core.index.Indexed
*/
Expand All @@ -121,7 +119,7 @@ public boolean isAutoIndexCreation() {
* <strong>NOTE:</strong>Index creation should happen at a well-defined time that is ideally controlled by the
* application itself.
*
* @param autoCreateIndexes set to {@literal false} to disable auto-index creation.
* @param autoCreateIndexes set to {@literal true} to enable auto-index creation.
* @since 2.2
* @see org.springframework.data.mongodb.core.index.Indexed
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,16 @@ The base package in which to scan for entities annotated with @Document
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="auto-index-creation" use="optional">
<xsd:annotation>
<xsd:documentation>
Enable/Disable index creation for annotated properties/entities.
</xsd:documentation>
</xsd:annotation>
<xsd:simpleType>
<xsd:union memberTypes="xsd:boolean xsd:string"/>
</xsd:simpleType>
</xsd:attribute>
</xsd:complexType>
</xsd:element>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ public MongoClient mongoClient() {
protected Set<Class<?>> getInitialEntitySet() throws ClassNotFoundException {
return Collections.emptySet();
}

@Override
protected boolean autoIndexCreation() {
return true;
}
}

@Autowired MongoOperations operations;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ void beforeEach() {
when(distinctIterable.into(any())).thenReturn(Collections.emptyList());

this.mappingContext = new MongoMappingContext();
mappingContext.setAutoIndexCreation(true);
mappingContext.afterPropertiesSet();

this.converter = spy(new MappingMongoConverter(new DefaultDbRefResolver(factory), mappingContext));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import static org.springframework.data.mongodb.test.util.Assertions.*;

import lombok.Data;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.convert.NoOpDbRefResolver;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;

Expand Down Expand Up @@ -66,7 +69,10 @@ public class ReactiveMongoTemplateIndexTests {
void setUp() {

factory = new SimpleReactiveMongoDatabaseFactory(client, "reactive-template-index-tests");
template = new ReactiveMongoTemplate(factory);
MongoMappingContext mappingContext = new MongoMappingContext();
mappingContext.setAutoIndexCreation(true);
template = new ReactiveMongoTemplate(factory, new MappingMongoConverter(NoOpDbRefResolver.INSTANCE, mappingContext));


MongoTestUtils.dropCollectionNow(template.getMongoDatabase().getName(), "person", client);
MongoTestUtils.dropCollectionNow(template.getMongoDatabase().getName(), "indexfail", client);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ TimeoutResolver myTimeoutResolver() {
protected Set<Class<?>> getInitialEntitySet() throws ClassNotFoundException {
return Collections.emptySet();
}

@Override
protected boolean autoIndexCreation() {
return true;
}
}

@AfterEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,9 @@ public LoggingEventListener mappingEventsListener() {
protected Set<Class<?>> getInitialEntitySet() throws ClassNotFoundException {
return Collections.emptySet();
}

@Override
protected boolean autoIndexCreation() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
import org.springframework.data.mongodb.MongoCollectionUtils;
import org.springframework.data.mongodb.core.CollectionCallback;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.convert.NoOpDbRefResolver;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.test.util.Client;
Expand Down Expand Up @@ -186,7 +189,10 @@ public void testWriteEntity() {
@SuppressWarnings({ "unchecked", "rawtypes" })
public void testUniqueIndex() {

MongoTemplate template = new MongoTemplate(client, DB_NAME);
MongoMappingContext mappingContext = new MongoMappingContext();
mappingContext.setAutoIndexCreation(true);

MongoTemplate template = new MongoTemplate(new SimpleMongoClientDatabaseFactory(client, DB_NAME), new MappingMongoConverter(NoOpDbRefResolver.INSTANCE, mappingContext));

Address addr = new Address();
addr.setLines(new String[] { "1234 W. 1st Street", "Apt. 12" });
Expand Down Expand Up @@ -227,7 +233,11 @@ public void testPrimitivesAndCustomCollectionName() {
@Test
public void testIndexesCreatedInRightCollection() {

MongoTemplate template = new MongoTemplate(client, DB_NAME);
MongoMappingContext mappingContext = new MongoMappingContext();
mappingContext.setAutoIndexCreation(true);

MongoTemplate template = new MongoTemplate(new SimpleMongoClientDatabaseFactory(client, DB_NAME), new MappingMongoConverter(NoOpDbRefResolver.INSTANCE, mappingContext));


CustomCollectionWithIndex ccwi = new CustomCollectionWithIndex("test");
template.insert(ccwi);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ ReactiveContactRepository reactiveContactRepository(ReactiveMongoRepositoryFacto
ReactiveCappedCollectionRepository reactiveCappedCollectionRepository(ReactiveMongoRepositoryFactory factory) {
return factory.getRepository(ReactiveCappedCollectionRepository.class);
}

@Override
protected boolean autoIndexCreation() {
return true;
}
}

@BeforeAll
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
</property>
</bean>

<mongo:mapping-converter auto-index-creation="true" />

<mongo:repositories base-package="org.springframework.data.mongodb.repository">
<repository:exclude-filter type="regex" expression=".*MongoRepository"/>
</mongo:repositories>
Expand Down
1 change: 1 addition & 0 deletions src/main/asciidoc/new-features.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
== What's New in Spring Data MongoDB 3.0

* Upgrade to MongoDB Driver 4.0. See <<upgrading.2-3>> for further details.
* <<mapping.index-creation,Auto-index creation>> is now **disabled** by default.
* Support for <<mongo-template.aggregation-update,aggregation pipelines in update operations>>.
* Removal of `_id` flattening for composite Id's when using `MongoTemplate` aggregations.
* Apply pagination when using GridFS `find(Query)`.
Expand Down
Loading