Skip to content

Instantly share code, notes, and snippets.

@wsargent
Created November 26, 2020 19:04
Show Gist options
  • Save wsargent/4e34030f56f3ed894558d600128a150b to your computer and use it in GitHub Desktop.
Save wsargent/4e34030f56f3ed894558d600128a150b to your computer and use it in GitHub Desktop.
package com.tersesystems.rifter.spi;
import com.google.flatbuffers.ArrayReadWriteBuf;
import com.google.flatbuffers.FlatBufferBuilder;
import com.google.flatbuffers.FlexBuffersBuilder;
import java.nio.ByteBuffer;
import java.util.function.Consumer;
import static com.tersesystems.rifter.spi.LogInstant.createLogInstant;
import static java.nio.ByteBuffer.allocateDirect;
public class ToFlatBuffer {
private final int initialSize;
private final FlatBufferBuilder flatBuilder;
private final ArrayReadWriteBuf arrayBuff;
private final ByteBuffer byteBuffer;
private final FlatBufferBuilder.HeapByteBufferFactory byteBufferFactory;
public ToFlatBuffer() {
this(1024);
}
public ToFlatBuffer(int initialSize) {
this.initialSize = initialSize;
this.arrayBuff = new ArrayReadWriteBuf(initialSize);
this.byteBufferFactory = new FlatBufferBuilder.HeapByteBufferFactory();
this.byteBuffer = ByteBuffer.allocate(initialSize);
this.flatBuilder = new FlatBufferBuilder(byteBuffer, byteBufferFactory);
}
public ByteBuffer event(String id, String loggerName, int level, long epochSecond, long nanos, String message, Consumer<FlexBuffersBuilder> attrConsumer) {
FlatBufferBuilder builder = getBuilder();
int entryOffset = entry(builder, message, attrConsumer);
return event(builder, id, loggerName, level, epochSecond, nanos, entryOffset);
}
public ByteBuffer event(String id, String loggerName, int level, long epochSecond, long nanos, Consumer<FlexBuffersBuilder> attrConsumer) {
FlatBufferBuilder builder = getBuilder();
int entryOffset = entry(builder, attrConsumer);
return event(builder, id, loggerName, level, epochSecond, nanos, entryOffset);
}
public ByteBuffer event(String id, String loggerName, int level, long epochSecond, long nanos, String message) {
FlatBufferBuilder builder = getBuilder();
int entryOffset = entry(builder, message);
return event(builder, id, loggerName, level, epochSecond, nanos, entryOffset);
}
ByteBuffer event(FlatBufferBuilder builder, String id, String loggerName, int level, long epochSecond, long nanos, int entryOffset) {
int idOffset = builder.createString(id);
int name = builder.createString(loggerName);
LogEvent.startLogEvent(builder);
LogEvent.addId(builder, idOffset);
LogEvent.addTimestamp(builder, createLogInstant(builder, epochSecond, nanos));
LogEvent.addLevel(builder, level);
LogEvent.addName(builder, name);
LogEvent.addEntry(builder, entryOffset);
LogEvent.finishLogEventBuffer(builder, LogEvent.endLogEvent(builder));
ByteBuffer result = builder.dataBuffer();
clear();
return result;
}
private void clear() {
flatBuilder.init(byteBuffer, byteBufferFactory);
}
public int entry(FlatBufferBuilder builder, String message) {
int messageOffset = builder.createString(message);
LogEntry.startLogEntry(builder);
LogEntry.addMessage(builder, messageOffset);
return LogEntry.endLogEntry(builder);
}
public int entry(FlatBufferBuilder builder, Consumer<FlexBuffersBuilder> attrConsumer) {
int attributesOffset = createAttributes(builder, attrConsumer);
LogEntry.startLogEntry(builder);
LogEntry.addAttributes(builder, attributesOffset);
return LogEntry.endLogEntry(builder);
}
public int entry(FlatBufferBuilder builder, String message, Consumer<FlexBuffersBuilder> attrConsumer) {
int messageOffset = builder.createString(message);
int attributesOffset = createAttributes(builder, attrConsumer);
return LogEntry.createLogEntry(builder, messageOffset, attributesOffset);
}
public int createAttributes(FlatBufferBuilder builder, Consumer<FlexBuffersBuilder> attrConsumer) {
ArrayReadWriteBuf arrayBuf = newArrayBuf();
FlexBuffersBuilder flex = newFlexBufferBuilder();
int smap = flex.startMap();
attrConsumer.accept(flex);
flex.endMap(null, smap);
ByteBuffer buffer = flex.finish();
return LogEntry.createAttributesVector(builder, buffer);
}
private FlexBuffersBuilder newFlexBufferBuilder() {
return new FlexBuffersBuilder(arrayBuff, FlexBuffersBuilder.BUILDER_FLAG_SHARE_KEYS_AND_STRINGS);
}
private FlatBufferBuilder getBuilder() {
return flatBuilder;
}
private ArrayReadWriteBuf newArrayBuf() {
return this.arrayBuff;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment