-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
RowMeta.java
1318 lines (1200 loc) · 40.3 KB
/
RowMeta.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*! ******************************************************************************
*
* Pentaho Data Integration
*
* Copyright (C) 2002-2017 by Hitachi Vantara : http://www.pentaho.com
*
*******************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
package org.pentaho.di.core.row;
import com.google.common.annotations.VisibleForTesting;
import org.pentaho.di.compatibility.Row;
import org.pentaho.di.compatibility.Value;
import org.pentaho.di.core.Const;
import org.pentaho.di.core.util.Utils;
import org.pentaho.di.core.exception.KettleEOFException;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.exception.KettleFileException;
import org.pentaho.di.core.exception.KettlePluginException;
import org.pentaho.di.core.exception.KettleValueException;
import org.pentaho.di.core.row.value.ValueMetaFactory;
import org.pentaho.di.core.xml.XMLHandler;
import org.w3c.dom.Node;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class RowMeta implements RowMetaInterface {
public static final String XML_META_TAG = "row-meta";
public static final String XML_DATA_TAG = "row-data";
private final ReentrantReadWriteLock lock;
private final RowMetaCache cache;
List<ValueMetaInterface> valueMetaList;
List<Integer> needRealClone;
public RowMeta() {
this( new ArrayList<ValueMetaInterface>(), new RowMetaCache() );
}
/**
* Copy constructor for clone
*
* @param rowMeta
* @throws KettlePluginException
*/
private RowMeta( RowMeta rowMeta, Integer targetType ) throws KettlePluginException {
this( new ArrayList<ValueMetaInterface>( rowMeta.valueMetaList.size() ), new RowMetaCache( rowMeta.cache ) );
for ( ValueMetaInterface valueMetaInterface : rowMeta.valueMetaList ) {
valueMetaList.add( ValueMetaFactory
.cloneValueMeta( valueMetaInterface, targetType == null ? valueMetaInterface.getType() : targetType ) );
}
this.needRealClone = rowMeta.needRealClone;
}
private RowMeta( List<ValueMetaInterface> valueMetaList, RowMetaCache rowMetaCache ) {
lock = new ReentrantReadWriteLock();
this.cache = rowMetaCache;
this.valueMetaList = valueMetaList;
this.needRealClone = new ArrayList<>();
}
@Override
public RowMeta clone() {
lock.readLock().lock();
try {
return new RowMeta( this, null );
} catch ( Exception e ) {
throw new RuntimeException( e );
} finally {
lock.readLock().unlock();
}
}
/**
* This method copies the row metadata and sets all values to the specified type (usually String)
*
* @param targetType The target type
* @return The cloned metadata
* @throws if the target type could not be loaded from the plugin registry
*/
@Override
public RowMetaInterface cloneToType( int targetType ) throws KettleValueException {
lock.readLock().lock();
try {
return new RowMeta( this, targetType );
} catch ( KettlePluginException e ) {
throw new KettleValueException( e );
} finally {
lock.readLock().unlock();
}
}
@Override
public String toString() {
StringBuilder buffer = new StringBuilder();
lock.readLock().lock();
try {
boolean notFirst = false;
for ( ValueMetaInterface valueMeta : valueMetaList ) {
if ( notFirst ) {
buffer.append( ", " );
} else {
notFirst = true;
}
buffer.append( "[" ).append( valueMeta.toString() ).append( "]" );
}
return buffer.toString();
} finally {
lock.readLock().unlock();
}
}
/**
* @return the list of value metadata
*/
@Override
public List<ValueMetaInterface> getValueMetaList() {
List<ValueMetaInterface> copy;
lock.readLock().lock();
try {
copy = new ArrayList<>( valueMetaList );
} finally {
lock.readLock().unlock();
}
// kept for backward compatibility
return Collections.unmodifiableList( copy );
}
/**
* @param valueMetaList the list of valueMeta to set
*/
@Override
public void setValueMetaList( List<ValueMetaInterface> valueMetaList ) {
lock.writeLock().lock();
try {
this.valueMetaList = valueMetaList;
this.cache.invalidate();
for ( int i = 0, len = valueMetaList.size(); i < len; i++ ) {
ValueMetaInterface valueMeta = valueMetaList.get( i );
cache.storeMapping( valueMeta.getName(), i );
}
this.needRealClone = null;
} finally {
lock.writeLock().unlock();
}
}
/**
* @return the number of values in the row
*/
@Override
public int size() {
lock.readLock().lock();
try {
return valueMetaList.size();
} finally {
lock.readLock().unlock();
}
}
/**
* @return true if there are no elements in the row metadata
*/
@Override
public boolean isEmpty() {
lock.readLock().lock();
try {
return valueMetaList.isEmpty();
} finally {
lock.readLock().unlock();
}
}
@Override
public boolean exists( ValueMetaInterface meta ) {
return ( meta != null ) && searchValueMeta( meta.getName() ) != null;
}
/**
* Add a metadata value. If a value with the same name already exists, it gets renamed.
*
* @param meta The metadata value to add
*/
@Override
public void addValueMeta( ValueMetaInterface meta ) {
if ( meta != null ) {
lock.writeLock().lock();
try {
ValueMetaInterface newMeta;
Integer existsIdx = cache.findAndCompare( meta.getName(), valueMetaList );
if ( existsIdx == null ) {
newMeta = meta;
} else {
newMeta = renameValueMetaIfInRow( meta, null );
}
int sz = valueMetaList.size();
valueMetaList.add( newMeta );
cache.storeMapping( newMeta.getName(), sz );
needRealClone = null;
} finally {
lock.writeLock().unlock();
}
}
}
/**
* Add a metadata value on a certain location in the row. If a value with the same name already exists, it gets
* renamed. Remember to change the data row according to this.
*
* @param index The index where the metadata value needs to be put in the row
* @param meta The metadata value to add to the row
*/
@Override
public void addValueMeta( int index, ValueMetaInterface meta ) {
if ( meta != null ) {
lock.writeLock().lock();
try {
ValueMetaInterface newMeta;
Integer existsIdx = cache.findAndCompare( meta.getName(), valueMetaList );
if ( existsIdx == null ) {
newMeta = meta;
} else {
newMeta = renameValueMetaIfInRow( meta, null );
}
valueMetaList.add( index, newMeta );
cache.invalidate();
needRealClone = null;
} finally {
lock.writeLock().unlock();
}
}
}
/**
* Get the value metadata on the specified index.
*
* @param index The index to get the value metadata from
* @return The value metadata specified by the index.
*/
@Override
public ValueMetaInterface getValueMeta( int index ) {
lock.readLock().lock();
try {
if ( ( index >= 0 ) && ( index < valueMetaList.size() ) ) {
return valueMetaList.get( index );
} else {
return null;
}
} finally {
lock.readLock().unlock();
}
}
/**
* 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();
}
}
}
/**
* Get a String value from a row of data. Convert data if this needed.
*
* @param dataRow the row of data
* @param index the index
* @return The string found on that position in the row
* @throws KettleValueException in case there was a problem converting the data.
*/
@Override
public String getString( Object[] dataRow, int index ) throws KettleValueException {
if ( dataRow == null ) {
return null;
}
ValueMetaInterface meta = getValueMeta( index );
return meta.getString( dataRow[ index ] );
}
/**
* Get an Integer value from a row of data. Convert data if this needed.
*
* @param dataRow the row of data
* @param index the index
* @return The integer found on that position in the row
* @throws KettleValueException in case there was a problem converting the data.
*/
@Override
public Long getInteger( Object[] dataRow, int index ) throws KettleValueException {
if ( dataRow == null ) {
return null;
}
ValueMetaInterface meta = getValueMeta( index );
return meta.getInteger( dataRow[ index ] );
}
/**
* Get a Number value from a row of data. Convert data if this needed.
*
* @param dataRow the row of data
* @param index the index
* @return The number found on that position in the row
* @throws KettleValueException in case there was a problem converting the data.
*/
@Override
public Double getNumber( Object[] dataRow, int index ) throws KettleValueException {
if ( dataRow == null ) {
return null;
}
ValueMetaInterface meta = getValueMeta( index );
return meta.getNumber( dataRow[ index ] );
}
/**
* Get a Date value from a row of data. Convert data if this needed.
*
* @param dataRow the row of data
* @param index the index
* @return The date found on that position in the row
* @throws KettleValueException in case there was a problem converting the data.
*/
@Override
public Date getDate( Object[] dataRow, int index ) throws KettleValueException {
if ( dataRow == null ) {
return null;
}
ValueMetaInterface meta = getValueMeta( index );
return meta.getDate( dataRow[ index ] );
}
/**
* Get a BigNumber value from a row of data. Convert data if this needed.
*
* @param dataRow the row of data
* @param index the index
* @return The bignumber found on that position in the row
* @throws KettleValueException in case there was a problem converting the data.
*/
@Override
public BigDecimal getBigNumber( Object[] dataRow, int index ) throws KettleValueException {
if ( dataRow == null ) {
return null;
}
ValueMetaInterface meta = getValueMeta( index );
return meta.getBigNumber( dataRow[ index ] );
}
/**
* Get a Boolean value from a row of data. Convert data if this needed.
*
* @param dataRow the row of data
* @param index the index
* @return The boolean found on that position in the row
* @throws KettleValueException in case there was a problem converting the data.
*/
@Override
public Boolean getBoolean( Object[] dataRow, int index ) throws KettleValueException {
if ( dataRow == null ) {
return null;
}
ValueMetaInterface meta = getValueMeta( index );
return meta.getBoolean( dataRow[ index ] );
}
/**
* Get a Binary value from a row of data. Convert data if this needed.
*
* @param dataRow the row of data
* @param index the index
* @return The binary found on that position in the row
* @throws KettleValueException in case there was a problem converting the data.
*/
@Override
public byte[] getBinary( Object[] dataRow, int index ) throws KettleValueException {
if ( dataRow == null ) {
return null;
}
ValueMetaInterface meta = getValueMeta( index );
return meta.getBinary( dataRow[ index ] );
}
/**
* Determines whether a value in a row is null. A value is null when the object is null or when it's an empty String
*
* @param dataRow The row of data
* @param index the index to reference
* @return true if the value on the index is null.
* @throws KettleValueException in case there is a conversion error (only thrown in case of lazy conversion)
*/
@Override
public boolean isNull( Object[] dataRow, int index ) throws KettleValueException {
if ( dataRow == null ) {
// I guess so...
return true;
}
return getValueMeta( index ).isNull( dataRow[ index ] );
}
/**
* @return a cloned Object[] object.
* @throws KettleValueException in case something is not quite right with the expected data
*/
@Override
public Object[] cloneRow( Object[] objects ) throws KettleValueException {
return cloneRow( objects, objects.clone() );
}
/**
* @return a cloned Object[] object.
* @throws KettleValueException in case something is not quite right with the expected data
*/
@Override
public Object[] cloneRow( Object[] objects, Object[] newObjects ) throws KettleValueException {
List<Integer> list = getOrCreateValuesThatNeedRealClone( valueMetaList );
lock.readLock().lock();
try {
for ( Integer i : list ) {
ValueMetaInterface valueMeta = valueMetaList.get( i );
newObjects[ i ] = valueMeta.cloneValueData( objects[ i ] );
}
return newObjects;
} finally {
lock.readLock().unlock();
}
}
@VisibleForTesting
List<Integer> getOrCreateValuesThatNeedRealClone( List<ValueMetaInterface> values ) {
lock.writeLock().lock();
try {
if ( needRealClone == null ) {
int len = values.size();
needRealClone = new ArrayList<>( len );
for ( int i = 0; i < len; i++ ) {
ValueMetaInterface valueMeta = values.get( i );
if ( valueMeta.requiresRealClone() ) {
needRealClone.add( i );
}
}
}
} finally {
lock.writeLock().unlock();
}
return needRealClone;
}
@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 );
}
@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 );
}
/**
* Searches the index of a value meta with a given name
*
* @param valueName the name of the value metadata to look for
* @return the index or -1 in case we didn't find the value
*/
@Override
public int indexOfValue( String valueName ) {
if ( valueName == null ) {
return -1;
}
lock.readLock().lock();
try {
Integer index = cache.findAndCompare( valueName, valueMetaList );
for ( int i = 0; ( index == null ) && ( i < valueMetaList.size() ); i++ ) {
if ( valueName.equalsIgnoreCase( valueMetaList.get( i ).getName() ) ) {
index = i;
// it is possible, that several threads can call storing simultaneously
// but it makes no harm as they will put the same value,
// because valueMetaList is defended from modifications by read lock
cache.storeMapping( valueName, index );
}
}
if ( index == null ) {
return -1;
}
return index;
} finally {
lock.readLock().unlock();
}
}
/**
* 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 addRowMeta( RowMetaInterface rowMeta ) {
for ( int i = 0; i < rowMeta.size(); i++ ) {
addValueMeta( rowMeta.getValueMeta( i ) );
}
}
/**
* Merge the values of row r to this Row. The values that are not yet in the row are added unchanged. The values that
* are in the row are renamed to name_2, name_3, etc.
*
* @param r The row to be merged with this row
*/
@Override
public void mergeRowMeta( RowMetaInterface r ) {
mergeRowMeta( r, null );
}
/**
* Merge the values of row r to this Row. The fields that are not yet in the row are added unchanged. The fields that
* are in the row are renamed to name_2, name_3, etc. If the fields are renamed, the provided originStepName will be
* assigned as the origin step for those fields.
*
* @param r The row to be merged with this row
* @param originStepName The name to use as the origin step
*/
@Override
public void mergeRowMeta( RowMetaInterface r, String originStepName ) {
lock.writeLock().lock();
try {
for ( int x = 0; x < r.size(); x++ ) {
ValueMetaInterface field = r.getValueMeta( x );
if ( searchValueMeta( field.getName() ) == null ) {
addValueMeta( field ); // Not in list yet: add
} else {
// We want to rename the field to Name[2], Name[3], ...
//
addValueMeta( renameValueMetaIfInRow( field, originStepName ) );
}
}
} finally {
lock.writeLock().unlock();
}
}
private ValueMetaInterface renameValueMetaIfInRow( ValueMetaInterface valueMeta, String originStep ) {
// We want to rename the field to Name[2], Name[3], ...
//
int index = 1;
String name = valueMeta.getName() + "_" + index;
while ( searchValueMeta( name ) != null ) {
index++;
name = valueMeta.getName() + "_" + index;
}
// Create a copy of the valueMeta object to make sure we don't rename any other value meta objects.
// It's only being renamed because of the addition to THIS row metadata object, not another.
//
ValueMetaInterface copy = valueMeta.clone();
// OK, this is the new name and origin to pick
//
copy.setName( name );
if ( originStep != null ) {
copy.setOrigin( originStep );
}
return copy;
}
/**
* Get an array of the names of all the Values in the Row.
*
* @return an array of Strings: the names of all the Values in the Row.
*/
@Override
public String[] getFieldNames() {
lock.readLock().lock();
try {
String[] retval = new String[ size() ];
for ( int i = 0; i < size(); i++ ) {
String valueName = getValueMeta( i ).getName();
retval[i] = valueName == null ? "" : valueName;
}
return retval;
} finally {
lock.readLock().unlock();
}
}
/**
* Write ONLY the specified data to the outputStream
*
* @throws KettleFileException in case things go awry
*/
@Override
public void writeData( DataOutputStream outputStream, Object[] data ) throws KettleFileException {
lock.readLock().lock();
try {
// Write all values in the row
for ( int i = 0; i < size(); i++ ) {
getValueMeta( i ).writeData( outputStream, data[ i ] );
}
// If there are 0 values in the row, we write a marker flag to be able to detect an EOF on the other end (sockets
// etc)
//
if ( size() == 0 ) {
try {
outputStream.writeBoolean( true );
} catch ( IOException e ) {
throw new KettleFileException( "Error writing marker flag", e );
}
}
} finally {
lock.readLock().unlock();
}
}
/**
* Write ONLY the specified metadata to the outputStream
*
* @throws KettleFileException in case things go awry
*/
@Override
public void writeMeta( DataOutputStream outputStream ) throws KettleFileException {
lock.readLock().lock();
try {
// First handle the number of fields in a row
try {
outputStream.writeInt( size() );
} catch ( IOException e ) {
throw new KettleFileException( "Unable to write nr of metadata values", e );
}
// Write all values in the row
for ( int i = 0; i < size(); i++ ) {
getValueMeta( i ).writeMeta( outputStream );
}
} finally {
lock.readLock().unlock();
}
}
public RowMeta( DataInputStream inputStream ) throws KettleFileException, SocketTimeoutException {
this();
int nr;
try {
nr = inputStream.readInt();
} catch ( SocketTimeoutException e ) {
throw e;
} catch ( EOFException e ) {
throw new KettleEOFException(
"End of file while reading the number of metadata values in the row metadata", e );
} catch ( IOException e ) {
throw new KettleFileException( "Unable to read nr of metadata values: " + e.toString(), e );
}
for ( int i = 0; i < nr; i++ ) {
try {
int type = inputStream.readInt();
ValueMetaInterface valueMeta = ValueMetaFactory.createValueMeta( type );
valueMeta.readMetaData( inputStream );
addValueMeta( valueMeta );
} catch ( EOFException e ) {
throw new KettleEOFException( e );
} catch ( Exception e ) {
throw new KettleFileException( toString() + " : Unable to read row metadata from input stream", e );
}
}
}
@Override
public Object[] readData( DataInputStream inputStream ) throws KettleFileException, SocketTimeoutException {
lock.readLock().lock();
try {
Object[] data = new Object[ size() ];
for ( int i = 0; i < size(); i++ ) {
data[ i ] = getValueMeta( i ).readData( inputStream );
}
if ( size() == 0 ) {
try {
inputStream.readBoolean();
} catch ( EOFException e ) {
throw new KettleEOFException( e );
} catch ( SocketTimeoutException e ) {
throw e;
} catch ( IOException e ) {
throw new KettleFileException( toString() + " : Unable to read the marker flag data from input stream", e );
}
}
return data;
} finally {
lock.readLock().unlock();
}
}
@Override
public void clear() {
lock.writeLock().lock();
try {
valueMetaList.clear();
cache.invalidate();
needRealClone = null;
} finally {
lock.writeLock().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
public void removeValueMeta( int index ) {
lock.writeLock().lock();
try {
valueMetaList.remove( index );
cache.invalidate();
needRealClone = null;
} finally {
lock.writeLock().unlock();
}
}
/**
* @return a string with a description of all the metadata values of the complete row of metadata
*/
@Override
public String toStringMeta() {
StringBuilder buffer = new StringBuilder();
lock.readLock().lock();
try {
boolean notFirst = false;
for ( ValueMetaInterface valueMeta : valueMetaList ) {
if ( notFirst ) {
buffer.append( ", " );
} else {
notFirst = true;
}
buffer.append( "[" ).append( valueMeta.toStringMeta() ).append( "]" );
}
return buffer.toString();
} finally {
lock.readLock().unlock();
}
}
/**
* Get the string representation of the data in a row of data
*
* @param row the row of data to convert to string
* @return the row of data in string form
* @throws KettleValueException in case of a conversion error
*/
@Override
public String getString( Object[] row ) throws KettleValueException {
lock.readLock().lock();
try {
StringBuilder buffer = new StringBuilder();
for ( int i = 0; i < size(); i++ ) {
if ( i > 0 ) {
buffer.append( ", " );
}
buffer.append( "[" );
buffer.append( getString( row, i ) );
buffer.append( "]" );
}
return buffer.toString();
} finally {
lock.readLock().unlock();
}
}
/**
* Get an array of strings showing the name of the values in the row padded to a maximum length, followed by the types
* of the values.
*
* @param maxlen The length to which the name will be padded.
* @return an array of strings: the names and the types of the fieldnames in the row.
*/
@Override
public String[] getFieldNamesAndTypes( int maxlen ) {
lock.readLock().lock();
try {
final int size = size();
String[] retval = new String[ size ];
for ( int i = 0; i < size; i++ ) {
ValueMetaInterface v = getValueMeta( i );
retval[ i ] = Const.rightPad( v.getName(), maxlen ) + " (" + v.getTypeDesc() + ")";
}
return retval;
} finally {
lock.readLock().unlock();
}
}
/**
* Compare 2 rows with each other using certain values in the rows and also considering the specified ascending
* clauses of the value metadata.
*
* @param rowData1 The first row of data
* @param rowData2 The second row of data
* @param fieldnrs the fields to compare on (in that order)
* @return 0 if the rows are considered equal, -1 is data1 is smaller, 1 if data2 is smaller.
* @throws KettleValueException
*/
@Override
public int compare( Object[] rowData1, Object[] rowData2, int[] fieldnrs ) throws KettleValueException {
lock.readLock().lock();
try {
for ( int fieldnr : fieldnrs ) {
ValueMetaInterface valueMeta = getValueMeta( fieldnr );
int cmp = valueMeta.compare( rowData1[ fieldnr ], rowData2[ fieldnr ] );
if ( cmp != 0 ) {
return cmp;
}
}
return 0;
} finally {
lock.readLock().unlock();
}
}
/**
* Compare 2 rows with each other for equality using certain values in the rows and also considering the case
* sensitivity flag.
*
* @param rowData1 The first row of data
* @param rowData2 The second row of data
* @param fieldnrs the fields to compare on (in that order)
* @return true if the rows are considered equal, false if they are not.
* @throws KettleValueException
*/
@Override
public boolean equals( Object[] rowData1, Object[] rowData2, int[] fieldnrs ) throws KettleValueException {
lock.readLock().lock();
try {
for ( int fieldnr : fieldnrs ) {
ValueMetaInterface valueMeta = getValueMeta( fieldnr );
int cmp = valueMeta.compare( rowData1[ fieldnr ], rowData2[ fieldnr ] );
if ( cmp != 0 ) {
return false;
}
}
return true;
} finally {
lock.readLock().unlock();
}
}
/**
* Compare 2 rows with each other using certain values in the rows and also considering the specified ascending
* clauses of the value metadata.
*
* @param rowData1 The first row of data
* @param rowData2 The second row of data
* @param fieldnrs1 The indexes of the values to compare in the first row
* @param fieldnrs2 The indexes of the values to compare with in the second row
* @return 0 if the rows are considered equal, -1 is data1 is smaller, 1 if data2 is smaller.
* @throws KettleValueException
*/
@Override
public int compare( Object[] rowData1, Object[] rowData2, int[] fieldnrs1, int[] fieldnrs2 )
throws KettleValueException {
int len = ( fieldnrs1.length < fieldnrs2.length ) ? fieldnrs1.length : fieldnrs2.length;
lock.readLock().lock();
try {
for ( int i = 0; i < len; i++ ) {
ValueMetaInterface valueMeta = getValueMeta( fieldnrs1[ i ] );
int cmp = valueMeta.compare( rowData1[ fieldnrs1[ i ] ], rowData2[ fieldnrs2[ i ] ] );
if ( cmp != 0 ) {
return cmp;
}
}
return 0;
} finally {
lock.readLock().unlock();
}
}
/**
* Compare 2 rows with each other using certain values in the rows and also considering the specified ascending
* clauses of the value metadata.
*
* @param rowData1 The first row of data
* @param rowMeta2 the metadata of the second row of data
* @param rowData2 The second row of data
* @param fieldnrs1 The indexes of the values to compare in the first row
* @param fieldnrs2 The indexes of the values to compare with in the second row
* @return 0 if the rows are considered equal, -1 is data1 is smaller, 1 if data2 is smaller.
* @throws KettleValueException
*/
@Override
public int compare( Object[] rowData1, RowMetaInterface rowMeta2, Object[] rowData2, int[] fieldnrs1,
int[] fieldnrs2 ) throws KettleValueException {
int len = ( fieldnrs1.length < fieldnrs2.length ) ? fieldnrs1.length : fieldnrs2.length;
lock.readLock().lock();
try {