From 561f9e3ac24ef0308f3b73a1b106a2ac4a04e35a Mon Sep 17 00:00:00 2001 From: Arne Juul Date: Thu, 27 Apr 2023 19:36:10 +0000 Subject: move MatchFeatureData class to vespajlib --- .../data/access/helpers/MatchFeatureData.java | 94 +++++++++++++++++++ .../yahoo/data/access/helpers/package-info.java | 5 ++ .../data/access/helpers/MatchFeatureDataTest.java | 100 +++++++++++++++++++++ 3 files changed, 199 insertions(+) create mode 100644 vespajlib/src/main/java/com/yahoo/data/access/helpers/MatchFeatureData.java create mode 100644 vespajlib/src/main/java/com/yahoo/data/access/helpers/package-info.java create mode 100644 vespajlib/src/test/java/com/yahoo/data/access/helpers/MatchFeatureDataTest.java (limited to 'vespajlib') diff --git a/vespajlib/src/main/java/com/yahoo/data/access/helpers/MatchFeatureData.java b/vespajlib/src/main/java/com/yahoo/data/access/helpers/MatchFeatureData.java new file mode 100644 index 00000000000..f7d72ade20d --- /dev/null +++ b/vespajlib/src/main/java/com/yahoo/data/access/helpers/MatchFeatureData.java @@ -0,0 +1,94 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.data.access.helpers; + +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 hashlet; + + public MatchFeatureData(List keys) { + this.hashlet = new Hashlet<>(); + hashlet.reserve(keys.size()); + int i = 0; + for (String key : keys) { + hashlet.put(key, i++); + } + } + + public static class HitValue extends Value { + private final Hashlet 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> fields() { + var list = new ArrayList>(hashlet.size()); + for (int i = 0; i < hashlet.size(); i++) { + String fn = hashlet.key(i); + int offset = hashlet.value(i); + list.add(new SimpleEntry(fn, valueAt(offset))); + } + return list; + } + + // use from enclosing class only + private HitValue(Hashlet hashlet) { + this.hashlet = hashlet; + this.dataValues = new byte[hashlet.size()][]; + this.doubleValues = new double[hashlet.size()]; + } + + public void set(int index, byte[] data) { + dataValues[index] = data; + } + public 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]); + } + } + + public HitValue addHit() { + return new HitValue(hashlet); + } + +} diff --git a/vespajlib/src/main/java/com/yahoo/data/access/helpers/package-info.java b/vespajlib/src/main/java/com/yahoo/data/access/helpers/package-info.java new file mode 100644 index 00000000000..96c12b63c69 --- /dev/null +++ b/vespajlib/src/main/java/com/yahoo/data/access/helpers/package-info.java @@ -0,0 +1,5 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +@ExportPackage +package com.yahoo.data.access.helpers; + +import com.yahoo.osgi.annotation.ExportPackage; diff --git a/vespajlib/src/test/java/com/yahoo/data/access/helpers/MatchFeatureDataTest.java b/vespajlib/src/test/java/com/yahoo/data/access/helpers/MatchFeatureDataTest.java new file mode 100644 index 00000000000..22f71107ec3 --- /dev/null +++ b/vespajlib/src/test/java/com/yahoo/data/access/helpers/MatchFeatureDataTest.java @@ -0,0 +1,100 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +package com.yahoo.data.access.helpers; + +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 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 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()); + } + +} -- cgit v1.2.3