Search in sources :

Example 1 with TeiidTableMarsheller

use of org.teiid.infinispan.api.TeiidTableMarsheller 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 TeiidTableMarsheller

use of org.teiid.infinispan.api.TeiidTableMarsheller 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 TeiidTableMarsheller

use of org.teiid.infinispan.api.TeiidTableMarsheller 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 TeiidTableMarsheller

use of org.teiid.infinispan.api.TeiidTableMarsheller in project teiid by teiid.

the class InfinispanUpdateExecution method execute.

@Override
public void execute() throws TranslatorException {
    if (useAliasCache) {
        if (useAliasCache) {
            InfinispanQueryExecution.useModifiedGroups(this.connection, this.executionContext, this.metadata, this.command);
        }
    }
    final InfinispanUpdateVisitor visitor = new InfinispanUpdateVisitor(this.metadata);
    visitor.append(this.command);
    if (!visitor.exceptions.isEmpty()) {
        throw visitor.exceptions.get(0);
    }
    TeiidTableMarsheller marshaller = null;
    try {
        Table table = visitor.getParentTable();
        Column pkColumn = visitor.getPrimaryKey();
        if (pkColumn == null) {
            throw new TranslatorException(InfinispanPlugin.Util.gs(InfinispanPlugin.Event.TEIID25013, table.getName()));
        }
        final String PK = MarshallerBuilder.getDocumentAttributeName(pkColumn, false, this.metadata);
        DocumentFilter docFilter = null;
        if (visitor.isNestedOperation() && visitor.getWhereClause() != null) {
            Action action = Action.ALWAYSADD;
            if (command instanceof Delete) {
                action = Action.REMOVE;
            }
            SQLStringVisitor ssv = new SQLStringVisitor() {

                @Override
                public void visit(ColumnReference obj) {
                    String groupName = null;
                    NamedTable group = obj.getTable();
                    if (group.getCorrelationName() != null) {
                        groupName = group.getCorrelationName();
                    } else {
                        Table groupID = group.getMetadataObject();
                        if (groupID.getFullName().equals(visitor.getParentTable().getFullName())) {
                            groupName = visitor.getParentNamedTable().getCorrelationName();
                        } else {
                            groupName = visitor.getQueryNamedTable().getCorrelationName();
                        }
                    }
                    buffer.append(groupName).append(Tokens.DOT).append(getName(obj.getMetadataObject()));
                }

                @Override
                public String getName(AbstractMetadataRecord object) {
                    return object.getName();
                }
            };
            ssv.append(visitor.getWhereClause());
            docFilter = new ComplexDocumentFilter(visitor.getParentNamedTable(), visitor.getQueryNamedTable(), this.metadata, ssv.toString(), action);
        }
        marshaller = MarshallerBuilder.getMarshaller(table, this.metadata, docFilter);
        this.connection.registerMarshaller(marshaller);
        // if the message in defined in different cache than the default, switch it out now.
        final RemoteCache<Object, Object> cache = InfinispanQueryExecution.getCache(table, connection);
        if (visitor.getOperationType() == OperationType.DELETE) {
            paginateDeleteResults(cache, visitor.getDeleteQuery(), new Task() {

                @Override
                public void run(Object row) throws TranslatorException {
                    if (visitor.isNestedOperation()) {
                        String childName = ProtobufMetadataProcessor.getMessageName(visitor.getQueryTable());
                        InfinispanDocument document = (InfinispanDocument) row;
                        cache.replace(document.getProperties().get(PK), document);
                        // false below means count that not matched, i.e. deleted count
                        updateCount = updateCount + document.getUpdateCount(childName, false);
                    } else {
                        Object key = ((Object[]) row)[0];
                        cache.remove(key);
                        updateCount++;
                    }
                }
            }, this.executionContext.getBatchSize());
        } else if (visitor.getOperationType() == OperationType.UPDATE) {
            paginateUpdateResults(cache, visitor.getUpdateQuery(), new Task() {

                @Override
                public void run(Object row) throws TranslatorException {
                    InfinispanDocument previous = (InfinispanDocument) row;
                    Object key = previous.getProperties().get(PK);
                    int count = previous.merge(visitor.getInsertPayload());
                    cache.replace(key, previous);
                    updateCount = updateCount + count;
                }
            }, this.executionContext.getBatchSize());
        } else if (visitor.getOperationType() == OperationType.INSERT) {
            performInsert(visitor, table, cache, false, marshaller);
        } else if (visitor.getOperationType() == OperationType.UPSERT) {
            performInsert(visitor, table, cache, true, marshaller);
        }
    } finally {
        if (marshaller != null) {
            this.connection.unRegisterMarshaller(marshaller);
        }
    }
}
Also used : Delete(org.teiid.language.Delete) NamedTable(org.teiid.language.NamedTable) Action(org.teiid.infinispan.api.DocumentFilter.Action) NamedTable(org.teiid.language.NamedTable) Table(org.teiid.metadata.Table) SQLStringVisitor(org.teiid.language.visitor.SQLStringVisitor) TeiidTableMarsheller(org.teiid.infinispan.api.TeiidTableMarsheller) InfinispanDocument(org.teiid.infinispan.api.InfinispanDocument) DocumentFilter(org.teiid.infinispan.api.DocumentFilter) AbstractMetadataRecord(org.teiid.metadata.AbstractMetadataRecord) Column(org.teiid.metadata.Column) TranslatorException(org.teiid.translator.TranslatorException) ColumnReference(org.teiid.language.ColumnReference)

Example 5 with TeiidTableMarsheller

use of org.teiid.infinispan.api.TeiidTableMarsheller 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

InfinispanDocument (org.teiid.infinispan.api.InfinispanDocument)5 TeiidTableMarsheller (org.teiid.infinispan.api.TeiidTableMarsheller)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 RawProtoStreamReader (org.infinispan.protostream.RawProtoStreamReader)4 RawProtoStreamWriter (org.infinispan.protostream.RawProtoStreamWriter)4 SerializationContext (org.infinispan.protostream.SerializationContext)4 Test (org.junit.Test)4 G1 (org.teiid.translator.marshallers.G1)2 G1Marshaller (org.teiid.translator.marshallers.G1Marshaller)2 G2 (org.teiid.translator.marshallers.G2)2 G2Marshaller (org.teiid.translator.marshallers.G2Marshaller)2 G3Marshaller (org.teiid.translator.marshallers.G3Marshaller)2 G4Marshaller (org.teiid.translator.marshallers.G4Marshaller)2 List (java.util.List)1 DocumentFilter (org.teiid.infinispan.api.DocumentFilter)1 Action (org.teiid.infinispan.api.DocumentFilter.Action)1 ColumnReference (org.teiid.language.ColumnReference)1 Delete (org.teiid.language.Delete)1 NamedTable (org.teiid.language.NamedTable)1 SQLStringVisitor (org.teiid.language.visitor.SQLStringVisitor)1