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);
}
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);
}
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);
}
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);
}
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));
}
}
Aggregations