Skip to content

Commit 7848da6

Browse files
christophstroblodrotbohm
authored andcommitted
DATAMONGO-899 - Ensure proper creation of index structures for nested entities.
Index creation did not consider the properties path when creating the index. This lead to broken index creation when nesting entities that might require index structures. Off now index creation traverses the entities property path for all top level entities (namely those holding the @document annotation) and creates the index using the full property path. This is a breaking change as having an entity to carry the @document annotation has not been required by now. Original Pull Request: spring-projects#168
1 parent f359a1d commit 7848da6

12 files changed

+1066
-156
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core.index;
17+
18+
import com.mongodb.BasicDBObject;
19+
import com.mongodb.DBObject;
20+
21+
/**
22+
* @author Christoph Strobl
23+
* @since 1.5
24+
*/
25+
public class CompoundIndexDefinition extends Index {
26+
27+
private DBObject keys;
28+
29+
public CompoundIndexDefinition(DBObject keys) {
30+
this.keys = keys;
31+
}
32+
33+
@Override
34+
public DBObject getIndexKeys() {
35+
36+
BasicDBObject dbo = new BasicDBObject();
37+
dbo.putAll(this.keys);
38+
dbo.putAll(super.getIndexKeys());
39+
return dbo;
40+
}
41+
42+
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/GeospatialIndex.java

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2013 the original author or authors.
2+
* Copyright 2010-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,6 +27,7 @@
2727
* @author Jon Brisbin
2828
* @author Oliver Gierke
2929
* @author Laurent Canet
30+
* @author Christoph Strobl
3031
*/
3132
public class GeospatialIndex implements IndexDefinition {
3233

@@ -38,6 +39,7 @@ public class GeospatialIndex implements IndexDefinition {
3839
private GeoSpatialIndexType type = GeoSpatialIndexType.GEO_2D;
3940
private Double bucketSize = 1.0;
4041
private String additionalField;
42+
private String collection;
4143

4244
/**
4345
* Creates a new {@link GeospatialIndex} for the given field.
@@ -120,6 +122,23 @@ public GeospatialIndex withAdditionalField(String fieldName) {
120122
return this;
121123
}
122124

125+
/*
126+
* (non-Javadoc)
127+
* @see org.springframework.data.mongodb.core.index.IndexDefinition#getCollection()
128+
*/
129+
@Override
130+
public String getCollection() {
131+
return collection;
132+
}
133+
134+
/**
135+
* @param collection
136+
* @since 1.5
137+
*/
138+
public void setCollection(String collection) {
139+
this.collection = collection;
140+
}
141+
123142
public DBObject getIndexKeys() {
124143

125144
DBObject dbo = new BasicDBObject();

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/Index.java

+76-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2013 the original author or authors.
2+
* Copyright 2010-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,13 +17,19 @@
1717

1818
import java.util.LinkedHashMap;
1919
import java.util.Map;
20+
import java.util.concurrent.TimeUnit;
2021

2122
import org.springframework.data.domain.Sort.Direction;
2223
import org.springframework.data.mongodb.core.query.Order;
24+
import org.springframework.util.Assert;
2325

2426
import com.mongodb.BasicDBObject;
2527
import com.mongodb.DBObject;
2628

29+
/**
30+
* @author Oliver Gierke
31+
* @author Christoph Strobl
32+
*/
2733
@SuppressWarnings("deprecation")
2834
public class Index implements IndexDefinition {
2935

@@ -41,6 +47,12 @@ public enum Duplicates {
4147

4248
private boolean sparse = false;
4349

50+
private boolean background = false;
51+
52+
private long expire = -1;
53+
54+
private String collection;
55+
4456
public Index() {}
4557

4658
public Index(String key, Direction direction) {
@@ -104,6 +116,61 @@ public Index sparse() {
104116
return this;
105117
}
106118

119+
/**
120+
* Build the index in background (non blocking).
121+
*
122+
* @return
123+
* @since 1.5
124+
*/
125+
public Index background() {
126+
127+
this.background = true;
128+
return this;
129+
}
130+
131+
/**
132+
* Specifies TTL in seconds.
133+
*
134+
* @param value
135+
* @return
136+
* @since 1.5
137+
*/
138+
public Index expire(long value) {
139+
return expire(value, TimeUnit.SECONDS);
140+
}
141+
142+
/**
143+
* Specifies TTL with given {@link TimeUnit}.
144+
*
145+
* @param value
146+
* @param unit
147+
* @return
148+
* @since 1.5
149+
*/
150+
public Index expire(long value, TimeUnit unit) {
151+
152+
Assert.notNull(unit, "TimeUnit for expiration must not be null.");
153+
this.expire = unit.toSeconds(value);
154+
return this;
155+
}
156+
157+
/*
158+
* (non-Javadoc)
159+
* @see org.springframework.data.mongodb.core.index.IndexDefinition#getCollection()
160+
*/
161+
@Override
162+
public String getCollection() {
163+
return collection;
164+
}
165+
166+
/**
167+
* @param collection
168+
* @since 1.5
169+
*/
170+
public void setCollection(String collection) {
171+
this.collection = collection;
172+
}
173+
107174
/**
108175
* @see http://docs.mongodb.org/manual/core/index-creation/#index-creation-duplicate-dropping
109176
* @param duplicates
@@ -125,9 +192,11 @@ public DBObject getIndexKeys() {
125192
}
126193

127194
public DBObject getIndexOptions() {
195+
128196
if (name == null && !unique) {
129197
return null;
130198
}
199+
131200
DBObject dbo = new BasicDBObject();
132201
if (name != null) {
133202
dbo.put("name", name);
@@ -141,6 +210,12 @@ public DBObject getIndexOptions() {
141210
if (sparse) {
142211
dbo.put("sparse", true);
143212
}
213+
if (background) {
214+
dbo.put("background", true);
215+
}
216+
if (expire >= 0) {
217+
dbo.put("expireAfterSeconds", expire);
218+
}
144219
return dbo;
145220
}
146221

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/IndexDefinition.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011 by the original author(s).
2+
* Copyright (c) 2011-2014 by the original author(s).
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,11 +20,19 @@
2020

2121
/**
2222
* @author Jon Brisbin <jbrisbin@vmware.com>
23+
* @author Christoph Strobl
2324
*/
2425
public interface IndexDefinition {
2526

2627
DBObject getIndexKeys();
2728

2829
DBObject getIndexOptions();
2930

31+
/**
32+
* Get the collection name for the index.
33+
*
34+
* @return
35+
* @since 1.5
36+
*/
37+
String getCollection();
3038
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core.index;
17+
18+
/**
19+
* {@link IndexResolver} finds those {@link IndexDefinition}s to be created for a given class.
20+
*
21+
* @author Christoph Strobl
22+
* @since 1.5
23+
*/
24+
public interface IndexResolver {
25+
26+
/**
27+
* Find and create {@link IndexDefinition}s for properties of given {@code type}. {@link IndexDefinition}s are created
28+
* for properties and types with {@link Indexed}, {@link CompoundIndexes} or {@link GeoSpatialIndexed}.
29+
*
30+
* @param type
31+
* @return Empty {@link Iterable} in case no {@link IndexDefinition} could be resolved for type.
32+
*/
33+
Iterable<? extends IndexDefinition> resolveIndexForClass(Class<?> type);
34+
35+
}

0 commit comments

Comments
 (0)