-
-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for aggregating transforms (see #53)
- Loading branch information
Showing
4 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class AggregateTransform | ||
def initialize(aggregate_size:) | ||
@aggregate_size = aggregate_size | ||
end | ||
|
||
def process(row) | ||
@buffer ||= [] | ||
@buffer << row | ||
if @buffer.size == @aggregate_size | ||
yield @buffer | ||
@buffer = [] | ||
end | ||
nil | ||
end | ||
|
||
def close | ||
yield @buffer unless @buffer.empty? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class NonClosingTransform | ||
def process(row) | ||
row | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
require_relative 'helper' | ||
require 'kiba/cli' | ||
require_relative 'support/test_aggregate_transform' | ||
require_relative 'support/test_non_closing_transform' | ||
|
||
class TestBufferingTransform < Kiba::Test | ||
def test_buffering_transform | ||
destination_array = [] | ||
job = Kiba.parse do | ||
extend Kiba::DSLExtensions::Config | ||
config :kiba, runner: Kiba::StreamingRunner | ||
|
||
source TestEnumerableSource, (1..12) | ||
# ensure that a non closing transform won't raise an error | ||
transform NonClosingTransform | ||
transform AggregateTransform, aggregate_size: 5 | ||
destination TestArrayDestination, destination_array | ||
end | ||
Kiba.run(job) | ||
assert_equal [ | ||
[1, 2, 3, 4, 5], | ||
[6, 7, 8, 9, 10], | ||
[11, 12] | ||
], destination_array | ||
end | ||
end |