debian-mirror-gitlab/lib/gitlab/import_export/json/ndjson_reader.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

63 lines
1.7 KiB
Ruby
Raw Normal View History

2020-04-22 19:07:51 +05:30
# frozen_string_literal: true
module Gitlab
module ImportExport
2021-09-04 01:27:46 +05:30
module Json
2020-04-22 19:07:51 +05:30
class NdjsonReader
MAX_JSON_DOCUMENT_SIZE = 50.megabytes
attr_reader :dir_path
def initialize(dir_path)
@dir_path = dir_path
@consumed_relations = Set.new
end
def exist?
Dir.exist?(@dir_path)
end
def consume_attributes(importable_path)
# This reads from `tree/project.json`
path = file_path("#{importable_path}.json")
2023-06-20 00:43:36 +05:30
2023-09-09 18:01:29 +05:30
if !File.exist?(path) || Gitlab::Utils::FileInfo.linked?(path)
raise Gitlab::ImportExport::Error, 'Invalid file'
end
2023-06-20 00:43:36 +05:30
2020-04-22 19:07:51 +05:30
data = File.read(path, MAX_JSON_DOCUMENT_SIZE)
json_decode(data)
end
2021-01-29 00:20:46 +05:30
def consume_relation(importable_path, key, mark_as_consumed: true)
2020-04-22 19:07:51 +05:30
Enumerator.new do |documents|
2021-01-29 00:20:46 +05:30
next if mark_as_consumed && !@consumed_relations.add?("#{importable_path}/#{key}")
2020-04-22 19:07:51 +05:30
# This reads from `tree/project/merge_requests.ndjson`
path = file_path(importable_path, "#{key}.ndjson")
2021-01-03 14:25:43 +05:30
2023-09-09 18:01:29 +05:30
next if !File.exist?(path) || Gitlab::Utils::FileInfo.linked?(path)
2020-04-22 19:07:51 +05:30
File.foreach(path, MAX_JSON_DOCUMENT_SIZE).with_index do |line, line_num|
documents << [json_decode(line), line_num]
end
end
end
private
def json_decode(string)
2021-10-27 15:23:28 +05:30
Gitlab::Json.parse(string)
rescue JSON::ParserError => e
2020-04-22 19:07:51 +05:30
Gitlab::ErrorTracking.log_exception(e)
raise Gitlab::ImportExport::Error, 'Incorrect JSON format'
end
def file_path(*path)
File.join(dir_path, *path)
end
end
end
end
end