summaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/tensor/serialization/TypedBinaryFormat.java
blob: 01a1d023f2bccf88e996fb444085c621e3527992 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.tensor.serialization;

import com.google.common.annotations.Beta;
import com.yahoo.io.GrowableByteBuffer;
import com.yahoo.tensor.IndexedTensor;
import com.yahoo.tensor.MixedTensor;
import com.yahoo.tensor.Tensor;
import com.yahoo.tensor.TensorType;

import java.util.Optional;

/**
 * Class used by clients for serializing a Tensor object into binary format or
 * de-serializing binary data into a Tensor object.
 *
 * The actual binary format used is not a concern for the client and
 * is hidden in this class and in the binary data.
 *
 * @author geirst
 */
@Beta
public class TypedBinaryFormat {

    private static final int SPARSE_BINARY_FORMAT_TYPE = 1;
    private static final int DENSE_BINARY_FORMAT_TYPE = 2;
    private static final int MIXED_BINARY_FORMAT_TYPE = 3;

    public static byte[] encode(Tensor tensor) {
        GrowableByteBuffer buffer = new GrowableByteBuffer();
        if (tensor instanceof MixedTensor) {
            buffer.putInt1_4Bytes(MIXED_BINARY_FORMAT_TYPE);
            new MixedBinaryFormat().encode(buffer, tensor);
        }
        else if (tensor instanceof IndexedTensor) {
            buffer.putInt1_4Bytes(DENSE_BINARY_FORMAT_TYPE);
            new DenseBinaryFormat().encode(buffer, tensor);
        }
        else {
            buffer.putInt1_4Bytes(SPARSE_BINARY_FORMAT_TYPE);
            new SparseBinaryFormat().encode(buffer, tensor);
        }
        buffer.flip();
        byte[] result = new byte[buffer.remaining()];
        buffer.get(result);
        return result;
    }

    /**
     * Decode some data to a tensor
     *
     * @param type the type to decode and validate to, or empty to use the type given in the data
     * @param buffer the buffer containing the data, use GrowableByteByffer.wrap(byte[]) if you have a byte array
     * @return the resulting tensor
     * @throws IllegalArgumentException if the tensor data was invalid
     */
    public static Tensor decode(Optional<TensorType> type, GrowableByteBuffer buffer) {
        int formatType = buffer.getInt1_4Bytes();
        switch (formatType) {
            case MIXED_BINARY_FORMAT_TYPE: return new MixedBinaryFormat().decode(type, buffer);
            case SPARSE_BINARY_FORMAT_TYPE: return new SparseBinaryFormat().decode(type, buffer);
            case DENSE_BINARY_FORMAT_TYPE: return new DenseBinaryFormat().decode(type, buffer);
            default: throw new IllegalArgumentException("Binary format type " + formatType + " is unknown");
        }
    }

}