-
-
Save sheeley/38fd734010401af716c2 to your computer and use it in GitHub Desktop.
Flatbuffers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private int[] addEventsToBuffer(FlatBufferBuilder fbb, List<Map<String, Object>> events) | |
throws Exception { | |
Checksum checksum = new CRC32(); | |
int[] eventOffsets = new int[events.size()]; | |
int idx = 0; | |
for (Map<String, Object> event : events) { | |
// Create body | |
Map<String, Object> bodyMap = clean(event); | |
byte[] body = bodySerializer.serialize(bodyMap); | |
int bodyOffset = Event.createBodyVector(fbb, body); | |
// Start event so creation isn't nested | |
// https://github.com/google/flatbuffers/issues/19 | |
Event.startEvent(fbb); | |
// Headers | |
Event.addHeader1(fbb, createHeader1(fbb, event, (short) body.length)); | |
Event.addHeader2(fbb, createHeader2(fbb, event)); | |
Event.addHeader3(fbb, createHeader3(fbb, event)); | |
Event.addHeader4(fbb, createHeader4(fbb, event)); | |
// it's written in reverse order, so the start is the current offset. | |
int headerStart = fbb.offset(); | |
// end is at bodyOffset | |
int headerLength = headerStart - (bodyOffset - 1); | |
int end = headerStart + headerLength; | |
int cap = fbb.dataBuffer().capacity(); | |
// ensure we don't read beyond the capacity of the databuffer | |
if (end > cap) { | |
headerLength -= (end - cap); | |
} | |
byte[] headerBytes = fbb.sizedByteArray(headerStart, headerLength); | |
checksum.update(headerBytes, 0, headerBytes.length); | |
Event.addHeaderChecksum(fbb, checksum.getValue()); | |
// Body | |
checksum.reset(); | |
checksum.update(body, 0, body.length); | |
Event.addBody(fbb, bodyOffset); | |
Event.addBodyChecksum(fbb, checksum.getValue()); | |
eventOffsets[idx++] = Event.endEvent(fbb); | |
} | |
return eventOffsets; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment