Search in sources :

Example 6 with DelimitedRecordChannel

use of org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel in project ballerina by ballerina-lang.

the class NextTextRecord method execute.

/**
 * {@inheritDoc}
 */
@Override
public void execute(Context context, CallableUnitCallback callback) {
    BStruct channel = (BStruct) context.getRefArgument(TXT_RECORD_CHANNEL_INDEX);
    DelimitedRecordChannel delimitedRecordChannel = (DelimitedRecordChannel) channel.getNativeData(IOConstants.TXT_RECORD_CHANNEL_NAME);
    EventContext eventContext = new EventContext(context, callback);
    IOUtils.read(delimitedRecordChannel, eventContext, NextTextRecord::response);
}
Also used : EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) BStruct(org.ballerinalang.model.values.BStruct) DelimitedRecordChannel(org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel)

Example 7 with DelimitedRecordChannel

use of org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel in project ballerina by ballerina-lang.

the class WriteTextRecord method execute.

/**
 * Writes records to a given file.
 * <p>
 * {@inheritDoc}
 */
@Override
public void execute(Context context, CallableUnitCallback callback) {
    BStruct channel = (BStruct) context.getRefArgument(RECORD_CHANNEL_INDEX);
    BStringArray content = (BStringArray) context.getRefArgument(CONTENT_INDEX);
    DelimitedRecordChannel delimitedRecordChannel = (DelimitedRecordChannel) channel.getNativeData(IOConstants.TXT_RECORD_CHANNEL_NAME);
    EventContext eventContext = new EventContext(context, callback);
    IOUtils.write(delimitedRecordChannel, content, eventContext, WriteTextRecord::writeResponse);
}
Also used : EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) BStruct(org.ballerinalang.model.values.BStruct) BStringArray(org.ballerinalang.model.values.BStringArray) DelimitedRecordChannel(org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel)

Example 8 with DelimitedRecordChannel

use of org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel in project ballerina by ballerina-lang.

the class AsyncReadWriteTest method readTextRecords.

@Test(description = "Test which will read text records from a given channel using async io framework")
public void readTextRecords() throws IOException, URISyntaxException, ExecutionException, InterruptedException {
    int expectedFieldCount = 3;
    // Number of characters in this file would be 6
    ByteChannel byteChannel = TestUtil.openForReading("datafiles/io/records/sample.csv");
    Channel channel = new MockByteChannel(byteChannel);
    CharacterChannel characterChannel = new CharacterChannel(channel, StandardCharsets.UTF_8.name());
    DelimitedRecordChannel recordChannel = new DelimitedRecordChannel(characterChannel, "\n", ",");
    DelimitedRecordReadEvent event = new DelimitedRecordReadEvent(recordChannel);
    Future<EventResult> future = eventManager.publish(event);
    EventResult eventResult = future.get();
    String[] readRecord = (String[]) eventResult.getResponse();
    Assert.assertEquals(readRecord.length, expectedFieldCount);
    event = new DelimitedRecordReadEvent(recordChannel);
    future = eventManager.publish(event);
    eventResult = future.get();
    readRecord = (String[]) eventResult.getResponse();
    Assert.assertEquals(readRecord.length, expectedFieldCount);
    event = new DelimitedRecordReadEvent(recordChannel);
    future = eventManager.publish(event);
    eventResult = future.get();
    readRecord = (String[]) eventResult.getResponse();
    Assert.assertEquals(readRecord.length, expectedFieldCount);
    event = new DelimitedRecordReadEvent(recordChannel);
    future = eventManager.publish(event);
    eventResult = future.get();
    readRecord = (String[]) eventResult.getResponse();
    Assert.assertEquals(readRecord.length, 0);
    recordChannel.close();
}
Also used : MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) EventResult(org.ballerinalang.nativeimpl.io.events.EventResult) 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) DelimitedRecordReadEvent(org.ballerinalang.nativeimpl.io.events.records.DelimitedRecordReadEvent) 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 9 with DelimitedRecordChannel

use of org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel in project ballerina by ballerina-lang.

the class AsyncReadWriteTest method writeRecords.

@Test(description = "Test which will write records to a channel using async io framework")
public void writeRecords() throws IOException, ExecutionException, InterruptedException {
    // 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);
    DelimitedRecordWriteEvent recordWriteEvent = new DelimitedRecordWriteEvent(recordChannel, recordOneArr);
    Future<EventResult> future = eventManager.publish(recordWriteEvent);
    future.get();
    String[] recordTwo = { "Jim", "Com", "119" };
    BStringArray recordTwoArr = new BStringArray(recordTwo);
    recordWriteEvent = new DelimitedRecordWriteEvent(recordChannel, recordTwoArr);
    future = eventManager.publish(recordWriteEvent);
    future.get();
    recordChannel.close();
}
Also used : MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) EventResult(org.ballerinalang.nativeimpl.io.events.EventResult) DelimitedRecordWriteEvent(org.ballerinalang.nativeimpl.io.events.records.DelimitedRecordWriteEvent) 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 10 with DelimitedRecordChannel

use of org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel in project ballerina by ballerina-lang.

the class RecordInputOutputTest method processRecordSequence.

@Test(description = "Processors records in sequence with hasNext()")
public void processRecordSequence() throws IOException, URISyntaxException {
    int expectedFieldCount = 3;
    boolean hasNext = false;
    // Number of characters in this file would be 6
    ByteChannel byteChannel = TestUtil.openForReading("datafiles/io/records/sample.csv");
    Channel channel = new MockByteChannel(byteChannel);
    CharacterChannel characterChannel = new CharacterChannel(channel, StandardCharsets.UTF_8.name());
    DelimitedRecordChannel recordChannel = new DelimitedRecordChannel(characterChannel, "\n", ",");
    hasNext = recordChannel.hasNext();
    String[] readRecord = recordChannel.read();
    Assert.assertEquals(readRecord.length, expectedFieldCount);
    Assert.assertTrue(hasNext);
    hasNext = recordChannel.hasNext();
    readRecord = recordChannel.read();
    Assert.assertEquals(readRecord.length, expectedFieldCount);
    Assert.assertTrue(hasNext);
    hasNext = recordChannel.hasNext();
    readRecord = recordChannel.read();
    Assert.assertEquals(readRecord.length, expectedFieldCount);
    Assert.assertTrue(hasNext);
    hasNext = recordChannel.hasNext();
    readRecord = recordChannel.read();
    Assert.assertEquals(readRecord.length, 0);
    Assert.assertFalse(hasNext);
    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)

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