Search in sources :

Example 1 with DelimitedRecordReadAllEvent

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));
    }
}
Also used : Path(java.nio.file.Path) EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) EventResult(org.ballerinalang.nativeimpl.io.events.EventResult) FileChannel(java.nio.channels.FileChannel) DelimitedRecordReadAllEvent(org.ballerinalang.nativeimpl.io.events.records.DelimitedRecordReadAllEvent) IOException(java.io.IOException) InvalidPathException(java.nio.file.InvalidPathException) DelimitedRecordChannel(org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel)

Aggregations

IOException (java.io.IOException)1 FileChannel (java.nio.channels.FileChannel)1 InvalidPathException (java.nio.file.InvalidPathException)1 Path (java.nio.file.Path)1 DelimitedRecordChannel (org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel)1 EventContext (org.ballerinalang.nativeimpl.io.events.EventContext)1 EventResult (org.ballerinalang.nativeimpl.io.events.EventResult)1 DelimitedRecordReadAllEvent (org.ballerinalang.nativeimpl.io.events.records.DelimitedRecordReadAllEvent)1