use of org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel in project ballerina by ballerina-lang.
the class RecordInputOutputTest method writeRecords.
@Test(description = "Writes records to channel")
public void writeRecords() throws IOException {
// Number of characters in this file would be 6
ByteChannel byteChannel = TestUtil.openForWriting(currentDirectoryPath + "records.csv");
Channel channel = new MockByteChannel(byteChannel);
CharacterChannel characterChannel = new CharacterChannel(channel, StandardCharsets.UTF_8.name());
DelimitedRecordChannel recordChannel = new DelimitedRecordChannel(characterChannel, "\n", ",");
String[] recordOne = { "Foo", "Bar", "911" };
BStringArray recordOneArr = new BStringArray(recordOne);
recordChannel.write(recordOneArr);
String[] recordTwo = { "Jim", "Com", "119" };
BStringArray recordTwoArr = new BStringArray(recordTwo);
recordChannel.write(recordTwoArr);
recordChannel.close();
}
use of org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel in project ballerina by ballerina-lang.
the class RecordInputOutputTest method readNonIndentedRecords.
@Test(description = "Read records which are not indented properly")
public void readNonIndentedRecords() throws IOException, URISyntaxException {
// Number of characters in this file would be 6
ByteChannel byteChannel = TestUtil.openForReading("datafiles/io/records/sample2.csv");
Channel channel = new MockByteChannel(byteChannel);
CharacterChannel characterChannel = new CharacterChannel(channel, StandardCharsets.UTF_8.name());
DelimitedRecordChannel recordChannel = new DelimitedRecordChannel(characterChannel, "\n", ",");
String[] readRecord = recordChannel.read();
Assert.assertEquals(readRecord.length, 9);
Assert.assertTrue(recordChannel.hasNext(), "Expecting more records but received as EOL.");
// This will be a blank record
readRecord = recordChannel.read();
Assert.assertEquals(readRecord.length, 1);
Assert.assertTrue(recordChannel.hasNext(), "Expecting more records but received as EOL.");
readRecord = recordChannel.read();
Assert.assertEquals(readRecord.length, 9);
Assert.assertFalse(recordChannel.hasNext(), "Last record received, but indicate as more records available.");
recordChannel.close();
}
use of org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel in project ballerina by ballerina-lang.
the class CloseDelimitedRecordChannel method execute.
/**
* <p>
* Closes a text record channel.
* </p>
* <p>
* {@inheritDoc}
*/
@Override
public void execute(Context context, CallableUnitCallback callback) {
BStruct channel = (BStruct) context.getRefArgument(RECORD_CHANNEL_INDEX);
DelimitedRecordChannel recordChannel = (DelimitedRecordChannel) channel.getNativeData(IOConstants.TXT_RECORD_CHANNEL_NAME);
EventContext eventContext = new EventContext(context, callback);
IOUtils.close(recordChannel, eventContext, CloseDelimitedRecordChannel::closeResponse);
}
use of org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel in project ballerina by ballerina-lang.
the class CreateDelimitedRecordChannel method execute.
/**
* {@inheritDoc}
*/
@Override
public void execute(Context context) {
try {
// File which holds access to the channel information
BStruct textRecordChannelInfo = (BStruct) context.getRefArgument(RECORD_CHANNEL_INDEX);
String recordSeparator = context.getStringArgument(RECORD_SEPARATOR_INDEX);
String fieldSeparator = context.getStringArgument(FIELD_SEPARATOR_INDEX);
BStruct textRecordChannel = BLangConnectorSPIUtil.createBStruct(context, RECORD_CHANNEL_PACKAGE, STRUCT_TYPE);
// Will get the relevant byte channel and will create a character channel
CharacterChannel characterChannel = (CharacterChannel) textRecordChannelInfo.getNativeData(IOConstants.CHARACTER_CHANNEL_NAME);
DelimitedRecordChannel bCharacterChannel = new DelimitedRecordChannel(characterChannel, recordSeparator, fieldSeparator);
textRecordChannel.addNativeData(IOConstants.TXT_RECORD_CHANNEL_NAME, bCharacterChannel);
context.setReturnValues(textRecordChannel);
} catch (Throwable e) {
String message = "Error occurred while converting character channel to textRecord channel:" + e.getMessage();
log.error(message, e);
context.setReturnValues(IOUtils.createError(context, message));
}
}
use of org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel 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