Project

General

Profile

1
package eu.dnetlib.data.mapreduce.hbase.propagation;
2

    
3
import com.google.protobuf.InvalidProtocolBufferException;
4
import eu.dnetlib.data.proto.*;
5
import org.apache.hadoop.hbase.client.Result;
6
import org.apache.hadoop.hbase.util.Bytes;
7
import org.apache.hadoop.mapreduce.Mapper;
8

    
9
import java.util.HashSet;
10
import java.util.Map;
11
import java.util.Objects;
12
import java.util.Set;
13
import java.util.stream.Collectors;
14

    
15
import static eu.dnetlib.data.mapreduce.hbase.propagation.PropagationConstants.*;
16
import static eu.dnetlib.data.mapreduce.hbase.propagation.PropagationConstants.DATA_INFO_TYPE;
17

    
18
public final class Utils {
19
    public static  OafProtos.OafEntity getEntity(Result value, TypeProtos.Type type) throws InvalidProtocolBufferException {
20
        final Map<byte[],byte[]> map = value.getFamilyMap(Bytes.toBytes(type.toString()));
21

    
22
        final byte[] body = map.get(Bytes.toBytes("body"));
23
        if (body != null){
24
            OafProtos.Oaf oaf = OafProtos.Oaf.parseFrom(body);
25
            if(oaf.getDataInfo().getDeletedbyinference())
26
                return null;
27
            return oaf.getEntity();
28
        }
29
        return null;
30
    }
31

    
32
    public static FieldTypeProtos.DataInfo.Builder getDataInfo(String trust, String class_id,String schema_id,String schema_name, String data_info_type,String class_name) {
33
        FieldTypeProtos.DataInfo.Builder builder = FieldTypeProtos.DataInfo.newBuilder()
34
                .setInferred(true)
35
                .setProvenanceaction(getQualifier(class_id,schema_id,schema_name,class_name)                        )
36
                .setInferenceprovenance(data_info_type)
37
                .setTrust(trust);
38
        return builder;
39

    
40
    }
41

    
42
    public static FieldTypeProtos.Qualifier.Builder getQualifier(String class_id, String schema_id, String schema_name, String class_name){
43
        return FieldTypeProtos.Qualifier.newBuilder()
44
                .setClassid(class_id)
45
                .setClassname(class_name)
46
                .setSchemeid(schema_id)
47
                .setSchemename(schema_name);
48
    }
49

    
50

    
51
    public static FieldTypeProtos.Qualifier.Builder getCountry(String countryValue, String trust,String dnet_schema, String class_id,String schema_id,String schema_name, String data_info_type, String class_name) {
52

    
53
        final FieldTypeProtos.Qualifier.Builder country = FieldTypeProtos.Qualifier.newBuilder()
54
                .setClassid(countryValue)
55
                .setClassname(countryValue)
56
                .setSchemeid(dnet_schema)
57
                .setSchemename(dnet_schema);
58
        country.setDataInfo(getDataInfo(trust,class_id,schema_id,schema_name,data_info_type,class_name));//"Propagation of country information from datasources belonging to institutional repositories"));
59

    
60
        return country;
61
    }
62

    
63
    public static  ResultProtos.Result.Context getContext(String contextid, String trust) {
64
        final ResultProtos.Result.Context.Builder cBuilder = ResultProtos.Result.Context.newBuilder()
65
                .setId(contextid)
66
                .addDataInfo(getDataInfo(trust, CLASS_COMMUNITY_ID, SCHEMA_ID, SCHEMA_NAME, DATA_INFO_TYPE, DATA_INFO_TYPE));
67
        return cBuilder.build();
68
    }
69

    
70
    public static  ResultProtos.Result.Context getContext(String contextid, String trust, String class_id, String dataInfoType, String class_name) {
71
        final ResultProtos.Result.Context.Builder cBuilder = ResultProtos.Result.Context.newBuilder()
72
                .setId(contextid)
73
                .addDataInfo(getDataInfo(trust, class_id, SCHEMA_ID, SCHEMA_NAME, dataInfoType, class_name));
74
        return cBuilder.build();
75
    }
76

    
77
    public static Set<String> getRelationTarget(Result value, String sem_rel, final Mapper.Context context, String counter) {
78

    
79
        final Map<byte[], byte[]> relationMap = value.getFamilyMap(Bytes.toBytes(sem_rel));
80

    
81
        /*
82
        we could extract the target qualifiers from the familyMap's keyset, but we also need to check the relationship is not deletedbyinference
83
        return relationMap.keySet().stream()
84
                .map(String::new)
85
                .collect(Collectors.toCollection(HashSet::new));
86
        */
87

    
88
        HashSet<String> valid_relation = relationMap.values().stream()
89
                .map(b -> asOaf(b))
90
                .filter(Objects::nonNull)
91
                .filter(o -> isValid(o))
92
                .filter(o -> !o.getDataInfo().getDeletedbyinference())
93
                .map(o -> o.getRel().getTarget())
94
                .collect(Collectors.toCollection(HashSet::new));
95

    
96
        context.getCounter(counter, sem_rel).increment(valid_relation.size());
97

    
98
        return valid_relation;
99
    }
100

    
101
    private static OafProtos.Oaf asOaf(byte[] r) {
102
        try {
103
            return OafProtos.Oaf.parseFrom(r);
104
        } catch (InvalidProtocolBufferException e) {
105
            return null;
106
        }
107
    }
108

    
109
    public static OafProtos.Oaf getUpdate(ResultProtos.Result.Metadata.Builder metadata, String resultId) {
110
        final ResultProtos.Result.Builder result = ResultProtos.Result.newBuilder().setMetadata(metadata);
111
        final OafProtos.OafEntity.Builder entity = OafProtos.OafEntity.newBuilder()
112
                .setType(TypeProtos.Type.result)
113
                .setId(resultId)
114
                .setResult(result);
115

    
116
        return OafProtos.Oaf.newBuilder()
117
                .setKind(KindProtos.Kind.entity)
118
                .setEntity(entity)
119
                .build();
120
    }
121

    
122
    private static boolean isValid(final OafProtos.Oaf oaf) {
123
        return (oaf != null) && oaf.isInitialized();
124
    }
125

    
126
}
(4-4/5)