-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTable.java
More file actions
409 lines (345 loc) · 13.4 KB
/
Table.java
File metadata and controls
409 lines (345 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
* Copyright 2023 Greptime Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.greptime.models;
import io.greptime.common.Into;
import io.greptime.common.util.Ensures;
import io.greptime.v1.Common;
import io.greptime.v1.Database;
import io.greptime.v1.RowData;
import java.util.ArrayList;
import java.util.List;
import org.apache.arrow.vector.FieldVector;
import org.apache.arrow.vector.VectorSchemaRoot;
/**
* A table represents data in row format that can be written to the database.
*
* <p>
* The table is mutable and allows adding rows incrementally. However, the data cannot be written to
* the database until the table is marked as complete by calling {@link #complete()}. This design enables
* batch processing of rows before writing.
* </p>
*
* <p>
* Important notes:
* </p>
* <ul>
* <li>Tables are not thread-safe</li>
* <li>Tables cannot be reused - create a new instance for each write operation</li>
* <li>The associated {@link TableSchema} is immutable and can be reused</li>
* </ul>
*
* <p>
* Example usage:
* </p>
* <pre>{@code
* TableSchema schema = TableSchema.newBuilder("my_table")
* .addTag("tag1", DataType.String)
* .addTimestamp("ts", DataType.TimestampMillisecond)
* .addField("field1", DataType.Float64)
* .build();
*
* Table table = Table.from(schema);
* // The order of the values must match the schema definition.
* table.addRow(1, "2023-01-01 00:00:00", "value1");
* table.addRow(2, "2023-01-01 00:00:01", "value2");
* table.complete();
* }</pre>
*/
public interface Table {
/**
* The table name to write.
*/
String tableName();
/**··
* The rows count to write.
*/
int rowCount();
/**
* The columns count to write.
*/
int columnCount();
/**
* The points count to write.
*/
default int pointCount() {
return rowCount() * columnCount();
}
/**
* Insets one row with all columns.
*
* The order of the values must be the same as the order of the schema.
*/
Table addRow(Object... values);
Table subRange(int fromIndex, int toIndex);
/**
* Completes the table data construction and prevents further row additions.
* After calling this method, the table will be immutable and ready to be written
* to the database.
*/
Table complete();
/**
* Returns true if the table has been completed.
*/
boolean isCompleted();
/**
* Convert to {@link Database.RowInsertRequest}.
*
* @return {@link Database.RowInsertRequest}
*/
default Database.RowInsertRequest intoRowInsertRequest() {
throw new UnsupportedOperationException("Not supported for this table type");
}
/**
* Convert to {@link Database.RowDeleteRequest}.
*
* @return {@link Database.RowDeleteRequest}
*/
default Database.RowDeleteRequest intoRowDeleteRequest() {
throw new UnsupportedOperationException("Not supported for this table type");
}
default void checkNumValues(int len) {
int columnCount = columnCount();
Ensures.ensure(columnCount == len, "Expected values num: %d, actual: %d", columnCount, len);
}
/**
* `TableBufferRoot` is an internal interface that represents a reference to table data stored in direct memory.
* It provides access to the underlying memory allocation for efficient bulk data operations.
*
* <p>
* `TableBufferRoot` is not thread-safe.
* </p>
*/
interface TableBufferRoot extends Table {}
/**
* Create a table from a table schema.
*
* @param tableSchema the table schema
* @return a table
*/
static Table from(TableSchema tableSchema) {
return new Builder(tableSchema).build();
}
/**
* Create a bulk table buffer root from a table schema and a vector schema root.
*
* @param tableSchema the table schema
* @param root the vector schema root
* @return a table buffer root
*/
static TableBufferRoot tableBufferRoot(TableSchema tableSchema, VectorSchemaRoot root) {
return new BulkTableBuilder(tableSchema, root).build();
}
class Builder {
private final TableSchema tableSchema;
public Builder(TableSchema tableSchema) {
this.tableSchema = tableSchema;
}
public Table build() {
String tableName = this.tableSchema.getTableName();
List<String> columnNames = this.tableSchema.getColumnNames();
List<Common.SemanticType> semanticTypes = this.tableSchema.getSemanticTypes();
List<Common.ColumnDataType> dataTypes = this.tableSchema.getDataTypes();
List<Common.ColumnDataTypeExtension> dataTypeExtensions = this.tableSchema.getDataTypeExtensions();
Ensures.ensureNonNull(tableName, "Null table name");
Ensures.ensureNonNull(columnNames, "Null column names");
Ensures.ensureNonNull(semanticTypes, "Null semantic types");
Ensures.ensureNonNull(dataTypes, "Null data types");
int columnCount = columnNames.size();
Ensures.ensure(columnCount > 0, "Empty column names");
Ensures.ensure(columnCount == semanticTypes.size(), "Column names size not equal to semantic types size");
Ensures.ensure(columnCount == dataTypes.size(), "Column names size not equal to data types size");
Ensures.ensure(
columnCount == dataTypeExtensions.size(),
"Column names size not equal to data type extensions size");
return buildTable(tableName, columnCount, columnNames, semanticTypes, dataTypes, dataTypeExtensions);
}
private static Table buildTable(
String tableName,
int columnCount,
List<String> columnNames,
List<Common.SemanticType> semanticTypes,
List<Common.ColumnDataType> dataTypes,
List<Common.ColumnDataTypeExtension> dataTypeExtensions) {
RowBasedTable table = new RowBasedTable(tableName, new ArrayList<>(columnCount));
for (int i = 0; i < columnCount; i++) {
RowData.ColumnSchema.Builder builder = RowData.ColumnSchema.newBuilder();
builder.setColumnName(columnNames.get(i))
.setSemanticType(semanticTypes.get(i))
.setDatatype(dataTypes.get(i))
.setDatatypeExtension(dataTypeExtensions.get(i));
table.columnSchemas.add(builder.build());
}
return table;
}
}
class RowBasedTable implements Table, Into<RowData.Rows> {
private volatile boolean completed = false;
private final String tableName;
private final List<RowData.ColumnSchema> columnSchemas;
private final List<RowData.Row> rows;
public RowBasedTable(String tableName, List<RowData.ColumnSchema> columnSchemas) {
this(tableName, columnSchemas, new ArrayList<>());
}
private RowBasedTable(String tableName, List<RowData.ColumnSchema> columnSchemas, List<RowData.Row> rows) {
this.tableName = tableName;
this.columnSchemas = columnSchemas;
this.rows = rows;
}
@Override
public String tableName() {
return this.tableName;
}
@Override
public int rowCount() {
return this.rows.size();
}
@Override
public int columnCount() {
return this.columnSchemas.size();
}
@Override
public Table addRow(Object... values) {
Ensures.ensure(
!this.completed,
"Table data construction has been completed. Cannot add more rows. Please create a new table instance.");
checkNumValues(values.length);
RowData.Row.Builder rowBuilder = RowData.Row.newBuilder();
for (int i = 0; i < values.length; i++) {
RowData.ColumnSchema columnSchema = this.columnSchemas.get(i);
Object value = values[i];
RowHelper.addValue(rowBuilder, columnSchema.getDatatype(), columnSchema.getDatatypeExtension(), value);
}
this.rows.add(rowBuilder.build());
return this;
}
@Override
public Table subRange(int fromIndex, int toIndex) {
List<RowData.Row> rows = this.rows.subList(fromIndex, toIndex);
return new RowBasedTable(this.tableName, this.columnSchemas, rows);
}
@Override
public Database.RowInsertRequest intoRowInsertRequest() {
return Database.RowInsertRequest.newBuilder()
.setTableName(this.tableName)
.setRows(into())
.build();
}
@Override
public Database.RowDeleteRequest intoRowDeleteRequest() {
return Database.RowDeleteRequest.newBuilder()
.setTableName(this.tableName)
.setRows(into())
.build();
}
@Override
public RowData.Rows into() {
return RowData.Rows.newBuilder()
.addAllSchema(this.columnSchemas)
.addAllRows(this.rows)
.build();
}
@Override
public Table complete() {
this.completed = true;
return this;
}
@Override
public boolean isCompleted() {
return this.completed;
}
}
class BulkTableBuilder {
private final TableSchema tableSchema;
private final VectorSchemaRoot root;
public BulkTableBuilder(TableSchema tableSchema, VectorSchemaRoot root) {
this.tableSchema = tableSchema;
this.root = root;
}
public BulkTable build() {
String tableName = this.tableSchema.getTableName();
List<Common.ColumnDataType> dataTypes = this.tableSchema.getDataTypes();
List<Common.ColumnDataTypeExtension> dataTypeExtensions = this.tableSchema.getDataTypeExtensions();
Ensures.ensureNonNull(tableName, "Null table name");
Ensures.ensureNonNull(dataTypes, "Null data types");
int columnCount = dataTypes.size();
Ensures.ensure(columnCount > 0, "Empty column data types");
Ensures.ensure(
columnCount == dataTypeExtensions.size(),
"Column data types size not equal to data type extensions size");
return new BulkTable(tableName, dataTypes, dataTypeExtensions, this.root);
}
}
class BulkTable implements TableBufferRoot {
private volatile boolean completed = false;
private final String tableName;
private final List<Common.ColumnDataType> dataTypes;
private final List<Common.ColumnDataTypeExtension> dataTypeExtensions;
private final VectorSchemaRoot root;
public BulkTable(
String tableName,
List<Common.ColumnDataType> dataTypes,
List<Common.ColumnDataTypeExtension> dataTypeExtensions,
VectorSchemaRoot root) {
this.tableName = tableName;
this.dataTypes = dataTypes;
this.dataTypeExtensions = dataTypeExtensions;
this.root = root;
}
@Override
public String tableName() {
return this.tableName;
}
@Override
public int rowCount() {
return this.root.getRowCount();
}
@Override
public int columnCount() {
return this.root.getSchema().getFields().size();
}
@Override
public Table addRow(Object... values) {
Ensures.ensure(
!this.completed,
"Table data construction has been completed. Cannot add more rows. Please create a new table instance.");
checkNumValues(values.length);
int rowCount = this.root.getRowCount();
for (int i = 0; i < values.length; i++) {
FieldVector vector = this.root.getVector(i);
if (vector.getValueCapacity() < rowCount + 1) {
vector.reAlloc();
}
ArrowHelper.addValue(
vector, rowCount, this.dataTypes.get(i), this.dataTypeExtensions.get(i), values[i]);
}
this.root.setRowCount(rowCount + 1);
return this;
}
@Override
public Table subRange(int fromIndex, int toIndex) {
throw new UnsupportedOperationException("Unsupported method 'subRange' by BulkTable");
}
@Override
public Table complete() {
this.completed = true;
return this;
}
@Override
public boolean isCompleted() {
return this.completed;
}
}
}