aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/slime/BinaryEncoder.java
blob: 6e23aed18a4f8f1c528eae3473627e8cc765984d (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.slime;

import static com.yahoo.slime.BinaryFormat.encode_double;
import static com.yahoo.slime.BinaryFormat.encode_type_and_meta;
import static com.yahoo.slime.BinaryFormat.encode_zigzag;

final class BinaryEncoder implements ArrayTraverser, ObjectSymbolTraverser {

    private final BufferedOutput out;

    BinaryEncoder() {
        this(new BufferedOutput());
    }
    BinaryEncoder(BufferedOutput output) {
        out = output;
    }

    BufferedOutput encode(Slime slime) {
        out.reset();
        encodeSymbolTable(slime);
        encodeValue(slime.get());
        return out;
    }


    void encode_cmpr_int(int value) {
        byte next = (byte)(value & 0x7f);
        value >>>= 7; // unsigned shift
        while (value != 0) {
            next |= (byte)0x80;
            out.put(next);
            next = (byte)(value & 0x7f);
            value >>>= 7;
        }
        out.put(next);
    }

    void write_type_and_size(int type, int size) {
        if (size <= 30) {
            out.put(encode_type_and_meta(type, size + 1));
        } else {
            out.put(encode_type_and_meta(type, 0));
            encode_cmpr_int(size);
        }
    }

    void write_type_and_bytes_le(int type, long bits) {
        int pos = out.position();
        byte val = 0;
        out.put(val);
        while (bits != 0) {
            val = (byte)(bits & 0xff);
            bits >>>= 8;
            out.put(val);
        }
        val = encode_type_and_meta(type, out.position() - pos - 1);
        out.absolutePut(pos, val);
    }

    void write_type_and_bytes_be(int type, long bits) {
        int pos = out.position();
        byte val = 0;
        out.put(val);
        while (bits != 0) {
            val = (byte)(bits >> 56);
            bits <<= 8;
            out.put(val);
        }
        val = encode_type_and_meta(type, out.position() - pos - 1);
        out.absolutePut(pos, val);
    }

    void encodeNIX() {
        out.put(Type.NIX.ID);
    }

    void encodeBOOL(boolean value) {
        out.put(encode_type_and_meta(Type.BOOL.ID, value ? 1 : 0));
    }

    void encodeLONG(long value) {
        write_type_and_bytes_le(Type.LONG.ID, encode_zigzag(value));
    }

    void encodeDOUBLE(double value) {
        write_type_and_bytes_be(Type.DOUBLE.ID, encode_double(value));
    }

    void encodeSTRING(byte[] value) {
        write_type_and_size(Type.STRING.ID, value.length);
        out.put(value);
    }

    void encodeDATA(byte[] value) {
        write_type_and_size(Type.DATA.ID, value.length);
        out.put(value);
    }

    void encodeARRAY(Inspector inspector) {
        write_type_and_size(Type.ARRAY.ID, inspector.children());
        ArrayTraverser at = this;
        inspector.traverse(at);
    }

    void encodeOBJECT(Inspector inspector) {
        write_type_and_size(Type.OBJECT.ID, inspector.children());
        ObjectSymbolTraverser ot = this;
        inspector.traverse(ot);
    }

    void encodeValue(Inspector inspector) {
        switch(inspector.type()) {
        case NIX:    encodeNIX();                        return;
        case BOOL:   encodeBOOL(inspector.asBool());     return;
        case LONG:   encodeLONG(inspector.asLong());     return;
        case DOUBLE: encodeDOUBLE(inspector.asDouble()); return;
        case STRING: encodeSTRING(inspector.asUtf8());   return;
        case DATA:   encodeDATA(inspector.asData());     return;
        case ARRAY:  encodeARRAY(inspector);             return;
        case OBJECT: encodeOBJECT(inspector);            return;
        }
        assert false : "Should not be reached";
    }

    void encodeSymbolTable(Slime slime) {
        int numSymbols = slime.symbols();
        encode_cmpr_int(numSymbols);
        for (int i = 0 ; i < numSymbols; ++i) {
            String name = slime.inspect(i);
            byte[] bytes = Utf8Codec.encode(name);
            encode_cmpr_int(bytes.length);
            out.put(bytes);
        }
    }

    public void entry(int idx, Inspector inspector) {
        encodeValue(inspector);
    }

    public void field(int symbol, Inspector inspector) {
        encode_cmpr_int(symbol);
        encodeValue(inspector);
    }

}