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

import com.yahoo.search.schema.DocumentSummary;
import com.yahoo.search.schema.Schema;
import com.yahoo.slime.BinaryFormat;
import com.yahoo.data.access.Inspector;
import com.yahoo.slime.Slime;
import com.yahoo.data.access.slime.SlimeAdapter;
import com.yahoo.prelude.ConfigurationException;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Collection;
import java.util.Map;
import java.util.stream.Collectors;

import static com.yahoo.data.access.Type.OBJECT;

/**
 * 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 Map<String, DocsumDefinition> definitionsByName;

    public DocsumDefinitionSet(Schema schema) {
        this(schema.documentSummaries().values());
    }

    public DocsumDefinitionSet(Collection<DocumentSummary> docsumDefinitions) {
        this.definitionsByName = docsumDefinitions.stream()
                                                  .map(summary -> new DocsumDefinition(summary))
                                                  .collect(Collectors.toUnmodifiableMap(summary -> summary.name(),
                                                                                        summary -> summary));
    }

    /**
     * Returns the summary definition of the given name, or the default if not found.
     *
     * @throws ConfigurationException if the requested summary class is not found and there is none called "default"
     */
    public DocsumDefinition getDocsum(String summaryClass) {
        if (summaryClass == null)
            summaryClass = "default";
        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 (" + this + ")" +
                                             " (that is, you asked for something unknown, and no default was found)");
        return ds;
    }

    /** Do we have a summary definition with the given name */
    public boolean hasDocsum(String summaryClass) {
        if (summaryClass == null)
            summaryClass = "default";
        return definitionsByName.containsKey(summaryClass);
    }

    /**
     * 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
     * @return Error message or null on success.
     * @throws ConfigurationException if the summary class of this hit is missing
     */
    public String 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) {
            throw new IllegalArgumentException("Only expecting SchemaLess docsums - summary class:" + summaryClass + " hit:" + hit);
        }
        DocsumDefinition docsumDefinition = getDocsum(summaryClass);
        Slime value = BinaryFormat.decode(buffer.array(), buffer.arrayOffset()+buffer.position(), buffer.remaining());
        Inspector docsum = new SlimeAdapter(value.get());
        if (docsum.type() != OBJECT) {
            return "Hit " + hit + " failed: " + docsum.asString();
        }
        hit.addSummary(docsumDefinition, docsum);
        return null;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, DocsumDefinition> e : definitionsByName.entrySet() ) {
            if (sb.length() != 0) {
                sb.append(",");
            }
            sb.append("[").append(e.getKey()).append(",").append(e.getValue().name()).append("]");
        }
        return sb.toString();
    }

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

}