aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/fs4/MapEncoder.java
blob: 4f31db0fc86f13eb88bd2581d5883d74ea0d2957 (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
// Copyright Vespa.ai. 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.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 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 byte[] getUtf8(Object value) {
        if (value == null) {
            return Utf8.toBytes("");
        } else if (value instanceof Tensor) {
            return TypedBinaryFormat.encode((Tensor)value);
        } else {
            return Utf8.toBytes(value.toString());
        }
    }

    /**
     * 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 = getUtf8(value);
        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, ?> entry : map.entrySet()) {
            String key = entry.getKey();
            utf8 = Utf8.toBytes(key);
            buffer.putInt(utf8.length);
            buffer.put(utf8);
            Object value = entry.getValue();
            utf8 = getUtf8(value);
            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 <T> int encodeMultiMap(String mapName, Map<String,List<T>> map, ByteBuffer buffer) {
        if (map.isEmpty()) return 0;

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

        return 1;
    }

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

}