1
1
/*
2
- * Copyright 2011-2015 the original author or authors.
2
+ * Copyright 2011-2016 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
15
15
*/
16
16
package org .springframework .data .mongodb .core ;
17
17
18
- import static org .springframework .data .domain .Sort .Direction .*;
19
-
20
18
import java .util .ArrayList ;
21
- import java .util .Arrays ;
22
19
import java .util .Collection ;
23
20
import java .util .List ;
24
21
25
22
import org .springframework .dao .DataAccessException ;
23
+ import org .springframework .data .mongodb .core .convert .QueryMapper ;
26
24
import org .springframework .data .mongodb .core .index .IndexDefinition ;
27
- import org .springframework .data .mongodb .core .index .IndexField ;
28
25
import org .springframework .data .mongodb .core .index .IndexInfo ;
26
+ import org .springframework .data .mongodb .core .mapping .MongoPersistentEntity ;
29
27
import org .springframework .util .Assert ;
30
28
31
29
import com .mongodb .DBCollection ;
42
40
*/
43
41
public class DefaultIndexOperations implements IndexOperations {
44
42
45
- private static final Double ONE = Double .valueOf (1 );
46
- private static final Double MINUS_ONE = Double .valueOf (-1 );
47
- private static final Collection <String > TWO_D_IDENTIFIERS = Arrays .asList ("2d" , "2dsphere" );
48
-
43
+ public static final String PARTIAL_FILTER_EXPRESSION_KEY = "partialFilterExpression" ;
49
44
private final MongoOperations mongoOperations ;
50
45
private final String collectionName ;
46
+ private final QueryMapper mapper ;
47
+ private final Class <?> type ;
51
48
52
49
/**
53
50
* Creates a new {@link DefaultIndexOperations}.
@@ -56,29 +53,72 @@ public class DefaultIndexOperations implements IndexOperations {
56
53
* @param collectionName must not be {@literal null}.
57
54
*/
58
55
public DefaultIndexOperations (MongoOperations mongoOperations , String collectionName ) {
56
+ this (mongoOperations , collectionName , null );
57
+ }
58
+
59
+ /**
60
+ * Creates a new {@link DefaultIndexOperations}.
61
+ *
62
+ * @param mongoOperations must not be {@literal null}.
63
+ * @param collectionName must not be {@literal null}.
64
+ * @param type Type used for mapping potential partial index filter expression. Can be {@literal null}.
65
+ * @since 1.10
66
+ */
67
+ public DefaultIndexOperations (MongoOperations mongoOperations , String collectionName , Class <?> type ) {
59
68
60
69
Assert .notNull (mongoOperations , "MongoOperations must not be null!" );
61
70
Assert .notNull (collectionName , "Collection name can not be null!" );
62
71
63
72
this .mongoOperations = mongoOperations ;
64
73
this .collectionName = collectionName ;
74
+ this .mapper = new QueryMapper (mongoOperations .getConverter ());
75
+ this .type = type ;
65
76
}
66
77
67
78
/*
68
79
* (non-Javadoc)
69
80
* @see org.springframework.data.mongodb.core.IndexOperations#ensureIndex(org.springframework.data.mongodb.core.index.IndexDefinition)
70
81
*/
71
82
public void ensureIndex (final IndexDefinition indexDefinition ) {
83
+
72
84
mongoOperations .execute (collectionName , new CollectionCallback <Object >() {
73
85
public Object doInCollection (DBCollection collection ) throws MongoException , DataAccessException {
74
86
DBObject indexOptions = indexDefinition .getIndexOptions ();
87
+
88
+ if (indexOptions != null && indexOptions .containsField (PARTIAL_FILTER_EXPRESSION_KEY )) {
89
+
90
+ Assert .isInstanceOf (DBObject .class , indexOptions .get (PARTIAL_FILTER_EXPRESSION_KEY ));
91
+
92
+ indexOptions .put (PARTIAL_FILTER_EXPRESSION_KEY ,
93
+ mapper .getMappedObject ((DBObject ) indexOptions .get (PARTIAL_FILTER_EXPRESSION_KEY ),
94
+ lookupPersistentEntity (type , collectionName )));
95
+ }
96
+
75
97
if (indexOptions != null ) {
76
98
collection .createIndex (indexDefinition .getIndexKeys (), indexOptions );
77
99
} else {
78
100
collection .createIndex (indexDefinition .getIndexKeys ());
79
101
}
80
102
return null ;
81
103
}
104
+
105
+ private MongoPersistentEntity <?> lookupPersistentEntity (Class <?> entityType , String collection ) {
106
+
107
+ if (entityType != null ) {
108
+ return mongoOperations .getConverter ().getMappingContext ().getPersistentEntity (entityType );
109
+ }
110
+
111
+ Collection <? extends MongoPersistentEntity <?>> entities = mongoOperations .getConverter ().getMappingContext ()
112
+ .getPersistentEntities ();
113
+
114
+ for (MongoPersistentEntity <?> entity : entities ) {
115
+ if (entity .getCollection ().equals (collection )) {
116
+ return entity ;
117
+ }
118
+ }
119
+
120
+ return null ;
121
+ }
82
122
});
83
123
}
84
124
@@ -136,44 +176,7 @@ private List<IndexInfo> getIndexData(List<DBObject> dbObjectList) {
136
176
List <IndexInfo > indexInfoList = new ArrayList <IndexInfo >();
137
177
138
178
for (DBObject ix : dbObjectList ) {
139
-
140
- DBObject keyDbObject = (DBObject ) ix .get ("key" );
141
- int numberOfElements = keyDbObject .keySet ().size ();
142
-
143
- List <IndexField > indexFields = new ArrayList <IndexField >(numberOfElements );
144
-
145
- for (String key : keyDbObject .keySet ()) {
146
-
147
- Object value = keyDbObject .get (key );
148
-
149
- if (TWO_D_IDENTIFIERS .contains (value )) {
150
- indexFields .add (IndexField .geo (key ));
151
- } else if ("text" .equals (value )) {
152
-
153
- DBObject weights = (DBObject ) ix .get ("weights" );
154
- for (String fieldName : weights .keySet ()) {
155
- indexFields .add (IndexField .text (fieldName , Float .valueOf (weights .get (fieldName ).toString ())));
156
- }
157
-
158
- } else {
159
-
160
- Double keyValue = new Double (value .toString ());
161
-
162
- if (ONE .equals (keyValue )) {
163
- indexFields .add (IndexField .create (key , ASC ));
164
- } else if (MINUS_ONE .equals (keyValue )) {
165
- indexFields .add (IndexField .create (key , DESC ));
166
- }
167
- }
168
- }
169
-
170
- String name = ix .get ("name" ).toString ();
171
-
172
- boolean unique = ix .containsField ("unique" ) ? (Boolean ) ix .get ("unique" ) : false ;
173
- boolean dropDuplicates = ix .containsField ("dropDups" ) ? (Boolean ) ix .get ("dropDups" ) : false ;
174
- boolean sparse = ix .containsField ("sparse" ) ? (Boolean ) ix .get ("sparse" ) : false ;
175
- String language = ix .containsField ("default_language" ) ? (String ) ix .get ("default_language" ) : "" ;
176
- indexInfoList .add (new IndexInfo (indexFields , name , unique , dropDuplicates , sparse , language ));
179
+ indexInfoList .add (IndexInfo .indexInfoOf (ix ));
177
180
}
178
181
179
182
return indexInfoList ;
0 commit comments