blob: 8ffc972072cf155a2e38fc07cf1101aea331d542 (
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
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.predicate.index.conjunction;
import com.yahoo.document.predicate.FeatureConjunction;
import com.yahoo.document.predicate.FeatureSet;
import com.yahoo.document.predicate.Negation;
import com.yahoo.document.predicate.Predicate;
import com.yahoo.search.predicate.index.Feature;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* IndexableFeatureConjunction is a post-processed {@link FeatureConjunction} which can be indexed by {@link ConjunctionIndex}.
*
* @author bjorncs
*/
public class IndexableFeatureConjunction {
/** Conjunction id */
public final long id;
/** K value - number of non-negated operands */
public final int k;
// Hashed features from non-negated feature sets.
public final Set<Long> features = new HashSet<>();
// Hash features from negated feature sets.
public final Set<Long> negatedFeatures = new HashSet<>();
public IndexableFeatureConjunction(FeatureConjunction conjunction) {
List<Predicate> operands = conjunction.getOperands();
int nNegatedFeatureSets = 0;
for (Predicate operand : operands) {
if (operand instanceof FeatureSet) {
addFeatures((FeatureSet)operand, features);
} else {
FeatureSet featureSet = (FeatureSet)((Negation) operand).getOperand();
addFeatures(featureSet, negatedFeatures);
++nNegatedFeatureSets;
}
}
id = calculateConjunctionId();
k = operands.size() - nNegatedFeatureSets;
}
private static void addFeatures(FeatureSet featureSet, Set<Long> features) {
String key = featureSet.getKey();
featureSet.getValues().forEach(value -> features.add(Feature.createHash(key, value)));
}
private long calculateConjunctionId() {
long posHash = 0;
for (long feature : features) {
posHash ^= feature;
}
long negHash = 0;
for (long feature : negatedFeatures) {
negHash ^= feature;
}
return (posHash + 3 * negHash) | 1;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
IndexableFeatureConjunction that = (IndexableFeatureConjunction) o;
return id == that.id;
}
@Override
public int hashCode() {
return (int) (id ^ (id >>> 32));
}
}
|