2021-05-27 18:10:52 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module BulkImports
|
|
|
|
module Common
|
|
|
|
module Extractors
|
|
|
|
class NdjsonExtractor
|
|
|
|
include Gitlab::ImportExport::CommandLineUtil
|
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
|
|
|
def initialize(relation:)
|
|
|
|
@relation = relation
|
|
|
|
@tmp_dir = Dir.mktmpdir
|
|
|
|
end
|
|
|
|
|
|
|
|
def extract(context)
|
|
|
|
download_service(tmp_dir, context).execute
|
|
|
|
decompression_service(tmp_dir).execute
|
|
|
|
relations = ndjson_reader(tmp_dir).consume_relation('', relation)
|
|
|
|
|
|
|
|
BulkImports::Pipeline::ExtractedData.new(data: relations)
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_tmp_dir
|
|
|
|
FileUtils.remove_entry(tmp_dir)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :relation, :tmp_dir
|
|
|
|
|
|
|
|
def filename
|
|
|
|
@filename ||= "#{relation}.ndjson.gz"
|
|
|
|
end
|
|
|
|
|
|
|
|
def download_service(tmp_dir, context)
|
|
|
|
@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),
|
2021-05-27 18:10:52 +00:00
|
|
|
dir: tmp_dir,
|
2021-10-19 18:13:24 +00:00
|
|
|
filename: filename
|
2021-05-27 18:10:52 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def decompression_service(tmp_dir)
|
2021-10-19 18:13:24 +00:00
|
|
|
@decompression_service ||= BulkImports::FileDecompressionService.new(dir: tmp_dir, filename: filename)
|
2021-05-27 18:10:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def ndjson_reader(tmp_dir)
|
2021-06-11 18:10:13 +00:00
|
|
|
@ndjson_reader ||= Gitlab::ImportExport::Json::NdjsonReader.new(tmp_dir)
|
2021-05-27 18:10:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|