`...ByKey()` returns null in Kotlin flatbuffers impl

117 Views Asked by At

I have the following schema:

...
table FiltersByDomain {
  domain: string (key);
  filters: [FilterIsIncluded] (required);
}

table Index {
  ...
  filters_by_domain: [FiltersByDomain];
   ...
}

root_type Index;

When i use the getter by index and collect the keys i'm able to find some value:

        val domains = mutableMapOf<String, Int>()
        for (i in 0 until index.filtersByDomainLength) {
            val pair = index.filtersByDomain(i)!!
            domains[pair.domain!!] = pair.filtersLength
        }
        assertTrue(domains.keys.contains("scifinow.co.uk"))
        val someDomain = "dcw50.com"
        assertEquals(3, domains[someDomain]) // succeeds

If i use generated ..ByKey method it returns null:

        assertEquals(3, index.filtersByDomainByKey(someDomain)!!.filtersLength) // fails

Here is the test (on Android).

PS. Using api 'com.google.flatbuffers:flatbuffers-java:1.12.0', the files were generated with flatc --kotlin -o ../java schema.fbs

PPS. Just out of curiosity i've tried generated with flatc --java -o ../java schema.fbs Java classes and it also fails.

> flatc --version
flatc version 1.12.0
1

There are 1 best solutions below

0
On BEST ANSWER

The vector that contains the tables needs to be sorted by the key field before serialization for lookup with the ByKey method to work (it is implemented as a binary search, since FlatBuffers in an in-place zero-copy format, the lookup happens in-place too, instead of constructing a dictionary at the destination).