Search in sources :

Example 1 with ByteArrayOutputStreamEx

use of org.infinispan.protostream.impl.ByteArrayOutputStreamEx 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)

Aggregations

IOException (java.io.IOException)1 RawProtoStreamWriter (org.infinispan.protostream.RawProtoStreamWriter)1 ByteArrayOutputStreamEx (org.infinispan.protostream.impl.ByteArrayOutputStreamEx)1 Document (org.teiid.translator.document.Document)1