aboutsummaryrefslogtreecommitdiffstats
path: root/predicate-search-core/src/main/java/com/yahoo/document/predicate/FeatureRange.java
blob: 496e84fd4a5e9e3bf2681ce4132e17f3d421f42b (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document.predicate;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author Simon Thoresen Hult
 */
public class FeatureRange extends PredicateValue {

    private String key;
    private Long from;
    private Long to;
    private List<RangePartition> partitions;
    private List<RangeEdgePartition> edgePartitions;

    public FeatureRange(String key) {
        this(key, null, null);
    }

    public FeatureRange(String key, Long fromInclusive, Long toInclusive) {
        setKey(key);
        setFromInclusive(fromInclusive);
        setToInclusive(toInclusive);
        partitions = new ArrayList<>();
        edgePartitions = new ArrayList<>();
    }

    public FeatureRange setKey(String key) {
        Objects.requireNonNull(key, "key");
        this.key = key;
        return this;
    }

    public String getKey() {
        return key;
    }

    public FeatureRange setFromInclusive(Long from) {
        if (from != null && to != null && from > to) {
            throw new IllegalArgumentException("Expected 'from' less than or equal to " + to + ", got " + from + ".");
        }
        this.from = from;
        return this;
    }

    public Long getFromInclusive() {
        return from;
    }

    public FeatureRange setToInclusive(Long to) {
        if (from != null && to != null && from > to) {
            throw new IllegalArgumentException("Expected 'to' greater than or equal to " + from + ", got " + to + ".");
        }
        this.to = to;
        return this;
    }

    public Long getToInclusive() {
        return to;
    }

    public void addPartition(RangePartition p) {
        if (p instanceof RangeEdgePartition) {
            edgePartitions.add((RangeEdgePartition)p);
        } else {
            partitions.add(p);
        }
    }

    public List<RangeEdgePartition> getEdgePartitions() {
        return edgePartitions;
    }

    public List<RangePartition> getPartitions() {
        return partitions;
    }

    public void clearPartitions() {
        partitions.clear();
        edgePartitions.clear();
    }

    @Override
    public FeatureRange clone() throws CloneNotSupportedException {
        return (FeatureRange)super.clone();
    }

    @Override
    public int hashCode() {
        return ((key.hashCode() + (from != null ? from.hashCode() : 0)) * 31 + (to != null ? to.hashCode() : 0)) * 31;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }
        if (!(obj instanceof FeatureRange)) {
            return false;
        }
        FeatureRange rhs = (FeatureRange)obj;
        if (!key.equals(rhs.key)) {
            return false;
        }
        if (!Objects.equals(from, rhs.from)) {
            return false;
        }
        if (!Objects.equals(to, rhs.to)) {
            return false;
        }
        return partitions.equals(rhs.partitions) && edgePartitions.equals(rhs.edgePartitions);
    }

    @Override
    protected void appendTo(StringBuilder out) {
        appendQuotedTo(key, out);
        out.append(" in [");
        if (from != null) {
            out.append(from);
        }
        out.append("..");
        if (to != null) {
            out.append(to);
        }
        if (!partitions.isEmpty() || !edgePartitions.isEmpty()) {
            out.append(" (");
            for (RangeEdgePartition p : edgePartitions) {
                p.appendTo(out);
                out.append(',');
            }
            for (RangePartition p : partitions) {
                p.appendTo(out);
                out.append(',');
            }
            out.deleteCharAt(out.length() - 1);  // Remove extra ','
            out.append(")");
        }
        out.append("]");
    }

    public static FeatureRange buildFromMixedIn(String key, List<String> partitions, int arity) {
        Long fromInclusive = null;
        Long toInclusive = null;
        long from = 0;
        long to = 0;
        for (String p : partitions) {
            String[] parts = p.split(",");
            if (parts.length == 1) {
                String[] subparts = parts[0].split("=|-");
                int offset = subparts.length == 3? 0 : 1;
                if (subparts.length < 3 || subparts.length > 4) {
                    throw new IllegalArgumentException("MIXED_IN range partition must be on the form label=val-val");
                }
                from = Long.parseLong(subparts[offset + 1]);
                to = Long.parseLong(subparts[offset + 2]);
                if (parts[0].contains("=-")) {
                    long tmp = from;
                    from = -to;
                    to = -tmp;
                }
            } else {
                if (parts.length != 3) {
                    throw new IllegalArgumentException("MIXED_IN range edge partition must be on the form label=val,val,payload");
                }
                long value = Long.parseLong(parts[1]);
                long payload = Long.parseLong(parts[2]);
                if ((payload & 0xc0000000) == 0x80000000L) {
                    from = value + (payload & 0xffff);
                    to = value + arity - 1;
                } else if ((payload & 0xc0000000) == 0x40000000L) {
                    from = value;
                    to = value + (payload & 0xffff);
                } else {
                    from = value + (payload >> 16);
                    to = value + (payload & 0xffff);
                }
            }
            if (fromInclusive == null || fromInclusive > from) {
                fromInclusive = from;
            }
            if (toInclusive == null || toInclusive > to) {
                toInclusive = to;
            }
        }
        return new FeatureRange(key, fromInclusive, toInclusive);
    }

}