1 | /** |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
3 | * or more contributor license agreements. See the NOTICE file |
---|
4 | * distributed with this work for additional information |
---|
5 | * regarding copyright ownership. The ASF licenses this file |
---|
6 | * to you under the Apache License, Version 2.0 (the |
---|
7 | * "License"); you may not use this file except in compliance |
---|
8 | * with the License. You may obtain a copy of the License at |
---|
9 | * |
---|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
11 | * |
---|
12 | * Unless required by applicable law or agreed to in writing, software |
---|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
15 | * See the License for the specific language governing permissions and |
---|
16 | * limitations under the License. |
---|
17 | */ |
---|
18 | |
---|
19 | package org.apache.hadoop.record.meta; |
---|
20 | |
---|
21 | import java.io.IOException; |
---|
22 | import java.util.*; |
---|
23 | |
---|
24 | import org.apache.hadoop.record.RecordInput; |
---|
25 | import org.apache.hadoop.record.RecordOutput; |
---|
26 | |
---|
27 | |
---|
28 | /** |
---|
29 | * A record's Type Information object which can read/write itself. |
---|
30 | * |
---|
31 | * Type information for a record comprises metadata about the record, |
---|
32 | * as well as a collection of type information for each field in the record. |
---|
33 | */ |
---|
34 | public class RecordTypeInfo extends org.apache.hadoop.record.Record |
---|
35 | { |
---|
36 | |
---|
37 | private String name; |
---|
38 | // A RecordTypeInfo is really just a wrapper around StructTypeID |
---|
39 | StructTypeID sTid; |
---|
40 | // A RecordTypeInfo object is just a collection of TypeInfo objects for each of its fields. |
---|
41 | //private ArrayList<FieldTypeInfo> typeInfos = new ArrayList<FieldTypeInfo>(); |
---|
42 | // we keep a hashmap of struct/record names and their type information, as we need it to |
---|
43 | // set filters when reading nested structs. This map is used during deserialization. |
---|
44 | //private Map<String, RecordTypeInfo> structRTIs = new HashMap<String, RecordTypeInfo>(); |
---|
45 | |
---|
46 | /** |
---|
47 | * Create an empty RecordTypeInfo object. |
---|
48 | */ |
---|
49 | public RecordTypeInfo() { |
---|
50 | sTid = new StructTypeID(); |
---|
51 | } |
---|
52 | |
---|
53 | /** |
---|
54 | * Create a RecordTypeInfo object representing a record with the given name |
---|
55 | * @param name Name of the record |
---|
56 | */ |
---|
57 | public RecordTypeInfo(String name) { |
---|
58 | this.name = name; |
---|
59 | sTid = new StructTypeID(); |
---|
60 | } |
---|
61 | |
---|
62 | /* |
---|
63 | * private constructor |
---|
64 | */ |
---|
65 | private RecordTypeInfo(String name, StructTypeID stid) { |
---|
66 | this.sTid = stid; |
---|
67 | this.name = name; |
---|
68 | } |
---|
69 | |
---|
70 | /** |
---|
71 | * return the name of the record |
---|
72 | */ |
---|
73 | public String getName() { |
---|
74 | return name; |
---|
75 | } |
---|
76 | |
---|
77 | /** |
---|
78 | * set the name of the record |
---|
79 | */ |
---|
80 | public void setName(String name) { |
---|
81 | this.name = name; |
---|
82 | } |
---|
83 | |
---|
84 | /** |
---|
85 | * Add a field. |
---|
86 | * @param fieldName Name of the field |
---|
87 | * @param tid Type ID of the field |
---|
88 | */ |
---|
89 | public void addField(String fieldName, TypeID tid) { |
---|
90 | sTid.getFieldTypeInfos().add(new FieldTypeInfo(fieldName, tid)); |
---|
91 | } |
---|
92 | |
---|
93 | private void addAll(Collection<FieldTypeInfo> tis) { |
---|
94 | sTid.getFieldTypeInfos().addAll(tis); |
---|
95 | } |
---|
96 | |
---|
97 | /** |
---|
98 | * Return a collection of field type infos |
---|
99 | */ |
---|
100 | public Collection<FieldTypeInfo> getFieldTypeInfos() { |
---|
101 | return sTid.getFieldTypeInfos(); |
---|
102 | } |
---|
103 | |
---|
104 | /** |
---|
105 | * Return the type info of a nested record. We only consider nesting |
---|
106 | * to one level. |
---|
107 | * @param name Name of the nested record |
---|
108 | */ |
---|
109 | public RecordTypeInfo getNestedStructTypeInfo(String name) { |
---|
110 | StructTypeID stid = sTid.findStruct(name); |
---|
111 | if (null == stid) return null; |
---|
112 | return new RecordTypeInfo(name, stid); |
---|
113 | } |
---|
114 | |
---|
115 | /** |
---|
116 | * Serialize the type information for a record |
---|
117 | */ |
---|
118 | public void serialize(RecordOutput rout, String tag) throws IOException { |
---|
119 | // write out any header, version info, here |
---|
120 | rout.startRecord(this, tag); |
---|
121 | rout.writeString(name, tag); |
---|
122 | sTid.writeRest(rout, tag); |
---|
123 | rout.endRecord(this, tag); |
---|
124 | } |
---|
125 | |
---|
126 | /** |
---|
127 | * Deserialize the type information for a record |
---|
128 | */ |
---|
129 | public void deserialize(RecordInput rin, String tag) throws IOException { |
---|
130 | // read in any header, version info |
---|
131 | rin.startRecord(tag); |
---|
132 | // name |
---|
133 | this.name = rin.readString(tag); |
---|
134 | sTid.read(rin, tag); |
---|
135 | rin.endRecord(tag); |
---|
136 | } |
---|
137 | |
---|
138 | /** |
---|
139 | * This class doesn't implement Comparable as it's not meant to be used |
---|
140 | * for anything besides de/serializing. |
---|
141 | * So we always throw an exception. |
---|
142 | * Not implemented. Always returns 0 if another RecordTypeInfo is passed in. |
---|
143 | */ |
---|
144 | public int compareTo (final Object peer_) throws ClassCastException { |
---|
145 | if (!(peer_ instanceof RecordTypeInfo)) { |
---|
146 | throw new ClassCastException("Comparing different types of records."); |
---|
147 | } |
---|
148 | throw new UnsupportedOperationException("compareTo() is not supported"); |
---|
149 | } |
---|
150 | } |
---|
151 | |
---|