View Javadoc

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.hbase;
20  
21  import com.google.protobuf.InvalidProtocolBufferException;
22  import org.apache.hadoop.hbase.classification.InterfaceAudience;
23  import org.apache.hadoop.hbase.exceptions.DeserializationException;
24  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
25  import org.apache.hadoop.hbase.protobuf.generated.HBase170CompatibilityProtos.TableDescriptor;
26  import org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos.Table;
27  import org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos.Table.State;
28  
29  /**
30   * Only used for HBase 1.7.0 compatibility sakes.
31   */
32  @InterfaceAudience.Private
33  public final class DeprecatedTableDescriptor {
34  
35    private HTableDescriptor hTableDescriptor;
36    private Table tableState;
37  
38    private DeprecatedTableDescriptor(HTableDescriptor hTableDescriptor, Table tableState) {
39      this.hTableDescriptor = hTableDescriptor;
40      this.tableState = tableState;
41    }
42  
43    public HTableDescriptor getHTableDescriptor() {
44      return hTableDescriptor;
45    }
46  
47    public Table getTableState() {
48      return tableState;
49    }
50  
51    /**
52     * Utility method to parse bytes serialized as incompatible TableDescriptors.
53     * @param bytes A pb serialized {@link TableDescriptor} instance with pb magic prefix
54     */
55    public static DeprecatedTableDescriptor parseFrom(final byte [] bytes)
56        throws DeserializationException {
57      if (!ProtobufUtil.isPBMagicPrefix(bytes)) {
58        throw new DeserializationException("Expected PB encoded TableDescriptor");
59      }
60      int pblen = ProtobufUtil.lengthOfPBMagic();
61      TableDescriptor.Builder builder = TableDescriptor.newBuilder();
62      TableDescriptor ts;
63      try {
64        ts = builder.mergeFrom(bytes, pblen, bytes.length - pblen).build();
65      } catch (InvalidProtocolBufferException e) {
66        throw new DeserializationException(e);
67      }
68      return convert(ts);
69    }
70  
71    private static DeprecatedTableDescriptor convert(TableDescriptor proto) {
72      HTableDescriptor hTableDescriptor = HTableDescriptor.convert(proto.getSchema());
73      State state = State.valueOf(proto.getState().getNumber());
74      Table tableState = Table.newBuilder().setState(state).build();
75      return new DeprecatedTableDescriptor(hTableDescriptor, tableState);
76    }
77  }