summaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/logging/LoggerEntry.java
blob: e02bb8da8c0853b891b5f9d2ac3ccaa8156b7fd7 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

package com.yahoo.search.logging;

import com.yahoo.search.Query;
import com.yahoo.slime.Cursor;
import com.yahoo.slime.Slime;
import com.yahoo.slime.SlimeUtils;
import com.yahoo.text.Utf8;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.ByteBuffer;
import java.util.Base64;

public class LoggerEntry {

    private final Long timestamp;
    private final Query query;
    private final ByteBuffer blob;

    private LoggerEntry(Builder builder) {
        timestamp = builder.timestamp;  // or set automatically if not set
        query = builder.query;
        blob = builder.blob;
    }

    public Long timestamp() {
        return timestamp;
    }

    public Query query() {
        return query;
    }

    public String queryString() {
        String queryString = null;
        if (query != null) {
            if (query.getHttpRequest() != null && query.getHttpRequest().getUri() != null) {
                queryString = query.getHttpRequest().getUri().getPath();
                if (query.getHttpRequest().getUri().getQuery() != null) {
                    queryString += "?" + query.getHttpRequest().getUri().getRawQuery();
                }
            }
        }
        return queryString;
    }

    public ByteBuffer blob() {
        return blob;
    }

    public String toString() {
        return serialize();
    }

    public String serialize() {
        try {
            Slime slime = new Slime();
            Cursor root = slime.setObject();

            root.setLong("timestamp", timestamp == null ? 0 : timestamp);
            root.setString("query", queryString());
            root.setString("blob", Base64.getEncoder().encodeToString(blob.array()));

            return Utf8.toString(SlimeUtils.toJsonBytes(slime));  // TODO
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }

    public static LoggerEntry deserialize(String content) throws IOException {
        var slime = SlimeUtils.jsonToSlime(content);

        var timestamp = slime.get().field("timestamp").asLong();
        var query = new Query(slime.get().field("query").asString());
        var blob = slime.get().field("blob").asString();

        return new LoggerEntry(new Builder().timestamp(timestamp).query(query).blob(blob));
    }

    public static class Builder {

        private final Logger logger;

        private Long timestamp;
        private Query query;
        private ByteBuffer blob;

        // For testing
        public Builder() { this(entry -> false); }

        public Builder(Logger logger) {
            this.logger = logger;
        }

        public Builder timestamp(long timestamp) {
            this.timestamp = timestamp;
            return this;
        }

        public Builder query(Query query) {
            this.query = query;
            return this;
        }

        public Builder blob(byte[] bytes) {
            blob = ByteBuffer.allocate(bytes.length);
            blob.put(bytes).limit(blob.position()).position(0);
            return this;
        }

        public Builder blob(String blob) {
            byte[] bytes = Utf8.toBytes(blob);
            this.blob = ByteBuffer.allocate(bytes.length);
            this.blob.put(bytes);
            return this;
        }

        public boolean send() {
            return logger.send(new LoggerEntry(this));
        }

    }

}