Search in sources :

Example 1 with InfinispanDocument

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

the class InfinispanUpdateVisitor method buildTargetDocument.

private InfinispanDocument buildTargetDocument(Table table, boolean addDefaults) throws TranslatorException {
    TreeMap<Integer, TableWireFormat> wireMap = MarshallerBuilder.getWireMap(getParentTable(), metadata);
    String messageName = ProtobufMetadataProcessor.getMessageName(getParentTable());
    InfinispanDocument parentDocument = new InfinispanDocument(messageName, wireMap, null);
    // if there are any one-2-one relation build them and add defaults
    addDefaults(parentDocument, getParentTable(), addDefaults);
    // now create the document at child node, this is one-2-many case
    if (!table.equals(getParentTable())) {
        messageName = ProtobufMetadataProcessor.getMessageName(table);
        int parentTag = ProtobufMetadataProcessor.getParentTag(table);
        TableWireFormat twf = wireMap.get(TableWireFormat.buildNestedTag(parentTag));
        this.nested = true;
        InfinispanDocument child = new InfinispanDocument(messageName, twf.getNestedWireMap(), parentDocument);
        addDefaults(child, table, addDefaults);
        parentDocument.addChildDocument(messageName, child);
    }
    return parentDocument;
}
Also used : InfinispanDocument(org.teiid.infinispan.api.InfinispanDocument) TableWireFormat(org.teiid.infinispan.api.TableWireFormat)

Example 2 with InfinispanDocument

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

the class InfinispanUpdateVisitor method getBulkInsertPayload.

Map<Object, InfinispanDocument> getBulkInsertPayload(Insert obj, int batchSize, Iterator<? extends List<Expression>> iter) {
    Map<Object, InfinispanDocument> updates = new TreeMap<>();
    int count = 0;
    while (iter.hasNext()) {
        InfinispanDocument doc = buildInsertPayload(obj, iter.next());
        updates.put(doc.getIdentifier(), doc);
        if (count++ == batchSize) {
            break;
        }
    }
    return updates;
}
Also used : InfinispanDocument(org.teiid.infinispan.api.InfinispanDocument) TreeMap(java.util.TreeMap)

Example 3 with InfinispanDocument

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

the class InfinispanUpdateVisitor method updateDocument.

@SuppressWarnings("unchecked")
private void updateDocument(InfinispanDocument parentDocument, Column column, Object value) throws TranslatorException {
    boolean complexObject = this.nested;
    InfinispanDocument targetDocument = parentDocument;
    int parentTag = ProtobufMetadataProcessor.getParentTag(column);
    if (parentTag != -1) {
        // this is in one-2-one case. Dummy child will be there due to buildTargetDocument logic.
        String messageName = ProtobufMetadataProcessor.getMessageName(column);
        InfinispanDocument child = (InfinispanDocument) parentDocument.getChildDocuments(messageName).get(0);
        targetDocument = child;
        complexObject = true;
    } else if (this.nested) {
        Table table = (Table) column.getParent();
        String messageName = ProtobufMetadataProcessor.getMessageName(table);
        InfinispanDocument child = (InfinispanDocument) parentDocument.getChildDocuments(messageName).get(0);
        targetDocument = child;
        complexObject = true;
    }
    if (!ProtobufMetadataProcessor.isPseudo(column)) {
        if (value instanceof List) {
            List<Object> l = (List<Object>) value;
            for (Object o : l) {
                targetDocument.addArrayProperty(getName(column), o);
            }
        } else {
            targetDocument.addProperty(getName(column), value);
        }
        String attrName = MarshallerBuilder.getDocumentAttributeName(column, complexObject, this.metadata);
        this.updatePayload.put(attrName, value);
    }
}
Also used : Table(org.teiid.metadata.Table) InfinispanDocument(org.teiid.infinispan.api.InfinispanDocument) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with InfinispanDocument

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

the class InfinispanUpdateVisitor method addDefaults.

private void addDefaults(InfinispanDocument parentDocument, Table table, boolean addDefaults) throws TranslatorException {
    for (Column column : table.getColumns()) {
        int parentTag = ProtobufMetadataProcessor.getParentTag(column);
        if (parentTag != -1) {
            String messageName = ProtobufMetadataProcessor.getMessageName(column);
            List<?> children = parentDocument.getChildDocuments(messageName);
            InfinispanDocument child = null;
            if (children == null || children.isEmpty()) {
                TableWireFormat twf = parentDocument.getWireMap().get(TableWireFormat.buildNestedTag(parentTag));
                child = new InfinispanDocument(messageName, twf.getNestedWireMap(), parentDocument);
                parentDocument.addChildDocument(messageName, child);
            } else {
                child = (InfinispanDocument) children.get(0);
            }
            if (addDefaults && column.getDefaultValue() != null) {
                child.addProperty(getName(column), column.getDefaultValue());
            }
        } else {
            if (addDefaults && column.getDefaultValue() != null) {
                parentDocument.addProperty(getName(column), column.getDefaultValue());
            }
        }
    }
}
Also used : InfinispanDocument(org.teiid.infinispan.api.InfinispanDocument) Column(org.teiid.metadata.Column) TableWireFormat(org.teiid.infinispan.api.TableWireFormat)

Example 5 with InfinispanDocument

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

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