Search in sources :

Example 1 with DelimitedRecordChannel

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();
}
Also used : MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) DelimitedRecordChannel(org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) CharacterChannel(org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel) CharacterChannel(org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel) DelimitedRecordChannel(org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel) BStringArray(org.ballerinalang.model.values.BStringArray) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) Test(org.testng.annotations.Test)

Example 2 with DelimitedRecordChannel

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();
}
Also used : MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) DelimitedRecordChannel(org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) CharacterChannel(org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel) CharacterChannel(org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel) DelimitedRecordChannel(org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) Test(org.testng.annotations.Test)

Example 3 with DelimitedRecordChannel

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);
}
Also used : EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) BStruct(org.ballerinalang.model.values.BStruct) DelimitedRecordChannel(org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel)

Example 4 with DelimitedRecordChannel

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));
    }
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) CharacterChannel(org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel) DelimitedRecordChannel(org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel)

Example 5 with DelimitedRecordChannel

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

DelimitedRecordChannel (org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel)14 CharacterChannel (org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel)9 ByteChannel (java.nio.channels.ByteChannel)7 Channel (org.ballerinalang.nativeimpl.io.channels.base.Channel)7 MockByteChannel (org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel)7 Test (org.testng.annotations.Test)7 BStruct (org.ballerinalang.model.values.BStruct)5 EventContext (org.ballerinalang.nativeimpl.io.events.EventContext)5 EventResult (org.ballerinalang.nativeimpl.io.events.EventResult)4 BStringArray (org.ballerinalang.model.values.BStringArray)3 IOException (java.io.IOException)1 FileChannel (java.nio.channels.FileChannel)1 InvalidPathException (java.nio.file.InvalidPathException)1 Path (java.nio.file.Path)1 FileIOChannel (org.ballerinalang.nativeimpl.io.channels.FileIOChannel)1 DelimitedRecordReadAllEvent (org.ballerinalang.nativeimpl.io.events.records.DelimitedRecordReadAllEvent)1 DelimitedRecordReadEvent (org.ballerinalang.nativeimpl.io.events.records.DelimitedRecordReadEvent)1 DelimitedRecordWriteEvent (org.ballerinalang.nativeimpl.io.events.records.DelimitedRecordWriteEvent)1 HasNextDelimitedRecordEvent (org.ballerinalang.nativeimpl.io.events.records.HasNextDelimitedRecordEvent)1