@Override public Date getDate( Object[] dataRow, String valueName, Date defaultValue ) throws KettleValueException { int index = indexOfValue( valueName ); if ( index < 0 ) { return defaultValue; } return getDate( dataRow, index ); }
@Override public String getString( Object[] dataRow, String valueName, String defaultValue ) throws KettleValueException { int index = indexOfValue( valueName ); if ( index < 0 ) { return defaultValue; } return getString( dataRow, index ); }
@Override public Long getInteger( Object[] dataRow, String valueName, Long defaultValue ) throws KettleValueException { int index = indexOfValue( valueName ); if ( index < 0 ) { return defaultValue; } return getInteger( dataRow, index ); }
/** * Searches for a value with a certain name in the value meta list * * @param valueName The value name to search for * @return The value metadata or null if nothing was found */ @Override public ValueMetaInterface searchValueMeta( String valueName ) { lock.readLock().lock(); try { Integer index = indexOfValue( valueName ); if ( index < 0 ) { return null; } return valueMetaList.get( index ); } finally { lock.readLock().unlock(); } }
@Override public void removeValueMeta( String valueName ) throws KettleValueException { lock.writeLock().lock(); try { int index = indexOfValue( valueName ); if ( index < 0 ) { throw new KettleValueException( "Unable to find value metadata with name '" + valueName + "', so I can't delete it." ); } removeValueMeta( index ); } finally { lock.writeLock().unlock(); } }
@Override Object doCall() throws Exception { Random random = new Random(); while ( condition.get() ) { int index = rowMeta.indexOfValue( name ); if ( index < 0 ) { throw new IllegalStateException( name + " was not found among " + rowMeta.getValueMetaList() ); } Thread.sleep( random.nextInt( 100 ) ); } return null; } }
/** * Replaces a value meta entry in the row metadata with another one * * @param index The index in the row to replace at * @param valueMeta the metadata to replace with */ @Override public void setValueMeta( int index, ValueMetaInterface valueMeta ) { if ( valueMeta != null ) { lock.writeLock().lock(); try { ValueMetaInterface old = valueMetaList.get( index ); ValueMetaInterface newMeta = valueMeta; // try to check if a ValueMeta with the same name already exists int existsIndex = indexOfValue( valueMeta.getName() ); // if it exists and it's not in the requested position // we need to take care of renaming if ( existsIndex >= 0 && existsIndex != index ) { newMeta = renameValueMetaIfInRow( valueMeta, null ); } valueMetaList.set( index, newMeta ); cache.replaceMapping( old.getName(), newMeta.getName(), index ); needRealClone = null; } finally { lock.writeLock().unlock(); } } }
@Test public void fiveShufflersAgainstTenSearchers() throws Exception { final int elementsAmount = 100; final int shufflersAmount = 5; final int searchersAmount = 10; final RowMeta rowMeta = new RowMeta(); for ( int i = 0; i < elementsAmount; i++ ) { rowMeta.addValueMeta( new ValueMetaString( "meta_" + i ) ); } final AtomicBoolean condition = new AtomicBoolean( true ); List<Shuffler> shufflers = new ArrayList<Shuffler>( shufflersAmount ); for ( int i = 0; i < shufflersAmount; i++ ) { shufflers.add( new Shuffler( condition, rowMeta, cycles ) ); } List<Searcher> searchers = new ArrayList<Searcher>( searchersAmount ); for ( int i = 0; i < searchersAmount; i++ ) { String name = "meta_" + ( new Random().nextInt( elementsAmount ) ); assertTrue( rowMeta.indexOfValue( name ) >= 0 ); searchers.add( new Searcher( condition, rowMeta, name ) ); } ConcurrencyTestRunner.runAndCheckNoExceptionRaised( shufflers, searchers, condition ); }
assertTrue( result.getResult().isEmpty() ); for ( String name : remover.getToRemove() ) { assertEquals( name, -1, rowMeta.indexOfValue( name ) );