diff options
Diffstat (limited to 'container-search/src/main/java/com/yahoo/search/dispatch/rpc/MatchFeatureData.java')
-rw-r--r-- | container-search/src/main/java/com/yahoo/search/dispatch/rpc/MatchFeatureData.java | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/container-search/src/main/java/com/yahoo/search/dispatch/rpc/MatchFeatureData.java b/container-search/src/main/java/com/yahoo/search/dispatch/rpc/MatchFeatureData.java new file mode 100644 index 00000000000..3a7a1796f04 --- /dev/null +++ b/container-search/src/main/java/com/yahoo/search/dispatch/rpc/MatchFeatureData.java @@ -0,0 +1,98 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.search.dispatch.rpc; + +import com.yahoo.collections.Hashlet; + +import com.yahoo.data.access.ArrayTraverser; +import com.yahoo.data.access.Inspector; +import com.yahoo.data.access.ObjectTraverser; +import com.yahoo.data.access.Type; +import com.yahoo.data.access.simple.Value; + +import java.util.ArrayList; +import java.util.AbstractMap.SimpleEntry; +import java.util.List; +import java.util.Map; + +/** + * MatchFeatureData helps pack match features for hits into + * inspectable HitValue objects, all sharing the same Hashlet + * for the field names. + * @author arnej + */ +public class MatchFeatureData { + + private final Hashlet<String,Integer> hashlet; + + // package-private: + MatchFeatureData(List<String> keys) { + this.hashlet = new Hashlet<>(); + hashlet.reserve(keys.size()); + int i = 0; + for (String key : keys) { + hashlet.put(key, i++); + } + } + + static class HitValue extends Value { + private final Hashlet<String,Integer> hashlet; + private final byte[][] dataValues; + private final double[] doubleValues; + private int index = 0; + + public Type type() { return Type.OBJECT; } + public boolean valid() { return index == doubleValues.length; } + public int fieldCount() { return hashlet.size(); } + public void traverse(ObjectTraverser ot) { + for (int i = 0; i < hashlet.size(); i++) { + String fn = hashlet.key(i); + int offset = hashlet.value(i); + ot.field(fn, valueAt(offset)); + } + } + public Inspector field(String name) { + int offset = hashlet.getIndexOfKey(name); + if (offset < 0 || ! valid()) { + return invalid(); + } + return valueAt(offset); + } + public Iterable<Map.Entry<String,Inspector>> fields() { + if (! valid()) { return List.of(); } + var list = new ArrayList<Map.Entry<String,Inspector>>(hashlet.size()); + for (int i = 0; i < hashlet.size(); i++) { + String fn = hashlet.key(i); + int offset = hashlet.value(i); + list.add(new SimpleEntry<String,Inspector>(fn, valueAt(offset))); + } + return list; + } + + // use from enclosing class only + HitValue(Hashlet<String,Integer> hashlet) { + this.hashlet = hashlet; + this.dataValues = new byte[hashlet.size()][]; + this.doubleValues = new double[hashlet.size()]; + } + + // package-private: + void add(byte[] data) { + dataValues[index++] = data; + } + void add(double value) { + doubleValues[index++] = value; + } + + private Inspector valueAt(int index) { + if (dataValues[index] != null) { + return new Value.DataValue(dataValues[index]); + } + return new Value.DoubleValue(doubleValues[index]); + } + } + + HitValue addHit() { + return new HitValue(hashlet); + } + +} |