Search in sources :

Example 1 with RawProtoStreamWriter

use of org.infinispan.protostream.RawProtoStreamWriter in project teiid by teiid.

the class TestTeiidTableMarsheller method testReadSimple.

@Test
public void testReadSimple() throws Exception {
    IckleConversionVisitor visitor = helpExecute("select * from G1");
    TeiidTableMarsheller g1ReadMarshaller = new TeiidTableMarsheller(ProtobufMetadataProcessor.getMessageName(visitor.getParentTable()), MarshallerBuilder.getWireMap(visitor.getParentTable(), visitor.getMetadata()));
    G1Marshaller g1WriteMarshller = new G1Marshaller() {

        @Override
        public G1 readFrom(ProtoStreamReader reader) throws IOException {
            throw new RuntimeException("Use Teiid marshaller for reading for this test..");
        }
    };
    SerializationContext ctx = ProtobufUtil.newSerializationContext(Configuration.builder().build());
    ctx.registerProtoFiles(FileDescriptorSource.fromString("tables.proto", ObjectConverterUtil.convertFileToString(UnitTestUtil.getTestDataFile("tables.proto"))));
    G1 g1 = buildG1();
    ctx.registerMarshaller(g1WriteMarshller);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    RawProtoStreamWriter out = RawProtoStreamWriterImpl.newInstance(baos);
    WrappedMessage.writeMessage(ctx, out, g1);
    out.flush();
    baos.flush();
    ctx.unregisterMarshaller(g1WriteMarshller);
    ctx.registerMarshaller(g1ReadMarshaller);
    RawProtoStreamReader in = RawProtoStreamReaderImpl.newInstance(baos.toByteArray());
    Document result = WrappedMessage.readMessage(ctx, in);
    Map<String, Object> row = result.flatten().get(0);
    assertEquals(1, row.get("e1"));
    assertEquals("foo", row.get("e2"));
    assertEquals(1.234f, row.get("e3"));
    assertNull(row.get("e4"));
    List<String> e5 = (List<String>) row.get("e5");
    assertArrayEquals(new String[] { "hello", "world" }, e5.toArray(new String[e5.size()]));
    ctx.unregisterMarshaller(g1ReadMarshaller);
}
Also used : SerializationContext(org.infinispan.protostream.SerializationContext) RawProtoStreamReader(org.infinispan.protostream.RawProtoStreamReader) G1Marshaller(org.teiid.translator.marshallers.G1Marshaller) TeiidTableMarsheller(org.teiid.infinispan.api.TeiidTableMarsheller) RawProtoStreamReader(org.infinispan.protostream.RawProtoStreamReader) G1(org.teiid.translator.marshallers.G1) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.teiid.translator.document.Document) InfinispanDocument(org.teiid.infinispan.api.InfinispanDocument) RawProtoStreamWriter(org.infinispan.protostream.RawProtoStreamWriter) List(java.util.List) Test(org.junit.Test)

Example 2 with RawProtoStreamWriter

use of org.infinispan.protostream.RawProtoStreamWriter in project teiid by teiid.

the class TestTeiidTableMarsheller method testWriteSimple.

@Test
public void testWriteSimple() throws Exception {
    IckleConversionVisitor visitor = helpExecute("select * from G1");
    TeiidTableMarsheller g1WriteMarshaller = new TeiidTableMarsheller(ProtobufMetadataProcessor.getMessageName(visitor.getParentTable()), MarshallerBuilder.getWireMap(visitor.getParentTable(), visitor.getMetadata()));
    G1Marshaller g1ReadMarshaller = new G1Marshaller() {

        @Override
        public void writeTo(ProtoStreamWriter writer, G1 g1) throws IOException {
            throw new RuntimeException("Use Teiid marshaller for writing for this test..");
        }
    };
    SerializationContext ctx = ProtobufUtil.newSerializationContext(Configuration.builder().build());
    ctx.registerProtoFiles(FileDescriptorSource.fromString("tables.proto", ObjectConverterUtil.convertFileToString(UnitTestUtil.getTestDataFile("tables.proto"))));
    InfinispanDocument g1 = new InfinispanDocument("pm1.G1", MarshallerBuilder.getWireMap(visitor.getParentTable(), visitor.getMetadata()), null);
    g1.addProperty("e1", 1);
    g1.addProperty("e2", "foo");
    g1.addProperty("e3", 1.234f);
    g1.addProperty("e4", null);
    g1.addArrayProperty("e5", "hello");
    g1.addArrayProperty("e5", "world");
    // write to buffer
    ctx.registerMarshaller(g1WriteMarshaller);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    RawProtoStreamWriter out = RawProtoStreamWriterImpl.newInstance(baos);
    WrappedMessage.writeMessage(ctx, out, g1);
    out.flush();
    baos.flush();
    ctx.unregisterMarshaller(g1WriteMarshaller);
    // read from buffer
    ctx.registerMarshaller(g1ReadMarshaller);
    RawProtoStreamReader in = RawProtoStreamReaderImpl.newInstance(baos.toByteArray());
    G1 result = WrappedMessage.readMessage(ctx, in);
    ctx.unregisterMarshaller(g1ReadMarshaller);
    assertEquals(buildG1(), result);
}
Also used : SerializationContext(org.infinispan.protostream.SerializationContext) RawProtoStreamReader(org.infinispan.protostream.RawProtoStreamReader) G1Marshaller(org.teiid.translator.marshallers.G1Marshaller) TeiidTableMarsheller(org.teiid.infinispan.api.TeiidTableMarsheller) InfinispanDocument(org.teiid.infinispan.api.InfinispanDocument) RawProtoStreamWriter(org.infinispan.protostream.RawProtoStreamWriter) RawProtoStreamWriter(org.infinispan.protostream.RawProtoStreamWriter) G1(org.teiid.translator.marshallers.G1) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 3 with RawProtoStreamWriter

use of org.infinispan.protostream.RawProtoStreamWriter in project teiid by teiid.

the class TestTeiidTableMarsheller method testReadComplex.

@Test
public void testReadComplex() throws Exception {
    IckleConversionVisitor visitor = helpExecute("select * from G2");
    TeiidTableMarsheller readMarshaller = new TeiidTableMarsheller(ProtobufMetadataProcessor.getMessageName(visitor.getParentTable()), MarshallerBuilder.getWireMap(visitor.getParentTable(), visitor.getMetadata()));
    SerializationContext ctx = ProtobufUtil.newSerializationContext(Configuration.builder().build());
    ctx.registerProtoFiles(FileDescriptorSource.fromString("tables.proto", ObjectConverterUtil.convertFileToString(UnitTestUtil.getTestDataFile("tables.proto"))));
    ctx.registerMarshaller(new G3Marshaller());
    ctx.registerMarshaller(new G4Marshaller());
    G2Marshaller writeMarshaller = new G2Marshaller() {

        @Override
        public G2 readFrom(ProtoStreamReader reader) throws IOException {
            throw new RuntimeException("Use Teiid marshaller for reading for this test..");
        }
    };
    G2 g2 = buildG2();
    // this is used for writing, if reading is being attempted then fail
    ctx.registerMarshaller(writeMarshaller);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    RawProtoStreamWriter out = RawProtoStreamWriterImpl.newInstance(baos);
    WrappedMessage.writeMessage(ctx, out, g2);
    out.flush();
    baos.flush();
    ctx.unregisterMarshaller(writeMarshaller);
    ctx.registerMarshaller(readMarshaller);
    RawProtoStreamReader in = RawProtoStreamReaderImpl.newInstance(baos.toByteArray());
    InfinispanDocument result = WrappedMessage.readMessage(ctx, in);
    // System.out.println(result.flatten());
    assertG2(result);
    ctx.unregisterMarshaller(readMarshaller);
}
Also used : SerializationContext(org.infinispan.protostream.SerializationContext) RawProtoStreamReader(org.infinispan.protostream.RawProtoStreamReader) TeiidTableMarsheller(org.teiid.infinispan.api.TeiidTableMarsheller) InfinispanDocument(org.teiid.infinispan.api.InfinispanDocument) RawProtoStreamReader(org.infinispan.protostream.RawProtoStreamReader) G3Marshaller(org.teiid.translator.marshallers.G3Marshaller) G2(org.teiid.translator.marshallers.G2) ByteArrayOutputStream(java.io.ByteArrayOutputStream) G2Marshaller(org.teiid.translator.marshallers.G2Marshaller) RawProtoStreamWriter(org.infinispan.protostream.RawProtoStreamWriter) G4Marshaller(org.teiid.translator.marshallers.G4Marshaller) Test(org.junit.Test)

Example 4 with RawProtoStreamWriter

use of org.infinispan.protostream.RawProtoStreamWriter in project teiid by teiid.

the class TeiidTableMarsheller method writeTo.

// Write from Teiid Types >> ISPN Types
@Override
public void writeTo(ImmutableSerializationContext ctx, RawProtoStreamWriter out, InfinispanDocument document) throws IOException {
    TreeMap<Integer, TableWireFormat> wireMap = document.getWireMap();
    for (Entry<Integer, TableWireFormat> entry : wireMap.entrySet()) {
        TableWireFormat twf = entry.getValue();
        if (twf == null) {
            throw new IOException("Error in wireformat");
        }
        int tag = twf.getWriteTag();
        if (twf.isNested()) {
            List<? extends Document> children = document.getChildDocuments(twf.getAttributeName());
            if (children != null) {
                for (Document d : children) {
                    ByteArrayOutputStreamEx baos = new ByteArrayOutputStreamEx();
                    RawProtoStreamWriter rpsw = RawProtoStreamWriterImpl.newInstance(baos);
                    writeTo(ctx, rpsw, (InfinispanDocument) d);
                    rpsw.flush();
                    baos.flush();
                    // here readtag because this is inner object, even other one uses write tag but calculated
                    // based on the write operation used.
                    out.writeBytes(tag, baos.getByteBuffer());
                }
            }
            continue;
        }
        Object value = document.getProperties().get(twf.getAttributeName());
        if (value == null) {
            continue;
        }
        ArrayList<Object> values = null;
        boolean array = twf.isArrayType();
        if (array) {
            values = (ArrayList<Object>) value;
        }
        switch(twf.getProtobufType()) {
            case DOUBLE:
                if (array) {
                    for (Object o : values) {
                        out.writeDouble(tag, ProtobufDataManager.convertToInfinispan(Double.class, o));
                    }
                } else {
                    out.writeDouble(tag, ProtobufDataManager.convertToInfinispan(Double.class, value));
                }
                break;
            case FLOAT:
                if (array) {
                    for (Object o : values) {
                        out.writeFloat(tag, ProtobufDataManager.convertToInfinispan(Float.class, o));
                    }
                } else {
                    out.writeFloat(tag, ProtobufDataManager.convertToInfinispan(Float.class, value));
                }
                break;
            case BOOL:
                if (array) {
                    for (Object o : values) {
                        out.writeBool(tag, ProtobufDataManager.convertToInfinispan(Boolean.class, o));
                    }
                } else {
                    out.writeBool(tag, ProtobufDataManager.convertToInfinispan(Boolean.class, value));
                }
                break;
            case STRING:
                if (array) {
                    for (Object o : values) {
                        out.writeString(tag, ProtobufDataManager.convertToInfinispan(String.class, o));
                    }
                } else {
                    out.writeString(tag, ProtobufDataManager.convertToInfinispan(String.class, value));
                }
                break;
            case BYTES:
                if (array) {
                    for (Object o : values) {
                        out.writeBytes(tag, ProtobufDataManager.convertToInfinispan(byte[].class, o));
                    }
                } else {
                    out.writeBytes(tag, ProtobufDataManager.convertToInfinispan(byte[].class, value));
                }
                break;
            case INT32:
                if (array) {
                    for (Object o : values) {
                        out.writeInt32(tag, ProtobufDataManager.convertToInfinispan(Integer.class, o));
                    }
                } else {
                    out.writeInt32(tag, ProtobufDataManager.convertToInfinispan(Integer.class, value));
                }
                break;
            case SFIXED32:
                if (array) {
                    for (Object o : values) {
                        out.writeSFixed32(tag, ProtobufDataManager.convertToInfinispan(Integer.class, o));
                    }
                } else {
                    out.writeSFixed32(tag, ProtobufDataManager.convertToInfinispan(Integer.class, value));
                }
                break;
            case FIXED32:
                if (array) {
                    for (Object o : values) {
                        out.writeFixed32(tag, ProtobufDataManager.convertToInfinispan(Integer.class, o));
                    }
                } else {
                    out.writeFixed32(tag, ProtobufDataManager.convertToInfinispan(Integer.class, value));
                }
                break;
            case UINT32:
                if (array) {
                    for (Object o : values) {
                        out.writeUInt32(tag, ProtobufDataManager.convertToInfinispan(Integer.class, o));
                    }
                } else {
                    out.writeUInt32(tag, ProtobufDataManager.convertToInfinispan(Integer.class, value));
                }
                break;
            case SINT32:
                if (array) {
                    for (Object o : values) {
                        out.writeSInt32(tag, ProtobufDataManager.convertToInfinispan(Integer.class, o));
                    }
                } else {
                    out.writeSInt32(tag, ProtobufDataManager.convertToInfinispan(Integer.class, value));
                }
                break;
            case INT64:
                if (array) {
                    for (Object o : values) {
                        out.writeInt64(tag, ProtobufDataManager.convertToInfinispan(Long.class, o));
                    }
                } else {
                    out.writeInt64(tag, ProtobufDataManager.convertToInfinispan(Long.class, value));
                }
                break;
            case UINT64:
                if (array) {
                    for (Object o : values) {
                        out.writeUInt64(tag, ProtobufDataManager.convertToInfinispan(Long.class, o));
                    }
                } else {
                    out.writeUInt64(tag, ProtobufDataManager.convertToInfinispan(Long.class, value));
                }
                break;
            case FIXED64:
                if (array) {
                    for (Object o : values) {
                        out.writeFixed64(tag, ProtobufDataManager.convertToInfinispan(Long.class, o));
                    }
                } else {
                    out.writeFixed64(tag, ProtobufDataManager.convertToInfinispan(Long.class, value));
                }
                break;
            case SFIXED64:
                if (array) {
                    for (Object o : values) {
                        out.writeSFixed64(tag, ProtobufDataManager.convertToInfinispan(Long.class, o));
                    }
                } else {
                    out.writeSFixed64(tag, ProtobufDataManager.convertToInfinispan(Long.class, value));
                }
                break;
            case SINT64:
                if (array) {
                    for (Object o : values) {
                        out.writeSInt64(tag, ProtobufDataManager.convertToInfinispan(Long.class, o));
                    }
                } else {
                    out.writeSInt64(tag, ProtobufDataManager.convertToInfinispan(Long.class, value));
                }
                break;
            default:
                throw new IOException("Unexpected field type : " + twf.getProtobufType());
        }
    }
}
Also used : ByteArrayOutputStreamEx(org.infinispan.protostream.impl.ByteArrayOutputStreamEx) IOException(java.io.IOException) Document(org.teiid.translator.document.Document) RawProtoStreamWriter(org.infinispan.protostream.RawProtoStreamWriter)

Example 5 with RawProtoStreamWriter

use of org.infinispan.protostream.RawProtoStreamWriter in project teiid by teiid.

the class TestTeiidTableMarsheller method testWriteComplex.

@Test
public void testWriteComplex() throws Exception {
    IckleConversionVisitor visitor = helpExecute("select * from G2");
    TeiidTableMarsheller writeMarshaller = new TeiidTableMarsheller(ProtobufMetadataProcessor.getMessageName(visitor.getParentTable()), MarshallerBuilder.getWireMap(visitor.getParentTable(), visitor.getMetadata()));
    SerializationContext ctx = ProtobufUtil.newSerializationContext(Configuration.builder().build());
    ctx.registerProtoFiles(FileDescriptorSource.fromString("tables.proto", ObjectConverterUtil.convertFileToString(UnitTestUtil.getTestDataFile("tables.proto"))));
    ctx.registerMarshaller(new G3Marshaller());
    ctx.registerMarshaller(new G4Marshaller());
    G2Marshaller readMarshaller = new G2Marshaller() {

        @Override
        public void writeTo(ProtoStreamWriter writer, G2 g2) throws IOException {
            throw new RuntimeException("Use Teiid marshaller for writing for this test..");
        }
    };
    InfinispanDocument g2 = buildG2(visitor);
    ctx.registerMarshaller(writeMarshaller);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    RawProtoStreamWriter out = RawProtoStreamWriterImpl.newInstance(baos);
    WrappedMessage.writeMessage(ctx, out, g2);
    out.flush();
    baos.flush();
    ctx.unregisterMarshaller(writeMarshaller);
    // this is used for writing, if reading is being attempted then fail
    ctx.registerMarshaller(readMarshaller);
    RawProtoStreamReader in = RawProtoStreamReaderImpl.newInstance(baos.toByteArray());
    G2 result = WrappedMessage.readMessage(ctx, in);
    ctx.unregisterMarshaller(readMarshaller);
    assertEquals(buildG2(), result);
}
Also used : SerializationContext(org.infinispan.protostream.SerializationContext) RawProtoStreamReader(org.infinispan.protostream.RawProtoStreamReader) TeiidTableMarsheller(org.teiid.infinispan.api.TeiidTableMarsheller) InfinispanDocument(org.teiid.infinispan.api.InfinispanDocument) G3Marshaller(org.teiid.translator.marshallers.G3Marshaller) RawProtoStreamWriter(org.infinispan.protostream.RawProtoStreamWriter) G2(org.teiid.translator.marshallers.G2) ByteArrayOutputStream(java.io.ByteArrayOutputStream) G2Marshaller(org.teiid.translator.marshallers.G2Marshaller) RawProtoStreamWriter(org.infinispan.protostream.RawProtoStreamWriter) G4Marshaller(org.teiid.translator.marshallers.G4Marshaller) Test(org.junit.Test)

Aggregations

RawProtoStreamWriter (org.infinispan.protostream.RawProtoStreamWriter)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 RawProtoStreamReader (org.infinispan.protostream.RawProtoStreamReader)5 SerializationContext (org.infinispan.protostream.SerializationContext)5 Test (org.junit.Test)5 InfinispanDocument (org.teiid.infinispan.api.InfinispanDocument)4 TeiidTableMarsheller (org.teiid.infinispan.api.TeiidTableMarsheller)4 G2 (org.teiid.translator.marshallers.G2)3 G2Marshaller (org.teiid.translator.marshallers.G2Marshaller)3 G3Marshaller (org.teiid.translator.marshallers.G3Marshaller)3 G4Marshaller (org.teiid.translator.marshallers.G4Marshaller)3 Document (org.teiid.translator.document.Document)2 G1 (org.teiid.translator.marshallers.G1)2 G1Marshaller (org.teiid.translator.marshallers.G1Marshaller)2 IOException (java.io.IOException)1 List (java.util.List)1 ByteArrayOutputStreamEx (org.infinispan.protostream.impl.ByteArrayOutputStreamEx)1