Search in sources :

Example 1 with ChannelReader

use of com.ociweb.pronghorn.pipe.ChannelReader in project GreenLightning by oci-pronghorn.

the class GreenParserTest method extractionsReaderTest.

@Test
public void extractionsReaderTest() {
    GreenReader gr = new GreenTokenMap().add(1, "age: %i\n").add(2, // note %b MUST be followed by stop char
    "name: %b, %b\n").add(3, // note this counts as 2 fields
    "speed: %i%.\n").add(0, "\n").newReader();
    ChannelReader testToRead = BlobReaderFactory.generateExtractionDataToTest(new MyConsumer<DataOutputBlobWriter<?>>() {

        @Override
        public void accept(DataOutputBlobWriter<?> dataOutputBlobWriter) {
            defaultStreamAppend(dataOutputBlobWriter);
        }
    });
    long age = Integer.MIN_VALUE;
    StringBuilder name = new StringBuilder();
    double speed = -1;
    gr.beginRead(testToRead);
    while (gr.hasMore()) {
        long token = gr.readToken();
        logger.trace("extractionsReader token: {} ", token);
        switch((int) token) {
            case 1:
                age = gr.extractedLong(0);
                break;
            case 2:
                gr.copyExtractedUTF8ToAppendable(1, name);
                name.append(" ");
                gr.copyExtractedUTF8ToAppendable(0, name);
                break;
            case 3:
                speed = gr.extractedDouble(0);
                break;
            case 0:
                // skips known white space
                break;
            default:
                // skip unknown data the extra return
                gr.skipByte();
        }
    }
    assertEquals(42, age);
    assertEquals("billy bob", name.toString());
    assertEquals(7.2d, speed, .00001);
}
Also used : ChannelReader(com.ociweb.pronghorn.pipe.ChannelReader) DataOutputBlobWriter(com.ociweb.pronghorn.pipe.DataOutputBlobWriter) Test(org.junit.Test)

Example 2 with ChannelReader

use of com.ociweb.pronghorn.pipe.ChannelReader in project PronghornPipes by oci-pronghorn.

the class JSONParseTest method simpleParseTest.

@Test
public void simpleParseTest() {
    Pipe<RawDataSchema> targetData = parseJSON(simpleExample, simpleExtractor);
    // confirm data on the pipe is good...
    Pipe.takeMsgIdx(targetData);
    ChannelReader dataStream = (ChannelReader) Pipe.openInputStream(targetData);
    long header = dataStream.readPackedLong();
    assertEquals(0, header);
    String valueB = dataStream.readUTFOfLength(dataStream.readPackedInt());
    assertEquals("hello", valueB);
    long valueA = dataStream.readPackedLong();
    assertEquals(123, valueA);
}
Also used : RawDataSchema(com.ociweb.pronghorn.pipe.RawDataSchema) ChannelReader(com.ociweb.pronghorn.pipe.ChannelReader) Test(org.junit.Test)

Example 3 with ChannelReader

use of com.ociweb.pronghorn.pipe.ChannelReader in project PronghornPipes by oci-pronghorn.

the class JSONParseTest method simpleMissingATest.

@Test
public void simpleMissingATest() {
    Pipe<RawDataSchema> targetData = parseJSON(missingAExample, simpleExtractor);
    // confirm data on the pipe is good...
    Pipe.takeMsgIdx(targetData);
    ChannelReader dataStream = (ChannelReader) Pipe.openInputStream(targetData);
    long header = dataStream.readPackedLong();
    // the second field is null so the bit pattern is 10
    assertEquals(2, header);
    String valueB = dataStream.readUTFOfLength(dataStream.readPackedInt());
    assertEquals("hello", valueB);
    // a is missing by design
    // this MUST be zero but it is not not sure why??
    assertEquals(0, dataStream.available());
}
Also used : RawDataSchema(com.ociweb.pronghorn.pipe.RawDataSchema) ChannelReader(com.ociweb.pronghorn.pipe.ChannelReader) Test(org.junit.Test)

Example 4 with ChannelReader

use of com.ociweb.pronghorn.pipe.ChannelReader in project PronghornPipes by oci-pronghorn.

the class JSONParseTest method simpleArrayParseTest.

@Test
public void simpleArrayParseTest() {
    try {
        Pipe<RawDataSchema> targetData = parseJSON(simpleArrayExample, simpleArrayExtractor);
        // confirm data on the pipe is good...
        Pipe.takeMsgIdx(targetData);
        ChannelReader dataStream = (ChannelReader) Pipe.openInputStream(targetData);
        long header = dataStream.readPackedLong();
        assertEquals(0, header);
        assertEquals(5, dataStream.readPackedInt());
        // these are 19 bytes plus 10, 29
        assertEquals("one", dataStream.readUTFOfLength(dataStream.readPackedInt()));
        assertEquals("two", dataStream.readUTFOfLength(dataStream.readPackedInt()));
        assertEquals("three", dataStream.readUTFOfLength(dataStream.readPackedInt()));
        assertEquals("four", dataStream.readUTFOfLength(dataStream.readPackedInt()));
        assertEquals("five", dataStream.readUTFOfLength(dataStream.readPackedInt()));
        assertEquals(5, dataStream.readPackedInt());
        assertEquals(1, dataStream.readPackedLong());
        assertEquals(2, dataStream.readPackedLong());
        assertEquals(3, dataStream.readPackedLong());
        assertEquals(4, dataStream.readPackedLong());
        assertEquals(5, dataStream.readPackedLong());
        assertEquals(0, dataStream.available());
    } catch (Throwable t) {
        t.printStackTrace();
        throw new AssertionError("rethrow", t);
    }
}
Also used : RawDataSchema(com.ociweb.pronghorn.pipe.RawDataSchema) ChannelReader(com.ociweb.pronghorn.pipe.ChannelReader) Test(org.junit.Test)

Example 5 with ChannelReader

use of com.ociweb.pronghorn.pipe.ChannelReader in project PronghornPipes by oci-pronghorn.

the class JSONParseTest method simple2DArrayParseTest.

@Test
public void simple2DArrayParseTest() {
    Pipe<RawDataSchema> targetData = parseJSON(simple2DArrayExample, simple2DArrayExtractor);
    // confirm data on the pipe is good...
    Pipe.takeMsgIdx(targetData);
    ChannelReader dataStream = (ChannelReader) Pipe.openInputStream(targetData);
    long header = dataStream.readPackedLong();
    assertEquals(0, header);
    assertEquals(2, dataStream.readPackedInt());
    assertEquals(5, dataStream.readPackedInt());
    assertEquals(2, dataStream.readPackedInt());
    assertEquals("one", dataStream.readUTFOfLength(dataStream.readPackedInt()));
    assertEquals("two", dataStream.readUTFOfLength(dataStream.readPackedInt()));
    assertEquals("three", dataStream.readUTFOfLength(dataStream.readPackedInt()));
    assertEquals("four", dataStream.readUTFOfLength(dataStream.readPackedInt()));
    assertEquals("five", dataStream.readUTFOfLength(dataStream.readPackedInt()));
    assertEquals("six", dataStream.readUTFOfLength(dataStream.readPackedInt()));
    assertEquals("seven", dataStream.readUTFOfLength(dataStream.readPackedInt()));
    assertEquals(2, dataStream.readPackedInt());
    assertEquals(5, dataStream.readPackedInt());
    assertEquals(2, dataStream.readPackedInt());
    assertEquals(1, dataStream.readPackedLong());
    assertEquals(2, dataStream.readPackedLong());
    assertEquals(3, dataStream.readPackedLong());
    assertEquals(4, dataStream.readPackedLong());
    assertEquals(5, dataStream.readPackedLong());
    assertEquals(6, dataStream.readPackedLong());
    assertEquals(7, dataStream.readPackedLong());
    assertEquals(0, dataStream.available());
}
Also used : RawDataSchema(com.ociweb.pronghorn.pipe.RawDataSchema) ChannelReader(com.ociweb.pronghorn.pipe.ChannelReader) Test(org.junit.Test)

Aggregations

ChannelReader (com.ociweb.pronghorn.pipe.ChannelReader)14 Test (org.junit.Test)13 RawDataSchema (com.ociweb.pronghorn.pipe.RawDataSchema)10 DataOutputBlobWriter (com.ociweb.pronghorn.pipe.DataOutputBlobWriter)3 FieldType (com.ociweb.gl.api.FieldType)1 ChannelWriter (com.ociweb.pronghorn.pipe.ChannelWriter)1 DecimalFormat (java.text.DecimalFormat)1 NumberFormat (java.text.NumberFormat)1 Date (java.util.Date)1