diff options
Diffstat (limited to 'container-search/src/main/java/com/yahoo/prelude/query/PredicateQueryItem.java')
-rw-r--r-- | container-search/src/main/java/com/yahoo/prelude/query/PredicateQueryItem.java | 129 |
1 files changed, 94 insertions, 35 deletions
diff --git a/container-search/src/main/java/com/yahoo/prelude/query/PredicateQueryItem.java b/container-search/src/main/java/com/yahoo/prelude/query/PredicateQueryItem.java index da94070c94f..91a0ea4eb51 100644 --- a/container-search/src/main/java/com/yahoo/prelude/query/PredicateQueryItem.java +++ b/container-search/src/main/java/com/yahoo/prelude/query/PredicateQueryItem.java @@ -6,6 +6,8 @@ import com.yahoo.compress.IntegerCompressor; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.Collection; +import java.util.List; +import java.util.Objects; /** * A PredicateQueryItem is a collection of feature/value-pairs @@ -18,30 +20,26 @@ import java.util.Collection; public class PredicateQueryItem extends SimpleTaggableItem { private String fieldName = "predicate"; - private ArrayList<Entry> features = new ArrayList<>(); - private ArrayList<RangeEntry> rangeFeatures = new ArrayList<>(); + private List<Entry> features = new ArrayList<>(); + private List<RangeEntry> rangeFeatures = new ArrayList<>(); public static final long ALL_SUB_QUERIES = 0xffffffffffffffffL; - /** - * Sets the field name to be used for the predicates. - * @param index name of the field. - */ + /** Sets the name of the index (field) to be used for the predicates. */ @Override public void setIndexName(String index) { this.fieldName = index; } - /** - * @return the field name used for the predicates. - */ + /** Returns the name of the index (field) used for the predicates. */ public String getIndexName() { return fieldName; } /** * Adds a feature/value-pair to the predicate query. This feature is applied to all sub queries. - * @param key name of the feature to be set in this query. - * @param value value of the feature. + * + * @param key name of the feature to be set in this query + * @param value value of the feature */ public void addFeature(String key, String value) { addFeature(key, value, ALL_SUB_QUERIES); @@ -49,9 +47,10 @@ public class PredicateQueryItem extends SimpleTaggableItem { /** * Adds a feature/value-pair to the predicate query. - * @param key name of the feature to be set in this query. - * @param value value of the feature. - * @param subQueryBitmap bitmap specifying which sub queries this feature applies to. + * + * @param key name of the feature to be set in this query + * @param value value of the feature + * @param subQueryBitmap bitmap specifying which sub queries this feature applies to */ public void addFeature(String key, String value, long subQueryBitmap) { addFeature(new Entry(key, value, subQueryBitmap)); @@ -59,7 +58,8 @@ public class PredicateQueryItem extends SimpleTaggableItem { /** * Adds a feature/value-pair to the predicate query. - * @param entry the feature to add. + * + * @param entry the feature to add */ public void addFeature(Entry entry) { features.add(entry); @@ -68,8 +68,9 @@ public class PredicateQueryItem extends SimpleTaggableItem { /** * Adds a range feature with a given value to the predicate query. * This feature is applied to all sub queries. - * @param key name of the feature to be set in this query. - * @param value value of the feature. + * + * @param key name of the feature to be set in this query + * @param value value of the feature */ public void addRangeFeature(String key, long value) { addRangeFeature(key, value, ALL_SUB_QUERIES); @@ -77,9 +78,10 @@ public class PredicateQueryItem extends SimpleTaggableItem { /** * Adds a range feature with a given value to the predicate query. - * @param key name of the feature to be set in this query. - * @param value value of the feature. - * @param subQueryBitmap bitmap specifying which sub queries this feature applies to. + * + * @param key name of the feature to be set in this query + * @param value value of the feature + * @param subQueryBitmap bitmap specifying which sub queries this feature applies to */ public void addRangeFeature(String key, long value, long subQueryBitmap) { addRangeFeature(new RangeEntry(key, value, subQueryBitmap)); @@ -87,22 +89,19 @@ public class PredicateQueryItem extends SimpleTaggableItem { /** * Adds a range feature with a given value to the predicate query. - * @param entry the feature to add. + * + * @param entry the feature to add */ public void addRangeFeature(RangeEntry entry) { rangeFeatures.add(entry); } - /** - * @return a mutable collection of feature entries. - */ + /** Returns a mutable collection of feature entries. */ public Collection<Entry> getFeatures() { return features; } - /** - * @return a mutable collection of range feature entries. - */ + /** Returns a mutable collection of range feature entries. */ public Collection<RangeEntry> getRangeFeatures() { return rangeFeatures; } @@ -126,7 +125,7 @@ public class PredicateQueryItem extends SimpleTaggableItem { return 1; // number of encoded stack dump items } - private void encodeFeatures(ArrayList<? extends EntryBase> features, ByteBuffer buffer) { + private void encodeFeatures(List<? extends EntryBase> features, ByteBuffer buffer) { IntegerCompressor.putCompressedPositiveNumber(features.size(), buffer); for (EntryBase e : features) { e.encode(buffer); @@ -173,9 +172,26 @@ public class PredicateQueryItem extends SimpleTaggableItem { return clone; } + @Override + public boolean equals(Object o) { + if ( ! super.equals(o)) return false; + var other = (PredicateQueryItem)o; + if ( ! this.fieldName.equals(other.fieldName)) return false; + if ( ! this.features.equals(other.features)) return false; + if ( ! this.rangeFeatures.equals(other.rangeFeatures)) return false; + return true; + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), fieldName, features, rangeFeatures); + } + + /** An entry in a predicate item. This is immutable. */ public abstract static class EntryBase { - private String key; - private long subQueryBitmap; + + private final String key; + private final long subQueryBitmap; public EntryBase(String key, long subQueryBitmap) { this.key = key; @@ -190,15 +206,29 @@ public class PredicateQueryItem extends SimpleTaggableItem { return subQueryBitmap; } - public void setSubQueryBitmap(long subQueryBitmap) { - this.subQueryBitmap = subQueryBitmap; + public abstract void encode(ByteBuffer buffer); + + @Override + public boolean equals(Object o) { + if ( ! super.equals(o)) return false; + + var other = (EntryBase)o; + if ( ! Objects.equals(this.key, other.key)) return false; + if ( this.subQueryBitmap != other.subQueryBitmap) return false; + return true; + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), key, subQueryBitmap); } - public abstract void encode(ByteBuffer buffer); } + /** A unique entry in a predicate item. This is immutable. */ public static class Entry extends EntryBase { - private String value; + + private final String value; public Entry(String key, String value) { this(key, value, ALL_SUB_QUERIES); @@ -218,10 +248,25 @@ public class PredicateQueryItem extends SimpleTaggableItem { putString(getValue(), buffer); buffer.putLong(getSubQueryBitmap()); } + + @Override + public boolean equals(Object other) { + if ( ! super.equals(other)) return false; + if ( ! Objects.equals(this.value, ((Entry)other).value)) return false; + return true; + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), value); + } + } + /** A range entry in a predicate item. This is immutable. */ public static class RangeEntry extends EntryBase { - private long value; + + private final long value; public RangeEntry(String key, long value) { this(key, value, ALL_SUB_QUERIES); @@ -242,5 +287,19 @@ public class PredicateQueryItem extends SimpleTaggableItem { buffer.putLong(getValue()); buffer.putLong(getSubQueryBitmap()); } + + @Override + public boolean equals(Object other) { + if ( ! super.equals(other)) return false; + if ( this.value != ((RangeEntry)other).value) return false; + return true; + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), value); + } + } + } |