Search in sources :

Example 1 with EventResult

use of org.ballerinalang.nativeimpl.io.events.EventResult in project ballerina by ballerina-lang.

the class IOUtils method close.

/**
 * Closes the character channel asynchronously.
 *
 * @param charChannel  channel which should be closed.
 * @param eventContext context of the event.
 * @param function     callback function which will be triggered.
 */
public static void close(CharacterChannel charChannel, EventContext eventContext, Function<EventResult, EventResult> function) {
    CloseCharacterChannelEvent closeEvent = new CloseCharacterChannelEvent(charChannel, eventContext);
    CompletableFuture<EventResult> future = EventManager.getInstance().publish(closeEvent);
    future.thenApply(function);
}
Also used : CloseCharacterChannelEvent(org.ballerinalang.nativeimpl.io.events.characters.CloseCharacterChannelEvent) EventResult(org.ballerinalang.nativeimpl.io.events.EventResult)

Example 2 with EventResult

use of org.ballerinalang.nativeimpl.io.events.EventResult in project ballerina by ballerina-lang.

the class IOUtils method write.

/**
 * Writes characters to a channel.
 *
 * @param characterChannel the channel the characters will be written
 * @param content          the content which will be written.
 * @param offset           if an offset should be specified while writing.
 * @param context          context of the event.
 * @param function         callback function which should be triggered
 */
public static void write(CharacterChannel characterChannel, String content, int offset, EventContext context, Function<EventResult, EventResult> function) {
    WriteCharactersEvent event = new WriteCharactersEvent(characterChannel, content, offset, context);
    CompletableFuture<EventResult> future = EventManager.getInstance().publish(event);
    future.thenApply(function);
}
Also used : WriteCharactersEvent(org.ballerinalang.nativeimpl.io.events.characters.WriteCharactersEvent) EventResult(org.ballerinalang.nativeimpl.io.events.EventResult)

Example 3 with EventResult

use of org.ballerinalang.nativeimpl.io.events.EventResult in project ballerina by ballerina-lang.

the class IOUtils method read.

/**
 * Reads delimited records asynchronously.
 *
 * @param recordChannel channel the bytes should be read from.
 * @param context       event context.
 * @param function      callback function which will be triggered.
 */
public static void read(DelimitedRecordChannel recordChannel, EventContext context, Function<EventResult, EventResult> function) {
    DelimitedRecordReadEvent event = new DelimitedRecordReadEvent(recordChannel, context);
    CompletableFuture<EventResult> future = EventManager.getInstance().publish(event);
    future.thenApply(function);
}
Also used : EventResult(org.ballerinalang.nativeimpl.io.events.EventResult) DelimitedRecordReadEvent(org.ballerinalang.nativeimpl.io.events.records.DelimitedRecordReadEvent)

Example 4 with EventResult

use of org.ballerinalang.nativeimpl.io.events.EventResult in project ballerina by ballerina-lang.

the class IOUtils method write.

/**
 * Asynchronously writes delimited records to the channel.
 *
 * @param recordChannel channel the records should be written.
 * @param records       the record content.
 * @param context       event context.
 * @param function      callback function which will be triggered.
 */
public static void write(DelimitedRecordChannel recordChannel, BStringArray records, EventContext context, Function<EventResult, EventResult> function) {
    DelimitedRecordWriteEvent recordWriteEvent = new DelimitedRecordWriteEvent(recordChannel, records, context);
    CompletableFuture<EventResult> future = EventManager.getInstance().publish(recordWriteEvent);
    future.thenApply(function);
}
Also used : EventResult(org.ballerinalang.nativeimpl.io.events.EventResult) DelimitedRecordWriteEvent(org.ballerinalang.nativeimpl.io.events.records.DelimitedRecordWriteEvent)

Example 5 with EventResult

use of org.ballerinalang.nativeimpl.io.events.EventResult 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

EventResult (org.ballerinalang.nativeimpl.io.events.EventResult)17 ByteChannel (java.nio.channels.ByteChannel)4 Channel (org.ballerinalang.nativeimpl.io.channels.base.Channel)4 CharacterChannel (org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel)4 DelimitedRecordChannel (org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel)4 EventContext (org.ballerinalang.nativeimpl.io.events.EventContext)4 MockByteChannel (org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel)4 Test (org.testng.annotations.Test)4 ExecutionException (java.util.concurrent.ExecutionException)2 ReadBytesEvent (org.ballerinalang.nativeimpl.io.events.bytes.ReadBytesEvent)2 WriteBytesEvent (org.ballerinalang.nativeimpl.io.events.bytes.WriteBytesEvent)2 ReadCharactersEvent (org.ballerinalang.nativeimpl.io.events.characters.ReadCharactersEvent)2 WriteCharactersEvent (org.ballerinalang.nativeimpl.io.events.characters.WriteCharactersEvent)2 DelimitedRecordReadEvent (org.ballerinalang.nativeimpl.io.events.records.DelimitedRecordReadEvent)2 DelimitedRecordWriteEvent (org.ballerinalang.nativeimpl.io.events.records.DelimitedRecordWriteEvent)2 IOException (java.io.IOException)1 FileChannel (java.nio.channels.FileChannel)1 InvalidPathException (java.nio.file.InvalidPathException)1 Path (java.nio.file.Path)1 BStringArray (org.ballerinalang.model.values.BStringArray)1