Search in sources :

Example 1 with DataOutputBlobWriter

use of com.ociweb.pronghorn.pipe.DataOutputBlobWriter 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 DataOutputBlobWriter

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

the class JSONParserTest method buildPopulatedPipe.

private Pipe buildPopulatedPipe(String json) {
    Pipe pipe = new Pipe(new PipeConfig(RawDataSchema.instance));
    pipe.initBuffers();
    int size = Pipe.addMsgIdx(pipe, 0);
    DataOutputBlobWriter output = pipe.outputStream(pipe);
    output.openField();
    output.append(json);
    output.closeLowLevelField();
    Pipe.confirmLowLevelWrite(pipe, size);
    Pipe.publishWrites(pipe);
    return pipe;
}
Also used : PipeConfig(com.ociweb.pronghorn.pipe.PipeConfig) DataOutputBlobWriter(com.ociweb.pronghorn.pipe.DataOutputBlobWriter) Pipe(com.ociweb.pronghorn.pipe.Pipe)

Example 3 with DataOutputBlobWriter

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

the class TrieParserTest method testwriteCapturedUTF8ToPipe.

@Test
public void testwriteCapturedUTF8ToPipe() throws IOException {
    TrieParserReader reader = new TrieParserReader(3);
    TrieParser map = new TrieParser(16);
    // map.setValue(wrapping(dataBytesExtractStart,4), 0,
    // dataBytesExtractStart.length, 15, value2);
    map.setUTF8Value("%b1234", 33);
    CharSequence test1 = "abcd1234";
    reader.query(map, test1);
    Pipe<RawDataSchema> pipe = RawDataSchema.instance.newPipe(2, 64);
    pipe.initBuffers();
    int size = Pipe.addMsgIdx(pipe, RawDataSchema.MSG_CHUNKEDSTREAM_1);
    DataOutputBlobWriter x = Pipe.outputStream(pipe);
    DataOutputBlobWriter.openField(x);
    // just like in test above, will return the number of captured bytes 4
    // in this case(abcd).
    int num = TrieParserReader.writeCapturedUTF8ToPipe(reader, pipe, 0, RawDataSchema.MSG_CHUNKEDSTREAM_1_FIELD_BYTEARRAY_2);
    x.closeLowLevelField();
    Pipe.confirmLowLevelWrite(pipe, size);
    Pipe.publishWrites(pipe);
    // /////////
    int msg = Pipe.takeMsgIdx(pipe);
    assertEquals(RawDataSchema.MSG_CHUNKEDSTREAM_1, msg);
    DataInputBlobReader y = Pipe.inputStream(pipe);
    y.openLowLevelAPIField();
    StringBuilder str = new StringBuilder();
    y.readUTFOfLength(y.available(), str);
    Pipe.confirmLowLevelRead(pipe, size);
    Pipe.releaseReadLock(pipe);
    assertEquals(num, 4);
}
Also used : RawDataSchema(com.ociweb.pronghorn.pipe.RawDataSchema) DataInputBlobReader(com.ociweb.pronghorn.pipe.DataInputBlobReader) DataOutputBlobWriter(com.ociweb.pronghorn.pipe.DataOutputBlobWriter) Test(org.junit.Test)

Example 4 with DataOutputBlobWriter

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

the class TrieParserTest method testwriteCapturedUTF8.

@Test
public void testwriteCapturedUTF8() {
    TrieParserReader reader = new TrieParserReader(3);
    TrieParser map = new TrieParser(16);
    map.setUTF8Value("12%b1234", 33);
    CharSequence test = "12abcd1234";
    // query holds most recent thing
    reader.query(map, test);
    Pipe<RawDataSchema> pipe = RawDataSchema.instance.newPipe(2, 64);
    pipe.initBuffers();
    int size = Pipe.addMsgIdx(pipe, RawDataSchema.MSG_CHUNKEDSTREAM_1);
    DataOutputBlobWriter x = Pipe.outputStream(pipe);
    DataOutputBlobWriter.openField(x);
    // will append "abcd" to blobWriter
    int val = TrieParserReader.writeCapturedUTF8(reader, 0, x);
    x.closeLowLevelField();
    Pipe.confirmLowLevelWrite(pipe, size);
    Pipe.publishWrites(pipe);
    // /////////
    int msg = Pipe.takeMsgIdx(pipe);
    assertEquals(RawDataSchema.MSG_CHUNKEDSTREAM_1, msg);
    DataInputBlobReader y = Pipe.inputStream(pipe);
    y.openLowLevelAPIField();
    StringBuilder str = new StringBuilder();
    y.readUTFOfLength(y.available(), str);
    // value of blobwriter sent to stringbuilder to sompare with other
    // string
    assertEquals(str.toString().trim(), "abcd");
}
Also used : RawDataSchema(com.ociweb.pronghorn.pipe.RawDataSchema) DataInputBlobReader(com.ociweb.pronghorn.pipe.DataInputBlobReader) DataOutputBlobWriter(com.ociweb.pronghorn.pipe.DataOutputBlobWriter) Test(org.junit.Test)

Example 5 with DataOutputBlobWriter

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

the class GreenParserMessageTest method complexStringTest.

@Test
public void complexStringTest() {
    NumberFormat formatter = new DecimalFormat("#0.0000");
    final GreenReader reader = buildParser().newReader();
    ChannelReader testToRead = BlobReaderFactory.generateExtractionDataToTest(new MyConsumer<DataOutputBlobWriter<?>>() {

        @Override
        public void accept(DataOutputBlobWriter<?> dataOutputBlobWriter) {
            complexStreamAppend(dataOutputBlobWriter);
        }
    });
    reader.beginRead(testToRead);
    StringBuilder rebuild = new StringBuilder();
    while (reader.hasMore()) {
        int parsedId = (int) reader.readToken();
        if (parsedId == -1) {
            reader.skipByte();
        } else {
            final MsgField msgField = messages[parsedId];
            final FieldType fieldType = msgField.type;
            final String key = msgField.key;
            rebuild.append(key);
            switch(fieldType) {
                case integer:
                    {
                        int value = (int) reader.extractedLong(0);
                        rebuild.append(value);
                        break;
                    }
                case int64:
                    {
                        long value = reader.extractedLong(0);
                        rebuild.append(value);
                        break;
                    }
                case string:
                    {
                        StringBuilder value = new StringBuilder();
                        reader.copyExtractedUTF8ToAppendable(0, value);
                        rebuild.append("\"");
                        rebuild.append(value);
                        rebuild.append("\"");
                        break;
                    }
                case floatingPoint:
                    {
                        double value = reader.extractedDouble(0);
                        rebuild.append(formatter.format(value));
                        break;
                    }
            }
        }
    }
    assertEquals(complexData, rebuild.toString());
}
Also used : DecimalFormat(java.text.DecimalFormat) DataOutputBlobWriter(com.ociweb.pronghorn.pipe.DataOutputBlobWriter) FieldType(com.ociweb.gl.api.FieldType) ChannelReader(com.ociweb.pronghorn.pipe.ChannelReader) NumberFormat(java.text.NumberFormat) Test(org.junit.Test)

Aggregations

DataOutputBlobWriter (com.ociweb.pronghorn.pipe.DataOutputBlobWriter)11 Test (org.junit.Test)9 DataInputBlobReader (com.ociweb.pronghorn.pipe.DataInputBlobReader)6 RawDataSchema (com.ociweb.pronghorn.pipe.RawDataSchema)6 ChannelReader (com.ociweb.pronghorn.pipe.ChannelReader)3 Pipe (com.ociweb.pronghorn.pipe.Pipe)2 FieldType (com.ociweb.gl.api.FieldType)1 ClientConnection (com.ociweb.pronghorn.network.ClientConnection)1 ClientHTTPRequestSchema (com.ociweb.pronghorn.network.schema.ClientHTTPRequestSchema)1 NetPayloadSchema (com.ociweb.pronghorn.network.schema.NetPayloadSchema)1 PipeConfig (com.ociweb.pronghorn.pipe.PipeConfig)1 DecimalFormat (java.text.DecimalFormat)1 NumberFormat (java.text.NumberFormat)1