MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TableImpl.java
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.ArrayList;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24 
25 import com.mysql.clusterj.core.store.Column;
26 import com.mysql.clusterj.core.store.PartitionKey;
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 import com.mysql.ndbjtie.ndbapi.NdbRecord;
34 import com.mysql.ndbjtie.ndbapi.NdbDictionary.ColumnConst;
35 import com.mysql.ndbjtie.ndbapi.NdbDictionary.TableConst;
36 
40 class TableImpl implements Table {
41 
43  static final I18NHelper local = I18NHelper
44  .getInstance(TableImpl.class);
45 
47  static final Logger logger = LoggerFactoryService.getFactory()
48  .getInstance(TableImpl.class);
49 
51  private String tableName;
52 
54  private String[] columnNames;
55 
57  private String[] primaryKeyColumnNames;
58 
60  private String[] partitionKeyColumnNames;
61 
63  private Map<String, ColumnImpl> columns = new HashMap<String, ColumnImpl>();
64 
66  private String[] indexNames;
67 
69  private int[] lengths;
70 
72  private int[] offsets;
73 
75  private int bufferSize;
76 
78  private int maximumColumnId;
79 
81  private int maximumColumnLength = 0;
82 
83  public TableImpl(TableConst ndbTable, String[] indexNames) {
84  this.tableName = ndbTable.getName();
85  // process columns and partition key columns
86  List<String> partitionKeyColumnNameList = new ArrayList<String>();
87  List<String> primaryKeyColumnNameList = new ArrayList<String>();
88 
89  int noOfColumns = ndbTable.getNoOfColumns();
90  ColumnImpl[] columnImpls = new ColumnImpl[noOfColumns];
91  columnNames = new String[noOfColumns];
92  for (int i = 0; i < noOfColumns; ++i) {
93  ColumnConst column = ndbTable.getColumn(i);
94  // primary key and partition key columns are listed in the order declared in the schema
95  if (column.getPartitionKey()) {
96  partitionKeyColumnNameList.add(column.getName());
97  }
98  if (column.getPrimaryKey()) {
99  primaryKeyColumnNameList.add(column.getName());
100  }
101  ColumnConst ndbColumn = ndbTable.getColumn(i);
102  String columnName = ndbColumn.getName();
103  ColumnImpl columnImpl = new ColumnImpl(tableName, ndbColumn);
104  columns.put(columnName, columnImpl);
105  columnImpls[i] = columnImpl;
106  columnNames[i] = columnName;
107  // find maximum column id
108  int columnId = ndbColumn.getColumnNo();
109  if (columnId > maximumColumnId) {
110  maximumColumnId = columnId;
111  }
112  }
113  // iterate columns again and construct layout of record in memory
114  offsets = new int[maximumColumnId + 1];
115  lengths = new int[maximumColumnId + 1];
116  int offset = 0;
117  for (int i = 0; i < noOfColumns; ++i) {
118  ColumnImpl columnImpl = columnImpls[i];
119  int columnId = columnImpl.getColumnId();
120  int columnSpace = columnImpl.getColumnSpace();
121  lengths[columnId] = columnSpace;
122  offsets[columnId] = offset;
123  offset += columnSpace;
124  if (columnSpace > maximumColumnLength ) {
125  maximumColumnLength = columnSpace;
126  }
127  }
128  bufferSize = offset;
129  this.primaryKeyColumnNames =
130  primaryKeyColumnNameList.toArray(new String[primaryKeyColumnNameList.size()]);
131  this.partitionKeyColumnNames =
132  partitionKeyColumnNameList.toArray(new String[partitionKeyColumnNameList.size()]);
133  this.indexNames = indexNames;
134  }
135 
136  public Column getColumn(String columnName) {
137  return columns.get(columnName);
138  }
139 
140  public String getName() {
141  return tableName;
142  }
143 
144  public String[] getPrimaryKeyColumnNames() {
145  return primaryKeyColumnNames;
146  }
147 
148  public String[] getPartitionKeyColumnNames() {
149  return partitionKeyColumnNames;
150  }
151 
152  public PartitionKey createPartitionKey() {
153  PartitionKeyImpl result = new PartitionKeyImpl();
154  result.setTable(tableName);
155  return result;
156  }
157 
158  public String[] getIndexNames() {
159  return indexNames;
160  }
161 
162  public String[] getColumnNames() {
163  return columnNames;
164  }
165 
166  public int getMaximumColumnId() {
167  return maximumColumnId;
168  }
169 
170  public int getBufferSize() {
171  return bufferSize;
172  }
173 
174  public int[] getOffsets() {
175  return offsets;
176  }
177 
178  public int[] getLengths() {
179  return lengths;
180  }
181 
182  public int getMaximumColumnLength() {
183  return maximumColumnLength;
184  }
185 
186 }