use of org.ballerinalang.nativeimpl.io.events.records.DelimitedRecordReadAllEvent in project ballerina by ballerina-lang.
the class LoadToTable method execute.
@Override
public void execute(Context context, CallableUnitCallback callback) {
final String filePath = context.getStringArgument(0);
Path path;
try {
path = Paths.get(filePath);
} catch (InvalidPathException e) {
String msg = "Unable to resolve the file path[" + filePath + "]: " + e.getMessage();
context.setReturnValues(IOUtils.createError(context, msg));
callback.notifySuccess();
return;
}
if (Files.notExists(path)) {
String msg = "Unable to find a file in given path: " + filePath;
context.setReturnValues(IOUtils.createError(context, msg));
callback.notifySuccess();
return;
}
try {
FileChannel sourceChannel = FileChannel.open(path, StandardOpenOption.READ);
// FileChannel will close once we completely read the content.
// Close will happen in DelimitedRecordReadAllEvent.
DelimitedRecordChannel recordChannel = getDelimitedRecordChannel(context, sourceChannel);
EventContext eventContext = new EventContext(context, callback);
DelimitedRecordReadAllEvent event = new DelimitedRecordReadAllEvent(recordChannel, eventContext);
CompletableFuture<EventResult> future = EventManager.getInstance().publish(event);
future.thenApply(LoadToTable::response);
} catch (IOException e) {
String msg = "Failed to process the delimited file: " + e.getMessage();
log.error(msg, e);
context.setReturnValues(IOUtils.createError(context, msg));
}
}
Aggregations