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.statement;
22  
23  import org.apache.hadoop.hbase.hbql.client.ExecutionResults;
24  import org.apache.hadoop.hbase.hbql.client.HBqlException;
25  import org.apache.hadoop.hbase.hbql.impl.HConnectionImpl;
26  import org.apache.hadoop.hbase.hbql.mapping.AttribMapping;
27  import org.apache.hadoop.hbase.hbql.mapping.FamilyMapping;
28  import org.apache.hadoop.hbase.hbql.mapping.TableMapping;
29  import org.apache.hadoop.hbase.hbql.statement.args.KeyInfo;
30  import org.apache.hadoop.hbase.hbql.util.Sets;
31  
32  import java.util.List;
33  import java.util.Set;
34  
35  public class CreateMappingStatement extends StatementWithMapping implements ConnectionStatement {
36  
37      private final boolean             tempMapping;
38      private final boolean             systemMapping;
39      private final String              tableName;
40      private final KeyInfo             keyInfo;
41      private final List<FamilyMapping> familyMappingList;
42  
43      public CreateMappingStatement(final StatementPredicate predicate,
44                                    final boolean tempMapping,
45                                    final boolean systemMapping,
46                                    final String mappingName,
47                                    final String tableName,
48                                    final AttribMapping attribMapping) {
49          super(predicate, mappingName);
50          this.tempMapping = tempMapping;
51          this.systemMapping = systemMapping;
52          this.tableName = (tableName == null || tableName.length() == 0) ? mappingName : tableName;
53          this.keyInfo = attribMapping != null ? attribMapping.getKeyInfo() : null;
54          this.familyMappingList = attribMapping != null ? attribMapping.getFamilyMappingList() : null;
55      }
56  
57      private boolean isTempMapping() {
58          return this.tempMapping;
59      }
60  
61      private boolean isSystemMapping() {
62          return this.systemMapping;
63      }
64  
65      private String getTableName() {
66          return this.tableName;
67      }
68  
69      public KeyInfo getKeyInfo() {
70          return this.keyInfo;
71      }
72  
73      private List<FamilyMapping> getFamilyMappingList() {
74          return familyMappingList;
75      }
76  
77      public void validate() throws HBqlException {
78  
79          // Compute keywidth expression if present
80          if (this.getKeyInfo() != null)
81              this.getKeyInfo().validate();
82  
83          // Make sure family names are unique
84          if (this.getFamilyMappingList() != null) {
85              final Set<String> nameSet = Sets.newHashSet();
86              for (final FamilyMapping familyMapping : this.getFamilyMappingList()) {
87  
88                  familyMapping.validate();
89  
90                  final String familyName = familyMapping.getFamilyName();
91                  if (nameSet.contains(familyName))
92                      throw new HBqlException("Family name already mapped: " + familyName);
93  
94                  nameSet.add(familyName);
95              }
96          }
97      }
98  
99      protected ExecutionResults execute(final HConnectionImpl conn) throws HBqlException {
100 
101         final TableMapping tableMapping = conn.createMapping(this.isTempMapping(),
102                                                              this.isSystemMapping(),
103                                                              this.getMappingContext().getMappingName(),
104                                                              this.getTableName(),
105                                                              this.getKeyInfo(),
106                                                              this.getFamilyMappingList());
107         this.getMappingContext().setMapping(tableMapping);
108         tableMapping.validate(tableMapping.getMappingName());
109         return new ExecutionResults("Mapping " + tableMapping.getMappingName() + " defined.");
110     }
111 
112     public static String usage() {
113         return "CREATE [TEMP] MAPPING mapping_name [FOR TABLE table_name] [(key_name KEY [WIDTH int_expr], family_mapping_list)] [IF bool_expr]";
114     }
115 }