diff options
author | Arne Juul <arnej@yahooinc.com> | 2023-04-27 19:36:10 +0000 |
---|---|---|
committer | Arne Juul <arnej@yahooinc.com> | 2023-04-27 20:46:39 +0000 |
commit | 561f9e3ac24ef0308f3b73a1b106a2ac4a04e35a (patch) | |
tree | 5a846e17c71be428e8faf874d00b65d2501d3624 /container-search | |
parent | ce388205e5323a2c94c4ea20b4c208487e3ad983 (diff) |
move MatchFeatureData class to vespajlib
Diffstat (limited to 'container-search')
3 files changed, 1 insertions, 194 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 deleted file mode 100644 index 7781a01b781..00000000000 --- a/container-search/src/main/java/com/yahoo/search/dispatch/rpc/MatchFeatureData.java +++ /dev/null @@ -1,94 +0,0 @@ -// 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 - */ -class MatchFeatureData { - - private final Hashlet<String,Integer> hashlet; - - 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; - - public Type type() { return Type.OBJECT; } - public boolean valid() { return true; } - 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) { - return invalid(); - } - return valueAt(offset); - } - public Iterable<Map.Entry<String,Inspector>> fields() { - 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 - private HitValue(Hashlet<String,Integer> hashlet) { - this.hashlet = hashlet; - this.dataValues = new byte[hashlet.size()][]; - this.doubleValues = new double[hashlet.size()]; - } - - void set(int index, byte[] data) { - dataValues[index] = data; - } - void set(int index, 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); - } - -} diff --git a/container-search/src/main/java/com/yahoo/search/dispatch/rpc/ProtobufSerialization.java b/container-search/src/main/java/com/yahoo/search/dispatch/rpc/ProtobufSerialization.java index 0b6a6a97a35..37f374b8c1b 100644 --- a/container-search/src/main/java/com/yahoo/search/dispatch/rpc/ProtobufSerialization.java +++ b/container-search/src/main/java/com/yahoo/search/dispatch/rpc/ProtobufSerialization.java @@ -6,6 +6,7 @@ import ai.vespa.searchlib.searchprotocol.protobuf.SearchProtocol.StringProperty; import ai.vespa.searchlib.searchprotocol.protobuf.SearchProtocol.TensorProperty; import com.google.protobuf.ByteString; import com.google.protobuf.InvalidProtocolBufferException; +import com.yahoo.data.access.helpers.MatchFeatureData; import com.yahoo.data.access.simple.Value; import com.yahoo.data.access.slime.SlimeAdapter; import com.yahoo.fs4.GetDocSumsPacket; diff --git a/container-search/src/test/java/com/yahoo/search/dispatch/rpc/MatchFeatureDataTest.java b/container-search/src/test/java/com/yahoo/search/dispatch/rpc/MatchFeatureDataTest.java deleted file mode 100644 index 861ed4d165d..00000000000 --- a/container-search/src/test/java/com/yahoo/search/dispatch/rpc/MatchFeatureDataTest.java +++ /dev/null @@ -1,100 +0,0 @@ -// 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.data.access.ArrayTraverser; -import com.yahoo.data.access.Inspector; -import com.yahoo.data.access.ObjectTraverser; -import com.yahoo.data.access.Type; -import org.junit.jupiter.api.Test; - -import java.util.List; -import java.util.Map; - -import static org.junit.jupiter.api.Assertions.*; - -/** - * @author arnej - */ -public class MatchFeatureDataTest { - - @Test - void testHitValueAPI() { - List<String> names = List.of("foo", "bar", "baz", "qux", "quux"); - var mf = new MatchFeatureData(names); - var hit = mf.addHit(); - assertEquals(hit.type(), Type.OBJECT); - assertTrue(hit.valid()); - hit.set(0, 1.0); - byte[] somebytes = {42, 0, 17}; - hit.set(2, somebytes); - hit.set(4, 5.0); - hit.set(1, 2.0); - hit.set(3, 4.0); - assertEquals(0, hit.entryCount()); - assertEquals(5, hit.fieldCount()); - var f0 = hit.field("not"); - assertFalse(f0.valid()); - - var f1 = hit.field("foo"); - assertTrue(f1.valid()); - assertEquals(f1.type(), Type.DOUBLE); - assertEquals(f1.asDouble(), 1.0, 0.0); - - var f2 = hit.field("bar"); - assertTrue(f2.valid()); - assertEquals(f2.type(), Type.DOUBLE); - assertEquals(f2.asDouble(), 2.0, 0.0); - - var f3 = hit.field("baz"); - assertTrue(f3.valid()); - assertEquals(f3.type(), Type.DATA); - var gotbytes = f3.asData(); - assertEquals(3, gotbytes.length); - assertEquals(42, gotbytes[0]); - assertEquals(0, gotbytes[1]); - assertEquals(17, gotbytes[2]); - - var f5 = hit.field("quux"); - assertTrue(f5.valid()); - assertEquals(f5.type(), Type.DOUBLE); - assertEquals(f5.asDouble(), 5.0, 0.0); - - var fields = hit.fields().iterator(); - assertTrue(fields.hasNext()); - Map.Entry<String, Inspector> entry = fields.next(); - assertEquals("foo", entry.getKey()); - assertEquals(f1.type(), entry.getValue().type()); - assertEquals(f1.asDouble(), entry.getValue().asDouble(), 0.0); - - assertTrue(fields.hasNext()); - entry = fields.next(); - assertEquals("bar", entry.getKey()); - - assertTrue(fields.hasNext()); - entry = fields.next(); - assertEquals("baz", entry.getKey()); - assertEquals(f3.type(), entry.getValue().type()); - assertEquals(f3.asData(), entry.getValue().asData()); - - assertTrue(fields.hasNext()); - entry = fields.next(); - assertEquals("qux", entry.getKey()); - var f4 = entry.getValue(); - assertTrue(f4.valid()); - assertEquals(f4.type(), Type.DOUBLE); - assertEquals(f4.asDouble(), 4.0, 0.0); - - assertTrue(fields.hasNext()); - entry = fields.next(); - assertEquals("quux", entry.getKey()); - assertEquals(f5.type(), entry.getValue().type()); - assertEquals(f5.asDouble(), entry.getValue().asDouble(), 0.0); - - assertFalse(fields.hasNext()); - - assertEquals("{\"foo\":1.0,\"bar\":2.0,\"baz\":\"0x2A0011\",\"qux\":4.0,\"quux\":5.0}", - hit.toString()); - } - -} |