summaryrefslogtreecommitdiffstats
path: root/document/src/main/java/com/yahoo/document/json/TokenBuffer.java
blob: e6d2021f90b84ff6bdf6e945aad50b29bb228a94 (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document.json;

import java.io.IOException;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Iterator;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.google.common.base.Preconditions;

/**
 * Helper class to enable lookahead in the token stream.
 *
 * @author Steinar Knutsen
 */
public class TokenBuffer {

    public static final class Token {
        public final JsonToken token;
        public final String name;
        public final String text;

        Token(JsonToken token, String name, String text) {
            this.token = token;
            this.name = name;
            this.text = text;
        }
    }

    private final Deque<Token> buffer;
    private int nesting = 0;

    public TokenBuffer() {
        this(new ArrayDeque<>());
    }

    private TokenBuffer(Deque<Token> buffer) {
        this.buffer = buffer;
        if (buffer.size() > 0) {
            updateNesting(buffer.peekFirst().token);
        }
    }

    /** Returns whether any tokens are available in this */
    public boolean isEmpty() { return size() == 0; }

    public JsonToken next() {
        buffer.removeFirst();
        Token t = buffer.peekFirst();
        if (t == null) {
            return null;
        }
        updateNesting(t.token);
        return t.token;
    }

    /** Returns the current token without changing position, or null if none */
    public JsonToken currentToken() {
        Token token = buffer.peekFirst();
        if (token == null) return null;
        return token.token;
    }

    /** Returns the current token name without changing position, or null if none */
    public String currentName() {
        Token token = buffer.peekFirst();
        if (token == null) return null;
        return token.name;
    }

    /** Returns the current token text without changing position, or null if none */
    public String currentText() {
        Token token = buffer.peekFirst();
        if (token == null) return null;
        return token.text;
    }

    public int size() {
        return buffer.size();
    }

    private void add(JsonToken token, String name, String text) {
        buffer.addLast(new Token(token, name, text));
    }

    public void bufferObject(JsonToken first, JsonParser tokens) {
        bufferJsonStruct(first, tokens, JsonToken.START_OBJECT);
    }

    public void bufferArray(JsonToken first, JsonParser tokens) {
        bufferJsonStruct(first, tokens, JsonToken.START_ARRAY);
    }

    private void bufferJsonStruct(JsonToken first, JsonParser tokens, JsonToken firstToken) {
        int localNesting = 0;
        JsonToken t = first;

        Preconditions.checkArgument(first == firstToken,
                "Expected %s, got %s.", firstToken.name(), t);
        if (size() == 0) {
            updateNesting(t);
        }
        localNesting = storeAndPeekNesting(t, localNesting, tokens);
        while (localNesting > 0) {
            t = nextValue(tokens);
            localNesting = storeAndPeekNesting(t, localNesting, tokens);
        }
    }

    private int storeAndPeekNesting(JsonToken t, int nesting, JsonParser tokens) {
        addFromParser(t, tokens);
        return nesting + nestingOffset(t);
    }

    private int nestingOffset(JsonToken t) {
        if (t.isStructStart()) {
            return 1;
        } else if (t.isStructEnd()) {
            return -1;
        } else {
            return 0;
        }
    }

    private void addFromParser(JsonToken t, JsonParser tokens) {
        try {
            add(t, tokens.getCurrentName(), tokens.getText());
        } catch (IOException e) {
            // TODO something sane
            throw new IllegalArgumentException(e);
        }
    }

    private JsonToken nextValue(JsonParser tokens) {
        try {
            return tokens.nextValue();
        } catch (IOException e) {
            // TODO something sane
            throw new IllegalArgumentException(e);
        }
    }

    private void updateNesting(JsonToken t) {
        nesting += nestingOffset(t);
    }

    public int nesting() {
        return nesting;
    }

    public String dumpContents() {
        StringBuilder b = new StringBuilder();
        b.append("[nesting: ").append(nesting()).append("\n");
        for (Token t : buffer) {
            b.append("(").append(t.token).append(", \"").append(t.name).append("\", \"").append(t.text).append("\")\n");
        }
        b.append("]\n");
        return b.toString();
    }

    public void fastForwardToEndObject() {
        JsonToken t = currentToken();
        while (t != JsonToken.END_OBJECT) {
            t = next();
        }
    }

    public TokenBuffer prefetchCurrentElement() {
        Deque<Token> copy = new ArrayDeque<>();

        if (currentToken().isScalarValue()) {
            copy.add(buffer.peekFirst());
        } else {
            int localNesting = nesting();
            int nestingBarrier = localNesting;
            for (Token t : buffer) {
                copy.add(t);
                localNesting += nestingOffset(t.token);
                if (localNesting < nestingBarrier) {
                    break;
                }
            }
        }
        return new TokenBuffer(copy);
    }

    public Token prefetchScalar(String name) {
        int localNesting = nesting();
        int nestingBarrier = localNesting;
        Token toReturn = null;
        Iterator<Token> i;

        if (name.equals(currentName()) && currentToken().isScalarValue()) {
            toReturn = buffer.peekFirst();
        } else {
            i = buffer.iterator();
            i.next(); // just ignore the first value, as we know it's not what
                      // we're looking for, and it's nesting effect is already
                      // included
            while (i.hasNext()) {
                Token t = i.next();
                if (localNesting == nestingBarrier && name.equals(t.name) && t.token.isScalarValue()) {
                    toReturn = t;
                    break;
                }
                localNesting += nestingOffset(t.token);
                if (localNesting < nestingBarrier) {
                    break;
                }
            }
        }
        return toReturn;
    }

    public void skipToRelativeNesting(int relativeNesting) {
        int initialNesting = nesting();
        do {
            next();
        } while ( nesting() > initialNesting + relativeNesting);
    }

}