Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ndjson_extractor.rb « extractors « common « bulk_imports « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 04febebff8e08c53167d03cce85ac56c09102b85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# frozen_string_literal: true

module BulkImports
  module Common
    module Extractors
      class NdjsonExtractor
        def initialize(relation:)
          @relation = relation
          @tmpdir = Dir.mktmpdir
        end

        def extract(context)
          download_service(context).execute
          decompression_service.execute

          records = ndjson_reader.consume_relation('', relation)

          BulkImports::Pipeline::ExtractedData.new(data: records)
        end

        def remove_tmpdir
          FileUtils.remove_entry(tmpdir) if Dir.exist?(tmpdir)
        end

        private

        attr_reader :relation, :tmpdir

        def filename
          "#{relation}.ndjson.gz"
        end

        def download_service(context)
          @download_service ||= BulkImports::FileDownloadService.new(
            configuration: context.configuration,
            relative_url: context.entity.relation_download_url_path(relation),
            tmpdir: tmpdir,
            filename: filename
          )
        end

        def decompression_service
          @decompression_service ||= BulkImports::FileDecompressionService.new(tmpdir: tmpdir, filename: filename)
        end

        def ndjson_reader
          @ndjson_reader ||= Gitlab::ImportExport::Json::NdjsonReader.new(tmpdir)
        end
      end
    end
  end
end