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

import java.nio.ByteBuffer;

/**
 * A term which wraps a set of similar simple terms, all joined with {@code AND} or {@code OR}.
 * <p>
 * This class exists to encompass similarities in the serialization of such multi-term constructs.
 *
 * @author jonmv
 */
abstract class MultiTermItem extends SimpleTaggableItem {

    enum OperatorType {

        AND(0),
        OR(1);

        private final byte code;

        OperatorType(int code) { this.code = (byte) code; }

    }

    enum TermType {

        RANGES(0);

        private final byte code;

        TermType(int code) { this.code = (byte) code; }

    }

    /** The operator used to join all wrapped terms. */
    abstract OperatorType operatorType();

    /** The term type of the wrapped terms. */
    abstract TermType termType();

    /** The number of wrapped terms. */
    abstract int terms();

    /** Encode term type and common properties to the buffer. */
    abstract void encodeBlueprint(ByteBuffer buffer);

    /** Encode all wrapped terms to the buffer. */
    abstract void encodeTerms(ByteBuffer buffer);

    abstract Item asCompositeItem();

    @Override
    public final ItemType getItemType() {
        return ItemType.MULTI_TERM;
    }

    @Override
    public final String getName() {
        return getItemType().name();
    }

    @Override
    public final int encode(ByteBuffer buffer) {
        // TODO: Remove once backend support deserialisation of this type.
        if (getClass() == MultiRangeItem.class) return asCompositeItem().encode(buffer);

        super.encodeThis(buffer);
        byte metadata = 0;
        metadata |= (byte)((byte)(operatorType().code << 5) & (byte)0b11100000);
        metadata |= (byte)(termType().code & (byte)0b00011111);
        buffer.put(metadata);
        buffer.putInt(terms());
        encodeBlueprint(buffer);
        encodeTerms(buffer);
        return 1;
    }

    @Override
    public final int getTermCount() {
        return 1;
    }

}