2 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
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.
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.
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
18 package com.mysql.clusterj.tie;
20 import java.util.Arrays;
22 import com.mysql.ndbjtie.ndbapi.NdbDictionary.IndexConst;
23 import com.mysql.ndbjtie.ndbapi.NdbDictionary.TableConst;
24 import com.mysql.clusterj.core.store.Index;
25 import com.mysql.clusterj.core.util.I18NHelper;
26 import com.mysql.clusterj.core.util.Logger;
27 import com.mysql.clusterj.core.util.LoggerFactoryService;
32 class IndexImpl implements Index {
34 /** My message translator */
35 static final I18NHelper local = I18NHelper
36 .getInstance(IndexImpl.class);
39 static final Logger logger = LoggerFactoryService.getFactory()
40 .getInstance(IndexImpl.class);
42 private String tableName;
44 /** The name of the index as known by the schem and user */
47 /** The number of columns in the index */
48 private int noOfColumns;
50 /** The names of the columns, in the order declared in the KEY clause */
51 private String[] columnNames;
53 /** Is the index unique? */
54 private boolean unique;
56 /** The actual name of the index, e.g. idx_name$unique */
57 private String internalName;
60 * Create a new IndexImpl for the index.
61 * @param ndbIndex the ndbIndex for this index
62 * @param indexAlias the name as known by the schema
64 public IndexImpl(IndexConst ndbIndex, String indexAlias) {
65 this.internalName = ndbIndex.getName();
66 this.tableName = ndbIndex.getTable();
67 this.name = indexAlias;
68 this.unique = ndbIndex.getType() == IndexConst.Type.UniqueHashIndex;
69 this.noOfColumns = ndbIndex.getNoOfColumns();
70 this.columnNames = new String[noOfColumns];
71 for (int i = 0; i < noOfColumns; ++i) {
72 String columnName = ndbIndex.getColumn(i).getName();
73 this.columnNames[i] = columnName;
75 if (logger.isDetailEnabled()) logger.detail(toString());
78 /** Create a pseudo IndexImpl for the primary key. The primary key pseudo index
79 * is not an index but is treated as an index by query. This index is not used
80 * with an index scan but is only used for primary key lookup.
82 * @param ndbTable the ndb Table
84 public IndexImpl(TableConst ndbTable) {
85 this.internalName = "PRIMARY";
86 this.name = "PRIMARY";
87 this.tableName = ndbTable.getName();
89 this.noOfColumns = ndbTable.getNoOfPrimaryKeys();
90 this.columnNames = new String[noOfColumns];
91 for (int i = 0; i < noOfColumns; ++i) {
92 this.columnNames[i] = ndbTable.getPrimaryKey(i);
94 if (logger.isDetailEnabled()) logger.detail(toString());
97 public boolean isUnique() {
101 public String getName() {
105 public String getInternalName() {
109 public String[] getColumnNames() {
114 public String toString() {
115 return "IndexImpl name: " + name + " internal name: " + internalName + " table: " + tableName + " unique: " + unique + " columns: " + Arrays.toString(columnNames);