aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/schema/Schema.java
blob: ed56d0e506f15e0bcacca170c529e1e639c4e60b (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.schema;

import com.yahoo.api.annotations.Beta;

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

/**
 * Information about a schema which is part of the application running this.
 *
 * This is immutable.
 *
 * @author bratseth
 */
@Beta
public class Schema {

    private final String name;
    private final Map<String, Field> fields;
    private final Map<String, FieldSet> fieldSets;
    private final Map<String, RankProfile> rankProfiles;
    private final Map<String, DocumentSummary> documentSummaries;

    /** Fields indexed by both name and aliases. */
    private final Map<String, Field> fieldsByAliases;

    private Schema(Builder builder) {
        this.name = builder.name;
        this.fields = Collections.unmodifiableMap(builder.fields);
        this.fieldSets = Collections.unmodifiableMap(builder.fieldSets);
        this.rankProfiles = Collections.unmodifiableMap(builder.rankProfiles);
        this.documentSummaries = Collections.unmodifiableMap(builder.documentSummaries);

        fieldSets.values().forEach(fieldSet -> fieldSet.setSchema(this));
        rankProfiles.values().forEach(rankProfile -> rankProfile.setSchema(this));

        fieldsByAliases = new HashMap<>();
        for (Field field : fields.values()) {
            fieldsByAliases.put(field.name(), field);
            field.aliases().forEach(alias -> fieldsByAliases.put(alias, field));
        }
    }

    public String name() { return name; }
    public Map<String, Field> fields() { return fields; }
    public Map<String, RankProfile> rankProfiles() { return rankProfiles; }
    public Map<String, DocumentSummary> documentSummaries() { return documentSummaries; }

    /**
     * Looks up a field or field set by the given name or alias in this schema.
     *
     * @param fieldName the name or alias of the field or field set. If this is empty, the name "default" is looked up
     * @return information about the field or field set with the given name, or empty if no item with this name exists
     */
    public Optional<FieldInfo> fieldInfo(String fieldName) {
        if (fieldName.isEmpty())
            fieldName = "default";
        Field field = fieldsByAliases.get(fieldName);
        if (field != null) return Optional.of(field);
        return Optional.ofNullable(fieldSets.get(fieldName));
    }

    @Override
    public boolean equals(Object o) {
        if (o == this) return true;
        if ( ! (o instanceof Schema other)) return false;
        if ( ! other.name.equals(this.name)) return false;
        if ( ! other.fields.equals(this.fields)) return false;
        if ( ! other.fieldSets.equals(this.fieldSets)) return false;
        if ( ! other.rankProfiles.equals(this.rankProfiles)) return false;
        if ( ! other.documentSummaries.equals(this.documentSummaries)) return false;
        return true;
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, rankProfiles, documentSummaries);
    }

    @Override
    public String toString() {
        return "schema '" + name + "'";
    }

    public static class Builder {

        private final String name;
        private final Map<String, Field> fields = new LinkedHashMap<>();
        private final Map<String, FieldSet> fieldSets = new LinkedHashMap<>();
        private final Map<String, RankProfile> rankProfiles = new LinkedHashMap<>();
        private final Map<String, DocumentSummary> documentSummaries = new LinkedHashMap<>();

        public Builder(String name) {
            this.name = Objects.requireNonNull(name);
        }

        public Builder add(Field field) {
            fields.put(field.name(), field);
            return this;
        }

        public Builder add(FieldSet fieldSet) {
            fieldSets.put(fieldSet.name(), fieldSet);
            return this;
        }

        public Builder add(RankProfile profile) {
            rankProfiles.put(profile.name(), profile);
            return this;
        }

        public Builder add(DocumentSummary documentSummary) {
            documentSummaries.put(documentSummary.name(), documentSummary);
            return this;
        }

        public Schema build() {
            return new Schema(this);
        }

    }

}