001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    
019    package org.apache.hadoop.record.meta;
020    
021    import java.io.IOException;
022    import java.util.*;
023    
024    import org.apache.hadoop.classification.InterfaceAudience;
025    import org.apache.hadoop.classification.InterfaceStability;
026    import org.apache.hadoop.record.RecordInput;
027    import org.apache.hadoop.record.RecordOutput;
028    
029    
030    /** 
031     * A record's Type Information object which can read/write itself. 
032     * 
033     * Type information for a record comprises metadata about the record, 
034     * as well as a collection of type information for each field in the record. 
035     * 
036     * @deprecated Replaced by <a href="https://hadoop.apache.org/avro/">Avro</a>.
037     */
038    @Deprecated
039    @InterfaceAudience.Public
040    @InterfaceStability.Stable
041    public class RecordTypeInfo extends org.apache.hadoop.record.Record 
042    {
043    
044      private String name;
045      // A RecordTypeInfo is really just a wrapper around StructTypeID
046      StructTypeID sTid;
047       // A RecordTypeInfo object is just a collection of TypeInfo objects for each of its fields.  
048      //private ArrayList<FieldTypeInfo> typeInfos = new ArrayList<FieldTypeInfo>();
049      // we keep a hashmap of struct/record names and their type information, as we need it to 
050      // set filters when reading nested structs. This map is used during deserialization.
051      //private Map<String, RecordTypeInfo> structRTIs = new HashMap<String, RecordTypeInfo>();
052    
053      /**
054       * Create an empty RecordTypeInfo object.
055       */
056      public RecordTypeInfo() {
057        sTid = new StructTypeID();
058      }
059    
060      /**
061       * Create a RecordTypeInfo object representing a record with the given name
062       * @param name Name of the record
063       */
064      public RecordTypeInfo(String name) {
065        this.name = name;
066        sTid = new StructTypeID();
067      }
068    
069      /*
070       * private constructor
071       */
072      private RecordTypeInfo(String name, StructTypeID stid) {
073        this.sTid = stid;
074        this.name = name;
075      }
076      
077      /**
078       * return the name of the record
079       */
080      public String getName() {
081        return name;
082      }
083    
084      /**
085       * set the name of the record
086       */
087      public void setName(String name) {
088        this.name = name;
089      }
090    
091      /**
092       * Add a field. 
093       * @param fieldName Name of the field
094       * @param tid Type ID of the field
095       */
096      public void addField(String fieldName, TypeID tid) {
097        sTid.getFieldTypeInfos().add(new FieldTypeInfo(fieldName, tid));
098      }
099      
100      private void addAll(Collection<FieldTypeInfo> tis) {
101        sTid.getFieldTypeInfos().addAll(tis);
102      }
103    
104      /**
105       * Return a collection of field type infos
106       */
107      public Collection<FieldTypeInfo> getFieldTypeInfos() {
108        return sTid.getFieldTypeInfos();
109      }
110      
111      /**
112       * Return the type info of a nested record. We only consider nesting 
113       * to one level. 
114       * @param name Name of the nested record
115       */
116      public RecordTypeInfo getNestedStructTypeInfo(String name) {
117        StructTypeID stid = sTid.findStruct(name);
118        if (null == stid) return null;
119        return new RecordTypeInfo(name, stid);
120      }
121    
122      /**
123       * Serialize the type information for a record
124       */
125      @Override
126      public void serialize(RecordOutput rout, String tag) throws IOException {
127        // write out any header, version info, here
128        rout.startRecord(this, tag);
129        rout.writeString(name, tag);
130        sTid.writeRest(rout, tag);
131        rout.endRecord(this, tag);
132      }
133    
134      /**
135       * Deserialize the type information for a record
136       */
137      @Override
138      public void deserialize(RecordInput rin, String tag) throws IOException {
139        // read in any header, version info 
140        rin.startRecord(tag);
141        // name
142        this.name = rin.readString(tag);
143        sTid.read(rin, tag);
144        rin.endRecord(tag);
145      }
146      
147      /**
148       * This class doesn't implement Comparable as it's not meant to be used 
149       * for anything besides de/serializing.
150       * So we always throw an exception.
151       * Not implemented. Always returns 0 if another RecordTypeInfo is passed in. 
152       */
153      @Override
154      public int compareTo (final Object peer_) throws ClassCastException {
155        if (!(peer_ instanceof RecordTypeInfo)) {
156          throw new ClassCastException("Comparing different types of records.");
157        }
158        throw new UnsupportedOperationException("compareTo() is not supported");
159      }
160    }
161