aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/main/java/com/yahoo/searchlib/aggregation/GroupingLevel.java
blob: 0db933966a8fbb805513508bcaefa0c0c8a750db (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// 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.ExpressionNode;
import com.yahoo.vespa.objects.Deserializer;
import com.yahoo.vespa.objects.Identifiable;
import com.yahoo.vespa.objects.ObjectVisitor;
import com.yahoo.vespa.objects.Serializer;

public class GroupingLevel extends Identifiable {

    // The global class identifier shared with C++.
    public static final int classId = registerClass(0x4000 + 93, GroupingLevel.class);

    // The maximum number of groups allowed at this level.
    private long maxGroups = -1;

    // The precision used for estimation. This is number of groups returned up when using orderby that need more info to get it correct.
    private long precision = -1;

    // The classifier expression; the result of this is the group key.
    private ExpressionNode classify = null;

    // The prototype of the groups to create for each class.
    private Group collect = new Group();

    /** Returns the precision (i.e number of groups) returned up from this level. */
    public long getPrecision() {
        return precision;
    }

    /** Returns the maximum number of groups allowed at this level. */
    public long getMaxGroups() {
        return maxGroups;
    }

    /** Sets the maximum number of groups allowed at this level. */
    public GroupingLevel setMaxGroups(long max) {
        maxGroups = max;
        if (precision < maxGroups) {
            precision = maxGroups;
        }
        return this;
    }

    /**
     * Sets the precision (i.e number of groups) returned up from this level.
     *
     * @param precision the precision to set
     * @return this, to allow chaining
     */
    public GroupingLevel setPrecision(long precision) {
        this.precision = precision;
        return this;
    }

    /** Returns the expression used to classify hits into groups. */
    public ExpressionNode getExpression() {
        return classify;
    }

    /** Sets the expression used to classify hits into groups. */
    public GroupingLevel setExpression(ExpressionNode exp) {
        classify = exp;
        return this;
    }

    /**
     * <p>Sets the prototype to use when creating groups at this level.</p>
     *
     * @param group The group prototype.
     * @return This, to allow chaining.
     */
    public GroupingLevel setGroupPrototype(Group group) {
        this.collect = group;
        return this;
    }

    /**
     * <p>Returns the prototype to use when creating groups at this level.</p>
     *
     * @return The group prototype.
     */
    public Group getGroupPrototype() {
        return collect;
    }

    /**
     * <p>Tell if ordering will need results collected in children.</p>
     *
     * @return If deeper resultcollection is needed.
     */
    public boolean needResultCollection() {
        return !collect.isRankedByRelevance();
    }

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

    @Override
    protected void onSerialize(Serializer buf) {
        buf.putLong(null, maxGroups);
        buf.putLong(null, precision);
        serializeOptional(buf, classify);
        collect.serializeWithId(buf);
    }

    @Override
    protected void onDeserialize(Deserializer buf) {
        maxGroups = buf.getLong(null);
        precision = buf.getLong(null);
        classify = (ExpressionNode)deserializeOptional(buf);
        collect.deserializeWithId(buf);
    }

    @Override
    public int hashCode() {
        return super.hashCode() + (int)maxGroups + (int)precision + collect.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (!super.equals(obj)) {
            return false;
        }
        GroupingLevel rhs = (GroupingLevel)obj;
        if (maxGroups != rhs.maxGroups) {
            return false;
        }
        if (precision != rhs.precision) {
            return false;
        }
        if (!equals(classify, rhs.classify)) {
            return false;
        }
        if (!collect.equals(rhs.collect)) {
            return false;
        }
        return true;
    }

    @Override
    public GroupingLevel clone() {
        GroupingLevel obj = (GroupingLevel)super.clone();
        if (classify != null) {
            obj.classify = classify.clone();
        }
        obj.collect = collect.clone();
        return obj;
    }

    @Override
    public void visitMembers(ObjectVisitor visitor) {
        super.visitMembers(visitor);
        visitor.visit("maxGroups", maxGroups);
        visitor.visit("precision", precision);
        visitor.visit("classify", classify);
        visitor.visit("collect", collect);
    }
}