issue #83 - add syntactic sugar to the Tuple class for more idiomatic Ruby Tuple usage in the DSL

This commit is contained in:
Colin Surprenant 2013-06-14 01:38:27 -04:00
parent 3d717383e9
commit 28c9ca3009
4 changed files with 111 additions and 14 deletions

View File

@ -0,0 +1,34 @@
java_import 'backtype.storm.tuple.Tuple'
java_import 'backtype.storm.tuple.TupleImpl'
module RedStorm
module DSL
class TupleError < StandardError; end
end
end
class TupleImpl
def value(i)
case i
when Fixnum
getValue(i)
when String
getValueByField(i)
when Symbol
getValueByField(i.to_s)
else
raise(RedStorm::DSL::TupleError, "unsupported tuple index class=#{i.class.to_s} for #{i.inspect}")
end
end
alias_method :[], :value
def field_index(field)
fieldIndex(field.to_s)
end
def contains?(field)
contains(field.to_s)
end
end

View File

@ -7,7 +7,7 @@ rescue
end
require 'jruby/jrubyc'
require 'red_storm'
require 'red_storm/environment'
require 'red_storm/application'
INSTALL_IVY_VERSION = "2.3.0"
@ -15,7 +15,7 @@ INSTALL_IVY_VERSION = "2.3.0"
task :launch, :env, :ruby_mode, :class_file do |t, args|
# use ruby mode parameter or default to current interpreter version
version_token = RedStorm.jruby_mode_token(args[:ruby_mode])
command = case args[:env]
when "local"
RedStorm::Application.local_storm_command(args[:class_file], args[:ruby_mode])
@ -43,16 +43,16 @@ end
task :setup do
puts("\n--> Setting up target directories")
ant.mkdir 'dir' => TARGET_DIR
ant.mkdir 'dir' => TARGET_CLASSES_DIR
ant.mkdir 'dir' => TARGET_DIR
ant.mkdir 'dir' => TARGET_CLASSES_DIR
ant.mkdir 'dir' => TARGET_DEPENDENCY_DIR
ant.mkdir 'dir' => TARGET_SRC_DIR
ant.mkdir 'dir' => TARGET_GEM_DIR
ant.mkdir 'dir' => TARGET_SPECS_DIR
ant.path 'id' => 'classpath' do
fileset 'dir' => TARGET_DEPENDENCY_DIR
fileset 'dir' => TARGET_CLASSES_DIR
end
ant.path 'id' => 'classpath' do
fileset 'dir' => TARGET_DEPENDENCY_DIR
fileset 'dir' => TARGET_CLASSES_DIR
end
end
task :install => [:deps, :build] do
@ -148,10 +148,10 @@ task :deps => "ivy:install" do
ant.configure 'file' => File.exists?(CUSTOM_IVY_SETTINGS) ? CUSTOM_IVY_SETTINGS : DEFAULT_IVY_SETTINGS
ant.resolve 'file' => File.exists?(CUSTOM_IVY_STORM_DEPENDENCIES) ? CUSTOM_IVY_STORM_DEPENDENCIES : DEFAULT_IVY_STORM_DEPENDENCIES
ant.retrieve 'pattern' => "#{TARGET_DEPENDENCY_DIR}/storm/[conf]/[artifact]-[revision].[ext]", 'sync' => "true"
ant.resolve 'file' => File.exists?(CUSTOM_IVY_STORM_DEPENDENCIES) ? CUSTOM_IVY_STORM_DEPENDENCIES : DEFAULT_IVY_STORM_DEPENDENCIES
ant.retrieve 'pattern' => "#{TARGET_DEPENDENCY_DIR}/storm/[conf]/[artifact]-[revision].[ext]", 'sync' => "true"
ant.resolve 'file' => File.exists?(CUSTOM_IVY_TOPOLOGY_DEPENDENCIES) ? CUSTOM_IVY_TOPOLOGY_DEPENDENCIES : DEFAULT_IVY_TOPOLOGY_DEPENDENCIES
ant.resolve 'file' => File.exists?(CUSTOM_IVY_TOPOLOGY_DEPENDENCIES) ? CUSTOM_IVY_TOPOLOGY_DEPENDENCIES : DEFAULT_IVY_TOPOLOGY_DEPENDENCIES
ant.retrieve 'pattern' => "#{TARGET_DEPENDENCY_DIR}/topology/[conf]/[artifact]-[revision].[ext]", 'sync' => "true"
end
@ -204,7 +204,7 @@ def build_java_dir(source_folder)
ant.javac(
'srcdir' => source_folder,
'destdir' => TARGET_CLASSES_DIR,
'classpathref' => 'classpath',
'classpathref' => 'classpath',
'source' => "1.7",
'target' => "1.7",
'debug' => "yes",
@ -213,8 +213,8 @@ def build_java_dir(source_folder)
'listfiles' => true
) do
# compilerarg :value => "-Xlint:unchecked"
end
end
end
end
def build_jruby(source_path)
puts("\n--> Compiling JRuby")

View File

@ -0,0 +1,59 @@
require 'java'
require 'spec_helper'
require 'red_storm/dsl/tuple'
java_import 'backtype.storm.Testing'
java_import 'backtype.storm.tuple.Values'
describe "Tuple" do
it "should return value by index" do
tuple = Testing.testTuple(Values.new("james", "bond"))
tuple[0].should == "james"
tuple[1].should == "bond"
lambda {tuple[2]}.should raise_error
end
it "should return value by field string" do
tuple = Testing.testTuple(Values.new("james", "bond"))
tuple["field1"].should == "james"
tuple["field2"].should == "bond"
lambda {tuple["field3"]}.should raise_error
end
it "should return value by field symbol" do
tuple = Testing.testTuple(Values.new("james", "bond"))
tuple[:field1].should == "james"
tuple[:field2].should == "bond"
lambda {tuple[:field3]}.should raise_error
end
it "should return field_index with field string" do
tuple = Testing.testTuple(Values.new("james", "bond"))
tuple.field_index("field1").should == 0
tuple.field_index("field2").should == 1
lambda {tuple.field_index("field3")}.should raise_error
end
it "should return field_index with field symbol" do
tuple = Testing.testTuple(Values.new("james", "bond"))
tuple.field_index(:field1).should == 0
tuple.field_index(:field2).should == 1
lambda {tuple.field_index(:field3)}.should raise_error
end
it "should return contains? with field string" do
tuple = Testing.testTuple(Values.new("james", "bond"))
tuple.contains?("field1").should be_true
tuple.contains?("field2").should be_true
tuple.contains?("field3").should be_false
end
it "should return contains? with field symbol" do
tuple = Testing.testTuple(Values.new("james", "bond"))
tuple.contains?(:field1).should be_true
tuple.contains?(:field2).should be_true
tuple.contains?(:field3).should be_false
end
end

View File

@ -2,3 +2,7 @@ $:.unshift File.dirname(__FILE__) + '/../lib/'
$:.unshift File.dirname(__FILE__) + '/../spec'
require 'rspec'
# load Storm jars
storm_jars = File.dirname(__FILE__) + '/../target/dependency/storm/default/*.jar'
Dir.glob(storm_jars).each{|f| require f}