View Javadoc

1   /*
2    * Copyright (c) 2011.  The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.hbql.mapping;
22  
23  import org.apache.expreval.expr.node.GenericValue;
24  import org.apache.hadoop.hbase.hbql.client.HBqlException;
25  import org.apache.hadoop.hbase.hbql.impl.Utils;
26  import org.apache.hadoop.hbase.hbql.statement.args.ColumnWidth;
27  import org.apache.hadoop.hbase.hbql.statement.args.KeyInfo;
28  
29  import java.io.Serializable;
30  import java.lang.reflect.Field;
31  
32  
33  public final class ColumnDefinition implements Serializable {
34  
35      private static final long serialVersionUID = 1L;
36  
37      private           String       columnName;
38      private           String       aliasName;
39      private           boolean      isArray;
40      private           FieldType    fieldType;
41      private transient GenericValue defaultValue;
42      private           ColumnWidth  columnWidth;
43      private           String       getter;
44      private           String       setter;
45  
46      private FamilyMapping familyMapping = null;
47  
48      public ColumnDefinition() {
49      }
50  
51      private ColumnDefinition(final String familyName,
52                               final String columnName,
53                               final String aliasName,
54                               final FieldType fieldType,
55                               final boolean isArray,
56                               final ColumnWidth columnWidth,
57                               final GenericValue defaultValue,
58                               final String getter,
59                               final String setter) {
60  
61          this.columnName = columnName;
62          this.fieldType = fieldType;
63          this.isArray = isArray;
64          this.columnWidth = columnWidth != null ? columnWidth : new ColumnWidth();
65          this.aliasName = aliasName;
66          this.defaultValue = defaultValue;
67          this.getter = getter;
68          this.setter = setter;
69  
70          if (familyName != null)
71              this.setFamilyMapping(new FamilyMapping(familyName, false, null));
72      }
73  
74      // For KEY attribs
75      public static ColumnDefinition newKeyColumn(final KeyInfo keyInfo) {
76          return new ColumnDefinition("",
77                                      keyInfo.getKeyName(),
78                                      keyInfo.getKeyName(),
79                                      FieldType.KeyType,
80                                      false,
81                                      keyInfo,
82                                      null,
83                                      null,
84                                      null);
85      }
86  
87      // For Family Default attribs
88      public static ColumnDefinition newUnMappedColumn(final String familyName) {
89          return new ColumnDefinition(familyName, "", familyName, null, false, null, null, null, null);
90      }
91  
92      // For regular attribs
93      public static ColumnDefinition newMappedColumn(final String columnName,
94                                                     final String typeName,
95                                                     final boolean isArray,
96                                                     final ColumnWidth columnWidth,
97                                                     final String aliasName,
98                                                     final GenericValue defaultValue) {
99          final FieldType fieldType = getFieldType(typeName);
100         return new ColumnDefinition(null,
101                                     columnName,
102                                     aliasName,
103                                     fieldType,
104                                     isArray,
105                                     columnWidth,
106                                     defaultValue,
107                                     null,
108                                     null);
109     }
110 
111     // For FieldAttrib columns
112     public static ColumnDefinition newFieldAttribColumn(final String familyName,
113                                                         final String columnName,
114                                                         final Field field,
115                                                         final FieldType fieldType,
116                                                         final String getter,
117                                                         final String setter) {
118         return new ColumnDefinition(familyName,
119                                     Utils.isValidString(columnName) ? columnName : field.getName(),
120                                     field.getName(),
121                                     fieldType,
122                                     field.getType().isArray(),
123                                     null,
124                                     null,
125                                     getter,
126                                     setter);
127     }
128 
129     // For regular attribs
130     public static ColumnDefinition newIndexedColumn(final String columnName, final String typeName) {
131         final FieldType fieldType = getFieldType(typeName);
132         return new ColumnDefinition(null,
133                                     columnName,
134                                     null,
135                                     fieldType,
136                                     false,
137                                     null,
138                                     null,
139                                     null,
140                                     null);
141     }
142 
143 
144     // For SelectFamilyAttrib columns
145     public static ColumnDefinition newSelectFamilyAttribColumn(final String familyName) {
146         return new ColumnDefinition(familyName, "", "", null, false, null, null, null, null);
147     }
148 
149     private FamilyMapping getFamilyMapping() {
150         return this.familyMapping;
151     }
152 
153     void setFamilyMapping(final FamilyMapping familyMapping) {
154         this.familyMapping = familyMapping;
155     }
156 
157     private static FieldType getFieldType(final String typeName) {
158         try {
159             return FieldType.getFieldType(typeName);
160         }
161         catch (HBqlException e) {
162             return null;
163         }
164     }
165 
166     public String getFamilyName() {
167         return (this.getFamilyMapping() != null) ? this.getFamilyMapping().getFamilyName() : null;
168     }
169 
170     // This is used for validating width of values
171     public ColumnWidth getColumnWidth() {
172         return this.columnWidth;
173     }
174 
175     public String getColumnName() {
176         return this.columnName;
177     }
178 
179     public String getAliasName() {
180         return this.aliasName;
181     }
182 
183     public FieldType getFieldType() {
184         return this.fieldType;
185     }
186 
187     public boolean isAnArray() {
188         return this.isArray;
189     }
190 
191     public GenericValue getDefaultValue() {
192         return this.defaultValue;
193     }
194 
195     public String getGetter() {
196         return this.getter;
197     }
198 
199     public String getSetter() {
200         return this.setter;
201     }
202 }