Skip to content

Commit

Permalink
[BACKLOG-16027] Slimming the Row api
Browse files Browse the repository at this point in the history
Removing methods requiring metadata.

http://jira.pentaho.com/browse/BACKLOG-16027
  • Loading branch information
mkambol authored and hudak committed Apr 25, 2017
1 parent 6114a33 commit ddf78ec
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 164 deletions.
Expand Up @@ -24,68 +24,15 @@


package org.pentaho.di.engine.api.model; package org.pentaho.di.engine.api.model;


import org.pentaho.di.engine.api.RowException;

import java.io.Serializable; import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Optional;



/** /**
* Represents a single row of data consisting of 0..N columns, * Represents a single row of data consisting of 0..N columns,
* accessible via typed getters, along with methods for retrieving * along with column names.
* column name and type info.
*
* Each of the getXXXX() methods should return either:
* 1) an Optional of the appropriately typed value, if present and compatible
* 2) Optional.empty() if the column exists but has no defined value.
* If the requested type is not compatible, or if the index is OOB,
* the implementation should throw RowException.
*/ */
public interface Row extends Serializable { public interface Row extends Serializable {
int size();

List<String> getColumnNames(); List<String> getColumnNames();


List<Class> getColumnTypes(); Object[] getObjects();
Optional<String> getString( int index ) throws RowException;

Optional<Long> getLong( int index ) throws RowException;

Optional<Double> getNumber( int index ) throws RowException;

Optional<Date> getDate( int index ) throws RowException;

Optional<BigDecimal> getBigNumber( int index ) throws RowException;

Optional<Boolean> getBoolean( int index ) throws RowException;

Optional<byte[]> getBinary( int index ) throws RowException;

Optional<Object> getObject( int index ) throws RowException;

Optional<String> getString( String name ) throws RowException;

Optional<Long> getLong( String name ) throws RowException;

Optional<Double> getNumber( String name ) throws RowException;

Optional<Date> getDate( String name ) throws RowException;

Optional<BigDecimal> getBigNumber( String name ) throws RowException;

Optional<Boolean> getBoolean( String name ) throws RowException;

Optional<byte[]> getBinary( String name ) throws RowException;

Optional<Object> getObject( String name ) throws RowException;

Optional<Object[]> getObjects();

Optional<Integer> getInteger( int index ) throws RowException;

Optional<Integer> getInteger( String name) throws RowException;

} }
Expand Up @@ -84,14 +84,14 @@ public void serialize( DataEvent dataEvent, JsonGenerator jsonGenerator,
jsonGenerator.writeEndArray(); jsonGenerator.writeEndArray();


jsonGenerator.writeArrayFieldStart( "objects" ); jsonGenerator.writeArrayFieldStart( "objects" );
for ( Object obj : row.getObjects().get() ) { for ( Object obj : row.getObjects() ) {
jsonGenerator.writeStartObject(); jsonGenerator.writeStartObject();
if( obj == null ) { if ( obj == null ) {
jsonGenerator.writeStringField( "type", "Null" ); jsonGenerator.writeStringField( "type", "Null" );
jsonGenerator.writeEndObject(); jsonGenerator.writeEndObject();
continue; continue;
} }
switch( obj.getClass().getSimpleName() ) { switch ( obj.getClass().getSimpleName() ) {
case "String": case "String":
jsonGenerator.writeStringField( "type", "String" ); jsonGenerator.writeStringField( "type", "String" );
jsonGenerator.writeStringField( "obj", obj.toString() ); jsonGenerator.writeStringField( "obj", obj.toString() );
Expand Down Expand Up @@ -172,7 +172,7 @@ public void serialize( DataEvent dataEvent, JsonGenerator jsonGenerator,


Object object = null; Object object = null;
String objType = t.asText(); String objType = t.asText();
switch( objType ) { switch ( objType ) {
case "Null": case "Null":
types.add( Void.class ); types.add( Void.class );
break; break;
Expand Down Expand Up @@ -238,4 +238,4 @@ public void serialize( DataEvent dataEvent, JsonGenerator jsonGenerator,
mapper.registerModule( module ); mapper.registerModule( module );
} }


} }
Expand Up @@ -24,14 +24,10 @@


package org.pentaho.pdi.engine.serializers; package org.pentaho.pdi.engine.serializers;


import org.pentaho.di.engine.api.RowException;
import org.pentaho.di.engine.api.model.Row; import org.pentaho.di.engine.api.model.Row;


import java.math.BigDecimal;
import java.util.Collections; import java.util.Collections;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Optional;


/** /**
* We don't have access to a generic Row from the API. As such we're owning an implementation for deserialized rows. * We don't have access to a generic Row from the API. As such we're owning an implementation for deserialized rows.
Expand All @@ -54,92 +50,12 @@ public DeserializedRow( List<String> names, List<Class> types, List<Object> obje
this.objects = objects; this.objects = objects;
} }


@Override public int size() {
return objects.size();
}

@Override public List<String> getColumnNames() { @Override public List<String> getColumnNames() {
return Collections.unmodifiableList( names ); return Collections.unmodifiableList( names );
} }


@Override public List<Class> getColumnTypes() { @Override public Object[] getObjects() {
return Collections.unmodifiableList( types ); return Collections.unmodifiableList( objects ).toArray();
}

@Override public Optional<String> getString( int index ) throws RowException {
return Optional.of( (String) objects.get( index ) );
}

@Override public Optional<Long> getLong( int index ) throws RowException {
return Optional.of( (Long) objects.get( index ) );
}

@Override public Optional<Double> getNumber( int index ) throws RowException {
return Optional.of( (Double) objects.get( index ) );
}

@Override public Optional<Date> getDate( int index ) throws RowException {
return Optional.of( (Date) objects.get( index ) );
}

@Override public Optional<BigDecimal> getBigNumber( int index ) throws RowException {
return Optional.of( (BigDecimal) objects.get( index ) );
}

@Override public Optional<Boolean> getBoolean( int index ) throws RowException {
return Optional.of( (Boolean) objects.get( index ) );
}

@Override public Optional<byte[]> getBinary( int index ) throws RowException {
return Optional.of( (byte[]) objects.get( index ) );
}

@Override public Optional<Object> getObject( int index ) throws RowException {
return Optional.of( objects.get( index ) );
}

@Override public Optional<String> getString( String name ) throws RowException {
return getString( names.indexOf( name ) );
}

@Override public Optional<Long> getLong( String name ) throws RowException {
return getLong( names.indexOf( name ) );
}

@Override public Optional<Double> getNumber( String name ) throws RowException {
return getNumber( names.indexOf( name ) );
}

@Override public Optional<Date> getDate( String name ) throws RowException {
return getDate( names.indexOf( name ) );
}

@Override public Optional<BigDecimal> getBigNumber( String name ) throws RowException {
return getBigNumber( names.indexOf( name ) );
}

@Override public Optional<Boolean> getBoolean( String name ) throws RowException {
return getBoolean( names.indexOf( name ) );
}

@Override public Optional<byte[]> getBinary( String name ) throws RowException {
return getBinary( names.indexOf( name ) );
}

@Override public Optional<Object> getObject( String name ) throws RowException {
return getObject( names.indexOf( name ) );
}

@Override public Optional<Object[]> getObjects() {
return Optional.of( Collections.unmodifiableList( objects ).toArray() );
}

@Override public Optional<Integer> getInteger( int index ) throws RowException {
return Optional.of( (Integer) objects.get( index ) );
}

@Override public Optional<Integer> getInteger( String name ) throws RowException {
return getInteger( names.indexOf( name ) );
} }


@Override public boolean equals( Object o ) { @Override public boolean equals( Object o ) {
Expand Down Expand Up @@ -167,4 +83,4 @@ public DeserializedRow( List<String> names, List<Class> types, List<Object> obje
result = 31 * result + types.hashCode(); result = 31 * result + types.hashCode();
return result; return result;
} }
} }
Expand Up @@ -73,24 +73,14 @@ public void testRow() throws Exception {
Row row = new DeserializedRow( names, classes, objects ); Row row = new DeserializedRow( names, classes, objects );




assertEquals( new Integer( 100 ), (Integer) row.getInteger( 0 ).get() ); assertEquals( new Integer( 100 ), row.getObjects()[ 0 ] );
assertEquals( new Integer( 100 ), (Integer) row.getInteger( "some int" ).get() );


assertEquals( 100.50, (double) row.getNumber( 1 ).get(), 0.001D ); assertEquals( 100.50, (double) row.getObjects()[ 1 ], 0.001D );
assertEquals( 100.50, (double) row.getNumber( "some Double" ).get(), 0.001D ); assertEquals( bigDecimal, row.getObjects()[ 2 ] );
assertEquals( bigDecimal, row.getBigNumber( 2 ).get() );
assertEquals( bigDecimal, row.getBigNumber( "some Decimal" ).get() );


assertTrue( row.getBoolean( 3 ).get() ); assertTrue( (Boolean) row.getObjects()[ 3 ] );
assertTrue( row.getBoolean( "some Boolean" ).get() ); assertEquals( date, row.getObjects()[ 4 ] );

assertEquals( "A String", row.getObjects()[ 5 ] );
assertEquals( date, row.getDate( 4 ).get() ); assertEquals( uri, row.getObjects()[ 6 ] );
assertEquals( date, row.getDate( "some Date" ).get() );

assertEquals( "A String", row.getString( 5 ).get() );
assertEquals( "A String", row.getString( "some String" ).get() );

assertEquals( uri, row.getObject( 6 ).get() );
assertEquals( uri, row.getObject( "some Serializable" ).get() );
} }
} }

0 comments on commit ddf78ec

Please sign in to comment.