@@ -728,7 +728,6 @@ def write_once(metadata, data, format: nil, size: nil, &block)
728
728
729
729
def write_step_by_step ( metadata , data , format , splits_count , &block )
730
730
splits = [ ]
731
- errors = [ ]
732
731
if splits_count > data . size
733
732
splits_count = data . size
734
733
end
@@ -749,23 +748,23 @@ def write_step_by_step(metadata, data, format, splits_count, &block)
749
748
modified_chunks = [ ]
750
749
modified_metadata = metadata
751
750
get_next_chunk = -> ( ) {
752
- c = if staged_chunk_used
753
- # Staging new chunk here is bad idea:
754
- # Recovering whole state including newly staged chunks is much harder than current implementation.
755
- modified_metadata = modified_metadata . dup_next
756
- generate_chunk ( modified_metadata )
757
- else
758
- synchronize { @stage [ modified_metadata ] ||= generate_chunk ( modified_metadata ) . staged! }
759
- end
760
- modified_chunks << c
761
- c
751
+ if staged_chunk_used
752
+ # Staging new chunk here is bad idea:
753
+ # Recovering whole state including newly staged chunks is much harder than current implementation.
754
+ modified_metadata = modified_metadata . dup_next
755
+ generate_chunk ( modified_metadata )
756
+ else
757
+ synchronize { @stage [ modified_metadata ] ||= generate_chunk ( modified_metadata ) . staged! }
758
+ end
762
759
}
763
760
764
761
writing_splits_index = 0
765
762
enqueue_chunk_before_retry = false
766
763
767
764
while writing_splits_index < splits . size
768
765
chunk = get_next_chunk . call
766
+ errors = [ ]
767
+ modified_chunks << { chunk : chunk , adding_bytesize : 0 , errors : errors }
769
768
chunk . synchronize do
770
769
raise ShouldRetry unless chunk . writable?
771
770
staged_chunk_used = true if chunk . staged?
@@ -851,15 +850,18 @@ def write_step_by_step(metadata, data, format, splits_count, &block)
851
850
raise
852
851
end
853
852
854
- block . call ( chunk , chunk . bytesize - original_bytesize , errors )
855
- errors = [ ]
853
+ modified_chunks . last [ :adding_bytesize ] = chunk . bytesize - original_bytesize
856
854
end
857
855
end
856
+ modified_chunks . each do |data |
857
+ block . call ( data [ :chunk ] , data [ :adding_bytesize ] , data [ :errors ] )
858
+ end
858
859
rescue ShouldRetry
859
- modified_chunks . each do |mc |
860
- mc . rollback rescue nil
861
- if mc . unstaged?
862
- mc . purge rescue nil
860
+ modified_chunks . each do |data |
861
+ chunk = data [ :chunk ]
862
+ chunk . rollback rescue nil
863
+ if chunk . unstaged?
864
+ chunk . purge rescue nil
863
865
end
864
866
end
865
867
enqueue_chunk ( metadata ) if enqueue_chunk_before_retry
0 commit comments