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

import com.yahoo.searchlib.expression.ResultNode;
import com.yahoo.searchlib.expression.SingleResultNode;
import com.yahoo.vespa.objects.Deserializer;
import com.yahoo.vespa.objects.ObjectVisitor;
import com.yahoo.vespa.objects.Serializer;

/**
 * This is an aggregated result holding the sum of the aggregating expression for all matching hits.
 *
 * @author baldersheim
 * @author Simon Thoresen Hult
 */
public class SumAggregationResult extends AggregationResult {

    public static final int classId = registerClass(0x4000 + 82, SumAggregationResult.class);
    private SingleResultNode sum;

    /**
     * Constructs an empty result node. <b>NOTE:</b> This instance is broken until non-optional member data is set.
     */
    public SumAggregationResult() {

    }

    /**
     * Constructs an instance of this class with given sum.
     *
     * @param sum The initial sum to set.
     */
    public SumAggregationResult(SingleResultNode sum) {
        setSum(sum);
    }

    /**
     * Returns the sum of all results in this.
     *
     * @return The numeric sum.
     */
    public final SingleResultNode getSum() {
        return sum;
    }

    /**
     * Sets the sum of all results in this.
     *
     * @param sum The sum to set.
     * @return This, to allow chaining.
     */
    public final SumAggregationResult setSum(SingleResultNode sum) {
        this.sum = sum;
        return this;
    }

    @Override
    public ResultNode getRank() {
        return sum;
    }

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

    @Override
    protected void onSerialize(Serializer buf) {
        super.onSerialize(buf);
        serializeOptional(buf, sum);
    }

    @Override
    protected void onDeserialize(Deserializer buf) {
        super.onDeserialize(buf);
        sum = (SingleResultNode)deserializeOptional(buf);
    }

    @Override
    protected void onMerge(AggregationResult result) {
        sum.add(((SumAggregationResult)result).sum);
    }

    @Override
    public SumAggregationResult clone() {
        SumAggregationResult obj = (SumAggregationResult)super.clone();
        if (sum != null) {
            obj.sum = (SingleResultNode)sum.clone();
        }
        return obj;
    }

    @Override
    protected boolean equalsAggregation(AggregationResult obj) {
        return equals(sum, ((SumAggregationResult)obj).sum);
    }

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