summaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/fastsearch/DocsumDefinition.java
blob: bef0069d5257f54893605b7a0ffe99a1ccd55f55 (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.fastsearch;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.yahoo.vespa.config.search.SummaryConfig;
import com.yahoo.container.search.LegacyEmulationConfig;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A docsum definition which knows how to decode a certain class of document
 * summaries. The docsum definition has a name and a list of field definitions
 *
 * @author bratseth
 * @author Bjørn Borud
 */
public class DocsumDefinition {

    private String name;
    private final List<DocsumField> fields;

    /** True if this contains dynamic fields */
    private boolean dynamic = false;

    // Mapping between field names and their index in this.fields
    private final Map<String,Integer> fieldNameToIndex;

    DocsumDefinition(DocumentdbInfoConfig.Documentdb.Summaryclass config, LegacyEmulationConfig emulConfig) {
        this.name = config.name();
        List<DocsumField> fieldsBuilder = new ArrayList<>();
        Map<String,Integer> fieldNameToIndexBuilder = new HashMap<>();

        for (DocumentdbInfoConfig.Documentdb.Summaryclass.Fields field : config.fields()) {
            // no, don't switch the order of the two next lines :)
            fieldNameToIndexBuilder.put(field.name(), fieldsBuilder.size());
            fieldsBuilder.add(DocsumField.create(field.name(), field.type(), emulConfig));
            if (field.dynamic())
                dynamic = true;
        }
        fields = ImmutableList.copyOf(fieldsBuilder);
        fieldNameToIndex = ImmutableMap.copyOf(fieldNameToIndexBuilder);
    }

    /** Returns the field at this index, or null if none */
    public DocsumField getField(int fieldIndex) {
        if (fieldIndex >= fields.size()) return null;
        return fields.get(fieldIndex);
    }

    /** Returns the index of a field name */
    public Integer getFieldIndex(String fieldName) {
        return fieldNameToIndex.get(fieldName);
    }

    @Override
    public String toString() {
        return "docsum definition '" + getName() + "'";
    }

    public String getName() {
        return name;
    }

    public int getFieldCount() {
        return fields.size();
    }

    public List<DocsumField> getFields() {
        return fields;
    }

    /** Returns whether this summary contains one or more dynamic fields */
    public boolean isDynamic() {
        return dynamic;
    }

}