Complex Vector Examples?

160 megtekintés
Ugrás az első olvasatlan üzenetre

Johnny Sheeley

olvasatlan,
2015. jún. 11. 0:12:052015. 06. 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

olvasatlan,
2015. jún. 11. 11:57:352015. 06. 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

olvasatlan,
2015. jún. 15. 11:59:422015. 06. 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

olvasatlan,
2015. jún. 15. 14:16:232015. 06. 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

olvasatlan,
2015. jún. 15. 14:32:342015. 06. 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

olvasatlan,
2015. jún. 15. 16:35:272015. 06. 15.
– Wouter van Oortmerssen, flatb...@googlegroups.com
Will make changes, then! Thanks for the feedback.
Válasz mindenkinek
Válasz a szerzőnek
Továbbítás
0 új üzenet