Vectors of tables possible using Golang?

119 views
Skip to first unread message

Hamish Ogilvy

unread,
Jun 21, 2015, 11:19:17 AM6/21/15
to flatb...@googlegroups.com
Thanks for flatbuffers, very useful. I'm stuck on the following though, not sure how to get this working in Golang. Everything works fine except when using vectors of tables, so I'm wondering if this is a limitation because they aren't fixed length (e.g. contain strings)? Whichever way i write these the data seems to partially overwrite and get corrupted.

Schema below. Also included the code for vector packing of a table vector, which i then write into the Term table.

Thanks!

namespace sjfb;

table Shotgun {
	Term:string;
	Potency:float;
}

table Clue {
	Term:string;
	Intro:string;
	Potency:float;
}

table Term {
	TermStr:string;
	Slot:uint;
	NumDocuments:uint;
	NumWords:short;
	Shotgun:[Shotgun];
	Clues:[Clue];
	InteractionPos:short;
	InteractionNeg:short;
	HardcodedScore:short;
	Infogain:float;
}

root_type Term;


shotguns := make([]flatbuffers.UOffsetT, len(term.Shotgun))
for i := 0; i < len(term.Shotgun); i++ {
sjfb.ShotgunStart(builder)
sjfb.ShotgunAddTerm(builder, builder.CreateString(term.Shotgun[i].Term))
sjfb.ShotgunAddPotency(builder, term.Shotgun[i].Potency)
shotguns[i] = sjfb.ShotgunEnd(builder)
}
sjfb.TermStartShotgunVector(builder, len(term.Shotgun))
for i := len(term.Shotgun) - 1; i >= 0; i-- {
builder.PrependUOffsetT(shotguns[i])
}
shotgun_vec := builder.EndVector(len(term.Shotgun))

Wouter van Oortmerssen

unread,
Jun 22, 2015, 1:17:42 PM6/22/15
to Hamish Ogilvy, flatb...@googlegroups.com
The one issue I see is that builder.CreateString(term.Shotgun[i].Term) should be called before sjfb.ShotgunStart(builder). You're not supposed to nest objects.


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

Hamish Ogilvy

unread,
Jun 22, 2015, 8:22:41 PM6/22/15
to flatb...@googlegroups.com, hamish...@gmail.com
Thanks for the quick response. I didn't get a nested warning for the CreateString being nested so i assumed it was ok (note i did see that warning when nesting vectors). Makes sense though.

Here is my current encode function, it's still not right, but i'm thinking it's pretty close. Any ideas?

Thanks for your help!

Hamish Ogilvy

unread,
Jun 22, 2015, 8:43:48 PM6/22/15
to flatb...@googlegroups.com
I actually noticed i had the "termStr := builder.CreateString(term.TermStr)" within the Term creation also. Interestingly this packs and unpacks fine without errors. Moving it outside the Term object creation produces the exact same encoded output.

Wouter van Oortmerssen

unread,
Jun 23, 2015, 1:46:07 PM6/23/15
to Hamish Ogilvy, flatb...@googlegroups.com
In most cases having a string inside the table can actually work, it just shouldn't be done in the general case because if the string is very long, it can cause the vtable to not be able to address all elements. It also causes more vtables than necessary to be generated (bigger binaries).

The Go implementation should really assert no-nested objects though, not sure why this is not happening.

You say "Everything works fine except when using vectors of tables". What exactly does not work? what is the result?

Hamish Ogilvy

unread,
Jun 29, 2015, 8:59:10 PM6/29/15
to Wouter van Oortmerssen, flatb...@googlegroups.com
Got this working, Robert was very helpful. He's also updated the Go pkg to warn for nested strings, etc.

For reference the working code is here:

Thanks again.

Wouter van Oortmerssen

unread,
Jun 29, 2015, 9:02:22 PM6/29/15
to Hamish Ogilvy, flatb...@googlegroups.com
So what was the problem in the end?

Hamish Ogilvy

unread,
Jun 29, 2015, 9:11:58 PM6/29/15
to Wouter van Oortmerssen, flatb...@googlegroups.com
a) Needed to reverse the packing direction when building the vectors (i had actually already tried this, but the second point was still an issue, so i didn't realise it actually worked) 
b) fix the vector unpacking code. I was passing in a pointer (as required) as it looked like the compiled Init() function initialised the dereferenced object, but it doesn't. So had to initialise first and then pass in a pointer to the initialised object instead.


Reply all
Reply to author
Forward
0 new messages