1 /*
2  *  Copyright (c) 2010, 2021, Oracle and/or its affiliates.
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 java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 
32 import com.mysql.clusterj.core.store.Column;
33 import com.mysql.clusterj.core.store.PartitionKey;
34 import com.mysql.clusterj.core.store.Table;
35 
36 import com.mysql.clusterj.core.util.I18NHelper;
37 import com.mysql.clusterj.core.util.Logger;
38 import com.mysql.clusterj.core.util.LoggerFactoryService;
39 
40 import com.mysql.ndbjtie.ndbapi.NdbDictionary.ColumnConst;
41 import com.mysql.ndbjtie.ndbapi.NdbDictionary.TableConst;
42 
43 /**
44  *
45  */
46 class TableImpl implements Table {
47 
48     /** My message translator */
49     static final I18NHelper local = I18NHelper
50             .getInstance(TableImpl.class);
51 
52     /** My logger */
53     static final Logger logger = LoggerFactoryService.getFactory()
54             .getInstance(TableImpl.class);
55 
56     /** The TableConst wrapped by this */
57     protected TableConst ndbTable;
58 
59     /** The table name */
60     private String tableName;
61 
62     /** The column names */
63     private String[] columnNames;
64 
65     /** The primary key column names */
66     private String[] primaryKeyColumnNames;
67 
68     /** The partition key column names */
69     private String[] partitionKeyColumnNames;
70 
71     /** The columns in this table */
72     private Map<String, ColumnImpl> columns = new HashMap<String, ColumnImpl>();
73 
74     /** The index names for this table */
75     private String[] indexNames;
76 
77     /** The array of lengths of column space indexed by column id */
78     private int[] lengths;
79 
80     /** The array of offsets of column space indexed by column id */
81     private int[] offsets;
82 
83     /** The total size of buffer needed for all columns */
84     private int bufferSize;
85 
86     /** The maximum column id */
87     private int maximumColumnId;
88 
89     /** The maximum column length */
90     private int maximumColumnLength = 0;
91 
92     /** The autoincrement column */
93     private Column autoIncrementColumn = null;
94 
TableImpl(TableConst ndbTable, String[] indexNames)95     public TableImpl(TableConst ndbTable, String[] indexNames) {
96         this.ndbTable = ndbTable;
97         this.tableName = ndbTable.getName();
98         // process columns and partition key columns
99         List<String> partitionKeyColumnNameList = new ArrayList<String>();
100         List<String> primaryKeyColumnNameList = new ArrayList<String>();
101 
102         int noOfColumns = ndbTable.getNoOfColumns();
103         ColumnImpl[] columnImpls = new ColumnImpl[noOfColumns];
104         columnNames = new String[noOfColumns];
105         for (int i = 0; i < noOfColumns; ++i) {
106             ColumnConst ndbColumn = ndbTable.getColumn(i);
107             // primary key and partition key columns are listed in the order declared in the schema
108             if (ndbColumn.getPartitionKey()) {
109                 partitionKeyColumnNameList.add(ndbColumn.getName());
110             }
111             if (ndbColumn.getPrimaryKey()) {
112                 primaryKeyColumnNameList.add(ndbColumn.getName());
113             }
114             String columnName = ndbColumn.getName();
115             ColumnImpl columnImpl = new ColumnImpl(tableName, ndbColumn);
116             if (ndbColumn.getAutoIncrement()) {
117                 autoIncrementColumn = columnImpl;
118             }
119             columns.put(columnName, columnImpl);
120             columnImpls[i] = columnImpl;
121             columnNames[i] = columnName;
122             // find maximum column id
123             int columnId = ndbColumn.getColumnNo();
124             if (columnId > maximumColumnId) {
125                 maximumColumnId = columnId;
126             }
127         }
128         // iterate columns again and construct layout of record in memory
129         offsets = new int[maximumColumnId + 1];
130         lengths = new int[maximumColumnId + 1];
131         int offset = 0;
132         for (int i = 0; i < noOfColumns; ++i) {
133             ColumnImpl columnImpl = columnImpls[i];
134             int columnId = columnImpl.getColumnId();
135             int columnSpace = columnImpl.getColumnSpace();
136             lengths[columnId] = columnSpace;
137             offsets[columnId] = offset;
138             offset += columnSpace;
139             if (columnSpace > maximumColumnLength ) {
140                 maximumColumnLength = columnSpace;
141             }
142         }
143         bufferSize = offset;
144         this.primaryKeyColumnNames =
145             primaryKeyColumnNameList.toArray(new String[primaryKeyColumnNameList.size()]);
146         this.partitionKeyColumnNames =
147             partitionKeyColumnNameList.toArray(new String[partitionKeyColumnNameList.size()]);
148         this.indexNames = indexNames;
149     }
150 
getAutoIncrementColumn()151     public Column getAutoIncrementColumn() {
152         return autoIncrementColumn;
153     }
getColumn(String columnName)154     public Column getColumn(String columnName) {
155         return columns.get(columnName);
156     }
157 
getName()158     public String getName() {
159         return tableName;
160     }
161 
getPrimaryKeyColumnNames()162     public String[] getPrimaryKeyColumnNames() {
163         return primaryKeyColumnNames;
164     }
165 
getPartitionKeyColumnNames()166     public String[] getPartitionKeyColumnNames() {
167         return partitionKeyColumnNames;
168     }
169 
createPartitionKey()170     public PartitionKey createPartitionKey() {
171         PartitionKeyImpl result = new PartitionKeyImpl();
172         result.setTable(tableName);
173         return result;
174     }
175 
getIndexNames()176     public String[] getIndexNames() {
177         return indexNames;
178     }
179 
getColumnNames()180     public String[] getColumnNames() {
181         return columnNames;
182     }
183 
getMaximumColumnId()184     public int getMaximumColumnId() {
185         return maximumColumnId;
186     }
187 
getBufferSize()188     public int getBufferSize() {
189         return bufferSize;
190     }
191 
getOffsets()192     public int[] getOffsets() {
193         return offsets;
194     }
195 
getLengths()196     public int[] getLengths() {
197         return lengths;
198     }
199 
getMaximumColumnLength()200     public int getMaximumColumnLength() {
201         return maximumColumnLength;
202     }
203 
204 }
205