summaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/fs4/MapEncoder.java
blob: 2f915257938152d9998ec686c2a2eb2dc959f017 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.fs4;

import com.yahoo.tensor.Tensor;
import com.yahoo.tensor.serialization.TypedBinaryFormat;
import com.yahoo.text.Utf8;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * A static utility for encoding values to the binary map representation used in fs4 packets.
 *
 * @author bratseth
 */
public class MapEncoder {

    // TODO: Time to refactor

    private static final String TYPE_SUFFIX = ".type";
    private static final String TENSOR_TYPE = "tensor";

    /**
     * Encodes a single value as a complete binary map.
     * Does nothing if the value is null.
     *
     * Returns the number of maps encoded - 0 or 1
     */
    public static int encodeSingleValue(String mapName, String key, Object value, ByteBuffer buffer) {
        if (value == null) return 0;

        byte [] utf8 = Utf8.toBytes(mapName);
        buffer.putInt(utf8.length);
        buffer.put(utf8);
        buffer.putInt(1);
        utf8 = Utf8.toBytes(key);
        buffer.putInt(utf8.length);
        buffer.put(utf8);
        utf8 = Utf8.toBytes(value.toString());
        buffer.putInt(utf8.length);
        buffer.put(utf8);

        return 1;
    }

    /**
     * Encodes a map as binary.
     * Does nothing if the value is null.
     *
     * Returns the number of maps encoded - 0 or 1
     */
    public static int encodeMap(String mapName, Map<String,?> map, ByteBuffer buffer) {
        if (map.isEmpty()) return 0;

        byte [] utf8 = Utf8.toBytes(mapName);
        buffer.putInt(utf8.length);
        buffer.put(utf8);
        buffer.putInt(map.size());
        for (Map.Entry<String, ?> property : map.entrySet()) {
            String key = property.getKey();
            utf8 = Utf8.toBytes(key);
            buffer.putInt(utf8.length);
            buffer.put(utf8);
            utf8 = Utf8.toBytes(property.getValue() != null ? property.getValue().toString() : "");
            buffer.putInt(utf8.length);
            buffer.put(utf8);
        }

        return 1;
    }

    /**
     * Encodes a multi-map as binary.
     * Does nothing if the value is null.
     *
     * Returns the number of maps encoded - 0 or 1
     */
    public static int encodeStringMultiMap(String mapName, Map<String,List<String>> map, ByteBuffer buffer) {
        if (map.isEmpty()) return 0;

        byte [] utf8 = Utf8.toBytes(mapName);
        buffer.putInt(utf8.length);
        buffer.put(utf8);
        buffer.putInt(countStringEntries(map));
        for (Map.Entry<String, List<String>> property : map.entrySet()) {
            String key = property.getKey();
            for (Object value : property.getValue()) {
                utf8 = Utf8.toBytes(key);
                buffer.putInt(utf8.length);
                buffer.put(utf8);
                utf8 = Utf8.toBytes(value.toString());
                buffer.putInt(utf8.length);
                buffer.put(utf8);
            }
        }

        return 1;
    }

    /**
     * Encodes a multi-map as binary.
     * Does nothing if the value is null.
     *
     * Returns the number of maps encoded - 0 or 1
     */
    public static int encodeObjectMultiMap(String mapName, Map<String,List<Object>> map, ByteBuffer buffer) {
        if (map.isEmpty()) return 0;

        byte[] utf8 = Utf8.toBytes(mapName);
        buffer.putInt(utf8.length);
        buffer.put(utf8);
        addTensorTypeInfo(map);
        buffer.putInt(countObjectEntries(map));
        for (Map.Entry<String, List<Object>> property : map.entrySet()) {
            String key = property.getKey();
            for (Object value : property.getValue()) {
                utf8 = Utf8.toBytes(key);
                buffer.putInt(utf8.length);
                buffer.put(utf8);
                if (value instanceof Tensor) {
                    utf8 = TypedBinaryFormat.encode((Tensor)value);
                } else {
                    utf8 = Utf8.toBytes(value.toString());
                }
                buffer.putInt(utf8.length);
                buffer.put(utf8);
            }
        }

        return 1;
    }

    private static void addTensorTypeInfo(Map<String, List<Object>> map) {
        Map<String, Tensor> tensorsToTag = new HashMap<>();
        for (Map.Entry<String, List<Object>> entry : map.entrySet()) {
            for (Object value : entry.getValue()) {
                if (value instanceof Tensor) {
                    tensorsToTag.put(entry.getKey(), (Tensor)value);
                }
            }
        }
        for (Map.Entry<String, Tensor> entry : tensorsToTag.entrySet()) {
            // Ensure that we only have a single tensor associated with each key
            map.put(entry.getKey(), Arrays.asList(entry.getValue()));
            map.put(entry.getKey() + TYPE_SUFFIX, Arrays.asList(TENSOR_TYPE));
        }
    }

    private static int countStringEntries(Map<String, List<String>> value) {
        int entries = 0;
        for (Map.Entry<String, List<String>> property : value.entrySet())
            entries += property.getValue().size();
        return entries;
    }

    private static int countObjectEntries(Map<String, List<Object>> value) {
        int entries = 0;
        for (Map.Entry<String, List<Object>> property : value.entrySet())
            entries += property.getValue().size();
        return entries;
    }

}