Class: Fluent::Plugin::JsonSizeLimit::RecordReducer

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/plugin/json_size_limit/record_reducer.rb

Overview

Reduces string values proportionally while preserving record shape, aliases, key names, encodings, and non-string values.

Defined Under Namespace

Classes: StringLocation

Constant Summary collapse

HASH_EACH_PAIR =
Hash.instance_method(:each_pair)
ARRAY_EACH_WITH_INDEX =
Array.instance_method(:each_with_index)
HASH_SET =
Hash.instance_method(:[]=)
ARRAY_SET =
Array.instance_method(:[]=)

Instance Method Summary collapse

Constructor Details

#initialize(record, original_json_bytes:, max_json_bytes:, guard: nil, validated_record: false) ⇒ RecordReducer

Returns a new instance of RecordReducer.



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/fluent/plugin/json_size_limit/record_reducer.rb', line 37

def initialize(record, original_json_bytes:, max_json_bytes:, guard: nil, validated_record: false)
  @record = record
  @original_json_bytes = original_json_bytes
  @max_json_bytes = max_json_bytes
  @guard = guard || ProcessingGuard.new(
    timeout: 5,
    max_nodes: 1_000_000,
    max_input_bytes: Float::INFINITY,
    max_nesting: 100
  )
  @validated_record = validated_record
end

Instance Method Details

#reduce_to_limitObject



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fluent/plugin/json_size_limit/record_reducer.rb', line 50

def reduce_to_limit
  locations, original_content_bytes = collect_string_locations
  return @original_json_bytes if locations.empty?

  immutable_json_bytes = @original_json_bytes - original_content_bytes
  content_budget = @max_json_bytes - immutable_json_bytes
  @guard.reset_phase!(track_input: false)
  replacements, current_content_bytes = build_replacements(locations, content_budget, original_content_bytes)

  @guard.reset_phase!(track_input: false)
  apply_replacements(replacements)
  immutable_json_bytes + current_content_bytes
end