|
| 1 | +package com.scalar.db.dataloader.core.dataimport; |
| 2 | + |
| 3 | +import com.scalar.db.api.DistributedStorage; |
| 4 | +import com.scalar.db.api.DistributedTransactionManager; |
| 5 | +import com.scalar.db.api.TableMetadata; |
| 6 | +import com.scalar.db.dataloader.core.ScalarDBMode; |
| 7 | +import com.scalar.db.dataloader.core.dataimport.dao.ScalarDBDao; |
| 8 | +import com.scalar.db.dataloader.core.dataimport.datachunk.ImportDataChunkStatus; |
| 9 | +import com.scalar.db.dataloader.core.dataimport.processor.ImportProcessor; |
| 10 | +import com.scalar.db.dataloader.core.dataimport.processor.ImportProcessorFactory; |
| 11 | +import com.scalar.db.dataloader.core.dataimport.processor.ImportProcessorParams; |
| 12 | +import com.scalar.db.dataloader.core.dataimport.processor.TableColumnDataTypes; |
| 13 | +import com.scalar.db.dataloader.core.dataimport.task.result.ImportTaskResult; |
| 14 | +import com.scalar.db.dataloader.core.dataimport.transactionbatch.ImportTransactionBatchResult; |
| 15 | +import com.scalar.db.dataloader.core.dataimport.transactionbatch.ImportTransactionBatchStatus; |
| 16 | +import java.io.BufferedReader; |
| 17 | +import java.util.ArrayList; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Map; |
| 20 | +import java.util.concurrent.ConcurrentHashMap; |
| 21 | +import lombok.AllArgsConstructor; |
| 22 | +import lombok.NonNull; |
| 23 | + |
| 24 | +/** |
| 25 | + * Manages the data import process and coordinates event handling between the import processor and |
| 26 | + * listeners. This class implements {@link ImportEventListener} to receive events from the processor |
| 27 | + * and relay them to registered listeners. |
| 28 | + * |
| 29 | + * <p>The import process involves: |
| 30 | + * |
| 31 | + * <ul> |
| 32 | + * <li>Reading data from an input file |
| 33 | + * <li>Processing the data in configurable chunk sizes |
| 34 | + * <li>Managing database transactions in batches |
| 35 | + * <li>Notifying listeners of various import events |
| 36 | + * </ul> |
| 37 | + */ |
| 38 | +@AllArgsConstructor |
| 39 | +public class ImportManager implements ImportEventListener { |
| 40 | + |
| 41 | + @NonNull private final Map<String, TableMetadata> tableMetadata; |
| 42 | + @NonNull private final BufferedReader importFileReader; |
| 43 | + @NonNull private final ImportOptions importOptions; |
| 44 | + private final ImportProcessorFactory importProcessorFactory; |
| 45 | + private final List<ImportEventListener> listeners = new ArrayList<>(); |
| 46 | + private final ScalarDBMode scalarDBMode; |
| 47 | + private final DistributedStorage distributedStorage; |
| 48 | + private final DistributedTransactionManager distributedTransactionManager; |
| 49 | + private final ConcurrentHashMap<Integer, ImportDataChunkStatus> importDataChunkStatusMap = |
| 50 | + new ConcurrentHashMap<>(); |
| 51 | + |
| 52 | + /** |
| 53 | + * Starts the import process using the configured parameters. |
| 54 | + * |
| 55 | + * <p>If the data chunk size in {@link ImportOptions} is set to 0, the entire file will be |
| 56 | + * processed as a single chunk. Otherwise, the file will be processed in chunks of the specified |
| 57 | + * size. |
| 58 | + * |
| 59 | + * @return a map of {@link ImportDataChunkStatus} objects containing the status of each processed |
| 60 | + * chunk |
| 61 | + */ |
| 62 | + public ConcurrentHashMap<Integer, ImportDataChunkStatus> startImport() { |
| 63 | + ImportProcessorParams params = |
| 64 | + ImportProcessorParams.builder() |
| 65 | + .scalarDBMode(scalarDBMode) |
| 66 | + .importOptions(importOptions) |
| 67 | + .tableMetadataByTableName(tableMetadata) |
| 68 | + .dao(new ScalarDBDao()) |
| 69 | + .distributedTransactionManager(distributedTransactionManager) |
| 70 | + .distributedStorage(distributedStorage) |
| 71 | + .tableColumnDataTypes(getTableColumnDataTypes()) |
| 72 | + .build(); |
| 73 | + ImportProcessor processor = importProcessorFactory.createImportProcessor(params); |
| 74 | + processor.addListener(this); |
| 75 | + // If the data chunk size is 0, then process the entire file in a single data chunk |
| 76 | + int dataChunkSize = |
| 77 | + importOptions.getDataChunkSize() == 0 |
| 78 | + ? Integer.MAX_VALUE |
| 79 | + : importOptions.getDataChunkSize(); |
| 80 | + return processor.process( |
| 81 | + dataChunkSize, importOptions.getTransactionBatchSize(), importFileReader); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Registers a new listener to receive import events. |
| 86 | + * |
| 87 | + * @param listener the listener to add |
| 88 | + * @throws IllegalArgumentException if the listener is null |
| 89 | + */ |
| 90 | + public void addListener(ImportEventListener listener) { |
| 91 | + listeners.add(listener); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Removes a previously registered listener. |
| 96 | + * |
| 97 | + * @param listener the listener to remove |
| 98 | + */ |
| 99 | + public void removeListener(ImportEventListener listener) { |
| 100 | + listeners.remove(listener); |
| 101 | + } |
| 102 | + |
| 103 | + /** {@inheritDoc} Forwards the event to all registered listeners. */ |
| 104 | + @Override |
| 105 | + public void onDataChunkStarted(ImportDataChunkStatus status) { |
| 106 | + for (ImportEventListener listener : listeners) { |
| 107 | + listener.onDataChunkStarted(status); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * {@inheritDoc} Updates or adds the status of a data chunk in the status map. This method is |
| 113 | + * thread-safe. |
| 114 | + */ |
| 115 | + @Override |
| 116 | + public void addOrUpdateDataChunkStatus(ImportDataChunkStatus status) { |
| 117 | + importDataChunkStatusMap.put(status.getDataChunkId(), status); |
| 118 | + } |
| 119 | + |
| 120 | + /** {@inheritDoc} Forwards the event to all registered listeners. */ |
| 121 | + @Override |
| 122 | + public void onDataChunkCompleted(ImportDataChunkStatus status) { |
| 123 | + for (ImportEventListener listener : listeners) { |
| 124 | + listener.onDataChunkCompleted(status); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /** {@inheritDoc} Forwards the event to all registered listeners. */ |
| 129 | + @Override |
| 130 | + public void onTransactionBatchStarted(ImportTransactionBatchStatus status) { |
| 131 | + for (ImportEventListener listener : listeners) { |
| 132 | + listener.onTransactionBatchStarted(status); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + /** {@inheritDoc} Forwards the event to all registered listeners. */ |
| 137 | + @Override |
| 138 | + public void onTransactionBatchCompleted(ImportTransactionBatchResult batchResult) { |
| 139 | + for (ImportEventListener listener : listeners) { |
| 140 | + listener.onTransactionBatchCompleted(batchResult); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + /** {@inheritDoc} Forwards the event to all registered listeners. */ |
| 145 | + @Override |
| 146 | + public void onTaskComplete(ImportTaskResult taskResult) { |
| 147 | + for (ImportEventListener listener : listeners) { |
| 148 | + listener.onTaskComplete(taskResult); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + /** {@inheritDoc} Forwards the event to all registered listeners. */ |
| 153 | + @Override |
| 154 | + public void onAllDataChunksCompleted() { |
| 155 | + for (ImportEventListener listener : listeners) { |
| 156 | + listener.onAllDataChunksCompleted(); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Returns the current map of import data chunk status objects. |
| 162 | + * |
| 163 | + * @return a map of {@link ImportDataChunkStatus} objects |
| 164 | + */ |
| 165 | + public ConcurrentHashMap<Integer, ImportDataChunkStatus> getImportDataChunkStatus() { |
| 166 | + return importDataChunkStatusMap; |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Creates and returns a mapping of table column data types from the table metadata. |
| 171 | + * |
| 172 | + * @return a {@link TableColumnDataTypes} object containing the column data types for all tables |
| 173 | + */ |
| 174 | + public TableColumnDataTypes getTableColumnDataTypes() { |
| 175 | + TableColumnDataTypes tableColumnDataTypes = new TableColumnDataTypes(); |
| 176 | + tableMetadata.forEach( |
| 177 | + (name, metadata) -> |
| 178 | + metadata |
| 179 | + .getColumnDataTypes() |
| 180 | + .forEach((k, v) -> tableColumnDataTypes.addColumnDataType(name, k, v))); |
| 181 | + return tableColumnDataTypes; |
| 182 | + } |
| 183 | +} |
0 commit comments