org.apache.hadoop.record.meta.StructTypeID Maven / Gradle / Ivy
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.hadoop.record.meta;
import java.io.IOException;
import java.util.*;
import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.classification.InterfaceStability;
import org.apache.hadoop.record.RecordInput;
import org.apache.hadoop.record.RecordOutput;
/**
* Represents typeID for a struct
*
* @deprecated Replaced by Avro.
*/
@Deprecated
@InterfaceAudience.Public
@InterfaceStability.Stable
public class StructTypeID extends TypeID {
private ArrayList typeInfos = new ArrayList();
StructTypeID() {
super(RIOType.STRUCT);
}
/**
* Create a StructTypeID based on the RecordTypeInfo of some record
*/
public StructTypeID(RecordTypeInfo rti) {
super(RIOType.STRUCT);
typeInfos.addAll(rti.getFieldTypeInfos());
}
void add (FieldTypeInfo ti) {
typeInfos.add(ti);
}
public Collection getFieldTypeInfos() {
return typeInfos;
}
/*
* return the StructTypeiD, if any, of the given field
*/
StructTypeID findStruct(String name) {
// walk through the list, searching. Not the most efficient way, but this
// in intended to be used rarely, so we keep it simple.
// As an optimization, we can keep a hashmap of record name to its RTI, for later.
for (FieldTypeInfo ti : typeInfos) {
if ((0 == ti.getFieldID().compareTo(name)) && (ti.getTypeID().getTypeVal() == RIOType.STRUCT)) {
return (StructTypeID) ti.getTypeID();
}
}
return null;
}
@Override
void write(RecordOutput rout, String tag) throws IOException {
rout.writeByte(typeVal, tag);
writeRest(rout, tag);
}
/*
* Writes rest of the struct (excluding type value).
* As an optimization, this method is directly called by RTI
* for the top level record so that we don't write out the byte
* indicating that this is a struct (since top level records are
* always structs).
*/
void writeRest(RecordOutput rout, String tag) throws IOException {
rout.writeInt(typeInfos.size(), tag);
for (FieldTypeInfo ti : typeInfos) {
ti.write(rout, tag);
}
}
/*
* deserialize ourselves. Called by RTI.
*/
void read(RecordInput rin, String tag) throws IOException {
// number of elements
int numElems = rin.readInt(tag);
for (int i=0; i