|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package io.trino.plugin.deltalake; |
| 15 | + |
| 16 | +import com.google.common.collect.ImmutableList; |
| 17 | +import io.airlift.json.JsonCodec; |
| 18 | +import io.trino.filesystem.TrinoFileSystemFactory; |
| 19 | +import io.trino.plugin.deltalake.transactionlog.DeltaLakeTransactionLogEntry; |
| 20 | +import io.trino.plugin.deltalake.transactionlog.Transaction; |
| 21 | +import io.trino.plugin.deltalake.transactionlog.TransactionLogAccess; |
| 22 | +import io.trino.plugin.deltalake.util.PageListBuilder; |
| 23 | +import io.trino.spi.Page; |
| 24 | +import io.trino.spi.connector.ColumnMetadata; |
| 25 | +import io.trino.spi.connector.ConnectorSession; |
| 26 | +import io.trino.spi.connector.ConnectorTableMetadata; |
| 27 | +import io.trino.spi.connector.SchemaTableName; |
| 28 | +import io.trino.spi.type.TypeManager; |
| 29 | +import io.trino.spi.type.TypeSignature; |
| 30 | + |
| 31 | +import java.util.List; |
| 32 | + |
| 33 | +import static io.airlift.json.JsonCodec.listJsonCodec; |
| 34 | +import static io.trino.spi.type.BigintType.BIGINT; |
| 35 | +import static io.trino.spi.type.StandardTypes.JSON; |
| 36 | +import static java.util.Objects.requireNonNull; |
| 37 | + |
| 38 | +public class DeltaLakeTransactionsTable |
| 39 | + extends BaseTransactionsTable |
| 40 | +{ |
| 41 | + private static final JsonCodec<List<DeltaLakeTransactionLogEntry>> TRANSACTION_LOG_ENTRIES_CODEC = listJsonCodec(DeltaLakeTransactionLogEntry.class); |
| 42 | + |
| 43 | + public DeltaLakeTransactionsTable( |
| 44 | + SchemaTableName tableName, |
| 45 | + String tableLocation, |
| 46 | + TrinoFileSystemFactory fileSystemFactory, |
| 47 | + TransactionLogAccess transactionLogAccess, |
| 48 | + TypeManager typeManager) |
| 49 | + { |
| 50 | + super( |
| 51 | + tableName, |
| 52 | + tableLocation, |
| 53 | + fileSystemFactory, |
| 54 | + transactionLogAccess, |
| 55 | + typeManager, |
| 56 | + new ConnectorTableMetadata( |
| 57 | + requireNonNull(tableName, "tableName is null"), |
| 58 | + ImmutableList.<ColumnMetadata>builder() |
| 59 | + .add(new ColumnMetadata("version", BIGINT)) |
| 60 | + .add(new ColumnMetadata("transaction", typeManager.getType(new TypeSignature(JSON)))) |
| 61 | + .build())); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + protected List<Page> buildPages(ConnectorSession session, PageListBuilder pagesBuilder, List<Transaction> transactions) |
| 66 | + { |
| 67 | + for (Transaction transaction : transactions) { |
| 68 | + pagesBuilder.beginRow(); |
| 69 | + pagesBuilder.appendBigint(transaction.transactionId()); |
| 70 | + pagesBuilder.appendVarchar(TRANSACTION_LOG_ENTRIES_CODEC.toJson(transaction.transactionEntries())); |
| 71 | + pagesBuilder.endRow(); |
| 72 | + } |
| 73 | + return pagesBuilder.build(); |
| 74 | + } |
| 75 | +} |
0 commit comments