Search in sources :

Example 1 with Document

use of org.teiid.translator.document.Document 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 Document

use of org.teiid.translator.document.Document 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 3 with Document

use of org.teiid.translator.document.Document in project teiid by teiid.

the class InfinispanDocument method merge.

public int merge(InfinispanDocument updates) {
    int updated = 1;
    for (Entry<String, Object> entry : updates.getProperties().entrySet()) {
        addProperty(entry.getKey(), entry.getValue());
    }
    // update children if any
    for (Entry<String, List<Document>> entry : updates.getChildren().entrySet()) {
        String childName = entry.getKey();
        List<? extends Document> childUpdates = updates.getChildDocuments(childName);
        InfinispanDocument childUpdate = (InfinispanDocument) childUpdates.get(0);
        if (childUpdate.getProperties().isEmpty()) {
            continue;
        }
        List<? extends Document> previousChildren = getChildDocuments(childName);
        if (previousChildren == null || previousChildren.isEmpty()) {
            addChildDocument(childName, childUpdate);
        } else {
            for (Document doc : previousChildren) {
                InfinispanDocument previousChild = (InfinispanDocument) doc;
                if (previousChild.isMatched()) {
                    for (Entry<String, Object> childEntry : childUpdate.getProperties().entrySet()) {
                        String key = childEntry.getKey().substring(childEntry.getKey().lastIndexOf('/') + 1);
                        previousChild.addProperty(key, childEntry.getValue());
                        updated++;
                    }
                }
            }
        }
    }
    return updated;
}
Also used : List(java.util.List) Document(org.teiid.translator.document.Document)

Example 4 with Document

use of org.teiid.translator.document.Document in project teiid by teiid.

the class JsonSerializer method writeDocument.

private void writeDocument(Document doc, String name, JsonGenerator json, boolean writeName) throws IOException, SQLException {
    if (doc.getProperties().isEmpty() && doc.getChildren().isEmpty()) {
        return;
    }
    if (writeName) {
        json.writeObjectFieldStart(name);
    } else {
        json.writeStartObject();
    }
    for (Map.Entry<String, Object> entry : doc.getProperties().entrySet()) {
        String key = entry.getKey();
        Object value = entry.getValue();
        if (key.startsWith(name + "/")) {
            key = key.substring(name.length() + 1);
        }
        if (value instanceof org.teiid.language.Array) {
            json.writeArrayFieldStart(key);
            org.teiid.language.Array array = (org.teiid.language.Array) value;
            for (Expression expr : array.getExpressions()) {
                writeProperty(json, ((Literal) expr).getValue());
            }
            json.writeEndArray();
        } else {
            writeProperty(json, key, value);
        }
    }
    if (!doc.getChildren().isEmpty()) {
        for (Map.Entry<String, List<Document>> entry : doc.getChildren().entrySet()) {
            String docName = entry.getKey();
            List<Document> children = entry.getValue();
            boolean array = children.get(0).isArray();
            if (array) {
                json.writeArrayFieldStart(docName);
                for (Document child : children) {
                    writeDocument(child, docName, json, false);
                }
                json.writeEndArray();
            } else {
                writeDocument(children.get(0), docName, json, true);
            }
        }
    }
    json.writeEndObject();
}
Also used : Document(org.teiid.translator.document.Document) Expression(org.teiid.language.Expression) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Example 5 with Document

use of org.teiid.translator.document.Document in project teiid by teiid.

the class JsonSerializer method deserialize.

@Override
public List<Document> deserialize(InputStream stream) throws TranslatorException {
    try {
        JsonParser parser = new JsonFactory().createParser(stream);
        List<Document> list = null;
        Document current = null;
        Stack<String> fieldName = new Stack<String>();
        int arrayLevel = 0;
        int objectLevel = 0;
        while (parser.nextToken() != null) {
            switch(parser.getCurrentToken()) {
                case START_OBJECT:
                    objectLevel++;
                    if (current == null) {
                        // this is root
                        current = new Document();
                    } else {
                        Document child = new Document(fieldName.peek(), (arrayLevel >= objectLevel), current);
                        current.addChildDocument(fieldName.peek(), child);
                        current = child;
                    }
                    break;
                case END_OBJECT:
                    objectLevel--;
                    if (list != null && current.getParent() == null) {
                        list.add(current);
                        current = null;
                    } else {
                        Document parent = current.getParent();
                        if (parent != null) {
                            current = parent;
                        }
                    }
                    break;
                case START_ARRAY:
                    arrayLevel++;
                    if (current == null && list == null) {
                        // root document is a list
                        list = new ArrayList<Document>();
                    }
                    break;
                case END_ARRAY:
                    if (arrayLevel > objectLevel && !fieldName.empty()) {
                        fieldName.pop();
                    }
                    arrayLevel--;
                    break;
                case FIELD_NAME:
                    fieldName.push(parser.getCurrentName());
                    break;
                case VALUE_STRING:
                    if ((list != null && arrayLevel > objectLevel) || (list == null && arrayLevel >= objectLevel)) {
                        current.addArrayProperty(fieldName.peek(), parser.getValueAsString());
                    } else {
                        current.addProperty(fieldName.pop(), parser.getValueAsString());
                    }
                    break;
                case VALUE_NUMBER_INT:
                    if ((list != null && arrayLevel > objectLevel) || (list == null && arrayLevel >= objectLevel)) {
                        current.addArrayProperty(fieldName.peek(), parser.getValueAsLong());
                    } else {
                        current.addProperty(fieldName.pop(), parser.getValueAsLong());
                    }
                    break;
                case VALUE_NUMBER_FLOAT:
                    if ((list != null && arrayLevel > objectLevel) || (list == null && arrayLevel >= objectLevel)) {
                        current.addArrayProperty(fieldName.peek(), parser.getValueAsDouble());
                    } else {
                        current.addProperty(fieldName.pop(), parser.getValueAsDouble());
                    }
                    break;
                case VALUE_TRUE:
                    if ((list != null && arrayLevel > objectLevel) || (list == null && arrayLevel >= objectLevel)) {
                        current.addArrayProperty(fieldName.peek(), parser.getValueAsBoolean());
                    } else {
                        current.addProperty(fieldName.pop(), parser.getValueAsBoolean());
                    }
                    break;
                case VALUE_FALSE:
                    if ((list != null && arrayLevel > objectLevel) || (list == null && arrayLevel >= objectLevel)) {
                        current.addArrayProperty(fieldName.peek(), parser.getValueAsBoolean());
                    } else {
                        current.addProperty(fieldName.pop(), parser.getValueAsBoolean());
                    }
                    break;
                case VALUE_NULL:
                    if ((list != null && arrayLevel > objectLevel) || (list == null && arrayLevel >= objectLevel)) {
                        current.addArrayProperty(fieldName.peek(), null);
                    } else {
                        current.addProperty(fieldName.pop(), null);
                    }
                    break;
                default:
                    break;
            }
        }
        return list == null ? Arrays.asList(current) : list;
    } catch (JsonParseException e) {
        throw new TranslatorException(SwaggerPlugin.Event.TEIID28007, SwaggerPlugin.Util.gs(SwaggerPlugin.Event.TEIID28007, e));
    } catch (IOException e) {
        throw new TranslatorException(SwaggerPlugin.Event.TEIID28007, SwaggerPlugin.Util.gs(SwaggerPlugin.Event.TEIID28007, e));
    }
}
Also used : JsonFactory(com.fasterxml.jackson.core.JsonFactory) TranslatorException(org.teiid.translator.TranslatorException) IOException(java.io.IOException) Document(org.teiid.translator.document.Document) JsonParseException(com.fasterxml.jackson.core.JsonParseException) JsonParser(com.fasterxml.jackson.core.JsonParser) Stack(java.util.Stack)

Aggregations

Document (org.teiid.translator.document.Document)7 List (java.util.List)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 RawProtoStreamWriter (org.infinispan.protostream.RawProtoStreamWriter)2 JsonFactory (com.fasterxml.jackson.core.JsonFactory)1 JsonParseException (com.fasterxml.jackson.core.JsonParseException)1 JsonParser (com.fasterxml.jackson.core.JsonParser)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 LinkedHashMap (java.util.LinkedHashMap)1 Stack (java.util.Stack)1 RawProtoStreamReader (org.infinispan.protostream.RawProtoStreamReader)1 SerializationContext (org.infinispan.protostream.SerializationContext)1 ByteArrayOutputStreamEx (org.infinispan.protostream.impl.ByteArrayOutputStreamEx)1 Test (org.junit.Test)1 InfinispanDocument (org.teiid.infinispan.api.InfinispanDocument)1 TeiidTableMarsheller (org.teiid.infinispan.api.TeiidTableMarsheller)1 Expression (org.teiid.language.Expression)1 TranslatorException (org.teiid.translator.TranslatorException)1