]> review.fuel-infra Code Review - packages/trusty/mysql-wsrep-5.6.git/blob
35188d56060b53d16b655d99e797183ccf9cf88e
[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 com.mysql.ndbjtie.ndbapi.NdbDictionary.DictionaryConst;
21 import com.mysql.ndbjtie.ndbapi.NdbDictionary.IndexConst;
22 import com.mysql.ndbjtie.ndbapi.NdbDictionary.TableConst;
23 import com.mysql.ndbjtie.ndbapi.NdbDictionary.DictionaryConst.ListConst.Element;
24 import com.mysql.ndbjtie.ndbapi.NdbDictionary.DictionaryConst.ListConst.ElementArray;
25
26 import com.mysql.clusterj.core.store.Index;
27 import com.mysql.clusterj.core.store.Table;
28
29 import com.mysql.clusterj.core.util.I18NHelper;
30 import com.mysql.clusterj.core.util.Logger;
31 import com.mysql.clusterj.core.util.LoggerFactoryService;
32
33 /**
34  *
35  */
36 class DictionaryImpl implements com.mysql.clusterj.core.store.Dictionary {
37
38     /** My message translator */
39     static final I18NHelper local = I18NHelper
40             .getInstance(DictionaryImpl.class);
41
42     /** My logger */
43     static final Logger logger = LoggerFactoryService.getFactory()
44             .getInstance(DictionaryImpl.class);
45
46     private DictionaryConst ndbDictionary;
47
48     public DictionaryImpl(DictionaryConst ndbDictionary) {
49         this.ndbDictionary = ndbDictionary;
50     }
51
52     public Table getTable(String tableName) {
53         TableConst ndbTable = ndbDictionary.getTable(tableName);
54         if (ndbTable == null) {
55             // try the lower case table name
56             ndbTable = ndbDictionary.getTable(tableName.toLowerCase());
57         }
58         if (ndbTable == null) {
59             return null;
60         }
61         return new TableImpl(ndbTable, getIndexNames(ndbTable.getName()));
62     }
63
64     public Index getIndex(String indexName, String tableName, String indexAlias) {
65         if ("PRIMARY$KEY".equals(indexName)) {
66             // create a pseudo index for the primary key hash
67             TableConst ndbTable = ndbDictionary.getTable(tableName);
68             if (ndbTable == null) {
69                 // try the lower case table name
70                 ndbTable = ndbDictionary.getTable(tableName.toLowerCase());
71             }
72             handleError(ndbTable, ndbDictionary, "");
73             return new IndexImpl(ndbTable);
74         }
75         IndexConst ndbIndex = ndbDictionary.getIndex(indexName, tableName);
76         if (ndbIndex == null) {
77             // try the lower case table name
78             ndbIndex = ndbDictionary.getIndex(indexName, tableName.toLowerCase());
79         }
80         handleError(ndbIndex, ndbDictionary, indexAlias);
81         return new IndexImpl(ndbIndex, indexAlias);
82     }
83
84     public String[] getIndexNames(String tableName) {
85         // get all indexes for this table including ordered PRIMARY
86         com.mysql.ndbjtie.ndbapi.NdbDictionary.DictionaryConst.List indexList = 
87             com.mysql.ndbjtie.ndbapi.NdbDictionary.DictionaryConst.List.create();
88         final String[] result;
89         try {
90             int returnCode = ndbDictionary.listIndexes(indexList, tableName);
91             handleError(returnCode, ndbDictionary, tableName);
92             int count = indexList.count();
93             result = new String[count];
94             if (logger.isDetailEnabled()) logger.detail("Found " + count + " indexes for " + tableName);
95             ElementArray elementArray = indexList.elements();
96             for (int i = 0; i < count; ++i) {
97                 Element element = elementArray.at(i);
98                 handleError(element, ndbDictionary, String.valueOf(i));
99                 String indexName = element.name();
100                 result[i] = indexName;
101             }
102         } finally {
103             // free the list memory even if error
104             com.mysql.ndbjtie.ndbapi.NdbDictionary.DictionaryConst.List.delete(indexList);
105         }
106         return result;
107     }
108
109     protected static void handleError(int returnCode, DictionaryConst ndbDictionary, String extra) {
110         if (returnCode == 0) {
111             return;
112         } else {
113             Utility.throwError(returnCode, ndbDictionary.getNdbError(), extra);
114         }
115     }
116
117     protected static void handleError(Object object, DictionaryConst ndbDictionary, String extra) {
118         if (object != null) {
119             return;
120         } else {
121             Utility.throwError(null, ndbDictionary.getNdbError(), extra);
122         }
123     }
124
125 }