Complex Vector Examples?

瀏覽次數:160 次
跳到第一則未讀訊息

Johnny Sheeley

未讀,
2015年6月11日 凌晨12:12:052015/6/11
收件者:flatb...@googlegroups.com

I'm trying to build flatbuffers using a schema like this:

struct Header1 {
...
}

table Event {
  header1: Header1;
  ...
  headerChecksum: ulong;
  body: [ubyte];
  bodyChecksum:ulong;
}

table EventTable {
  events: [Event];
}

I see the basic Monster example in the codebase, and a reference to more complex vector usage, but I am running into an IndexOutOfBoundsException when I try to deserialize. Are there more in-depth examples of how this works? Here's a simplified version of how I'm building:

public byte[] serialize(List<EventData>Events){

         byte[] eventBytes = getBytesFromEvents(events);


    FlatBufferBuilder fbb = new FlatBufferBuilder();

    EventTable.startEventsVector(fbb, events.size());

    for (byte b : eventBytes) {

      fbb.addByte(b);

    }

    int eventsEnd = fbb.endVector();

    EventTable.startEventTable(fbb);

    EventTable.addEvents(fbb, eventsEnd);


    int end = EventTable.endEventTable(fbb);

    EventTable.finishEventTableBuffer(fbb, end);


    return fbb.sizedByteArray();

}


public byte[] getBytesFromEvents(List<EventData>Events){


  FlatBufferBuilder fbb = new FlatBufferBuilder();


  for (Map<String, Object> event : events) {

    Map<String, Object> bodyMap = clean(event);

    byte[] body = bodySerializer.serialize(bodyMap);

    int bodyOffset = Event.createBodyVector(fbb, body);


    Event.startEvent(fbb);


    // Headers

    Event.addHeader1(fbb, createHeader1(fbb, event);

    // ...


    // Body

    Event.addBody(fbb, bodyOffset);

    Event.addBodyChecksum(fbb, checksum.getValue());

  }


  return fbb.sizedByteArray();

}


And to deserialize:


EventTable eventRoot = EventTable.getRootAsEventTable(ByteBuffer.wrap(bytes));


Any tips/complex samples that may be helpful?

Thanks!

Johnny

Johnny Sheeley

未讀,
2015年6月11日 上午11:57:352015/6/11
收件者:flatb...@googlegroups.com
Had totally misread things with a half-awake brain. Have it working successfully like so:

         FlatBufferBuilder fbb = new FlatBufferBuilder();

    int[] eventOffsets = addEventsToBuffer(fbb, events);

    int eventVectorOffset = EventContainer.createEventsVector(fbb, eventOffsets);

    EventContainer.startEventContainer(fbb);

    EventContainer.addEvents(fbb, eventVectorOffset);

    int end = EventContainer.endEventContainer(fbb);

    EventContainer.finishEventContainerBuffer(fbb, end);

    return fbb.sizedByteArray();

Wouter van Oortmerssen

未讀,
2015年6月15日 上午11:59:422015/6/15
收件者:Johnny Sheeley、flatb...@googlegroups.com
Yup, the last one looks correct!

--
You received this message because you are subscribed to the Google Groups "FlatBuffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flatbuffers...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Johnny Sheeley

未讀,
2015年6月15日 下午2:16:232015/6/15
收件者:Wouter van Oortmerssen、flatb...@googlegroups.com
One thing that I ran into when adding multiple events was I had to add this check, which doesn't make sense to me:

Does that make sense to you? With this check, things appear to serialize/deserialize correctly, but I'm concerned that it doesn't seem like it should need to happen in certain cases but not others.

Wouter van Oortmerssen

未讀,
2015年6月15日 下午2:32:342015/6/15
收件者:Johnny Sheeley、flatb...@googlegroups.com
That definitely shouldn't be needed. That code makes assumptions about alignment and how things are put into a FlatBuffer, and may break. If you want to checksum your headers, you're better off doing that entirely outside the realm of the FlatBuffer serialized data.

Johnny Sheeley

未讀,
2015年6月15日 下午4:35:272015/6/15
收件者:Wouter van Oortmerssen、flatb...@googlegroups.com
Will make changes, then! Thanks for the feedback.
回覆所有人
回覆作者
轉寄
0 則新訊息