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

import com.yahoo.searchlib.aggregation.AggregationResult;
import com.yahoo.vespa.objects.Deserializer;
import com.yahoo.vespa.objects.ObjectVisitor;
import com.yahoo.vespa.objects.Serializer;

/**
 * This node holds the index of an ExpressionNode in an external array, and is used as a proxy in the back-end to allow
 * aggregators to be used in expressions.
 *
 * @author baldersheim
 */
public class AggregationRefNode extends ExpressionNode {

    public static final int classId = registerClass(0x4000 + 142, AggregationRefNode.class);
    private AggregationResult result = null;
    private int index = - 1;

    @SuppressWarnings("UnusedDeclaration")
    public AggregationRefNode() {
        // Used by deserializer.
    }

    public AggregationRefNode(int index) {
        this.index = index;
    }

    public AggregationRefNode(AggregationResult result) {
        this.result = result;
    }

    public AggregationResult getExpression() {
        return result;
    }

    public AggregationRefNode setExpression(AggregationResult result) {
        this.result = result;
        return this;
    }

    public AggregationRefNode setIndex(int index) {
        this.index = index;
        return this;
    }

    public int getIndex() {
        return index;
    }

    @Override
    public boolean onExecute() {
        return result.execute();
    }

    @Override
    public void onPrepare() {
        result.prepare();
    }

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

    @Override
    protected void onSerialize(Serializer buf) {
        super.onSerialize(buf);
        buf.putInt(null, index);
    }

    @Override
    protected void onDeserialize(Deserializer buf) {
        super.onDeserialize(buf);
        index = buf.getInt(null);
        result = null;
    }

    @Override
    public AggregationRefNode clone() {
        AggregationRefNode obj = (AggregationRefNode)super.clone();
        obj.index = this.index;
        obj.result = this.result.clone();
        return obj;
    }

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

    @Override
    public ResultNode getResult() {
        return result.getResult();
    }

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

    @Override
    public boolean equalsExpression(ExpressionNode obj) {
        AggregationRefNode rhs = (AggregationRefNode)obj;
        if (index != rhs.index) {
            return false;
        }
        if (!equals(result, rhs.result)) {
            return false;
        }
        return true;
    }
}