From ca2b57e1e9e773eb55d761c17b5b813b38789721 Mon Sep 17 00:00:00 2001 From: Koichi HARUNA Date: Thu, 26 Nov 2020 18:15:49 +0900 Subject: [PATCH] fix: count number of elements in change requests --- lib/roadworker/batch.rb | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/lib/roadworker/batch.rb b/lib/roadworker/batch.rb index ecb427a..76b0db0 100644 --- a/lib/roadworker/batch.rb +++ b/lib/roadworker/batch.rb @@ -85,10 +85,10 @@ def slice_operations(ops) total_ops = 0 ops.slice_before do |op| total_value_size += op.value_size - total_ops += 1 - if total_value_size > 32000 || total_ops > 1000 + total_ops += op.resource_record_set_size + if total_value_size >= 32000 || total_ops >= 1000 total_value_size = op.value_size - total_ops = 1 + total_ops = op.resource_record_set_size true else false @@ -163,6 +163,20 @@ def value_size end.sum || 0 end + # Count total size of ResourceRecord element in changes + # See also: Batch#slice_operations + # @return [Integer] + def resource_record_set_size + changes.map do |change| + upsert_multiplier = change[:action] == 'UPSERT' ? 2 : 1 + rrset = change[:resource_record_set] + next 0 unless rrset + rrs = rrset[:resource_records] + next 0 unless rrs + rrs.length * upsert_multiplier + end.sum || 0 + end + # @return [Array] def changes raise NotImplementedError @@ -311,11 +325,7 @@ def desired_rrset def changes [ { - action: 'DELETE', - resource_record_set: present_rrset.to_h, - }, - { - action: 'CREATE', + action: 'UPSERT', resource_record_set: desired_rrset.to_h, }, ]