2552ca99e12bab73c95c147de2166a13206eaf19
[packages/trusty/mysql-wsrep-5.6.git] /
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.tie;
19
20 import java.util.Arrays;
21
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;
28
29 /**
30  *
31  */
32 class IndexImpl implements Index {
33
34     /** My message translator */
35     static final I18NHelper local = I18NHelper
36             .getInstance(IndexImpl.class);
37
38     /** My logger */
39     static final Logger logger = LoggerFactoryService.getFactory()
40             .getInstance(IndexImpl.class);
41
42     private String tableName;
43
44     /** The name of the index as known by the schem and user */
45     private String name;
46
47     /** The number of columns in the index */
48     private int noOfColumns;
49
50     /** The names of the columns, in the order declared in the KEY clause */
51     private String[] columnNames;
52
53     /** Is the index unique? */
54     private boolean unique;
55
56     /** The actual name of the index, e.g. idx_name$unique */
57     private String internalName;
58
59     /**
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
63      */
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;
74         }
75         if (logger.isDetailEnabled()) logger.detail(toString());
76     }
77
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.
81      * 
82      * @param ndbTable the ndb Table
83      */
84     public IndexImpl(TableConst ndbTable) {
85         this.internalName = "PRIMARY";
86         this.name = "PRIMARY";
87         this.tableName = ndbTable.getName();
88         this.unique = true;
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);
93         }
94         if (logger.isDetailEnabled()) logger.detail(toString());
95     }
96
97     public boolean isUnique() {
98         return unique;
99     }
100
101     public String getName() {
102         return name;
103     }
104
105     public String getInternalName() {
106         return internalName;
107     }
108
109     public String[] getColumnNames() {
110         return columnNames;
111     }
112
113     @Override
114     public String toString() {
115         return "IndexImpl name: " + name + " internal name: " + internalName + " table: " + tableName + " unique: " + unique + " columns: " + Arrays.toString(columnNames);
116     }
117
118 }