// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.predicate;
import com.google.common.annotations.Beta;
import java.util.ArrayList;
import java.util.List;
/**
* Represents a query in the form of a set of boolean variables that are considered true.
*
* @author Magnar Nedland
* @author bjorncs
*/
@Beta
public class PredicateQuery {
private final ArrayList features = new ArrayList<>();
private final ArrayList rangeFeatures = new ArrayList<>();
/**
* Adds a feature to the query.
* @param key Feature key
* @param value Feature value
*/
public void addFeature(String key, String value) {
addFeature(key, value, SubqueryBitmap.DEFAULT_VALUE);
}
/**
* Adds a feature to the query, e.g. gender = male.
* @param key Feature key
* @param value Feature value
* @param subqueryBitMap The subquery bitmap for which this term is true
*/
public void addFeature(String key, String value, long subqueryBitMap) {
features.add(new Feature(key, value, subqueryBitMap));
}
public void addRangeFeature(String key, long value) { addRangeFeature(key, value, SubqueryBitmap.DEFAULT_VALUE);}
/**
* Adds a range feature to the query, e.g. age = 25.
* @param key Feature key
* @param value Feature value
* @param subqueryBitMap The subquery bitmap for which this term is true
*/
public void addRangeFeature(String key, long value, long subqueryBitMap) {
rangeFeatures.add(new RangeFeature(key, value, subqueryBitMap));
}
/**
* @return A list of features
*/
public List getFeatures() { return features; }
/**
* @return A list of range features
*/
public List getRangeFeatures() { return rangeFeatures; }
public static class Feature {
public final String key;
public final String value;
public final long subqueryBitmap;
public final long featureHash;
public Feature(String key, String value, long subqueryBitmap) {
this.featureHash = com.yahoo.search.predicate.index.Feature.createHash(key, value);
this.subqueryBitmap = subqueryBitmap;
this.value = value;
this.key = key;
}
}
public static class RangeFeature {
public final String key;
public final long value;
public final long subqueryBitmap;
public RangeFeature(String key, long value, long subqueryBitmap) {
this.key = key;
this.value = value;
this.subqueryBitmap = subqueryBitmap;
}
}
}