Search code examples
c#flatbuffers

encapsulate an old buffer in a new one


So I have this sort of scheme:

table Request {
  a:Sample;
  b:Sample;
}

Where table Sample has multiple string vectors and instances of it are pretty big. I have a lot of files on my filesystem with Sample instances, which took me some time to create. Now I want to take 2 files at random, read them into memory and create a new Request which encapsulates them.

I'm working with c# and this line works:

var a = Sample.GetRootAsSample(new ByteBuffer(System.IO.File.ReadAllBytes(pathToA))); 
var b = Sample.GetRootAsSample(new ByteBuffer(System.IO.File.ReadAllBytes(pathTob)));

but I can't seem to find a way to just reference them in a new Request instance. I need some way of adding those buffers as is to a new builder and then pass their Offset to the new Request all in the same builder. Building them all over again in a new builder wouldn't be efficient.

How can I achieve this?


Solution

  • There's currently no way to deep-copy a table automatically in C#. Since tables may refer to all sorts of locations in the buffer, this is not a trivial operation, which requires either special purpose code generation or reflection.

    There's a CopyTable in C++ using reflection. This could be ported to C# or called from C#.

    An alternative is to include the existing buffers in the new table in binary form, i.e. make a and b a vector of ubytes. It means you have to call GetRootAs on them to access them, but this is all still very efficient.