|
3 | 3 | import com.github.fakemongo.impl.ExpressionParser;
|
4 | 4 | import com.github.fakemongo.impl.Filter;
|
5 | 5 | import com.github.fakemongo.impl.Util;
|
| 6 | +import com.mongodb.BasicDBList; |
6 | 7 | import com.mongodb.DBObject;
|
7 | 8 | import com.mongodb.FongoDBCollection;
|
8 | 9 | import com.mongodb.MongoException;
|
9 |
| -import java.util.ArrayList; |
10 |
| -import java.util.Collection; |
11 |
| -import java.util.Collections; |
12 |
| -import java.util.List; |
13 |
| -import java.util.Map; |
14 |
| -import java.util.Set; |
15 | 10 | import org.bson.types.Binary;
|
16 | 11 |
|
| 12 | +import java.util.*; |
| 13 | + |
17 | 14 | /**
|
18 | 15 | * An index for the MongoDB.
|
19 | 16 | * <p/>
|
@@ -263,42 +260,43 @@ public void clear() {
|
263 | 260 | * @return true if index can be used.
|
264 | 261 | */
|
265 | 262 | public boolean canHandle(final DBObject queryFields) {
|
266 |
| - if(queryFields == null) { |
267 |
| - return false; |
268 |
| - } |
| 263 | + if (queryFields == null) { |
| 264 | + return false; |
| 265 | + } |
269 | 266 |
|
270 |
| - //get keys including embedded indexes |
271 |
| - for (String field : fields) { |
272 |
| - if (!queryFields.containsField(field) && !keyEmbeddedFieldMatch(field, queryFields)) { |
273 |
| - return false; |
274 |
| - } |
| 267 | + //get keys including embedded indexes |
| 268 | + for (String field : fields) { |
| 269 | + if (!queryFields.containsField(field) && !keyEmbeddedFieldMatch(field, queryFields)) { |
| 270 | + return false; |
275 | 271 | }
|
| 272 | + } |
276 | 273 | return true;
|
277 |
| - // return queryFields.containsAll(fields); |
278 | 274 | }
|
279 | 275 |
|
280 |
| - public boolean keyEmbeddedFieldMatch(String field, DBObject queryFields) |
281 |
| - { |
282 |
| - //if field embedded field type |
283 |
| - String[] fieldParts = field.split("\\."); |
284 |
| - if(fieldParts.length == 0) { |
285 |
| - return false; |
286 |
| - } |
287 |
| - |
288 |
| - DBObject searchQueryFields = queryFields; |
289 |
| - int count = 0; |
290 |
| - for(String fieldPart : fieldParts) { |
291 |
| - count++; |
292 |
| - if(!searchQueryFields.containsField(fieldPart)) { |
293 |
| - return false; |
294 |
| - } else if (searchQueryFields.get(fieldPart) instanceof DBObject) { |
295 |
| - searchQueryFields = (DBObject)searchQueryFields.get(fieldPart); |
296 |
| - } |
297 |
| - } |
| 276 | + private boolean keyEmbeddedFieldMatch(String field, DBObject queryFields) { |
| 277 | + //if field embedded field type |
| 278 | + String[] fieldParts = field.split("\\."); |
| 279 | + if (fieldParts.length == 0) { |
| 280 | + return false; |
| 281 | + } |
298 | 282 |
|
299 |
| - return fieldParts.length == count; |
| 283 | + DBObject searchQueryFields = queryFields; |
| 284 | + int count = 0; |
| 285 | + for (String fieldPart : fieldParts) { |
| 286 | + count++; |
| 287 | + if (searchQueryFields instanceof BasicDBList) { |
| 288 | + // when it's a list, there's no need to investigate nested documents |
| 289 | + return true; |
| 290 | + } else if (!searchQueryFields.containsField(fieldPart)) { |
| 291 | + return false; |
| 292 | + } else if (searchQueryFields.get(fieldPart) instanceof DBObject) { |
| 293 | + searchQueryFields = (DBObject) searchQueryFields.get(fieldPart); |
| 294 | + } |
300 | 295 | }
|
301 | 296 |
|
| 297 | + return fieldParts.length == count; |
| 298 | + } |
| 299 | + |
302 | 300 | @Override
|
303 | 301 | public String toString() {
|
304 | 302 | return "Index{" +
|
|
0 commit comments