1 /*
2  *  Copyright (c) 2010, 2012, 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, version 2.0,
6  *  as published by the Free Software Foundation.
7  *
8  *  This program is also distributed with certain software (including
9  *  but not limited to OpenSSL) that is licensed under separate terms,
10  *  as designated in a particular file or component or in included license
11  *  documentation.  The authors of MySQL hereby grant you an additional
12  *  permission to link the program and your derivative works with the
13  *  separately licensed software that they have included with MySQL.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License, version 2.0, for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
23  */
24 
25 package com.mysql.clusterj.tie;
26 
27 import com.mysql.ndbjtie.ndbapi.NdbDictionary.Dictionary;
28 import com.mysql.ndbjtie.ndbapi.NdbDictionary.DictionaryConst;
29 import com.mysql.ndbjtie.ndbapi.NdbDictionary.DictionaryConst.ListConst.Element;
30 import com.mysql.ndbjtie.ndbapi.NdbDictionary.DictionaryConst.ListConst.ElementArray;
31 import com.mysql.ndbjtie.ndbapi.NdbDictionary.IndexConst;
32 import com.mysql.ndbjtie.ndbapi.NdbDictionary.TableConst;
33 
34 import com.mysql.clusterj.core.store.Index;
35 import com.mysql.clusterj.core.store.Table;
36 
37 import com.mysql.clusterj.core.util.I18NHelper;
38 import com.mysql.clusterj.core.util.Logger;
39 import com.mysql.clusterj.core.util.LoggerFactoryService;
40 
41 /**
42  *
43  */
44 class DictionaryImpl implements com.mysql.clusterj.core.store.Dictionary {
45 
46     /** My message translator */
47     static final I18NHelper local = I18NHelper
48             .getInstance(DictionaryImpl.class);
49 
50     /** My logger */
51     static final Logger logger = LoggerFactoryService.getFactory()
52             .getInstance(DictionaryImpl.class);
53 
54     private Dictionary ndbDictionary;
55 
56     private ClusterConnectionImpl clusterConnection;
57 
DictionaryImpl(Dictionary ndbDictionary, ClusterConnectionImpl clusterConnection)58     public DictionaryImpl(Dictionary ndbDictionary, ClusterConnectionImpl clusterConnection) {
59         this.ndbDictionary = ndbDictionary;
60         this.clusterConnection = clusterConnection;
61     }
62 
getTable(String tableName)63     public Table getTable(String tableName) {
64         TableConst ndbTable = ndbDictionary.getTable(tableName);
65         if (ndbTable == null) {
66             // try the lower case table name
67             ndbTable = ndbDictionary.getTable(tableName.toLowerCase());
68         }
69         if (ndbTable == null) {
70             return null;
71         }
72         return new TableImpl(ndbTable, getIndexNames(ndbTable.getName()));
73     }
74 
getIndex(String indexName, String tableName, String indexAlias)75     public Index getIndex(String indexName, String tableName, String indexAlias) {
76         if ("PRIMARY$KEY".equals(indexName)) {
77             // create a pseudo index for the primary key hash
78             TableConst ndbTable = ndbDictionary.getTable(tableName);
79             if (ndbTable == null) {
80                 // try the lower case table name
81                 ndbTable = ndbDictionary.getTable(tableName.toLowerCase());
82             }
83             handleError(ndbTable, ndbDictionary, "");
84             return new IndexImpl(ndbTable);
85         }
86         IndexConst ndbIndex = ndbDictionary.getIndex(indexName, tableName);
87         if (ndbIndex == null) {
88             // try the lower case table name
89             ndbIndex = ndbDictionary.getIndex(indexName, tableName.toLowerCase());
90         }
91         handleError(ndbIndex, ndbDictionary, indexAlias);
92         return new IndexImpl(ndbIndex, indexAlias);
93     }
94 
getIndexNames(String tableName)95     public String[] getIndexNames(String tableName) {
96         // get all indexes for this table including ordered PRIMARY
97         com.mysql.ndbjtie.ndbapi.NdbDictionary.DictionaryConst.List indexList =
98             com.mysql.ndbjtie.ndbapi.NdbDictionary.DictionaryConst.List.create();
99         final String[] result;
100         try {
101             int returnCode = ndbDictionary.listIndexes(indexList, tableName);
102             handleError(returnCode, ndbDictionary, tableName);
103             int count = indexList.count();
104             result = new String[count];
105             if (logger.isDetailEnabled()) logger.detail("Found " + count + " indexes for " + tableName);
106             ElementArray elementArray = indexList.elements();
107             for (int i = 0; i < count; ++i) {
108                 Element element = elementArray.at(i);
109                 handleError(element, ndbDictionary, String.valueOf(i));
110                 String indexName = element.name();
111                 result[i] = indexName;
112             }
113         } finally {
114             // free the list memory even if error
115             com.mysql.ndbjtie.ndbapi.NdbDictionary.DictionaryConst.List.delete(indexList);
116         }
117         return result;
118     }
119 
handleError(int returnCode, DictionaryConst ndbDictionary, String extra)120     protected static void handleError(int returnCode, DictionaryConst ndbDictionary, String extra) {
121         if (returnCode == 0) {
122             return;
123         } else {
124             Utility.throwError(returnCode, ndbDictionary.getNdbError(), extra);
125         }
126     }
127 
handleError(Object object, DictionaryConst ndbDictionary, String extra)128     protected static void handleError(Object object, DictionaryConst ndbDictionary, String extra) {
129         if (object != null) {
130             return;
131         } else {
132             Utility.throwError(null, ndbDictionary.getNdbError(), extra);
133         }
134     }
135 
136     /** Remove cached table from this ndb dictionary. This allows schema change to work.
137      * @param tableName the name of the table
138      */
removeCachedTable(String tableName)139     public void removeCachedTable(String tableName) {
140         // remove the cached table from this dictionary
141         ndbDictionary.removeCachedTable(tableName);
142         // also remove the cached NdbRecord associated with this table
143         clusterConnection.unloadSchema(tableName);
144     }
145 
getNdbDictionary()146     public Dictionary getNdbDictionary() {
147         return ndbDictionary;
148     }
149 
150 }
151