From f6143649182c35cf22d518123db49e51b6f7b107 Mon Sep 17 00:00:00 2001 From: Philipp Schneider Date: Wed, 28 Nov 2012 13:53:49 +0100 Subject: [PATCH] DATAMONGO-554 - Add background attribute to @Indexed and @CompoundIndex. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit @Indexed and @CompoundIndex now carry a background flag that can be used to enable background indexing. Updated MongoPersistentEntityIndexCreator to consider the flag and hand it to the ensureIndex(…) call. --- .../mongodb/core/index/CompoundIndex.java | 11 +++- .../data/mongodb/core/index/Indexed.java | 17 +++++-- .../MongoPersistentEntityIndexCreator.java | 12 +++-- ...PersistentEntityIndexCreatorUnitTests.java | 51 +++++++++++++++---- 4 files changed, 71 insertions(+), 20 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/CompoundIndex.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/CompoundIndex.java index b8acbf5c7..4c9525d41 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/CompoundIndex.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/CompoundIndex.java @@ -1,5 +1,5 @@ /* - * Copyright 2011-2012 the original author or authors. + * Copyright 2011-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,6 +26,7 @@ import java.lang.annotation.Target; * * @author Jon Brisbin * @author Oliver Gierke + * @author Philipp Schneider */ @Target({ ElementType.TYPE }) @Documented @@ -69,4 +70,12 @@ public @interface CompoundIndex { * @return */ String collection() default ""; + + /** + * If {@literal true} the index will be created in the background. + * + * @see http://docs.mongodb.org/manual/core/indexes/#background-construction + * @return + */ + boolean background() default false; } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/Indexed.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/Indexed.java index 30ad8eee1..1d23f16b9 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/Indexed.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/Indexed.java @@ -1,11 +1,11 @@ /* - * Copyright (c) 2011 by the original author(s). + * Copyright 2011-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.springframework.data.mongodb.core.index; import java.lang.annotation.ElementType; @@ -24,7 +23,9 @@ import java.lang.annotation.Target; /** * Mark a field to be indexed using MongoDB's indexing feature. * - * @author Jon Brisbin + * @author Jon Brisbin + * @author Oliver Gierke + * @author Philipp Schneider */ @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @@ -41,4 +42,12 @@ public @interface Indexed { String name() default ""; String collection() default ""; + + /** + * If {@literal true} the index will be created in the background. + * + * @see http://docs.mongodb.org/manual/core/indexes/#background-construction + * @return + */ + boolean background() default false; } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexCreator.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexCreator.java index 6d78a5e76..862c3a523 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexCreator.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexCreator.java @@ -1,5 +1,5 @@ /* - * Copyright 2011-2012 the original author or authors. + * Copyright 2011-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -43,6 +43,7 @@ import com.mongodb.util.JSON; * * @author Jon Brisbin * @author Oliver Gierke + * @author Philipp Schneider */ public class MongoPersistentEntityIndexCreator implements ApplicationListener, MongoPersistentProperty>> { @@ -106,7 +107,8 @@ public class MongoPersistentEntityIndexCreator implements String indexColl = StringUtils.hasText(index.collection()) ? index.collection() : entity.getCollection(); DBObject definition = (DBObject) JSON.parse(index.def()); - ensureIndex(indexColl, index.name(), definition, index.unique(), index.dropDups(), index.sparse()); + ensureIndex(indexColl, index.name(), definition, index.unique(), index.dropDups(), index.sparse(), + index.background()); if (log.isDebugEnabled()) { log.debug("Created compound index " + index); @@ -140,7 +142,8 @@ public class MongoPersistentEntityIndexCreator implements int direction = index.direction() == IndexDirection.ASCENDING ? 1 : -1; DBObject definition = new BasicDBObject(persistentProperty.getFieldName(), direction); - ensureIndex(collection, name, definition, index.unique(), index.dropDups(), index.sparse()); + ensureIndex(collection, name, definition, index.unique(), index.dropDups(), index.sparse(), + index.background()); if (log.isDebugEnabled()) { log.debug("Created property index " + index); @@ -191,13 +194,14 @@ public class MongoPersistentEntityIndexCreator implements * @param sparse sparse or not */ protected void ensureIndex(String collection, String name, DBObject indexDefinition, boolean unique, - boolean dropDups, boolean sparse) { + boolean dropDups, boolean sparse, boolean background) { DBObject opts = new BasicDBObject(); opts.put("name", name); opts.put("dropDups", dropDups); opts.put("sparse", sparse); opts.put("unique", unique); + opts.put("background", background); mongoDbFactory.getDb().getCollection(collection).ensureIndex(indexDefinition, opts); } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexCreatorUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexCreatorUnitTests.java index 10c7dc72b..06ed0380e 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexCreatorUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexCreatorUnitTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 the original author or authors. + * Copyright 2012-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,7 +15,7 @@ */ package org.springframework.data.mongodb.core.index; -import static org.hamcrest.CoreMatchers.*; +import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; import java.util.Collections; @@ -38,6 +38,7 @@ import com.mongodb.DBObject; * Unit tests for {@link MongoPersistentEntityIndexCreator}. * * @author Oliver Gierke + * @author Philipp Schneider */ @RunWith(MockitoJUnitRunner.class) public class MongoPersistentEntityIndexCreatorUnitTests { @@ -50,25 +51,20 @@ public class MongoPersistentEntityIndexCreatorUnitTests { @Test public void buildsIndexDefinitionUsingFieldName() { - MongoMappingContext mappingContext = new MongoMappingContext(); - mappingContext.setInitialEntitySet(Collections.singleton(Person.class)); - mappingContext.initialize(); - + MongoMappingContext mappingContext = prepareMappingContext(Person.class); DummyMongoPersistentEntityIndexCreator creator = new DummyMongoPersistentEntityIndexCreator(mappingContext, factory); assertThat(creator.indexDefinition, is(notNullValue())); assertThat(creator.indexDefinition.keySet(), hasItem("fieldname")); assertThat(creator.name, is("indexName")); + assertThat(creator.background, is(false)); } @Test public void doesNotCreateIndexForEntityComingFromDifferentMappingContext() { MongoMappingContext mappingContext = new MongoMappingContext(); - - MongoMappingContext personMappingContext = new MongoMappingContext(); - personMappingContext.setInitialEntitySet(Collections.singleton(Person.class)); - personMappingContext.initialize(); + MongoMappingContext personMappingContext = prepareMappingContext(Person.class); DummyMongoPersistentEntityIndexCreator creator = new DummyMongoPersistentEntityIndexCreator(mappingContext, factory); @@ -95,17 +91,49 @@ public class MongoPersistentEntityIndexCreatorUnitTests { assertThat(creator.isIndexCreatorFor(new MongoMappingContext()), is(false)); } + /** + * @see DATAMONGO-554 + */ + @Test + public void triggersBackgroundIndexingIfConfigured() { + + MongoMappingContext mappingContext = prepareMappingContext(AnotherPerson.class); + DummyMongoPersistentEntityIndexCreator creator = new DummyMongoPersistentEntityIndexCreator(mappingContext, factory); + + assertThat(creator.indexDefinition, is(notNullValue())); + assertThat(creator.indexDefinition.keySet(), hasItem("lastname")); + assertThat(creator.name, is("lastname")); + assertThat(creator.background, is(true)); + } + + private static MongoMappingContext prepareMappingContext(Class type) { + + MongoMappingContext mappingContext = new MongoMappingContext(); + mappingContext.setInitialEntitySet(Collections.singleton(type)); + mappingContext.initialize(); + + return mappingContext; + } + static class Person { @Indexed(name = "indexName") @Field("fieldname") String field; + + } + + static class AnotherPerson { + + @Indexed(background = true) + String lastname; } static class DummyMongoPersistentEntityIndexCreator extends MongoPersistentEntityIndexCreator { DBObject indexDefinition; String name; + boolean background; public DummyMongoPersistentEntityIndexCreator(MongoMappingContext mappingContext, MongoDbFactory mongoDbFactory) { super(mappingContext, mongoDbFactory); @@ -113,10 +141,11 @@ public class MongoPersistentEntityIndexCreatorUnitTests { @Override protected void ensureIndex(String collection, String name, DBObject indexDefinition, boolean unique, - boolean dropDups, boolean sparse) { + boolean dropDups, boolean sparse, boolean background) { this.name = name; this.indexDefinition = indexDefinition; + this.background = background; } } }