Debian Codegraph @

src/main/org/h2/result/RowFactory.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.result;
7
8 import org.h2.engine.CastDataProvider;
9 import org.h2.mvstore.db.RowDataType;
10 import org.h2.store.DataHandler;
11 import org.h2.table.IndexColumn;
12 import org.h2.value.CompareMode;
13 import org.h2.value.TypeInfo;
14 import org.h2.value.Typed;
15 import org.h2.value.Value;
16
17 /**
18 * Creates rows.
19 *
20 * @author Sergi Vladykin
21 * @author <a href='mailto:[email protected]'>Andrei Tokar</a>
22 */
23 public abstract class RowFactory {
24
25 private static final class Holder {
26 static final RowFactory EFFECTIVE = DefaultRowFactory.INSTANCE;
27 }
28
29 public static DefaultRowFactory getDefaultRowFactory() {
30 return DefaultRowFactory.INSTANCE;
31 }
32
33 public static RowFactory getRowFactory() {
34 return Holder.EFFECTIVE;
35 }
36
37 /**
38 * Create a new row factory.
39 *
40 * @param provider the cast provider
41 * @param compareMode the compare mode
42 * @param handler the data handler
43 * @param columns the list of columns
44 * @param indexColumns the list of index columns
45 * @param storeKeys whether row keys are stored
46 * @return the (possibly new) row factory
47 */
48 public RowFactory createRowFactory(CastDataProvider provider, CompareMode compareMode, DataHandler handler,
49 Typed[] columns, IndexColumn[] indexColumns, boolean storeKeys) {
50 return this;
51 }
52
53 /**
54 * Create a new row.
55 *
56 * @param data the values
57 * @param memory the estimated memory usage in bytes
58 * @return the created row
59 */
60 public abstract Row createRow(Value[] data, int memory);
61
62 /**
63 * Create new row.
64 *
65 * @return the created row
66 */
67 public abstract SearchRow createRow();
68
69 public abstract RowDataType getRowDataType();
70
71 public abstract int[] getIndexes();
72
73 public abstract TypeInfo[] getColumnTypes();
74
75 public abstract int getColumnCount();
76
77 public abstract boolean getStoreKeys();
78
79
80 /**
81 * Default implementation of row factory.
82 */
83 public static final class DefaultRowFactory extends RowFactory {
84 private final RowDataType dataType;
85 private final int columnCount;
86 private final int[] indexes;
87 private TypeInfo[] columnTypes;
88 private final int[] map;
89
90 public static final DefaultRowFactory INSTANCE = new DefaultRowFactory();
91
92 DefaultRowFactory() {
93 this(new RowDataType(null, CompareMode.getInstance(null, 0), null, null, null, 0, true), 0, null, null);
94 }
95
96 private DefaultRowFactory(RowDataType dataType, int columnCount, int[] indexes, TypeInfo[] columnTypes) {
97 this.dataType = dataType;
98 this.columnCount = columnCount;
99 this.indexes = indexes;
100 if (indexes == null) {
101 map = null;
102 } else {
103 map = new int[columnCount];
104 for (int i = 0, l = indexes.length; i < l;) {
105 map[indexes[i]] = ++i;
106 }
107 }
108 this.columnTypes = columnTypes;
109 }
110
111 @Override
112 public RowFactory createRowFactory(CastDataProvider provider, CompareMode compareMode, DataHandler handler,
113 Typed[] columns, IndexColumn[] indexColumns, boolean storeKeys) {
114 int[] indexes = null;
115 int[] sortTypes = null;
116 TypeInfo[] columnTypes = null;
117 int columnCount = 0;
118 if (columns != null) {
119 columnCount = columns.length;
120 if (indexColumns == null) {
121 sortTypes = new int[columnCount];
122 for (int i = 0; i < columnCount; i++) {
123 sortTypes[i] = SortOrder.ASCENDING;
124 }
125 } else {
126 int len = indexColumns.length;
127 indexes = new int[len];
128 sortTypes = new int[len];
129 for (int i = 0; i < len; i++) {
130 IndexColumn indexColumn = indexColumns[i];
131 indexes[i] = indexColumn.column.getColumnId();
132 sortTypes[i] = indexColumn.sortType;
133 }
134 }
135 columnTypes = new TypeInfo[columnCount];
136 for (int i = 0; i < columnCount; i++) {
137 columnTypes[i] = columns[i].getType();
138 }
139 }
140 return createRowFactory(provider, compareMode, handler, sortTypes, indexes, columnTypes, columnCount,
141 storeKeys);
142 }
143
144 /**
145 * Create a new row factory.
146 *
147 * @param provider the cast provider
148 * @param compareMode the compare mode
149 * @param handler the data handler
150 * @param sortTypes the sort types
151 * @param indexes the list of indexed columns
152 * @param columnTypes the list of column data type information
153 * @param columnCount the number of columns
154 * @param storeKeys whether row keys are stored
155 * @return the (possibly new) row factory
156 */
157 public RowFactory createRowFactory(CastDataProvider provider, CompareMode compareMode, DataHandler handler,
158 int[] sortTypes, int[] indexes, TypeInfo[] columnTypes, int columnCount, boolean storeKeys) {
159 RowDataType rowDataType = new RowDataType(provider, compareMode, handler, sortTypes, indexes, columnCount,
160 storeKeys);
161 RowFactory rowFactory = new DefaultRowFactory(rowDataType, columnCount, indexes, columnTypes);
162 rowDataType.setRowFactory(rowFactory);
163 return rowFactory;
164 }
165
166 @Override
167 public Row createRow(Value[] data, int memory) {
168 return new DefaultRow(data, memory);
169 }
170
171 @Override
172 public SearchRow createRow() {
173 if (indexes == null) {
174 return new DefaultRow(columnCount);
175 } else if (indexes.length == 1) {
176 return new SimpleRowValue(columnCount, indexes[0]);
177 } else {
178 return new Sparse(columnCount, indexes.length, map);
179 }
180 }
181
182 @Override
183 public RowDataType getRowDataType() {
184 return dataType;
185 }
186
187 @Override
188 public int[] getIndexes() {
189 return indexes;
190 }
191
192 @Override
193 public TypeInfo[] getColumnTypes() {
194 return columnTypes;
195 }
196
197 @Override
198 public int getColumnCount() {
199 return columnCount;
200 }
201
202 @Override
203 public boolean getStoreKeys() {
204 return dataType.isStoreKeys();
205 }
206 }
207 }