18 package com.mysql.clusterj.core.metadata;
20 import java.util.ArrayList;
21 import java.util.BitSet;
22 import java.util.HashMap;
23 import java.util.HashSet;
24 import java.util.List;
28 import com.mysql.clusterj.ClusterJException;
29 import com.mysql.clusterj.ClusterJFatalInternalException;
30 import com.mysql.clusterj.ClusterJUserException;
31 import com.mysql.clusterj.core.CacheManager;
32 import com.mysql.clusterj.core.query.CandidateIndexImpl;
33 import com.mysql.clusterj.core.spi.DomainFieldHandler;
34 import com.mysql.clusterj.core.spi.DomainTypeHandler;
35 import com.mysql.clusterj.core.spi.ValueHandler;
36 import com.mysql.clusterj.core.store.Column;
37 import com.mysql.clusterj.core.store.Dictionary;
38 import com.mysql.clusterj.core.store.IndexOperation;
39 import com.mysql.clusterj.core.store.Operation;
40 import com.mysql.clusterj.core.store.PartitionKey;
41 import com.mysql.clusterj.core.store.ResultData;
42 import com.mysql.clusterj.core.store.Table;
43 import com.mysql.clusterj.core.util.I18NHelper;
44 import com.mysql.clusterj.core.util.Logger;
45 import com.mysql.clusterj.core.util.LoggerFactoryService;
52 public abstract class AbstractDomainTypeHandlerImpl<T>
implements DomainTypeHandler<T> {
82 protected int numberOfPartitionKeyColumns = 0;
91 protected int numberOfFields = 0;
94 protected List<DomainFieldHandler> persistentFieldHandlers =
new ArrayList<DomainFieldHandler>();
97 protected List<DomainFieldHandler> nonPKFieldHandlers =
new ArrayList<DomainFieldHandler>();
100 protected List<DomainFieldHandler> primitiveFieldHandlers =
new ArrayList<DomainFieldHandler>();
103 protected Map<String, Integer> fieldNameToNumber =
new HashMap<String, Integer>();
113 protected List<IndexHandlerImpl> indexHandlerImpls =
new ArrayList<IndexHandlerImpl>();
116 protected Set<String> indexNames =
new HashSet<String>();
128 for (
int i = 0;
i < primaryKeyColumnNames.length; ++
i) {
129 if (primaryKeyColumnNames[
i].equals(columnName)) {
130 idFieldHandlers[
i] = fmd;
131 idFieldNumbers[
i] = fmd.getFieldNumber();
132 if (logger.isDetailEnabled()) logger.detail(
"registerPrimaryKeyColumn found primary key " + columnName);
136 for (
int j = 0; j < partitionKeyColumnNames.length; ++j) {
137 if (partitionKeyColumnNames[j].equals(columnName)) {
138 partitionKeyFieldHandlers[j] = fmd;
139 if (logger.isDetailEnabled()) logger.detail(
"registerPrimaryKeyColumn found partition key " + columnName);
159 List<int[]> result =
new ArrayList<int[]>();
160 for (
int i = 0;
i < indexHandlerImpls.size(); ++
i) {
163 for (
int j = 0; j < columns.length; ++j) {
164 if (fmd.getColumnName().equals(columns[j])) {
165 if (logger.isDetailEnabled()) logger.detail(
"Found field " + fmd.getName()
166 +
" column " + fmd.getColumnName() +
" matching " + indexHandler.getIndexName());
168 result.add(
new int[]{
i,j});
173 if (logger.isDebugEnabled()) logger.debug(
"Found " + result.size() +
" indexes for " + columnName);
174 return result.toArray(
new int[result.size()][]);
184 Set<String> result =
new HashSet<String>();
185 for (
int[]
index: indexArray) {
186 result.add(indexHandlerImpls.get(
index[0]).getIndexName());
198 Set<String> columnNames =
new HashSet<String>();
199 for (
Column column : columns) {
200 String columnName = column.getName();
201 if (columnNames.contains(columnName)) {
204 local.message(
"ERR_Duplicate_Column",
205 name, indexName, columnName));
207 columnNames.add(columnName);
209 return columnNames.toArray(
new String[columnNames.size()]);
221 result[i++] = indexHandler.toCandidateIndexImpl();
226 public String getTableName() {
230 public int getNumberOfFields() {
231 return numberOfFields;
234 public DomainFieldHandler[] getIdFieldHandlers() {
235 return idFieldHandlers;
238 public DomainFieldHandler getFieldHandler(
String fieldName) {
239 for (DomainFieldHandler fmd: persistentFieldHandlers) {
240 if (fmd.getName().equals(fieldName)) {
244 throw new ClusterJUserException(
245 local.message(
"ERR_Not_A_Member", fieldName,
name));
248 public int getFieldNumber(
String fieldName) {
249 Integer fieldNumber = fieldNameToNumber.get(fieldName);
250 if (fieldNumber == null) {
251 throw new ClusterJFatalInternalException(
252 local.message(
"ERR_No_Field_Number", fieldName,
name));
254 return fieldNumber.intValue();
257 public void operationSetNonPKValues(ValueHandler
handler, Operation op) {
258 for (DomainFieldHandler fmd: nonPKFieldHandlers) {
259 if (handler.isModified(fmd.getFieldNumber())) {
260 fmd.operationSetValue(handler, op);
265 public void operationSetValues(ValueHandler handler, Operation op) {
266 for (DomainFieldHandler fmd: persistentFieldHandlers) {
267 if (logger.isDetailEnabled()) logger.detail(
"operationSetValues field: " + fmd.getName());
268 fmd.operationSetValue(handler, op);
272 public void operationSetModifiedNonPKValues(ValueHandler handler, Operation op) {
273 for (DomainFieldHandler fmd: nonPKFieldHandlers) {
274 fmd.operationSetModifiedValue(handler, op);
278 public void operationSetModifiedValues(ValueHandler handler, Operation op) {
279 for (DomainFieldHandler fmd: persistentFieldHandlers) {
280 fmd.operationSetModifiedValue(handler, op);
284 public void operationSetKeys(ValueHandler handler, Operation op) {
285 for (DomainFieldHandler fmd: idFieldHandlers) {
286 fmd.operationSetValue(handler, op);
290 public void operationGetValues(Operation op) {
291 for (DomainFieldHandler fmd: persistentFieldHandlers) {
292 fmd.operationGetValue(op);
296 public void operationGetValues(Operation op, BitSet fields) {
297 if (fields == null) {
298 operationGetValues(op);
301 for (DomainFieldHandler fmd: persistentFieldHandlers) {
302 if (fields.get(i++)) {
303 fmd.operationGetValue(op);
309 public void operationGetValuesExcept(IndexOperation op,
String index) {
310 for (DomainFieldHandler fmd: persistentFieldHandlers) {
311 if (!fmd.includedInIndex(index)) {
312 if (logger.isDetailEnabled()) logger.detail(
"operationGetValuesExcept index: " + index);
313 fmd.operationGetValue(op);
318 public void objectSetValues(ResultData rs, ValueHandler handler) {
319 for (DomainFieldHandler fmd: persistentFieldHandlers) {
320 fmd.objectSetValue(rs, handler);
324 public void objectSetValuesExcept(ResultData rs, ValueHandler handler,
String indexName) {
325 for (DomainFieldHandler fmd: persistentFieldHandlers) {
326 fmd.objectSetValueExceptIndex(rs, handler, indexName);
330 protected Table getTable(Dictionary dictionary) {
333 result = dictionary.getTable(tableName);
334 }
catch (Exception ex) {
335 throw new ClusterJException(
336 local.message(
"ERR_Get_NdbTable",
name, tableName), ex);
341 public int[] getKeyFieldNumbers() {
342 return idFieldNumbers;
345 public Table getStoreTable() {
357 if (logger.isDetailEnabled()) logger.detail(
358 "Field number " + fmd.getFieldNumber()
359 +
" column name " + fmd.getName() +
" field name " + fmd.getName());
360 fmd.partitionKeySetPart(result, handler);
373 public Set<com.mysql.clusterj.core.store.Column> getStoreColumns(BitSet fields) {
377 public ValueHandler createKeyValueHandler(Object keys) {
378 throw new ClusterJFatalInternalException(local.message(
"ERR_Implementation_Should_Not_Occur"));
381 public T getInstance(ValueHandler handler) {
382 throw new ClusterJFatalInternalException(local.message(
"ERR_Implementation_Should_Not_Occur"));
385 public Class<?> getOidClass() {
386 throw new ClusterJFatalInternalException(local.message(
"ERR_Implementation_Should_Not_Occur"));
389 public Class<T> getProxyClass() {
390 throw new ClusterJFatalInternalException(local.message(
"ERR_Implementation_Should_Not_Occur"));
393 public ValueHandler getValueHandler(Object instance) {
394 throw new ClusterJFatalInternalException(local.message(
"ERR_Implementation_Should_Not_Occur"));
397 public boolean isSupportedType() {
398 throw new ClusterJFatalInternalException(local.message(
"ERR_Implementation_Should_Not_Occur"));
401 public T newInstance() {
402 throw new ClusterJFatalInternalException(local.message(
"ERR_Implementation_Should_Not_Occur"));
405 public void objectMarkModified(ValueHandler handler,
String fieldName) {
406 throw new ClusterJFatalInternalException(local.message(
"ERR_Implementation_Should_Not_Occur"));
409 public void objectResetModified(ValueHandler handler) {
410 throw new ClusterJFatalInternalException(local.message(
"ERR_Implementation_Should_Not_Occur"));
413 public void objectSetCacheManager(CacheManager cm, Object instance) {
414 throw new ClusterJFatalInternalException(local.message(
"ERR_Implementation_Should_Not_Occur"));
418 int beginIndex = indexName.lastIndexOf(
"$unique");
419 if (beginIndex < 0) {
423 String result = indexName.substring(0, beginIndex);
427 public String[] getFieldNames() {