summaryrefslogtreecommitdiffstats
path: root/vespa-feed-client/src/main/java/ai/vespa/feed/client/DocumentId.java
blob: 5474bcfda01c6817f83324943202bd430f498763 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.feed.client;

import java.util.Objects;
import java.util.Optional;
import java.util.OptionalLong;

import static java.util.Objects.requireNonNull;

/**
 * Represents a Vespa document id
 *
 * @author jonmv
 */
public class DocumentId {

    private final String documentType;
    private final String namespace;
    private final OptionalLong number;
    private final Optional<String> group;
    private final String userSpecific;

    private DocumentId(String documentType, String namespace, OptionalLong number, Optional<String> group, String userSpecific) {
        this.documentType = requireNonNull(documentType);
        this.namespace = requireNonNull(namespace);
        this.number = requireNonNull(number);
        this.group = requireNonNull(group);
        this.userSpecific = requireNonNull(userSpecific);
    }

    public static DocumentId of(String namespace, String documentType, String userSpecific) {
        return new DocumentId(documentType, namespace, OptionalLong.empty(), Optional.empty(), userSpecific);
    }

    public static DocumentId of(String namespace, String documentType, long number, String userSpecific) {
        return new DocumentId(documentType, namespace, OptionalLong.of(number), Optional.empty(), userSpecific);
    }

    public static DocumentId of(String namespace, String documentType, String group, String userSpecific) {
        return new DocumentId(documentType, namespace, OptionalLong.empty(), Optional.of(group), userSpecific);
    }

    public static DocumentId of(String serialized) {
        DocumentId parsed = parse(serialized);
        if (parsed != null) return parsed;
        throw new IllegalArgumentException("Document ID must be on the form " +
                                           "'id:<namespace>:<document-type>:[n=<number>|g=<group>]:<user-specific>', " +
                                           "but was '" + serialized + "'");
    }

    private static DocumentId parse(String serialized) {
        int i, j = -1;
        if ((j = serialized.indexOf(':', i = j + 1)) < i) return null;
        if ( ! "id".equals(serialized.substring(i, j))) return null;
        if ((j = serialized.indexOf(':', i = j + 1)) <= i) return null;
        String namespace = serialized.substring(i, j);
        if ((j = serialized.indexOf(':', i = j + 1)) <= i) return null;
        String documentType = serialized.substring(i, j);
        if ((j = serialized.indexOf(':', i = j + 1)) < i) return null;
        String group = serialized.substring(i, j);
        if (serialized.length() <= (i = j + 1)) return null;
        String userSpecific = serialized.substring(i);
        if (group.startsWith("n=") && group.length() > 2)
            return DocumentId.of(namespace, documentType, Long.parseLong(group.substring(2)), userSpecific);
        if (group.startsWith("g=") && group.length() > 2)
            return DocumentId.of(namespace, documentType, group.substring(2), userSpecific);
        if (group.isEmpty())
            return DocumentId.of(namespace, documentType, userSpecific);
        return null;
    }

    public String documentType() {
        return documentType;
    }

    public String namespace() {
        return namespace;
    }

    public OptionalLong number() {
        return number;
    }

    public Optional<String> group() {
        return group;
    }

    public String userSpecific() {
        return userSpecific;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        DocumentId that = (DocumentId) o;
        return documentType.equals(that.documentType) && namespace.equals(that.namespace) && number.equals(that.number) && group.equals(that.group) && userSpecific.equals(that.userSpecific);
    }

    @Override
    public int hashCode() {
        return Objects.hash(documentType, namespace, number, group, userSpecific);
    }

    @Override
    public String toString() {
        return "id:" + namespace + ":" + documentType + ":" +
               (number.isPresent() ? "n=" + number.getAsLong() : group.map("g="::concat).orElse("")) +
               ":" + userSpecific;
    }

}