/
TestIndexCollections.java
157 lines (133 loc) · 4.86 KB
/
TestIndexCollections.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package dev.morphia;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBObject;
import dev.morphia.annotations.Embedded;
import dev.morphia.annotations.Entity;
import dev.morphia.annotations.Field;
import dev.morphia.annotations.Id;
import dev.morphia.annotations.Index;
import dev.morphia.annotations.Indexed;
import dev.morphia.annotations.Indexes;
import dev.morphia.annotations.Property;
import org.bson.types.ObjectId;
import org.junit.Assert;
import org.junit.Test;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import static dev.morphia.utils.IndexType.DESC;
public class TestIndexCollections extends TestBase {
@Test
public void testEmbedded() {
AdvancedDatastore ads = getAds();
DB db = getDb();
getMorphia().map(HasEmbeddedIndex.class);
ads.ensureIndexes();
ads.ensureIndexes("b_2", HasEmbeddedIndex.class);
DBObject[] indexes = new BasicDBObject[]{
new BasicDBObject("name", 1),
new BasicDBObject("embeddedIndex.color", -1),
new BasicDBObject("embeddedIndex.name", 1),
};
testIndex(db.getCollection("b_2").getIndexInfo(), indexes);
testIndex(ads.getCollection(HasEmbeddedIndex.class).getIndexInfo(), indexes);
}
@Test
public void testDisablingEmbedded() {
AdvancedDatastore ads = getAds();
ads.getMapper().getOptions().setDisableEmbeddedIndexes(true);
getDs().getCollection(HasEmbeddedIndex.class).drop();
getMorphia().map(HasEmbeddedIndex.class);
ads.ensureIndexes();
DBObject[] indexes = new BasicDBObject[]{
new BasicDBObject("name", 1),
};
testIndex(ads.getCollection(HasEmbeddedIndex.class).getIndexInfo(), indexes);
}
@Test
public void testOldStyleIndexing() {
getMorphia().map(OldStyleIndexing.class);
getDb().dropDatabase();
getAds().ensureIndexes();
testIndex(getAds().getCollection(OldStyleIndexing.class).getIndexInfo(),
new BasicDBObject("field", 1),
new BasicDBObject("field2", -1),
new BasicDBObject("f3", 1));
}
@Test
@SuppressWarnings("deprecation")
public void testSingleFieldIndex() {
AdvancedDatastore ads = getAds();
DB db = getDb();
ads.ensureIndexes("a_1", SingleFieldIndex.class);
testIndex(db.getCollection("a_1").getIndexInfo(),
new BasicDBObject("field", 1),
new BasicDBObject("field2", -1),
new BasicDBObject("f3", 1));
ads.ensureIndex("a_2", SingleFieldIndex.class, "-field2");
ads.ensureIndexes("a_2", SingleFieldIndex.class);
testIndex(db.getCollection("a_2").getIndexInfo(),
new BasicDBObject("field", 1),
new BasicDBObject("field2", 1),
new BasicDBObject("field2", -1),
new BasicDBObject("f3", 1));
ads.ensureIndex("a_3", SingleFieldIndex.class, "field, field2");
testIndex(db.getCollection("a_3").getIndexInfo(), new BasicDBObject("field", 1)
.append("field2", 1));
}
private void testIndex(final List<DBObject> indexInfo, final DBObject... indexes) {
Set<String> expected = new TreeSet<String>();
Set<String> actual = new TreeSet<String>();
for (final DBObject index : indexes) {
expected.addAll(index.keySet());
}
for (final DBObject dbObject : indexInfo) {
if (!"_id_".equals(dbObject.get("name"))) {
actual.addAll(((DBObject) dbObject.get("key")).keySet());
}
}
Assert.assertEquals("Should have found all the indexes.", expected, actual);
}
@Entity
@Indexes({@Index(fields = @Field(value = "field2", type = DESC)), @Index(fields = @Field("field3"))})
private static class SingleFieldIndex {
@Id
private ObjectId id;
@Indexed
private String field;
@Property
private String field2;
@Property("f3")
private String field3;
}
@Entity
@Indexes({@Index(fields = @Field(value = "field2", type = DESC)), @Index(fields = @Field("field3"))})
private static class OldStyleIndexing {
@Id
private ObjectId id;
@Indexed
private String field;
@Property
private String field2;
@Property("f3")
private String field3;
}
@Entity
private static class HasEmbeddedIndex {
@Id
private ObjectId id;
@Indexed
private String name;
@Embedded
private EmbeddedIndex embeddedIndex;
}
@Embedded
@Indexes(@Index(fields = @Field(value = "color", type = DESC)))
private static class EmbeddedIndex {
@Indexed
private String name;
private String color;
}
}