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.io;
22  
23  import org.apache.hadoop.hbase.hbql.client.HBqlException;
24  import org.apache.hadoop.hbase.hbql.mapping.FieldType;
25  
26  public abstract class Serialization {
27  
28      public enum TYPE {
29          JAVA, HADOOP
30      }
31  
32      private static final Serialization java   = new JavaSerialization();
33      private static final Serialization hadoop = new HadoopSerialization();
34  
35      public static Serialization getSerializationStrategy(final TYPE type) {
36  
37          switch (type) {
38              case JAVA:
39                  return java;
40              case HADOOP:
41                  return hadoop;
42          }
43  
44          return null;
45      }
46  
47      public Number getNumberFromBytes(FieldType fieldType, byte[] b) throws HBqlException {
48          return (Number)IO.getSerialization().getScalarFromBytes(fieldType, b);
49      }
50  
51      public String getStringFromBytes(final byte[] b) throws HBqlException {
52          return (String)this.getScalarFromBytes(FieldType.StringType, b);
53      }
54  
55      public Byte getByteFromBytes(final byte[] b) throws HBqlException {
56          return (Byte)this.getScalarFromBytes(FieldType.ByteType, b);
57      }
58  
59      public Boolean getBooleanFromBytes(final byte[] b) throws HBqlException {
60          return (Boolean)this.getScalarFromBytes(FieldType.BooleanType, b);
61      }
62  
63      public byte[] getStringAsBytes(final String obj) throws HBqlException {
64          return this.getScalarAsBytes(FieldType.StringType, obj);
65      }
66  
67      public byte[] getNumberEqualityBytes(FieldType fieldType, final Number val) throws HBqlException {
68  
69          switch (fieldType) {
70  
71              case ByteType: {
72                  final byte[] retval = {val.byteValue()};
73                  return retval;
74              }
75  
76              case ShortType:
77                  return this.getScalarAsBytes(fieldType, val.shortValue());
78  
79              case IntegerType:
80                  return this.getScalarAsBytes(fieldType, val.intValue());
81  
82              case LongType:
83                  return this.getScalarAsBytes(fieldType, val.longValue());
84  
85              case FloatType:
86                  return this.getScalarAsBytes(fieldType, val.floatValue());
87  
88              case DoubleType:
89                  return this.getScalarAsBytes(fieldType, val.doubleValue());
90  
91              default:
92                  throw new HBqlException("Unknown type in getNumbeEqualityBytes() - " + fieldType);
93          }
94      }
95  
96      public Object getObjectFromBytes(final byte[] b) throws HBqlException {
97          return this.getScalarFromBytes(FieldType.ObjectType, b);
98      }
99  
100     public byte[] getObjectAsBytes(final Object obj) throws HBqlException {
101         return this.getScalarAsBytes(FieldType.getFieldType(obj), obj);
102     }
103 
104     abstract public Object getScalarFromBytes(FieldType fieldType, byte[] b) throws HBqlException;
105 
106     abstract public byte[] getScalarAsBytes(FieldType fieldType, Object obj) throws HBqlException;
107 
108     abstract public Object getArrayFromBytes(FieldType fieldType, Class clazz, byte[] b) throws HBqlException;
109 
110     abstract public byte[] getArrayAsBytes(FieldType fieldType, Object obj) throws HBqlException;
111 
112     public boolean isSerializable(final Object obj) {
113 
114         try {
115             final byte[] b = getObjectAsBytes(obj);
116             final Object newobj = getScalarFromBytes(FieldType.getFieldType(obj), b);
117         }
118         catch (HBqlException e) {
119             e.printStackTrace();
120             return false;
121         }
122         return true;
123     }
124 
125     protected String getExceptionMessage(final String name, final Exception e) {
126         return "Error in " + name + ": " + e.getClass().getSimpleName() + "-" + e.getMessage();
127     }
128 }