blob: 817ecfe0091ffca02df8e848e72c7517586deaf6 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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));
}
}
}
}
}
|