aboutsummaryrefslogtreecommitdiffstats
path: root/config/src/main/java/com/yahoo/vespa/config/protocol/SlimeTraceDeserializer.java
blob: 4b1cb6607a1c3145f0fd6fc561d09fcb04cb26fd (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.protocol;

import com.yahoo.slime.ArrayTraverser;
import com.yahoo.slime.Inspector;
import com.yahoo.yolean.trace.TraceNode;

/**
 * Deserializing from a {@link Inspector} (slime) representation to a {@link TraceNode}
 *
 * @author Ulf Lilleengen
 */
public class SlimeTraceDeserializer {
    private final Inspector entry;
    public SlimeTraceDeserializer(Inspector inspector) {
        this.entry = inspector;
    }

    public TraceNode deserialize() {
        return deserialize(entry);
    }

    private static TraceNode deserialize(Inspector entry) {
        Object payload = decodePayload(entry.field(SlimeTraceSerializer.PAYLOAD));
        long timestamp = decodeTimestamp(entry.field(SlimeTraceSerializer.TIMESTAMP));
        final TraceNode node = new TraceNode(payload, timestamp);
        Inspector children = entry.field(SlimeTraceSerializer.CHILDREN);
        children.traverse(new ArrayTraverser() {
            @Override
            public void entry(int idx, Inspector inspector) {
                node.add(deserialize(inspector));
            }
        });
        return node;
    }

    private static long decodeTimestamp(Inspector entry) {
        return entry.asLong();
    }

    private static Object decodePayload(Inspector entry) {
        return switch (entry.type()) {
            case STRING -> entry.asString();
            case LONG -> entry.asLong();
            case BOOL -> entry.asBool();
            case DOUBLE -> entry.asDouble();
            case DATA -> entry.asData();
            default -> null;
        };
    }
}