org.apache.pig.scripting.jruby
Class PigJrubyLibrary

java.lang.Object
  extended by org.apache.pig.scripting.jruby.PigJrubyLibrary
All Implemented Interfaces:
org.jruby.runtime.load.Library

public class PigJrubyLibrary
extends Object
implements org.jruby.runtime.load.Library

This class provides the ability to present to Ruby a library that was written in Java. In JRuby, there are two ways to present a library to ruby: one is to implement it in ruby as a module or class, and the other is to implement it in Java and then register it with the runtime. For the Pig datatypes we provide Ruby implementations for, it was easier to implement them in Java and provide a Ruby interface via the annotations that Jruby provides. Additionally, this class provides static object conversion functionality to and from Pig and JRuby.


Constructor Summary
PigJrubyLibrary()
           
 
Method Summary
static org.jruby.runtime.builtin.IRubyObject enumeratorize(org.jruby.Ruby runtime, org.jruby.runtime.builtin.IRubyObject obj, String name)
          This is a hack to get around the fact that in JRuby 1.6.7, the enumeratorize method isn't public.
 void load(org.jruby.Ruby runtime, boolean wrap)
          This method is called from JRuby to register any classes in the library.
static org.jruby.RubyBoolean pigToRuby(org.jruby.Ruby ruby, Boolean object)
          A type specific conversion routine.
static RubyDataBag pigToRuby(org.jruby.Ruby ruby, DataBag object)
          A type specific conversion routine.
static RubyDataByteArray pigToRuby(org.jruby.Ruby ruby, DataByteArray object)
          A type specific conversion routine.
static org.jruby.RubyFloat pigToRuby(org.jruby.Ruby ruby, Double object)
          A type specific conversion routine.
static org.jruby.RubyFloat pigToRuby(org.jruby.Ruby ruby, Float object)
          A type specific conversion routine.
static org.jruby.RubyFixnum pigToRuby(org.jruby.Ruby ruby, Integer object)
          A type specific conversion routine.
static org.jruby.RubyFixnum pigToRuby(org.jruby.Ruby ruby, Long object)
          A type specific conversion routine.
static
<T> org.jruby.RubyHash
pigToRuby(org.jruby.Ruby ruby, Map<T,?> object)
          A type specific conversion routine for Pig Maps.
static org.jruby.runtime.builtin.IRubyObject pigToRuby(org.jruby.Ruby ruby, Object object)
          This is the method which provides conversion from Pig to Ruby.
static RubySchema pigToRuby(org.jruby.Ruby ruby, Schema object)
          A type specific conversion routine.
static org.jruby.RubyString pigToRuby(org.jruby.Ruby ruby, String object)
          A type specific conversion routine.
static org.jruby.RubyArray pigToRuby(org.jruby.Ruby ruby, Tuple object)
          A type specific conversion routine.
static Object rubyToPig(org.jruby.runtime.builtin.IRubyObject rbObject)
          This method facilitates conversion from Ruby objects to Pig objects.
static Tuple rubyToPig(org.jruby.RubyArray rbObject)
          A type specific conversion routine.
static Long rubyToPig(org.jruby.RubyBignum rbObject)
          A type specific conversion routine.
static Boolean rubyToPig(org.jruby.RubyBoolean rbObject)
          A type specific conversion routine.
static DataBag rubyToPig(RubyDataBag rbObject)
          A type specific conversion routine.
static DataByteArray rubyToPig(RubyDataByteArray rbObject)
          A type specific conversion routine.
static Long rubyToPig(org.jruby.RubyFixnum rbObject)
          A type specific conversion routine.
static Double rubyToPig(org.jruby.RubyFloat rbObject)
          A type specific conversion routine.
static Map<String,?> rubyToPig(org.jruby.RubyHash rbObject)
          A type specific conversion routine.
static Integer rubyToPig(org.jruby.RubyInteger rbObject)
          A type specific conversion routine.
static Object rubyToPig(org.jruby.RubyNil rbObject)
          A type specific conversion routine.
static Schema rubyToPig(RubySchema rbObject)
          A type specific conversion routine.
static String rubyToPig(org.jruby.RubyString rbObject)
          A type specific conversion routine.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PigJrubyLibrary

public PigJrubyLibrary()
Method Detail

load

public void load(org.jruby.Ruby runtime,
                 boolean wrap)
          throws IOException
This method is called from JRuby to register any classes in the library.

Specified by:
load in interface org.jruby.runtime.load.Library
Parameters:
runtime - the current Ruby runtime
wrap - ignored
Throws:
IOException

rubyToPig

public static Object rubyToPig(org.jruby.runtime.builtin.IRubyObject rbObject)
                        throws ExecException
This method facilitates conversion from Ruby objects to Pig objects. This is a general class which detects the subclass and invokes the appropriate conversion routine. It will fail on an unsupported datatype.

Parameters:
rbObject - a Ruby object to convert
Returns:
the Pig analogue of the Ruby object
Throws:
ExecException - if rbObject is not of a known type that can be converted

rubyToPig

public static Tuple rubyToPig(org.jruby.RubyArray rbObject)
                       throws ExecException
A type specific conversion routine.

Parameters:
rbObject - object to convert
Returns:
analogous Pig type
Throws:
ExecException

rubyToPig

public static Map<String,?> rubyToPig(org.jruby.RubyHash rbObject)
                               throws ExecException
A type specific conversion routine.

Parameters:
rbObject - object to convert
Returns:
analogous Pig type
Throws:
ExecException

rubyToPig

public static Boolean rubyToPig(org.jruby.RubyBoolean rbObject)
A type specific conversion routine.

Parameters:
rbObject - object to convert
Returns:
analogous Pig type

rubyToPig

public static Schema rubyToPig(RubySchema rbObject)
A type specific conversion routine.

Parameters:
rbObject - object to convert
Returns:
analogous Pig type

rubyToPig

public static String rubyToPig(org.jruby.RubyString rbObject)
A type specific conversion routine.

Parameters:
rbObject - object to convert
Returns:
analogous Pig type

rubyToPig

public static Long rubyToPig(org.jruby.RubyBignum rbObject)
A type specific conversion routine.

Parameters:
rbObject - object to convert
Returns:
analogous Pig type

rubyToPig

public static Long rubyToPig(org.jruby.RubyFixnum rbObject)
A type specific conversion routine.

Parameters:
rbObject - object to convert
Returns:
analogous Pig type

rubyToPig

public static Double rubyToPig(org.jruby.RubyFloat rbObject)
A type specific conversion routine.

Parameters:
rbObject - object to convert
Returns:
analogous Pig type

rubyToPig

public static Integer rubyToPig(org.jruby.RubyInteger rbObject)
A type specific conversion routine.

Parameters:
rbObject - object to convert
Returns:
analogous Pig type

rubyToPig

public static DataBag rubyToPig(RubyDataBag rbObject)
A type specific conversion routine.

Parameters:
rbObject - object to convert
Returns:
analogous Pig type

rubyToPig

public static DataByteArray rubyToPig(RubyDataByteArray rbObject)
A type specific conversion routine.

Parameters:
rbObject - object to convert
Returns:
analogous Pig type

rubyToPig

public static Object rubyToPig(org.jruby.RubyNil rbObject)
A type specific conversion routine.

Parameters:
rbObject - object to convert
Returns:
analogous Pig type

pigToRuby

public static org.jruby.runtime.builtin.IRubyObject pigToRuby(org.jruby.Ruby ruby,
                                                              Object object)
                                                       throws ExecException
This is the method which provides conversion from Pig to Ruby. In this case, an instance of the Ruby runtime is necessary. This method provides the general detection of the type and then calls the more specific conversion methods.

Parameters:
ruby - the Ruby runtime to create objects in
object - the Pig object to convert to Ruby
Returns:
Ruby analogue of object
Throws:
ExecException - object is not a convertible Pig type

pigToRuby

public static org.jruby.RubyArray pigToRuby(org.jruby.Ruby ruby,
                                            Tuple object)
                                     throws ExecException
A type specific conversion routine.

Parameters:
ruby - the Ruby runtime to create objects in
object - object to convert
Returns:
analogous Ruby type
Throws:
ExecException - object contained an object that #pigToRuby(Object) could not convert

pigToRuby

public static org.jruby.RubyBoolean pigToRuby(org.jruby.Ruby ruby,
                                              Boolean object)
A type specific conversion routine.

Parameters:
ruby - the Ruby runtime to create objects in
object - object to convert
Returns:
analogous Ruby type

pigToRuby

public static RubyDataBag pigToRuby(org.jruby.Ruby ruby,
                                    DataBag object)
A type specific conversion routine.

Parameters:
ruby - the Ruby runtime to create objects in
object - object to convert
Returns:
analogous Ruby type

pigToRuby

public static RubySchema pigToRuby(org.jruby.Ruby ruby,
                                   Schema object)
A type specific conversion routine.

Parameters:
ruby - the Ruby runtime to create objects in
object - object to convert
Returns:
analogous Ruby type

pigToRuby

public static <T> org.jruby.RubyHash pigToRuby(org.jruby.Ruby ruby,
                                               Map<T,?> object)
                                    throws ExecException
A type specific conversion routine for Pig Maps. This only deals with maps with String keys, which is all that Pig supports.

Parameters:
ruby - the Ruby runtime to create objects in
object - map to convert. In Pig, only maps with String keys are supported
Returns:
analogous Ruby type
Throws:
ExecException - object contains a key that can't be convert to a Ruby type

pigToRuby

public static RubyDataByteArray pigToRuby(org.jruby.Ruby ruby,
                                          DataByteArray object)
A type specific conversion routine.

Parameters:
ruby - the Ruby runtime to create objects in
object - object to convert
Returns:
analogous Ruby type

pigToRuby

public static org.jruby.RubyString pigToRuby(org.jruby.Ruby ruby,
                                             String object)
A type specific conversion routine.

Parameters:
ruby - the Ruby runtime to create objects in
object - object to convert
Returns:
analogous Ruby type

pigToRuby

public static org.jruby.RubyFixnum pigToRuby(org.jruby.Ruby ruby,
                                             Integer object)
A type specific conversion routine.

Parameters:
ruby - the Ruby runtime to create objects in
object - object to convert
Returns:
analogous Ruby type

pigToRuby

public static org.jruby.RubyFixnum pigToRuby(org.jruby.Ruby ruby,
                                             Long object)
A type specific conversion routine.

Parameters:
ruby - the Ruby runtime to create objects in
object - object to convert
Returns:
analogous Ruby type

pigToRuby

public static org.jruby.RubyFloat pigToRuby(org.jruby.Ruby ruby,
                                            Float object)
A type specific conversion routine.

Parameters:
ruby - the Ruby runtime to create objects in
object - object to convert
Returns:
analogous Ruby type

pigToRuby

public static org.jruby.RubyFloat pigToRuby(org.jruby.Ruby ruby,
                                            Double object)
A type specific conversion routine.

Parameters:
ruby - the Ruby runtime to create objects in
object - object to convert
Returns:
analogous Ruby type

enumeratorize

public static org.jruby.runtime.builtin.IRubyObject enumeratorize(org.jruby.Ruby runtime,
                                                                  org.jruby.runtime.builtin.IRubyObject obj,
                                                                  String name)
This is a hack to get around the fact that in JRuby 1.6.7, the enumeratorize method isn't public. In 1.7.0, it will be made public and this can be gotten rid of, but until then the Jruby API doesn't provide an easy way (or even a difficult way, really) to provide this functionality; thus, it felt much cleaner to use reflection to make public a method that will soon be public anyway instead of doing something much hairier.

Parameters:
runtime - the Ruby runtime to create objects in
obj - the Enumerable object to wrap
name - the name of the method that still needs a block (ie each or flatten)
Returns:
enumerator Ruby object wrapping the given Enumerable


Copyright © 2007-2012 The Apache Software Foundation