aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/fs4/QueryPacketData.java
blob: 673b9cc0c47a998f4ea4d6c6402b906664d11526 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.fs4;

import java.nio.ByteBuffer;

/**
 * Class for storing data which has to be constant between query and summary
 * fetch for a Vespa hit. Used to avoid to tagging Vespa summary hits with
 * the entire query as an immutable.
 *
 * @author Steinar Knutsen
 */
public final class QueryPacketData {

    private byte[] rankProfile = null;
    private int queryFlags = 0;
    private byte[] queryStack = null;
    private byte[] location = null;
    private byte[] propertyMaps = null;

    /**
     * Given src.position() bigger than startOfField, allocate a fresh byte
     * array, and copy the data from startOfField to src.position() into it.
     *
     * @param src
     *            the ByteBuffer to copy from
     * @param startOfField
     *            the position of the buffer at which the field starts
     * @return a copy of the data between startOfField and the buffer position
     *         before invokation
     * @throws IllegalArgumentException
     *             if startOfField is somewhere after src.position()
     */
    private byte[] copyField(final ByteBuffer src, final int startOfField) {
        if (startOfField > src.position()) {
            throw new IllegalArgumentException("startOfField after src.position()");
        }
        final byte[] dst = new byte[src.position() - startOfField];

        src.position(startOfField);
        src.get(dst);
        return dst;
    }

    ByteBuffer encodeRankProfile(final ByteBuffer buffer) {
        return buffer.put(rankProfile);
    }

    void setRankProfile(final ByteBuffer src, final int startOfField) {
        rankProfile = copyField(src, startOfField);
    }

    ByteBuffer encodeQueryFlags(final ByteBuffer buffer) {
        return buffer.putInt(queryFlags);
    }

    void setQueryFlags(final int queryFlags) {
        this.queryFlags = queryFlags;
    }

    ByteBuffer encodeQueryStack(final ByteBuffer buffer) {
        return buffer.put(queryStack);
    }

    void setQueryStack(final ByteBuffer src, final int startOfField) {
        queryStack = copyField(src, startOfField);
    }

    ByteBuffer encodePropertyMaps(final ByteBuffer buffer) {
        if (propertyMaps != null) {
            buffer.put(propertyMaps);
        }
        return buffer;
    }

    void setPropertyMaps(final ByteBuffer src, final int startOfField) {
        propertyMaps = copyField(src, startOfField);
    }

    void setLocation(final ByteBuffer src, final int startOfField) {
        this.location = copyField(src, startOfField);
    }

    ByteBuffer encodeLocation(final ByteBuffer buffer) {
        if (location != null) {
            buffer.put(location);
        }
        return buffer;
    }

}