Class: Hvor::Importer

Inherits:
Object
  • Object
show all
Defined in:
app/services/hvor/importer.rb

Overview

Streams IP2Location LITE CSV rows out of a downloaded zip archive and performs an atomic dump-and-repopulate of Hvor::IPRange.

Both files are fully parsed into batches before the database is touched, so a failure downloading/parsing the second file never leaves the table half-populated, and the transaction itself holds no network I/O (just delete_all + insert_all).

Instance Method Summary collapse

Constructor Details

#initialize(batch_size: Hvor.configuration.import_batch_size) ⇒ Importer

Returns a new instance of Importer.



15
16
17
# File 'app/services/hvor/importer.rb', line 15

def initialize(batch_size: Hvor.configuration.import_batch_size)
  @batch_size = batch_size
end

Instance Method Details

#import(ipv4_zip:, ipv6_zip:) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'app/services/hvor/importer.rb', line 19

def import(ipv4_zip:, ipv6_zip:)
  timestamp = Time.current
  ipv4_batches = read_batches(ipv4_zip, ip_version: 0, timestamp: timestamp) { |row| ipv4_attributes(row) }
  ipv6_batches = read_batches(ipv6_zip, ip_version: 1, timestamp: timestamp) { |row| ipv6_attributes(row) }

  Hvor::IPRange.transaction do
    Hvor::IPRange.delete_all
    ipv4_batches.each { |batch| Hvor::IPRange.insert_all(batch) }
    ipv6_batches.each { |batch| Hvor::IPRange.insert_all(batch) }
  end
end