forked from simdjson/simdjson-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimdJsonParser.java
More file actions
69 lines (58 loc) · 2.11 KB
/
SimdJsonParser.java
File metadata and controls
69 lines (58 loc) · 2.11 KB
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
package org.simdjson;
public class SimdJsonParser {
private static final int STEP_SIZE = 64;
private static final int PADDING = 64;
private static final int DEFAULT_CAPACITY = 34 * 1024 * 1024; // we should be able to handle jsons <= 34MiB
private static final int DEFAULT_MAX_DEPTH = 1024;
private final BlockReader reader;
private final StructuralIndexer indexer;
private final BitIndexes bitIndexes;
private final JsonIterator jsonIterator;
private final byte[] paddedBuffer;
public SimdJsonParser() {
this(DEFAULT_CAPACITY, DEFAULT_MAX_DEPTH);
}
public SimdJsonParser(int capacity, int maxDepth) {
bitIndexes = new BitIndexes(capacity);
jsonIterator = new JsonIterator(bitIndexes, capacity, maxDepth, PADDING);
paddedBuffer = new byte[capacity];
reader = new BlockReader(STEP_SIZE);
indexer = new StructuralIndexer(bitIndexes);
}
public JsonValue parse(byte[] buffer, int len) {
stage0(buffer);
byte[] padded = padIfNeeded(buffer, len);
reset(padded, len);
stage1(padded);
return stage2(padded, len);
}
private byte[] padIfNeeded(byte[] buffer, int len) {
if (buffer.length - len < PADDING) {
System.arraycopy(buffer, 0, paddedBuffer, 0, len);
return paddedBuffer;
}
return buffer;
}
private void reset(byte[] buffer, int len) {
indexer.reset();
reader.reset(buffer, len);
bitIndexes.reset();
jsonIterator.reset();
}
private void stage0(byte[] buffer) {
Utf8Validator.validate(buffer);
}
private void stage1(byte[] buffer) {
while (reader.hasFullBlock()) {
int blockIndex = reader.getBlockIndex();
indexer.step(buffer, blockIndex, blockIndex);
reader.advance();
}
indexer.step(reader.remainder(), 0, reader.getBlockIndex());
reader.advance();
indexer.finish(reader.getBlockIndex());
}
private JsonValue stage2(byte[] buffer, int len) {
return jsonIterator.walkDocument(buffer, len);
}
}