# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

require_relative "streaming-writer"

module ArrowFormat
  class FileWriter < StreamingWriter
    MAGIC = "ARROW1".b
    MAGIC_PADDING = "\x00\x00"

    def start(schema)
      @fb_schema = schema.to_flatbuffers
      write_data(MAGIC)
      write_data(MAGIC_PADDING)
      super
    end

    def finish(metadata=nil)
      super()
      write_footer(metadata)
      write_data(MAGIC)
      @output
    end

    private
    def build_footer(metadata)
      fb_footer = FB::Footer::Data.new
      fb_footer.version = FB::MetadataVersion::V5
      fb_footer.schema = @fb_schema
      fb_footer.dictionaries = @fb_dictionary_blocks
      fb_footer.record_batches = @fb_record_batch_blocks
      fb_footer.custom_metadata = FB.build_custom_metadata(metadata)
      FB::Footer.serialize(fb_footer)
    end

    def write_footer(metadata)
      footer = build_footer(metadata)
      write_data(footer)
      write_data([footer.bytesize].pack("l<"))
    end
  end
end
