How to get the size of a Table from a netsted table

380 views
Skip to first unread message

agallego

unread,
Aug 23, 2017, 10:54:31 PM8/23/17
to FlatBuffers
Hello

Say i parse a flatbuffers from the wire. 

Now I have an immutable array of bytes. For simplicity, say 

std::vector<char> bytes;

Great.

I can get the top level Table type via 


auto t = GetMutableRoot<T>(bytes.data())

and call appropriate methods like 

t->fireMonster()


However, I want to save a nested table to disk and I'm not sure how to get the byte range from the underlying 'bytes` array. 

For example:



Table Foo {
  x: uint;
  y: uint;
  z: [ ubyte ];


Table Bar {
  key:  uint;
  many_foo:  [ Foo ];
}


// top level 
Table Baz {
  key2:  uint;
  many_bar: [ Bar ];

}


How do i get the size of a Foo()

In this example, i have to manually do  something like:

sizeof( uint32_t ) /* x */ + sizeof ( uint32_t ) /* y */ +  t->z().size() /* flatbuffers::Vector<uint8_t> */;

etc. 

Is there a generic way to get the 'size' of a table ? 


Second question. 

Assuming i have the example above, indeed the underlying ` bytes ` is for this example a std::vector < char >; 

Is there a way to share a subset of the ` bytes ` array so I don't have to memcpy each Foo into a new buffer that I can then pass around to disk ? 


Thanks, I've been looking through the API and couldn't find an answer to either. 


Any guidance is appreciated. 


The real use case is to write transaction fragments ( key=value pairs) to disk, but the example above should suffice.



THanks again!

agallego

unread,
Aug 23, 2017, 10:59:19 PM8/23/17
to FlatBuffers
So to solve this one can do this 

for each Foo:
   create an fbb builder
   re insert values
   fbb
.GetBufferPointer() and fbb.GetSize()


But that would be inneficient. 

I can manage both client and server so I can make sure all clients sort the data correctly. 

I guess what I'm hoping the answer to be is this, or get to an answer like this. 

get the first offset of the underlying byte array and copy the sizeof ( struct ) + dynamic array size * 

Thanks!! 

Wouter van Oortmerssen

unread,
Aug 24, 2017, 1:24:21 PM8/24/17
to agallego, FlatBuffers
That is not generally possible, since a table consists of several parts (the table, a vtable, and any child strings/vectors/tables) which are not necessarily contiguous/ordered in the same way and/or may have alignment padding, etc.

Like agallego is saying, if you already know you want to memcpy parts of a large FlatBuffer out later, you can store the sub part as a byte array itself (see nested_flatbuffer).

An alternative is using reflection, there's a function called CopyTable that can lift arbitrary tables (with any children) out of a buffer into a new builder, but that is likely a lot less efficient than using nested_flatbuffer.

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages