diff options
author | Olli Virtanen <ovirtanen@gmail.com> | 2019-03-22 09:13:19 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-22 09:13:19 +0100 |
commit | 7989191b378263162e31f5226710a0cc9e9c5799 (patch) | |
tree | 51c57ea055ed97313126df96e706bc38abe365ef /container-search/src/main/java/com/yahoo/search/dispatch/rpc/MapConverter.java | |
parent | 87d95bebf84693da9ddc4859d616206110956e34 (diff) | |
parent | 828c6833bf7794060ab2d209b686db7116531b32 (diff) |
Merge pull request #8849 from vespa-engine/ollivir/searchprotocol-protobuf
Search protocol over jrt + protobuf (take 2)
Diffstat (limited to 'container-search/src/main/java/com/yahoo/search/dispatch/rpc/MapConverter.java')
-rw-r--r-- | container-search/src/main/java/com/yahoo/search/dispatch/rpc/MapConverter.java | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/container-search/src/main/java/com/yahoo/search/dispatch/rpc/MapConverter.java b/container-search/src/main/java/com/yahoo/search/dispatch/rpc/MapConverter.java new file mode 100644 index 00000000000..817ecfe0091 --- /dev/null +++ b/container-search/src/main/java/com/yahoo/search/dispatch/rpc/MapConverter.java @@ -0,0 +1,73 @@ +// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.search.dispatch.rpc; + +import ai.vespa.searchlib.searchprotocol.protobuf.SearchProtocol.StringProperty; +import ai.vespa.searchlib.searchprotocol.protobuf.SearchProtocol.TensorProperty; +import com.google.protobuf.ByteString; +import com.yahoo.tensor.Tensor; +import com.yahoo.tensor.serialization.TypedBinaryFormat; + +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +/** + * @author ollivir + */ +public class MapConverter { + @FunctionalInterface + public interface PropertyInserter<T> { + void add(T prop); + } + + public static void convertMapTensors(Map<String, Object> map, PropertyInserter<TensorProperty.Builder> inserter) { + for (var entry : map.entrySet()) { + var value = entry.getValue(); + if (value instanceof Tensor) { + byte[] tensor = TypedBinaryFormat.encode((Tensor) value); + inserter.add(TensorProperty.newBuilder().setName(entry.getKey()).setValue(ByteString.copyFrom(tensor))); + } + } + } + + public static void convertMapStrings(Map<String, Object> map, PropertyInserter<StringProperty.Builder> inserter) { + for (var entry : map.entrySet()) { + var value = entry.getValue(); + if (!(value instanceof Tensor)) { + inserter.add(StringProperty.newBuilder().setName(entry.getKey()).addValues(value.toString())); + } + } + } + + public static void convertStringMultiMap(Map<String, List<String>> map, PropertyInserter<StringProperty.Builder> inserter) { + for (var entry : map.entrySet()) { + var values = entry.getValue(); + if (values != null) { + inserter.add(StringProperty.newBuilder().setName(entry.getKey()).addAllValues(values)); + } + } + } + + public static void convertMultiMap(Map<String, List<Object>> map, PropertyInserter<StringProperty.Builder> stringInserter, + PropertyInserter<TensorProperty.Builder> tensorInserter) { + for (var entry : map.entrySet()) { + if (entry.getValue() != null) { + var key = entry.getKey(); + var stringValues = new LinkedList<String>(); + for (var value : entry.getValue()) { + if (value != null) { + if (value instanceof Tensor) { + byte[] tensor = TypedBinaryFormat.encode((Tensor) value); + tensorInserter.add(TensorProperty.newBuilder().setName(key).setValue(ByteString.copyFrom(tensor))); + } else { + stringValues.add(value.toString()); + } + } + } + if (!stringValues.isEmpty()) { + stringInserter.add(StringProperty.newBuilder().setName(key).addAllValues(stringValues)); + } + } + } + } +} |