Search in sources :

Example 6 with InfinispanDocument

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

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

the class TestTeiidTableMarsheller method buildG2.

private InfinispanDocument buildG2(IckleConversionVisitor visitor) throws TranslatorException {
    TreeMap<Integer, TableWireFormat> wireMap = MarshallerBuilder.getWireMap(visitor.getParentTable(), visitor.getMetadata());
    InfinispanDocument g2 = new InfinispanDocument("pm1.G2", wireMap, null);
    g2.addProperty("e1", 1);
    g2.addProperty("e2", "foo");
    InfinispanDocument g3 = new InfinispanDocument("pm1.G3", getWireMap(wireMap, "pm1.G3"), g2);
    g3.addProperty("e1", 1);
    g3.addProperty("e2", "bar");
    g2.addChildDocument("pm1.G3", g3);
    InfinispanDocument g41 = new InfinispanDocument("pm1.G4", getWireMap(wireMap, "pm1.G4"), g2);
    g41.addProperty("e1", 1);
    g41.addProperty("e2", "hello");
    g2.addChildDocument("pm1.G4", g41);
    InfinispanDocument g42 = new InfinispanDocument("pm1.G4", getWireMap(wireMap, "pm1.G4"), g2);
    g42.addProperty("e1", 2);
    g42.addProperty("e2", "world");
    g2.addChildDocument("pm1.G4", g42);
    g2.addProperty("e5", "Hello Infinispan");
    g2.addProperty("e6", new Timestamp(1489835322801L));
    return g2;
}
Also used : InfinispanDocument(org.teiid.infinispan.api.InfinispanDocument) Timestamp(java.sql.Timestamp) TableWireFormat(org.teiid.infinispan.api.TableWireFormat)

Example 8 with InfinispanDocument

use of org.teiid.infinispan.api.InfinispanDocument 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 9 with InfinispanDocument

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

the class InfinispanUpdateExecution method insertRow.

private void insertRow(RemoteCache<Object, Object> cache, Object rowKey, InfinispanDocument row, boolean upsert) throws TranslatorException {
    // this is always single row; putIfAbsent is not working correctly.
    InfinispanDocument previous = (InfinispanDocument) cache.get(rowKey);
    if (previous != null && !upsert) {
        throw new TranslatorException(InfinispanPlugin.Util.gs(InfinispanPlugin.Event.TEIID25005, previous.getName(), rowKey));
    }
    if (upsert && previous != null) {
        previous.merge(row);
        previous = (InfinispanDocument) cache.replace(rowKey, previous);
    } else {
        previous = row;
        previous = (InfinispanDocument) cache.put(rowKey, previous);
    }
}
Also used : InfinispanDocument(org.teiid.infinispan.api.InfinispanDocument) TranslatorException(org.teiid.translator.TranslatorException)

Example 10 with InfinispanDocument

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

the class InfinispanUpdateVisitor method buildInsertPayload.

private InfinispanDocument buildInsertPayload(Insert obj, List<Expression> values) {
    InfinispanDocument targetDocument = null;
    try {
        // table that insert issued for
        Table table = obj.getTable().getMetadataObject();
        Column pkColumn = getPrimaryKey();
        // create the top table parent document, where insert is actually being done at
        targetDocument = buildTargetDocument(table, true);
        // build the payload object from insert
        int elementCount = obj.getColumns().size();
        for (int i = 0; i < elementCount; i++) {
            ColumnReference columnReference = obj.getColumns().get(i);
            Column column = columnReference.getMetadataObject();
            Object value = null;
            if (values.get(i) instanceof Expression) {
                Expression expr = values.get(i);
                value = resolveExpressionValue(expr);
            } else {
                value = values.get(i);
            }
            updateDocument(targetDocument, column, value);
            if (column.equals(pkColumn) || pkColumn.equals(normalizePseudoColumn(column))) {
                targetDocument.setIdentifier(value);
            }
        }
        if (targetDocument.getIdentifier() == null) {
            this.exceptions.add(new TranslatorException(InfinispanPlugin.Util.gs(InfinispanPlugin.Event.TEIID25004, getParentTable().getName())));
        }
    } catch (NumberFormatException e) {
        this.exceptions.add(new TranslatorException(e));
    } catch (TranslatorException e) {
        this.exceptions.add(new TranslatorException(e));
    }
    return targetDocument;
}
Also used : Table(org.teiid.metadata.Table) InfinispanDocument(org.teiid.infinispan.api.InfinispanDocument) Column(org.teiid.metadata.Column) Expression(org.teiid.language.Expression) TranslatorException(org.teiid.translator.TranslatorException) ColumnReference(org.teiid.language.ColumnReference)

Aggregations

InfinispanDocument (org.teiid.infinispan.api.InfinispanDocument)13 TranslatorException (org.teiid.translator.TranslatorException)5 TeiidTableMarsheller (org.teiid.infinispan.api.TeiidTableMarsheller)4 Column (org.teiid.metadata.Column)4 Table (org.teiid.metadata.Table)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 RawProtoStreamReader (org.infinispan.protostream.RawProtoStreamReader)3 RawProtoStreamWriter (org.infinispan.protostream.RawProtoStreamWriter)3 SerializationContext (org.infinispan.protostream.SerializationContext)3 Test (org.junit.Test)3 TableWireFormat (org.teiid.infinispan.api.TableWireFormat)3 Expression (org.teiid.language.Expression)3 List (java.util.List)2 ColumnReference (org.teiid.language.ColumnReference)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 Timestamp (java.sql.Timestamp)1 ArrayList (java.util.ArrayList)1