1# Licensed to the Apache Software Foundation (ASF) under one
2# or more contributor license agreements.  See the NOTICE file
3# distributed with this work for additional information
4# regarding copyright ownership.  The ASF licenses this file
5# to you under the Apache License, Version 2.0 (the
6# "License"); you may not use this file except in compliance
7# with the License.  You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17struct Footer <: FlatBuffers.Table
18    bytes::Vector{UInt8}
19    pos::Base.Int
20end
21
22Base.propertynames(x::Footer) = (:version, :schema, :dictionaries, :recordBatches, :custom_metadata)
23
24function Base.getproperty(x::Footer, field::Symbol)
25    if field === :version
26        o = FlatBuffers.offset(x, 4)
27        o != 0 && return FlatBuffers.get(x, o + FlatBuffers.pos(x), MetadataVersion)
28        return MetadataVersion.V1
29    elseif field === :schema
30        o = FlatBuffers.offset(x, 6)
31        if o != 0
32            y = FlatBuffers.indirect(x, o + FlatBuffers.pos(x))
33            return FlatBuffers.init(Schema, FlatBuffers.bytes(x), y)
34        end
35    elseif field === :dictionaries
36        o = FlatBuffers.offset(x, 8)
37        if o != 0
38            return FlatBuffers.Array{Block}(x, o)
39        end
40    elseif field === :recordBatches
41        o = FlatBuffers.offset(x, 10)
42        if o != 0
43            return FlatBuffers.Array{Block}(x, o)
44        end
45    elseif field === :custom_metadata
46        o = FlatBuffers.offset(x, 12)
47        if o != 0
48            return FlatBuffers.Array{KeyValue}(x, o)
49        end
50    end
51    return nothing
52end
53
54footerStart(b::FlatBuffers.Builder) = FlatBuffers.startobject!(b, 4)
55footerAddVersion(b::FlatBuffers.Builder, version::MetadataVersion) = FlatBuffers.prependslot!(b, 0, version, 0)
56footerAddSchema(b::FlatBuffers.Builder, schema::FlatBuffers.UOffsetT) = FlatBuffers.prependoffsetslot!(b, 1, schema, 0)
57footerAddDictionaries(b::FlatBuffers.Builder, dictionaries::FlatBuffers.UOffsetT) = FlatBuffers.prependoffsetslot!(b, 2, dictionaries, 0)
58footerStartDictionariesVector(b::FlatBuffers.Builder, numelems) = FlatBuffers.startvector!(b, 24, numelems, 8)
59footerAddRecordBatches(b::FlatBuffers.Builder, recordbatches::FlatBuffers.UOffsetT) = FlatBuffers.prependoffsetslot!(b, 3, recordbatches, 0)
60footerStartRecordBatchesVector(b::FlatBuffers.Builder, numelems) = FlatBuffers.startvector!(b, 24, numelems, 8)
61footerEnd(b::FlatBuffers.Builder) = FlatBuffers.endobject!(b)
62
63struct Block <: FlatBuffers.Struct
64    bytes::Vector{UInt8}
65    pos::Base.Int
66end
67
68FlatBuffers.structsizeof(::Base.Type{Block}) = 24
69
70Base.propertynames(x::Block) = (:offset, :metaDataLength, :bodyLength)
71
72function Base.getproperty(x::Block, field::Symbol)
73    if field === :offset
74        return FlatBuffers.get(x, FlatBuffers.pos(x), Int64)
75    elseif field === :metaDataLength
76        return FlatBuffers.get(x, FlatBuffers.pos(x) + 8, Int32)
77    elseif field === :bodyLength
78        return FlatBuffers.get(x, FlatBuffers.pos(x) + 16, Int64)
79    end
80    return nothing
81end
82
83function createBlock(b::FlatBuffers.Builder, offset::Int64, metadatalength::Int32, bodylength::Int64)
84    FlatBuffers.prep!(b, 8, 24)
85    prepend!(b, bodylength)
86    FlatBuffers.pad!(b, 4)
87    prepend!(b, metadatalength)
88    prepend!(b, offset)
89    return FlatBuffers.offset(b)
90end