Search in sources :

Example 11 with InfinispanDocument

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

the class InfinispanUpdateVisitor method visit.

@Override
public void visit(Update obj) {
    this.operationType = OperationType.UPDATE;
    append(obj.getTable());
    if (obj.getWhere() != null) {
        buffer.append(Tokens.SPACE).append(SQLConstants.Reserved.WHERE).append(Tokens.SPACE);
        append(obj.getWhere());
        // Can't use the original where string because it is designed for the document model querying
        this.whereClause = obj.getWhere();
    }
    // table that update issued for
    Table table = obj.getTable().getMetadataObject();
    if (!table.equals(getParentTable())) {
        this.nested = true;
    }
    // read the properties
    try {
        InfinispanDocument targetDocument = buildTargetDocument(table, false);
        int elementCount = obj.getChanges().size();
        for (int i = 0; i < elementCount; i++) {
            Column column = obj.getChanges().get(i).getSymbol().getMetadataObject();
            if (isPartOfPrimaryKey(column.getName())) {
                throw new TranslatorException(InfinispanPlugin.Event.TEIID25019, InfinispanPlugin.Util.gs(InfinispanPlugin.Event.TEIID25019, column.getName()));
            }
            Expression expr = obj.getChanges().get(i).getValue();
            Object value = resolveExpressionValue(expr);
            // this.updatePayload.put(getName(column), value);
            updateDocument(targetDocument, column, value);
        }
        this.insertPayload = targetDocument;
    } catch (TranslatorException e) {
        this.exceptions.add(e);
    }
}
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)

Example 12 with InfinispanDocument

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

Example 13 with InfinispanDocument

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

the class InfinispanUpdateExecution method performInsert.

@SuppressWarnings("unchecked")
private void performInsert(final InfinispanUpdateVisitor visitor, Table table, final RemoteCache<Object, Object> cache, boolean upsert, TeiidTableMarsheller marshaller) throws TranslatorException {
    Insert insert = (Insert) this.command;
    if (visitor.isNestedOperation()) {
        InfinispanDocument previous = null;
        if (visitor.getIdentity() != null) {
            previous = (InfinispanDocument) cache.get(visitor.getIdentity());
        }
        if (insert.getParameterValues() != null) {
            throw new TranslatorException(InfinispanPlugin.Util.gs(InfinispanPlugin.Event.TEIID25017, table.getName(), visitor.getParentTable().getName()));
        }
        if (previous == null) {
            throw new TranslatorException(InfinispanPlugin.Util.gs(InfinispanPlugin.Event.TEIID25009, table.getName(), visitor.getIdentity()));
        }
        String childName = ProtobufMetadataProcessor.getMessageName(visitor.getQueryTable());
        previous.addChildDocument(childName, visitor.getInsertPayload().getChildDocuments(childName).get(0));
        if (upsert) {
            previous = (InfinispanDocument) cache.replace(visitor.getIdentity(), previous);
        } else {
            previous = (InfinispanDocument) cache.put(visitor.getIdentity(), previous);
        }
        this.updateCount++;
    } else {
        if (insert.getParameterValues() == null) {
            insertRow(cache, visitor.getIdentity(), visitor.getInsertPayload(), upsert);
            this.updateCount++;
        } else {
            boolean putAll = false;
            if (this.executionContext.getSourceHint() != null) {
                putAll = this.executionContext.getSourceHint().indexOf("use-putall") != -1;
            }
            // bulk insert
            int batchSize = this.executionContext.getBatchSize();
            Iterator<? extends List<Expression>> args = (Iterator<? extends List<Expression>>) insert.getParameterValues();
            while (true) {
                Map<Object, InfinispanDocument> rows = visitor.getBulkInsertPayload(insert, batchSize, args);
                if (rows.isEmpty()) {
                    break;
                }
                if (putAll) {
                    BulkInsert bi = new UsePutAll(cache);
                    bi.run(rows, false);
                } else {
                    BulkInsert bi = new OneAtATime(cache);
                    bi.run(rows, upsert);
                }
                this.updateCount += rows.size();
            }
        }
    }
}
Also used : InfinispanDocument(org.teiid.infinispan.api.InfinispanDocument) Insert(org.teiid.language.Insert) Expression(org.teiid.language.Expression) Iterator(java.util.Iterator) TranslatorException(org.teiid.translator.TranslatorException) List(java.util.List)

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