-
Notifications
You must be signed in to change notification settings - Fork 456
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
when collecting index definitions, look at the parent classes as well… #1102
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,9 +36,9 @@ | |
import org.mongodb.morphia.annotations.IndexOptions; | ||
import org.mongodb.morphia.annotations.Indexed; | ||
import org.mongodb.morphia.annotations.Indexes; | ||
import org.mongodb.morphia.annotations.Property; | ||
import org.mongodb.morphia.annotations.Text; | ||
import org.mongodb.morphia.mapping.MappedClass; | ||
import org.mongodb.morphia.mapping.Mapper; | ||
import org.mongodb.morphia.mapping.MappingException; | ||
import org.mongodb.morphia.utils.IndexDirection; | ||
import org.mongodb.morphia.utils.IndexType; | ||
|
@@ -64,7 +64,7 @@ public class IndexHelperTest extends TestBase { | |
|
||
@Before | ||
public void before() { | ||
getMorphia().map(IndexedClass.class, NestedClass.class, NestedClassImpl.class); | ||
getMorphia().map(AbstractParent.class, IndexedClass.class, NestedClass.class, NestedClassImpl.class); | ||
} | ||
|
||
@Test | ||
|
@@ -109,13 +109,14 @@ public void calculateKeys() { | |
@Test | ||
public void createIndex() { | ||
checkMinServerVersion(3.4); | ||
MongoCollection<Document> collection = getDatabase().getCollection("indexes"); | ||
MappedClass mappedClass = getMorphia().getMapper().getMappedClass(IndexedClass.class); | ||
String collectionName = getDs().getCollection(IndexedClass.class).getName(); | ||
MongoCollection<Document> collection = getDatabase().getCollection(collectionName); | ||
Mapper mapper = getMorphia().getMapper(); | ||
|
||
indexHelper.createIndex(collection, mappedClass, false); | ||
indexHelper.createIndex(collection, mapper.getMappedClass(IndexedClass.class), false); | ||
List<DBObject> indexInfo = getDs().getCollection(IndexedClass.class) | ||
.getIndexInfo(); | ||
assertEquals("Should have 5 indexes", 5, indexInfo.size()); | ||
assertEquals("Should have 6 indexes", 6, indexInfo.size()); | ||
for (DBObject dbObject : indexInfo) { | ||
String name = dbObject.get("name").toString(); | ||
if (name.equals("latitude_1")) { | ||
|
@@ -129,22 +130,27 @@ public void createIndex() { | |
assertEquals(parse("{ 'nest.name' : 1} "), dbObject.get("key")); | ||
} else if (name.equals("searchme")) { | ||
assertEquals(parse("{ 'text' : 10 }"), dbObject.get("weights")); | ||
} else if (name.equals("indexName_1")) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Checkstyle probably is complaining about the |
||
assertEquals(parse("{'indexName': 1 }"), dbObject.get("key")); | ||
} else { | ||
if (!"_id_".equals(dbObject.get("name"))) { | ||
throw new MappingException("Found an index I wasn't expecting: " + dbObject); | ||
} | ||
} | ||
|
||
} | ||
|
||
collection = getDatabase().getCollection(getDs().getCollection(AbstractParent.class).getName()); | ||
indexHelper.createIndex(collection, mapper.getMappedClass(AbstractParent.class), false); | ||
indexInfo = getDs().getCollection(AbstractParent.class).getIndexInfo(); | ||
assertTrue("Shouldn't find any indexes: " + indexInfo, indexInfo.isEmpty()); | ||
|
||
} | ||
|
||
@Test | ||
public void findField() { | ||
MappedClass mappedClass = getMorphia().getMapper().getMappedClass(IndexedClass.class); | ||
|
||
assertEquals("name", indexHelper.findField(mappedClass, new IndexOptionsBuilder(), singletonList("indexName"))); | ||
assertEquals("name", indexHelper.findField(mappedClass, new IndexOptionsBuilder(), singletonList("name"))); | ||
assertEquals("indexName", indexHelper.findField(mappedClass, new IndexOptionsBuilder(), singletonList("indexName"))); | ||
assertEquals("nest.name", indexHelper.findField(mappedClass, new IndexOptionsBuilder(), asList("nested", "name"))); | ||
assertEquals("nest.name", indexHelper.findField(mappedClass, new IndexOptionsBuilder(), asList("nest", "name"))); | ||
|
||
|
@@ -254,7 +260,7 @@ public void oldIndexForm() { | |
List<DBObject> indexInfo = getDs().getCollection(IndexedClass.class) | ||
.getIndexInfo(); | ||
for (DBObject dbObject : indexInfo) { | ||
if (dbObject.get("name").equals("index_name")) { | ||
if (dbObject.get("name").equals("index_indexName")) { | ||
checkIndex(dbObject); | ||
} | ||
} | ||
|
@@ -454,13 +460,9 @@ private interface NestedClass { | |
|
||
@Entity("indexes") | ||
@Indexes(@Index(fields = @Field("latitude"))) | ||
private static class IndexedClass { | ||
@Id | ||
private ObjectId id; | ||
private static class IndexedClass extends AbstractParent { | ||
@Text(value = 10, options = @IndexOptions(name = "searchme")) | ||
private String text; | ||
@Property("name") | ||
private double indexName; | ||
private double latitude; | ||
@Embedded("nest") | ||
private NestedClass nested; | ||
|
@@ -474,4 +476,11 @@ private static class NestedClassImpl implements NestedClass { | |
@Indexed | ||
private String name; | ||
} | ||
|
||
@Indexes(@Index(fields = @Field("indexName"))) | ||
private abstract static class AbstractParent { | ||
@Id | ||
private ObjectId id; | ||
private double indexName; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How come this check was removed - shouldn't there be a fixed number of indexes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well my initial thought was we'd catch any extra indexes in the loop but that won't catch missing indexes so I'll restore that check.