aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/main/java/com/yahoo/searchlib/expression/RawBucketResultNode.java
blob: 6d995bc1b340e8811f92e3fd155972eee7243a4d (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchlib.expression;

import com.yahoo.vespa.objects.Deserializer;
import com.yahoo.vespa.objects.ObjectVisitor;
import com.yahoo.vespa.objects.Serializer;

/**
 * @author baldersheim
 */
public class RawBucketResultNode extends BucketResultNode {

    // The global class identifier shared with C++.
    public static final int classId = registerClass(0x4000 + 125, RawBucketResultNode.class, RawBucketResultNode::new);

    // bucket start, inclusive
    private ResultNode from = RawResultNode.getNegativeInfinity();

    // bucket end, exclusive
    private ResultNode to = RawResultNode.getNegativeInfinity();

    @Override
    public boolean empty() {
        return to.equals(from);
    }

    public RawBucketResultNode() {}

    /**
     * Create a bucket with the given limits
     *
     * @param from bucket start
     * @param to   bucket end
     */
    public RawBucketResultNode(ResultNode from, ResultNode to) {
        this.from = from;
        this.to = to;
    }

    /**
     * Obtain the bucket start
     *
     * @return bucket start
     */
    public byte[] getFrom() {
        return from.getRaw();
    }

    /**
     * Obtain the bucket end
     *
     * @return bucket end
     */
    public byte[] getTo() {
        return to.getRaw();
    }

    @Override
    protected int onGetClassId() {
        return classId;
    }

    @Override
    protected void onSerialize(Serializer buf) {
        serializeOptional(buf, from);
        serializeOptional(buf, to);
    }

    @Override
    protected void onDeserialize(Deserializer buf) {
        from = (ResultNode)deserializeOptional(buf);
        to = (ResultNode)deserializeOptional(buf);
    }

    @Override
    protected int onCmp(ResultNode rhs) {
        if (classId != rhs.getClassId()) {
            return (classId - rhs.getClassId());
        }
        RawBucketResultNode b = (RawBucketResultNode)rhs;
        int diff = from.compareTo(b.from);
        return (diff == 0) ? to.compareTo(b.to) : diff;
    }

    @Override
    public int hashCode() {
        return super.hashCode() + from.hashCode() + to.hashCode();
    }

    @Override
    public void visitMembers(ObjectVisitor visitor) {
        super.visitMembers(visitor);
        visitor.visit("from", from);
        visitor.visit("to", to);
    }
}