The irony of FlatTables is that the automatic code generation produces all your package-specific library code
and godoc documentation, along with tests and a main that illustrates how to use your specific FlatTables
package. Which means there is absolutely no (zero, zilch) godoc for FlatTables itself. Your invocation
of the flattablesc CLI will generate your own godoc. Here's the
github.com/urban-wombat/flattables_sample godoc
to give you an idea of what your generated godoc will look like.
If you hit a wall or feel that something is missing or unclear, email to: urban.wombat.burrow@gmail.com
Don't be daunted by the large number of functions and methods that are generated by flattablesc.
Look at the following two links to help narrow down your own path through the code:-
- https://github.com/urban-wombat/flattables_sample/blob/master/cmd/flattables_sample/flattables_sample_main.go which is a runnable main example.
- https://godoc.org/github.com/urban-wombat/flattables_sample#pkg-examples
flattables_sample is nothing special. I just called it that (the package name) and ran flattablesc on this
tables.got file.
I did not hand-code any of it. flattablesc will generate all the same kinds of examples and useable code for you from your own set of tables. You will then see exactly how to write your own code to use FlatBuffers. The main restrictions are:-
flattablesis tabular only- there are some table and columns names that are illegal (such as
for) to not break generated Go code - some
FlatBuffersdata-type and style-guide constraints such as:-- fixed sizes (no int or uint)
- lowercase column names and uppercase table names
-
Install FlatBuffers
go get -u github.com/google/flatbuffersFor more information:
-
Install
gotablesandFlatTablesgo get -u github.com/urban-wombat/gotables go get -u github.com/urban-wombat/flattablesRelationships between the packages:
flattablesusesgotablesflattablescusesflattablesandgotables
-
Create your directory
my_package$ mkdir my_packagemy_package(or whatever you decide to call it) will be your namespace and tail-end of your package name. -
Create your
FlatTables/data-file - this is a set ofgotablestables, not a flatbuffers.fbsfile.It doesn't matter where you create it or what you call it. But for simplicity, let's call it
tables.gotand create it in your newly-created directorymy_package.The table names, column names and types are used to generate the
FlatBuffersschema file*.fbs(the*.fbsfile is auto-generated by flattablesc, not your problem).The data in the tables is used in the auto-generated bench tests. So add some dummy data for testing.
You can come up with your own tables of data, or you may copy and paste the tables below into
tables.gotThe
gotablessyntax is self-evident and mostGotypes are supported.Types not supported are:-
intanduint(their sizes are machine-dependent, andFlatBuffersallows fixed-sizes only)complex32complex64(not supported by gotables)rune(doesn't seem to be supported byFlatBuffers, perhaps because its size varies)
If you just want to get started and not deal with creating your own
gotablesschema right now, just copy and paste the tables below intotables.gotand proceed to the next step. You can go back later and whip up data that looks like your own.package flattables_sample is generated from tables.got which is end of week data from the Australian Stock Exchange (ASX) to give a sense of the kind of high volume data that can be written to Google
FlatBuffersbinary format.Below is a different example set of tables, different to what the current
flattables_samplecode is generated from, to give a sense of the flexibility of multiple tables. As many tables as you need to express your information payload.
[MyXyzTable]
x y z
int16 float32 float64
4 5 6
44 55 66
444 555 666
4444 5555 6666
16 32 64
[StringsAndThings]
flintstones nums spain female unsigned
string int8 string bool uint32
"Fred" 0 "The rain" false 0
"Wilma" 1 "in Spain" true 11
"Barney" 2 "stays mainly" false 22
"Betty" 3 "in" true 33
"Bam Bam" 4 "the" false 44
"Pebbles" 5 "plain." true 55
[Wombats]
housingPolicy string = "burrow"
topSpeedKmH int8 = 40
species string = "Vombatus"
class string = "Mammalia"
wild bool = true
-
Check its validity with
gotsyntax$ gotsyntax tables.gotThe
FlatTablesutilityflattablescwill also do a syntax check, but you might as well get earlier feedback withgotsyntax.flattablescalso invokes the GoogleFlatTablesflatccode generator. It doesn't seem to police the FlatBuffers style guide butflattablescdoes.flattablescalso guards against some gotchas specific to generatingGocode.
FlatTables is also a little more strict than gotables syntax:
- Table names must be
UpperCamelCaseas per the FlatBuffers style guide - Column names must be
lowerCamelCaseas per the FlatBuffers style guide - Table names or column names that so much as look like
Gokey words are not permitted. Table and column names end up as function and variable names in generatedGocode, and theGocompiler doesn't like key words as names. So we don't risk it. - Transfers between
Goslices andFlatBuffersrequire the field names to be exported (henceUpperCamelCase) which is done by code generation. So there's a (managed) difference between the FlatBuffers style guide and the need to exportGofields to user code. Languages such as Java convert field names tolowerCamelCase, which is whatFlatTablesrequires here, consistent withGounexported fields. But they are exported asUpperCamelCasewhere needed in rawGostructs used byFlatTables, namely:
type RootTableSlice struct {...}
See a sample RootTableSlice definition in
flattables_sample_NewSliceFromFlatBuffers.go
(near line 70)
type RootTableSlice is generated for you based on your tables.got schema file and the *fbs schema file.
-
From within dir
my_packagerun theFlatTablesutilityflattablesc$ flattablesc -f ../my_package/tables.got -n my_package -p github.com/your-github-name/my_packageflattablesccreates a flatbuffers schema*.fbsfile and a number ofGosource files in../my_package. -
Run the tests
$ go test -bench=.
Have a look at the Google FlatBuffers official documentation to see
why you should seriously consider FlatBuffers (and FlatTables)
for very fast binary data transfer:
If your data is tabular (or can be easily normalised to tabular) then FlatTables
may be right for your project.
The FlatTables utility flattablesc will generate all the code needed to convert
from gotables tabular format to
FlatBuffers and back again.
flattablesc also generates a Go main program with constructor and getter methods
specific to your FlatBuffers schema.
The generated code includes:
-
conversion functions (which include all the code generated by the
FlatBuffersutilityflatc) -
test code
-
test Example code (an Example for each of the key functions)
-
benchmark tests which will run with the data you put into your
tables.gotschema file. -
a main program with sample code
-
There is a sample implementation using a
gotablesfile tables.got as input to theflattablescutility. The same way you would create your own implementation. It is calledflattables_sample. It is an actual implementation, and not just a toy sample.
When you run flattablesc on your own gotables schema file, it will generate
your raw Go struct tables, functions, examples and benchtests.
Have a look at urban-wombat/flattables_sample
which is a sample of FlatBuffers code generated entirely by flatc (FlatBuffers utility)
and flattablesc (gotables FlatTables utility).
Here is the GoDoc of all 90 or so Go functions generated by the Google flatc utility and gotables flattablesc utility:
- https://godoc.org/github.com/urban-wombat/flattables_sample
- https://github.com/urban-wombat/flattables_sample/blob/master/cmd/flattables_sample/flattables_sample_main.go
And test and benchmark code:
The main function in urban-wombat/flattables_sample_main
is the simplest possible conversion code, if you don't want to get into
the weeds of moving data into and out of FlatBuffers.
ALL of the code, including the FlatBuffers schema and all Go code,
was generated automatically from flatc and flattablesc.
When you download and run flattablesc (referencing a simple
gotables file you write yourself)
you can run the tests and benchtest and see the speed of FlatBuffers.
-
FlatTablesauto-generates theFlatBuffersschema from your data. All you need to do is write a very simple self-describinggotablesdata file (sample below). This means normalising your objects to one or more tables (tabular tables, like database tables).FlatBuffersandFlatTablesuse 'table' in a slightly different sense, but if you see them as tabular tables, it makes sense.gotablesis the supporting library and file format used byFlatTables. -
FlatBuffersutilityflatcgeneratesGocode to write (and read) data conforming to theFlatBuffersschema. -
FlatTablesgeneratesGocode to writegotables-formatted data to aFlatBuffers[]byte array. -
FlatTablesgeneratesGocode to test that data has been written toFlatBufferscorrectly. -
The read step is very fast. There is no additional code between you and the auto-generated
FlatBufferscode. (Note: the read step at this stage is read-only. This may get better with the implementation of mutable tables) -
You write only your own code to call these auto-generated methods, and denormalise the data from tables to your prefered data structures.
-
FlatTablesuses a subset of GoogleFlatBuffersas a binary format forgotablesTable objects. -
FlatTablesis general purpose because it consists of tables, and your own data is probably capable of being normalised (in Ted Codd, C J Date fashion) to one or more relational tables ready for transmission and re-assembly at the receiving end. -
The Example functions in the
*.test.gofile will get you started coding data transfers. -
You don't have to write the
*.fbsflatbuffers schemaflattables_sample.fbs. It is done for you. -
You don't have to write the glue code to get data from
tables.gotto a flatbuffers []byte array.