summaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/fastsearch/DocsumDefinitionSet.java
blob: 04667f9b53f1ef749edaed0c20c775d4b81259b2 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.fastsearch;

import com.yahoo.slime.BinaryFormat;
import com.yahoo.slime.Slime;
import com.yahoo.data.access.slime.SlimeAdapter;
import com.yahoo.vespa.config.search.SummaryConfig;
import com.yahoo.prelude.ConfigurationException;
import com.yahoo.container.search.LegacyEmulationConfig;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;

/**
 * A set of docsum definitions
 *
 * @author bratseth
 * @author Bjørn Borud
 */
public final class DocsumDefinitionSet {

    public static final int SLIME_MAGIC_ID = 0x55555555;
    private final static Logger log = Logger.getLogger(DocsumDefinitionSet.class.getName());

    private final HashMap<Long, DocsumDefinition> definitions = new HashMap<>();
    private final HashMap<String, DocsumDefinition> definitionsByName = new HashMap<>();
    private final LegacyEmulationConfig emulationConfig;

    public DocsumDefinitionSet(DocumentdbInfoConfig.Documentdb config) {
        this.emulationConfig = new LegacyEmulationConfig(new LegacyEmulationConfig.Builder());
        configure(config);
    }

    public DocsumDefinitionSet(DocumentdbInfoConfig.Documentdb config, LegacyEmulationConfig emulConfig) {
        this.emulationConfig = emulConfig;
        configure(config);
    }

    /** Returns a docsum definition by id
     * @param id document summary class id
     * @return a DocsumDefinition for the id, if found.
     */
    public final DocsumDefinition getDocsumDefinition(long id) {
        return definitions.get(new Long(id));
    }

    /**
     * Returns a docsum definition by name, or null if not found
     *
     * @param name the name of the summary class to use, or null to use the name "default"
     * @return the summary class found, or null if none
     */
    public final DocsumDefinition getDocsumDefinition(String name) {
        if (name == null)
            name="default";
        return definitionsByName.get(name);
    }

    /**
     * Makes data available for decoding for the given hit.
     *
     * @param summaryClass the requested summary class
     * @param data docsum data from backend
     * @param hit the Hit corresponding to this document summary
     * @throws ConfigurationException if the summary class of this hit is missing
     */
    public final void lazyDecode(String summaryClass, byte[] data, FastHit hit) {
        ByteBuffer buffer = ByteBuffer.wrap(data);
        buffer.order(ByteOrder.LITTLE_ENDIAN);
        long docsumClassId = buffer.getInt();
        if (docsumClassId != SLIME_MAGIC_ID) {
            log.warning("Only expecting SchemaLess docsums");
            // TODO: Not used, remove   - bratseth 2017-01-016
            DocsumDefinition docsumDefinition = lookupDocsum(docsumClassId);
            Docsum docsum = new Docsum(docsumDefinition, data);
            hit.addSummary(docsum);
        } else {
            DocsumDefinition docsumDefinition = lookupDocsum(summaryClass);
            Slime value = BinaryFormat.decode(buffer.array(), buffer.arrayOffset()+buffer.position(), buffer.remaining());
            hit.addSummary(docsumDefinition, new SlimeAdapter(value.get()));
        }
    }

    private DocsumDefinition lookupDocsum(long docsumClassId) {
        DocsumDefinition docsumDefinition = getDocsumDefinition(docsumClassId);
        if (docsumDefinition == null) {
            throw new ConfigurationException("Received hit with summary id " + docsumClassId +
                    ", but this summary class is not in current summary config (" + toString() + ")" +
                    " (that is, the system is in an inconsistent state)");
        }
        return docsumDefinition;
    }

    private DocsumDefinition lookupDocsum(String summaryClass) {
        DocsumDefinition ds = definitionsByName.get(summaryClass);
        if (ds == null) {
            ds = definitionsByName.get("default");
        }
        if (ds == null) {
            throw new ConfigurationException("Fetched hit with summary class " + summaryClass +
                    ", but this summary class is not in current summary config (" + toString() + ")" +
                    " (that is, you asked for something unknown, and no default was found)");
        }
        return ds;
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        Set<Map.Entry<Long, DocsumDefinition>> entrySet = definitions.entrySet();
        boolean first = true;
        for (Iterator<Map.Entry<Long, DocsumDefinition>> itr = entrySet.iterator(); itr.hasNext(); ) {
            if (!first) {
                sb.append(",");
            } else {
                first = false;
            }
            Map.Entry<Long, DocsumDefinition> entry = itr.next();
            sb.append("[").append(entry.getKey()).append(",").append(entry.getValue().getName()).append("]");
        }
        return sb.toString();
    }

    public int size() {
        return definitions.size();
    }

    private void configure(DocumentdbInfoConfig.Documentdb config) {
        for (int i = 0; i < config.summaryclass().size(); ++i) {
            DocumentdbInfoConfig.Documentdb.Summaryclass sc = config.summaryclass(i);
            DocsumDefinition docSumDef = new DocsumDefinition(sc, emulationConfig);
            definitions.put((long) sc.id(), docSumDef);
            definitionsByName.put(sc.name(), docSumDef);
        }
        if (definitions.size() == 0) {
            log.warning("No summary classes found in DocumentdbInfoConfig.Documentdb");
        }
    }
}