aboutsummaryrefslogtreecommitdiffstats
path: root/config/src/main/java/com/yahoo/vespa/config/PayloadChecksum.java
blob: 177fb57116c61f45a1b135528ff86e8952b0b054 (plain) (blame)
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config;

import com.yahoo.text.AbstractUtf8Array;
import com.yahoo.vespa.config.protocol.Payload;
import com.yahoo.vespa.config.util.ConfigUtils;

import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.yahoo.vespa.config.PayloadChecksum.Type.MD5;
import static com.yahoo.vespa.config.PayloadChecksum.Type.XXHASH64;

/**
 * Checksums of config definition payload or config payload,
 * md5 and xxhash64 are the supported types at the moment.
 *
 * @author hmusum
 */
public class PayloadChecksum {

    private static final Pattern hexChecksumPattern = Pattern.compile("[0-9a-fA-F]+");

    private final String checksum;
    private final Type type;

    public PayloadChecksum(String checksum, Type type) {
        this.checksum = checksum;
        this.type = type;
    }

    public static PayloadChecksum empty(Type type) {
        return new PayloadChecksum("", type);
    }

    public static PayloadChecksum fromPayload(Payload payload, Type type) {
        switch (type) {
            case MD5: return fromMd5Data(payload.getData());
            case XXHASH64: return fromXxhash64Data(payload.getData());
            default: throw new IllegalArgumentException("Unknown type " + type);
        }
    }

    private static PayloadChecksum fromMd5Data(AbstractUtf8Array data) {
        return new PayloadChecksum(ConfigUtils.getMd5(data), MD5);
    }

    private static PayloadChecksum fromXxhash64Data(AbstractUtf8Array data) {
        return new PayloadChecksum(ConfigUtils.getXxhash64(data), XXHASH64);
    }

    public boolean isEmpty() {
        switch (type) {
            case MD5: return this.equals(empty(MD5));
            case XXHASH64: return this.equals(empty(XXHASH64));
            default: throw new IllegalArgumentException("Unknown type " + type);
        }
    }

    public String asString() { return checksum; }

    public Type type() { return type; }

    public enum Type {MD5, XXHASH64}

    public boolean valid() {
        if (checksum.equals("")) return true;  // Empty checksum is ok (e.g. when running 'vespa-get-config')

        Matcher m = hexChecksumPattern.matcher(checksum);
        return m.matches();
    }

    @Override
    public int hashCode() {
        return Objects.hash(checksum, type);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        PayloadChecksum that = (PayloadChecksum) o;
        return Objects.equals(checksum, that.checksum) && type == that.type;
    }

    @Override
    public String toString() {
        return type.name() + ":" + checksum;
    }
}