Class: Gitlab::ImportExport::FastHashSerializer::JSONBatchRelation

Inherits:
Object
  • Object
show all
Includes:
Utils::StrongMemoize
Defined in:
lib/gitlab/import_export/fast_hash_serializer.rb

Overview

Usage of this class results in delayed serialization of relation. The serialization will be triggered when the `JSON.generate` is exected.

This class uses memory-optimised, lazily initialised, fast to recycle relation serialization.

The `JSON.generate` does use `#to_json`, that returns raw JSON content that is written directly to file.

Instance Method Summary collapse

Methods included from Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Constructor Details

#initialize(relation, options, preloads) ⇒ JSONBatchRelation

Returns a new instance of JSONBatchRelation.


44
45
46
47
48
# File 'lib/gitlab/import_export/fast_hash_serializer.rb', line 44

def initialize(relation, options, preloads)
  @relation = relation
  @options = options
  @preloads = preloads
end

Instance Method Details

#as_jsonObject

Raises:

  • (NotImplementedError)

69
70
71
# File 'lib/gitlab/import_export/fast_hash_serializer.rb', line 69

def as_json(*)
  raise NotImplementedError
end

#raw_jsonObject


50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/gitlab/import_export/fast_hash_serializer.rb', line 50

def raw_json
  strong_memoize(:raw_json) do
    result = +''

    batch = @relation
    batch = batch.preload(@preloads) if @preloads
    batch.each do |item|
      result.concat(",") unless result.empty?
      result.concat(item.to_json(@options))
    end

    result
  end
end

#to_json(options = {}) ⇒ Object


65
66
67
# File 'lib/gitlab/import_export/fast_hash_serializer.rb', line 65

def to_json(options = {})
  raw_json
end