-
-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
StreamingRunner (improved version with config-based opt-in) (#44)
Introduce new StreamingRunner (#44)
- Loading branch information
Showing
11 changed files
with
142 additions
and
9 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
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,9 @@ | ||
module Kiba | ||
module DSLExtensions | ||
module Config | ||
def config(context, context_config) | ||
(@control.config[context] ||= {}).merge!(context_config) | ||
end | ||
end | ||
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
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,33 @@ | ||
module Kiba | ||
module StreamingRunner | ||
include Runner | ||
extend self | ||
|
||
def transform_stream(stream, t) | ||
Enumerator.new do |y| | ||
stream.each do |input_row| | ||
returned_row = t.process(input_row) do |yielded_row| | ||
y << yielded_row | ||
end | ||
y << returned_row if returned_row | ||
end | ||
end | ||
end | ||
|
||
def source_stream(sources) | ||
Enumerator.new do |y| | ||
sources.each do |source| | ||
source.each { |r| y << r } | ||
end | ||
end | ||
end | ||
|
||
def process_rows(sources, transforms, destinations) | ||
stream = source_stream(sources) | ||
recurser = lambda { |stream,t| transform_stream(stream, t) } | ||
transforms.inject(stream, &recurser).each do |r| | ||
destinations.each { |d| d.write(r) } | ||
end | ||
end | ||
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
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,9 @@ | ||
class TestArrayDestination | ||
def initialize(array) | ||
@array = array | ||
end | ||
|
||
def write(row) | ||
@array << 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,8 @@ | ||
class TestYieldingTransform | ||
def process(row) | ||
row.fetch(:tags).each do |value| | ||
yield({item: value}) | ||
end | ||
{item: "classic-return-value"} | ||
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
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 |
---|---|---|
@@ -1,14 +1,6 @@ | ||
require_relative 'helper' | ||
require 'minitest/mock' | ||
require_relative 'support/test_enumerable_source' | ||
require_relative 'common/runner' | ||
|
||
class TestRunner < Kiba::Test | ||
include SharedRunnerTests | ||
|
||
def kiba_run(job) | ||
runner = Object.new | ||
runner.extend(Kiba::Runner) | ||
runner.run(job) | ||
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,33 @@ | ||
require_relative 'helper' | ||
require_relative 'support/test_enumerable_source' | ||
require_relative 'support/test_array_destination' | ||
require_relative 'support/test_yielding_transform' | ||
require_relative 'common/runner' | ||
|
||
class TestStreamingRunner < Kiba::Test | ||
include SharedRunnerTests | ||
|
||
def test_yielding_class_transform | ||
input_row = {tags: ["one", "two", "three"]} | ||
destination_array = [] | ||
|
||
job = Kiba.parse do | ||
extend Kiba::DSLExtensions::Config | ||
|
||
config :kiba, runner: Kiba::StreamingRunner | ||
|
||
source TestEnumerableSource, [input_row] | ||
transform TestYieldingTransform | ||
destination TestArrayDestination, destination_array | ||
end | ||
|
||
kiba_run(job) | ||
|
||
assert_equal [ | ||
{item: 'one'}, | ||
{item: 'two'}, | ||
{item: 'three'}, | ||
{item: 'classic-return-value'} | ||
], destination_array | ||
end | ||
end |