org.apache.pig.scripting.jruby
Class RubyDataBag

java.lang.Object
  extended by org.jruby.RubyBasicObject
      extended by org.jruby.RubyObject
          extended by org.apache.pig.scripting.jruby.RubyDataBag
All Implemented Interfaces:
Serializable, Cloneable, Comparable<org.jruby.runtime.builtin.IRubyObject>, Iterable<Tuple>, org.jruby.runtime.builtin.InstanceVariables, org.jruby.runtime.builtin.InternalVariables, org.jruby.runtime.builtin.IRubyObject, org.jruby.runtime.marshal.CoreObjectType

public class RubyDataBag
extends org.jruby.RubyObject
implements Iterable<Tuple>

This provides a Ruby-esque way to interact with DataBag objects. It encapsulates a bag object, and provides an easy to use interface. One difference between the Ruby and the the Java API on DataBag is that in Ruby you iterate on the bag directly.

The RubyDataBag class uses JRuby's API for the defintion Ruby class using Java code. The comments in this class will more extensively explain the annotations for those not familiar with JRuby.

In JRuby, the annotations are provided for convenience, and are detected and used by the "defineAnnotatedMethods" method. The JRubyClass annotation sets the class name as it will be seen in the Ruby runtime, and alows you to include any modules. In the case of the RubyDataBag, within Ruby we just want it to be called DataBag, and we want it to be enumerable.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class org.jruby.RubyObject
org.jruby.RubyObject.Data
 
Nested classes/interfaces inherited from class org.jruby.RubyBasicObject
org.jruby.RubyBasicObject.Finalizer
 
Field Summary
 
Fields inherited from class org.jruby.RubyObject
OBJECT_ALLOCATOR, REIFYING_OBJECT_ALLOCATOR
 
Fields inherited from class org.jruby.RubyBasicObject
ALL_F, BASICOBJECT_ALLOCATOR, COMPARE_BY_IDENTITY_F, ERR_INSECURE_SET_INST_VAR, FALSE_F, FL_USHIFT, flags, FROZEN_F, metaClass, NEVER, NIL_F, TAINTED_F, UNDEF, UNTRUSTED_F, USER0_F, USER1_F, USER2_F, USER3_F, USER4_F, USER5_F, USER6_F, USER7_F, USER8_F
 
Fields inherited from interface org.jruby.runtime.builtin.IRubyObject
NULL_ARRAY
 
Constructor Summary
protected RubyDataBag(org.jruby.Ruby ruby, org.jruby.RubyClass rc)
          This constructor encapsulated an empty bag.
protected RubyDataBag(org.jruby.Ruby ruby, org.jruby.RubyClass rc, DataBag db)
          This constructor encapsulates the bag that is passed to it.
 
Method Summary
 void add(org.jruby.runtime.ThreadContext context, org.jruby.runtime.builtin.IRubyObject[] args)
          The add method accepts a varargs argument; each argument can be either a random object, a DataBag, or a RubyArray.
 void clear()
          This method deletes all of the entries in the underlying DataBag.
 RubyDataBag clone(org.jruby.runtime.ThreadContext context)
          This method returns a copy of the encapsulated DataBag.
static org.jruby.RubyClass define(org.jruby.Ruby runtime)
          This method registers the class with the given runtime.
 org.jruby.runtime.builtin.IRubyObject each(org.jruby.runtime.ThreadContext context, org.jruby.runtime.Block block)
          This is an implementation of the each method which opens up the Enumerable interface, and makes it very convenient to iterate over the elements of a DataBag.
 org.jruby.runtime.builtin.IRubyObject flatten(org.jruby.runtime.ThreadContext context, org.jruby.runtime.Block block)
          This is a convenience method which will run the given block on the first element of each tuple contained.
 DataBag getBag()
           
 RubyDataBag initialize()
          The initialize method is the method used on the Ruby side to construct the RubyDataBag object.
 RubyDataBag initialize(org.jruby.runtime.builtin.IRubyObject arg)
          The initialize method can optionally receive a DataBag.
 org.jruby.RubyString inspect(org.jruby.runtime.ThreadContext context, org.jruby.runtime.builtin.IRubyObject[] args)
          This method returns a string representation of the RubyDataBag.
 org.jruby.RubyBoolean isDistinct(org.jruby.runtime.ThreadContext context)
          This returns whether the encapsulated DatBag is distinct, per the distinct setting.
 org.jruby.RubyBoolean isEmpty(org.jruby.runtime.ThreadContext context)
          This method returns whether or not the encapsulated DataBag is empty.
 org.jruby.RubyBoolean isSorted(org.jruby.runtime.ThreadContext context)
          This returns whether the encapsulated DatBag is distinct, per the sorted setting.
 Iterator<Tuple> iterator()
           
 org.jruby.RubyFixnum size(org.jruby.runtime.ThreadContext context)
          This returns the size of the encapsulated DataBag.
 
Methods inherited from class org.jruby.RubyObject
attachToObjectSpace, callInit, callInit, callInit, callInit, callInit, convertToType, createObjectClass, eqlInternal, equalInternal, equals, getNativeTypeIndex, hashCode, initialize, op_eqq, puts, specificEval, toString
 
Methods inherited from class org.jruby.RubyBasicObject
addFinalizer, anyToString, asJavaString, asString, callMethod, callMethod, callMethod, callMethod, callMethod, callMethod, callMethod, callMethod, callSuper, checkArrayType, checkCallMethod, checkFrozen, checkStringType, checkStringType19, compareTo, convertToArray, convertToFloat, convertToHash, convertToInteger, convertToInteger, convertToInteger, convertToString, copyInstanceVariablesInto, copySpecialInstanceVariables, createBasicObjectClass, dataGetStruct, dataGetStructChecked, dataWrapStruct, display, dup, ensureInstanceVariablesSettable, eql_p, eql, equal_p, equal_p19, evalUnder, extend, fastGetInstanceVariable, fastGetInternalVariable, fastHasInstanceVariable, fastHasInternalVariable, fastSetInstanceVariable, fastSetInternalVariable, freeze, frozen_p, getFlag, getInstanceEvalClass, getInstanceVariable, getInstanceVariableList, getInstanceVariableNameList, getInstanceVariables, getInternalVariable, getInternalVariables, getJavaClass, getMetaClass, getObjectId, getRuntime, getSingletonClass, getSingletonClassClone, getType, getVariable, getVariableCount, getVariableList, getVariableNameList, getVariableTableForRead, getVariableTableForWrite, hash, hashyInspect, hasInstanceVariable, hasInternalVariable, hasVariables, id_deprecated, id, infectBy, initialize_copy, initialize19, initialize19, initialize19, initialize19, initialize19, initObjectId, inspect, inspectHashCode, instance_eval, instance_eval, instance_eval, instance_eval, instance_eval19, instance_eval19, instance_eval19, instance_eval19, instance_exec, instance_exec19, instance_of_p, instance_variable_defined_p, instance_variable_get, instance_variable_set, instance_variables, instance_variables19, isBuiltin, isClass, isFalse, isFrozen, isImmediate, isModule, isNil, isTaint, isTrue, isUntrusted, kind_of_p, makeMetaClass, method_missing19, method, method19, methods, methods, methods19, nil_p, op_cmp, op_equal_19, op_equal, op_match, op_match19, op_not_equal, op_not_match, op_not, private_methods, private_methods19, protected_methods, protected_methods19, public_methods, public_methods19, rbClone, remove_instance_variable, removeFinalizers, removeInstanceVariable, removeInternalVariable, respond_to_p, respond_to_p, respond_to_p19, respond_to_p19, respondsTo, respondsToMissing, respondsToMissing, send, send, send, send, send, send19, send19, send19, send19, send19, setFlag, setFrozen, setInstanceVariable, setInternalVariable, setMetaClass, setTaint, setUntrusted, setVariable, singleton_method_added19, singleton_method_removed19, singleton_method_undefined19, singleton_methods, singleton_methods19, specificEval, specificEval, specificEval, specificEval, syncVariables, syncVariables, taint, taint, tainted_p, testFrozen, testFrozen, to_a, to_s, toJava, trust, type_deprecated, type, untaint, untrust, untrusted_p, validateInstanceVariable, variableTableContains, variableTableFastContains, variableTableFastFetch, variableTableFastStore, variableTableFetch, variableTableRemove, variableTableStore, variableTableSync, yieldUnder, yieldUnder
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

RubyDataBag

protected RubyDataBag(org.jruby.Ruby ruby,
                      org.jruby.RubyClass rc)
This constructor encapsulated an empty bag.

Parameters:
ruby - an instance of the ruby runtime
rc - an instance of the class object with meatadata

RubyDataBag

protected RubyDataBag(org.jruby.Ruby ruby,
                      org.jruby.RubyClass rc,
                      DataBag db)
This constructor encapsulates the bag that is passed to it. Note: the resultant RubyDataBag will encapsulated that bag directly, not a copy.

Parameters:
ruby - an instance of the ruby runtime
rc - an instance of the class object with meatadata
db - a DataBag to encapsulate
Method Detail

getBag

public DataBag getBag()

define

public static org.jruby.RubyClass define(org.jruby.Ruby runtime)
This method registers the class with the given runtime. It is not necessary to do this here, but it is simpler to associate the methods necessary to register the class with the class itself, so on the Library side it is possible to just specify "RubyDataBag.define(runtime)".

Parameters:
runtime - an instance of the Ruby runtime
Returns:
a RubyClass object with metadata about the registered class

initialize

public RubyDataBag initialize()
The initialize method is the method used on the Ruby side to construct the RubyDataBag object. The default is just an empty bag.

Overrides:
initialize in class org.jruby.RubyObject
Returns:
the initialized RubyDataBag

initialize

public RubyDataBag initialize(org.jruby.runtime.builtin.IRubyObject arg)
The initialize method can optionally receive a DataBag. In the case of a RubyDataBag, a RubyDataBag will be returned that directly encapsulates it.

Parameters:
arg - an IRubyObject that is a RubyDataBag to encapsulate
Returns:
the initialized RubyDataBag

clear

public void clear()
This method deletes all of the entries in the underlying DataBag.


isDistinct

public org.jruby.RubyBoolean isDistinct(org.jruby.runtime.ThreadContext context)
This returns whether the encapsulated DatBag is distinct, per the distinct setting.

Parameters:
context - the context the method is being executed in
Returns:
true if it the encapsulated is distinct, false otherwise

isSorted

public org.jruby.RubyBoolean isSorted(org.jruby.runtime.ThreadContext context)
This returns whether the encapsulated DatBag is distinct, per the sorted setting.

Parameters:
context - the context the method is being executed in
Returns:
true if it the encapsulated is sorted, false otherwise

size

public org.jruby.RubyFixnum size(org.jruby.runtime.ThreadContext context)
This returns the size of the encapsulated DataBag.

Parameters:
context - the context the method is being executed in
Returns:
the size of the encapsulated DataBag

add

public void add(org.jruby.runtime.ThreadContext context,
                org.jruby.runtime.builtin.IRubyObject[] args)
         throws ExecException
The add method accepts a varargs argument; each argument can be either a random object, a DataBag, or a RubyArray. In the case of a random object, that object will be converted to a Pig object and put into a Tuple. In the case of a RubyArray, it will be treated as a Tuple and added. In the case of a DataBag, it will iterate over the DataBag and add all of the elements to the element encapsulated by RubyDataBag.

Parameters:
context - the context the method is being executed in
args - varargs passed to add. Each argument can be a RubyDataBag, whose contents will be copied; a RubyArray, which will be treated as a Tuple, or another object which will be converted over per PigJrubyLibrary.rubyToPig(org.jruby.runtime.builtin.IRubyObject).
Throws:
ExecException

clone

public RubyDataBag clone(org.jruby.runtime.ThreadContext context)
This method returns a copy of the encapsulated DataBag.

Parameters:
context - the context the method is being executed in
Returns:
the copied RubyDataBag

isEmpty

public org.jruby.RubyBoolean isEmpty(org.jruby.runtime.ThreadContext context)
This method returns whether or not the encapsulated DataBag is empty.

Parameters:
context - the context the method is being executed in i @return true if the encapsulated DAtaBag is empty, false otherwise

inspect

public org.jruby.RubyString inspect(org.jruby.runtime.ThreadContext context,
                                    org.jruby.runtime.builtin.IRubyObject[] args)
This method returns a string representation of the RubyDataBag. If given an optional argument, then if that argument is true, the contents of the bag will also be printed.

Parameters:
context - the context the method is being executed in
args - optional true/false argument passed to inspect
Returns:
string representation of the RubyDataBag

iterator

public Iterator<Tuple> iterator()
Specified by:
iterator in interface Iterable<Tuple>

each

public org.jruby.runtime.builtin.IRubyObject each(org.jruby.runtime.ThreadContext context,
                                                  org.jruby.runtime.Block block)
                                           throws ExecException
This is an implementation of the each method which opens up the Enumerable interface, and makes it very convenient to iterate over the elements of a DataBag. Note that currently, due to a deficiency in JRuby, it is not possible to call each without a block given.

Parameters:
context - the context the method is being executed in
block - a block to call on the elements of the bag
Returns:
enumerator object if null block given, nil otherwise
Throws:
ExecException

flatten

public org.jruby.runtime.builtin.IRubyObject flatten(org.jruby.runtime.ThreadContext context,
                                                     org.jruby.runtime.Block block)
                                              throws ExecException
This is a convenience method which will run the given block on the first element of each tuple contained.

Parameters:
context - the context the method is being executed in
block - a block to call on the elements of the bag
Returns:
enumerator object if null block given, nil otherwise
Throws:
ExecException


Copyright © 2007-2012 The Apache Software Foundation