MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
IndexHandlerImpl.java
1 /*
2  Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 package com.mysql.clusterj.core.metadata;
19 
20 import com.mysql.clusterj.core.query.CandidateIndexImpl;
21 import com.mysql.clusterj.core.spi.DomainTypeHandler;
22 import com.mysql.clusterj.core.store.Dictionary;
23 import com.mysql.clusterj.core.store.Index;
24 import com.mysql.clusterj.core.util.I18NHelper;
25 import com.mysql.clusterj.core.util.Logger;
26 import com.mysql.clusterj.core.util.LoggerFactoryService;
27 
28 import java.util.Arrays;
29 
56 public class IndexHandlerImpl {
57 
59  static final I18NHelper local = I18NHelper.getInstance(IndexHandlerImpl.class);
60 
62  static final Logger logger = LoggerFactoryService.getFactory().getInstance(IndexHandlerImpl.class);
63 
65  static final String UNIQUE_SUFFIX = "$unique";
66 
68  protected String className;
69 
71  protected String tableName;
72 
74  private String indexName;
75 
77  protected Index storeIndex;
78 
80  protected boolean unique = true;
81 
84 
86  protected final String[] columnNames;
87 
89  private boolean usable = true;
90 
92  private String reason = null;
93 
101  public IndexHandlerImpl(DomainTypeHandler<?> domainTypeHandler,
102  Dictionary dictionary, Index storeIndex, String[] columnNames) {
103  this.className = domainTypeHandler.getName();
104  this.storeIndex = storeIndex;
105  this.indexName = storeIndex.getName();
106  this.tableName = domainTypeHandler.getTableName();
107  this.columnNames = columnNames;
108  int numberOfColumns = columnNames.length;
109  // the fields are not yet known; will be filled later
110  this.fields = new AbstractDomainFieldHandlerImpl[numberOfColumns];
111  this.unique = storeIndex.isUnique();
112  if (logger.isDebugEnabled()) logger.debug(toString());
113  }
114 
123  public IndexHandlerImpl(DomainTypeHandler<?> domainTypeHandler,
124  Dictionary dictionary, String indexName, AbstractDomainFieldHandlerImpl fmd) {
125  this.className = domainTypeHandler.getName();
126  this.indexName = indexName;
127  this.tableName = domainTypeHandler.getTableName();
128  this.storeIndex = getIndex(dictionary, tableName, indexName);
129  this.unique = isUnique(storeIndex);
130  this.columnNames = fmd.getColumnNames();
131  this.fields = new AbstractDomainFieldHandlerImpl[]{fmd};
132  if (logger.isDebugEnabled()) logger.debug(toString());
133  }
134 
140  if (!usable) {
142  } else {
143  return new CandidateIndexImpl(
145  }
146  }
147 
148  @Override
149  public String toString() {
150  StringBuffer buffer = new StringBuffer();
151  buffer.append("IndexHandler for class ");
152  buffer.append(className);
153  buffer.append(" index: ");
154  buffer.append(indexName);
155  buffer.append(" unique: ");
156  buffer.append(unique);
157  buffer.append(" columns: ");
158  buffer.append(Arrays.toString(columnNames));
159  return buffer.toString();
160  }
161 
168  fields[i] = fmd;
169  fmd.validateIndexType(indexName, unique);
170  }
171 
173  public String[] getColumnNames() {
174  return columnNames;
175  }
176 
181  for (int i = 0; i < columnNames.length; ++i) {
183  if (fmd == null || !(columnNames[i].equals(fmd.getColumnName()))) {
184  usable = false;
185  reason = local.message(
186  "ERR_Index_Mismatch", className, indexName, columnNames[i]);
187  }
188  }
189  }
190 
191  protected boolean isUnique(Index storeIndex) {
192  return storeIndex.isUnique();
193  }
194 
195  public boolean isUsable() {
196  return usable;
197  }
198 
199  public String getReason() {
200  return reason;
201  }
202 
203  protected Index getIndex(Dictionary dictionary,
204  String tableName, String indexName) {
205  return dictionary.getIndex(indexName, tableName, indexName);
206  }
207 
208  public String getIndexName() {
209  return indexName;
210  }
211 
212 }