how to define map and list in the flatbuffers schema for compile a java class

4,153 views
Skip to first unread message

paul.ro...@googlemail.com

unread,
Nov 24, 2014, 9:05:03 AM11/24/14
to flatb...@googlegroups.com
hi community :)

i want to use map and list in my next flatbuffers project, but how?

namespace org.example.person;

table
Person {
  name
:string;
  age
:int;
  possession
:list;
  children
:map<string,int>;
}

root_type
Person

sincerly yours
Paul

paul.ro...@googlemail.com

unread,
Nov 25, 2014, 9:39:13 AM11/25/14
to flatb...@googlegroups.com
i get a email from flatb...@googlegroups.com:
Don't you want children to be a list of Person, e.g.

children:[Person]

Not sure what the int in your map is for, but if it is age, that is already in Person too.

A list is called a "vector" in FlatBuffers, denoted by []. So maybe possessions should be:

possessions:[Possession]
___________________________________________________________________________

thanks for the answer,

how to define a map for dynamic object, also key value pairs?

Wouter van Oortmerssen

unread,
Nov 26, 2014, 1:52:51 PM11/26/14
to paul.ro...@googlemail.com, flatb...@googlegroups.com
If the keys in your map are enumerable, then you should really be using a table (with keys as field names), even if few of keys are used.

If the keys are not enumerable, then creating a table that contains both a key and a value, and then storing a vector of those is simplest. If performance is an issue, you could sort this vector by key before you add it to the FlatBuffer, such that you look up elements by binary search when you access the buffer.

You  could even emulate either a binary tree or a hashtable in FlatBuffers, both of which are possible.

I should really add some helper functions for some of these use cases. 

--
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.

戴政

unread,
Dec 18, 2014, 12:35:10 PM12/18/14
to flatb...@googlegroups.com
I use a vector with table, but I can not get the table object by root.Childeren(1);
Could you show me your code?

在 2014年11月25日星期二UTC+8下午10时39分13秒,paul.ro...@googlemail.com写道:

Vince Harron

unread,
Dec 18, 2014, 12:42:08 PM12/18/14
to Wouter van Oortmerssen, paul.ro...@googlemail.com, flatb...@googlegroups.com

If someone needs to load into a map, (to modify after load, for example) it would be cool if the nodes were sorted in an insertion order that avoided rebalancing.  E.g. breadth first?

Wouter van Oortmerssen

unread,
Jan 5, 2015, 3:08:48 PM1/5/15
to 戴政, flatb...@googlegroups.com
reondz: in Java, root.children(1) should return the second table (of type Person) if you use my suggested definition, what exactly is not working?

Wouter van Oortmerssen

unread,
Jan 5, 2015, 3:14:33 PM1/5/15
to Vince Harron, paul.ro...@googlemail.com, flatb...@googlegroups.com
Vince: that would be best indeed, but tables are stored by the user before the vector/map gets created, so I have no control over that.

An optional conversion to map sounds like a nice feature though.
Reply all
Reply to author
Forward
0 new messages