1
|
package eu.dnetlib.data.mapreduce.hbase.index.config;
|
2
|
|
3
|
import java.io.StringReader;
|
4
|
import java.util.HashMap;
|
5
|
import java.util.List;
|
6
|
import java.util.Map;
|
7
|
import java.util.Set;
|
8
|
|
9
|
import com.google.common.collect.Maps;
|
10
|
import com.google.common.collect.Sets;
|
11
|
import com.google.gson.Gson;
|
12
|
import com.typesafe.config.Config;
|
13
|
import com.typesafe.config.ConfigFactory;
|
14
|
import com.typesafe.config.ConfigObject;
|
15
|
import eu.dnetlib.data.mapreduce.OptionalConfig;
|
16
|
import eu.dnetlib.data.mapreduce.util.RelDescriptor;
|
17
|
import eu.dnetlib.data.proto.TypeProtos.Type;
|
18
|
import org.apache.commons.collections.CollectionUtils;
|
19
|
|
20
|
public class IndexConfig extends OptionalConfig {
|
21
|
|
22
|
public static IndexConfig load(final String s) {
|
23
|
return new IndexConfig(ConfigFactory.parseReader(new StringReader(s)));
|
24
|
}
|
25
|
|
26
|
public IndexConfig(final Config config) {
|
27
|
super(config);
|
28
|
}
|
29
|
|
30
|
@SuppressWarnings("unchecked")
|
31
|
public EntityConfigTable getConfigMap() {
|
32
|
final ConfigObject modelMap = getConfig().getObject("index.conf");
|
33
|
final Map<String, EntityConfig> map = Maps.transformValues(modelMap, value -> {
|
34
|
|
35
|
final HashMap<RelDescriptor, LinkDescriptor> links = Maps.newHashMap();
|
36
|
for (final Object o : (List<?>) ((Map<String, ?>) value.unwrapped()).get("links")) {
|
37
|
final Map<String, ?> map1 = (Map<String, ?>) o;
|
38
|
|
39
|
final RelDescriptor relDescriptor = new RelDescriptor((String) map1.get("relType"));
|
40
|
final Set<String> fields = Sets.newHashSet((List<String>) map1.get("fields"));
|
41
|
final Type target = Type.valueOf((String) map1.get("targetEntity"));
|
42
|
final Boolean isChild = ((String) map1.get("expandAs")).equals("child");
|
43
|
final Boolean symmetric = (Boolean) map1.get("symmetric");
|
44
|
|
45
|
final LinkDescriptor ld = new LinkDescriptor(relDescriptor, target, isChild, symmetric, fields);
|
46
|
|
47
|
if (map1.containsKey("max")) {
|
48
|
ld.setMax((Integer) map1.get("max"));
|
49
|
}
|
50
|
|
51
|
links.put(relDescriptor, ld);
|
52
|
}
|
53
|
final List<String> includeFields = (List<String>) ((Map<String, ?>) value.unwrapped()).get("includeFields");
|
54
|
final List<String> excludeFields = (List<String>) ((Map<String, ?>) value.unwrapped()).get("excludeFields");
|
55
|
if(CollectionUtils.isNotEmpty(includeFields) & CollectionUtils.isNotEmpty(excludeFields)){
|
56
|
throw new RuntimeException("Cannot create the index configuration: includeFields and excludeFields are both not null");
|
57
|
}
|
58
|
return new EntityConfig((Boolean) ((Map<String, ?>) value.unwrapped()).get("dups"), links, includeFields, excludeFields);
|
59
|
});
|
60
|
|
61
|
final EntityConfigTable res = new EntityConfigTable();
|
62
|
for (final String key : map.keySet()) {
|
63
|
res.put(Type.valueOf(key), map.get(key));
|
64
|
}
|
65
|
return res;
|
66
|
}
|
67
|
|
68
|
@Override
|
69
|
public String toString() {
|
70
|
return new Gson().toJson(this);
|
71
|
}
|
72
|
|
73
|
}
|