forked from MorphiaOrg/morphia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IndexFactory.java
62 lines (56 loc) · 1.68 KB
/
IndexFactory.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
package com.github.fakemongo.impl.index;
import com.github.fakemongo.impl.Util;
import com.mongodb.DBObject;
import com.mongodb.MongoException;
import java.util.Map;
/**
* A factory for index.
*/
public final class IndexFactory {
private IndexFactory() {
}
public static IndexAbstract create(String name, DBObject keys, boolean unique) throws MongoException {
String geoIndex = getGeoKey(keys);
if (geoIndex != null) {
return new GeoIndex(name, keys, unique, geoIndex);
} else {
String hashed = getHashedKey(keys);
if (hashed != null) {
return new HashedIndex(name, keys, unique, hashed);
}
return new Index(name, keys, unique);
}
}
private static String getHashedKey(DBObject keys) {
String hashed = null;
for (Map.Entry<String, Object> entry : Util.entrySet(keys)) {
Object value = entry.getValue();
if (value instanceof String) {
boolean localHashed = "hashed".equals(value);
if (localHashed) {
hashed = entry.getKey();
}
}
}
return hashed;
}
private static String getGeoKey(DBObject keys) {
boolean first = true;
String geo = null;
for (Map.Entry<String, Object> entry : Util.entrySet(keys)) {
Object value = entry.getValue();
if (value instanceof String) {
boolean localGeo = "2d".equals(value);
if (localGeo) {
if (!first) {
// "err" : "2d has to be first in index", "code" : 13023, "n" : 0, "connectionId" : 206, "ok" : 1
throw new MongoException(13023, "2d has to be first in index");
}
geo = entry.getKey();
}
}
first = false;
}
return geo;
}
}