001/* 002 * Copyright (c) 2007-2017 Xplenty, Inc. All Rights Reserved. 003 * 004 * Project and contact information: http://www.cascading.org/ 005 * 006 * This file is part of the Cascading project. 007 * 008 * Licensed under the Apache License, Version 2.0 (the "License"); 009 * you may not use this file except in compliance with the License. 010 * You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, software 015 * distributed under the License is distributed on an "AS IS" BASIS, 016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 017 * See the License for the specific language governing permissions and 018 * limitations under the License. 019 */ 020 021package cascading.tuple.hadoop.io; 022 023import java.io.IOException; 024 025import cascading.tuple.Tuple; 026import cascading.tuple.hadoop.TupleSerialization; 027import cascading.tuple.io.TupleInputStream; 028import cascading.tuple.io.TuplePair; 029 030public class TuplePairDeserializer extends BaseDeserializer<TuplePair> 031 { 032 private final TupleInputStream.TupleElementReader[] keyReaders; 033 private final TupleInputStream.TupleElementReader[] sortReaders; 034 035 public TuplePairDeserializer( TupleSerialization.SerializationElementReader elementReader ) 036 { 037 super( elementReader ); 038 039 Class[] keyClasses = elementReader.getTupleSerialization().getKeyTypes(); 040 Class[] sortClasses = elementReader.getTupleSerialization().getSortTypes(); 041 042 if( elementReader.getTupleSerialization().areTypesRequired() ) 043 { 044 if( keyClasses == null ) 045 throw new IllegalStateException( "types are required to perform serialization, grouping declared fields: " + elementReader.getTupleSerialization().getKeyFields() ); 046 047 if( sortClasses == null ) 048 throw new IllegalStateException( "types are required to perform serialization, sorting declared fields: " + elementReader.getTupleSerialization().getSortFields() ); 049 } 050 051 keyReaders = HadoopTupleInputStream.getReadersFor( elementReader, keyClasses ); 052 sortReaders = HadoopTupleInputStream.getReadersFor( elementReader, sortClasses ); 053 } 054 055 public TuplePair deserialize( TuplePair tuple ) throws IOException 056 { 057 if( tuple == null ) 058 tuple = createTuple(); 059 060 Tuple[] tuples = TuplePair.tuples( tuple ); 061 062 if( keyReaders == null ) 063 tuples[ 0 ] = inputStream.readUnTyped( tuples[ 0 ] ); 064 else 065 tuples[ 0 ] = inputStream.readWith( keyReaders, tuples[ 0 ] ); 066 067 if( sortReaders == null ) 068 tuples[ 1 ] = inputStream.readUnTyped( tuples[ 1 ] ); 069 else 070 tuples[ 1 ] = inputStream.readWith( sortReaders, tuples[ 1 ] ); 071 072 return tuple; 073 } 074 075 @Override 076 protected TuplePair createTuple() 077 { 078 return new TuplePair(); 079 } 080 }