2021-05-27 18:10:52 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module BulkImports
|
|
|
|
module Common
|
|
|
|
module Extractors
|
|
|
|
class NdjsonExtractor
|
|
|
|
def initialize(relation:)
|
|
|
|
@relation = relation
|
2022-01-18 12:16:49 +00:00
|
|
|
@tmpdir = Dir.mktmpdir
|
2021-05-27 18:10:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def extract(context)
|
2022-01-18 12:16:49 +00:00
|
|
|
download_service(context).execute
|
|
|
|
decompression_service.execute
|
|
|
|
|
|
|
|
records = ndjson_reader.consume_relation('', relation)
|
2021-05-27 18:10:52 +00:00
|
|
|
|
2022-01-18 12:16:49 +00:00
|
|
|
BulkImports::Pipeline::ExtractedData.new(data: records)
|
2021-05-27 18:10:52 +00:00
|
|
|
end
|
|
|
|
|
2022-01-18 12:16:49 +00:00
|
|
|
def remove_tmpdir
|
|
|
|
FileUtils.remove_entry(tmpdir) if Dir.exist?(tmpdir)
|
2021-05-27 18:10:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2022-01-18 12:16:49 +00:00
|
|
|
attr_reader :relation, :tmpdir
|
2021-05-27 18:10:52 +00:00
|
|
|
|
|
|
|
def filename
|
2022-01-18 12:16:49 +00:00
|
|
|
"#{relation}.ndjson.gz"
|
2021-05-27 18:10:52 +00:00
|
|
|
end
|
|
|
|
|
2022-01-18 12:16:49 +00:00
|
|
|
def download_service(context)
|
2021-05-27 18:10:52 +00:00
|
|
|
@download_service ||= BulkImports::FileDownloadService.new(
|
|
|
|
configuration: context.configuration,
|
2021-10-19 18:13:24 +00:00
|
|
|
relative_url: context.entity.relation_download_url_path(relation),
|
2022-01-18 12:16:49 +00:00
|
|
|
tmpdir: tmpdir,
|
2021-10-19 18:13:24 +00:00
|
|
|
filename: filename
|
2021-05-27 18:10:52 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-01-18 12:16:49 +00:00
|
|
|
def decompression_service
|
|
|
|
@decompression_service ||= BulkImports::FileDecompressionService.new(tmpdir: tmpdir, filename: filename)
|
2021-05-27 18:10:52 +00:00
|
|
|
end
|
|
|
|
|
2022-01-18 12:16:49 +00:00
|
|
|
def ndjson_reader
|
|
|
|
@ndjson_reader ||= Gitlab::ImportExport::Json::NdjsonReader.new(tmpdir)
|
2021-05-27 18:10:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|