<commit_before>class Wdpa::Release
  IMPORT_VIEW_NAME = "imported_protected_areas"

  def self.download
    wdpa_release = self.new
    wdpa_release.download

    wdpa_release
  end

  def initialize
    @start_time = Time.now
  end

  def download
    Wdpa::S3.download_current_wdpa_to filename: zip_path
    system("unzip -j '#{zip_path}' '\*.gdb/\*' -d '#{gdb_path}'")

    import_tables.each do |original_table, std_table|
      Ogr::Postgres.import(
        gdb_path,
        original_table,
        std_table
      )
    end

    create_import_view
  end

  def import_tables
    src_table_name = source_table
    geometry_tables.merge({src_table_name => src_table_name})
  end

  def geometry_tables
    gdb_metadata = Ogr::Info.new(gdb_path)
    geometry_tables = gdb_metadata.layers_matching(
      Wdpa::DataStandard::Matchers::GEOMETRY_TABLE
    )

    geometry_tables.each_with_object({}) do |tbl, hash|
      hash[tbl] = Wdpa::DataStandard.standardise_table_name(tbl)
    end
  end

  def source_table
    gdb_metadata = Ogr::Info.new(gdb_path)
    gdb_metadata.layers_matching(Wdpa::DataStandard::Matchers::SOURCE_TABLE).first
  end

  def create_import_view
    attributes = Wdpa::DataStandard.common_attributes.join(', ')
    create_query = "CREATE OR REPLACE VIEW #{IMPORT_VIEW_NAME} AS "
<commit_msg>memoize source_table in release.rb<commit_after>class Wdpa::Release
  IMPORT_VIEW_NAME = "imported_protected_areas"

  def self.download
    wdpa_release = self.new
    wdpa_release.download

    wdpa_release
  end

  def initialize
    @start_time = Time.now
  end

  def download
    Wdpa::S3.download_current_wdpa_to filename: zip_path
    system("unzip -j '#{zip_path}' '\*.gdb/\*' -d '#{gdb_path}'")

    import_tables.each do |original_table, std_table|
      Ogr::Postgres.import(
        gdb_path,
        original_table,
        std_table
      )
    end

    create_import_view
  end

  def import_tables
    geometry_tables.merge({source_table => source_table})
  end

  def geometry_tables
    gdb_metadata = Ogr::Info.new(gdb_path)
    geometry_tables = gdb_metadata.layers_matching(
      Wdpa::DataStandard::Matchers::GEOMETRY_TABLE
    )

    geometry_tables.each_with_object({}) do |tbl, hash|
      hash[tbl] = Wdpa::DataStandard.standardise_table_name(tbl)
    end
  end

  def source_table
    gdb_metadata = Ogr::Info.new(gdb_path)
    @source_table ||= gdb_metadata.layers_matching(Wdpa::DataStandard::Matchers::SOURCE_TABLE).first
  end

  def create_import_view
    attributes = Wdpa::DataStandard.common_attributes.join(', ')
    create_query = "CREATE OR REPLACE VIEW #{IMPORT_VIEW_NAME} AS "

{'fraction_non_alphanumeric': 0.09708001516875236, 'fraction_numerical': 0.0007584376185058779, 'mean_word_length': 3.6773049645390072, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '635832', 'n_tokens_mistral': 941, 'n_tokens_neox': 912, 'n_words': 156}