Search in sources :

Example 6 with InputStreamFactory

use of org.teiid.core.types.InputStreamFactory in project teiid by teiid.

the class TestObjectDecoderInputStream method testReplaceObject.

@Test
public void testReplaceObject() throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectEncoderOutputStream out = new ObjectEncoderOutputStream(new DataOutputStream(baos), 512);
    ClobImpl clob = new ClobImpl(new InputStreamFactory() {

        @Override
        public InputStream getInputStream() throws IOException {
            // $NON-NLS-1$
            return new ReaderInputStream(new StringReader("Clob contents"), Charset.forName(Streamable.ENCODING));
        }
    }, -1);
    out.writeObject(clob);
    ObjectDecoderInputStream in = new ObjectDecoderInputStream(new AccessibleBufferedInputStream(new ByteArrayInputStream(baos.toByteArray()), 1024), Thread.currentThread().getContextClassLoader(), 1024);
    Object result = in.readObject();
    assertTrue(result instanceof ClobImpl);
}
Also used : DataOutputStream(java.io.DataOutputStream) AccessibleBufferedInputStream(org.teiid.core.util.AccessibleBufferedInputStream) ReaderInputStream(org.teiid.core.util.ReaderInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) InputStreamFactory(org.teiid.core.types.InputStreamFactory) AccessibleBufferedInputStream(org.teiid.core.util.AccessibleBufferedInputStream) ReaderInputStream(org.teiid.core.util.ReaderInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) StringReader(java.io.StringReader) ClobImpl(org.teiid.core.types.ClobImpl) Test(org.junit.Test)

Example 7 with InputStreamFactory

use of org.teiid.core.types.InputStreamFactory in project teiid by teiid.

the class SizeUtility method getSize.

/**
 * Get size of object
 * @return Size in bytes
 */
public static long getSize(Object obj, boolean accountForValueCache) {
    if (obj == null) {
        return 0;
    }
    Class<? extends Object> clazz = obj.getClass();
    if (clazz == DataTypeManager.DefaultDataClasses.STRING) {
        int length = ((String) obj).length();
        if (length > 0) {
            return alignMemory(40 + (2 * length));
        }
        return 40;
    } else if (clazz == DataTypeManager.DefaultDataClasses.VARBINARY) {
        int length = ((BinaryType) obj).getLength();
        if (length > 0) {
            return alignMemory(16 + length);
        }
        return 16;
    } else if (clazz == DataTypeManager.DefaultDataClasses.BIG_DECIMAL) {
        int bitLength = ((BigDecimal) obj).unscaledValue().bitLength();
        // TODO: this does not account for the possibility of a cached string
        long result = 88 + alignMemory(4 + (bitLength >> 3));
        return result;
    } else if (clazz == DataTypeManager.DefaultDataClasses.BIG_INTEGER) {
        int bitLength = ((BigInteger) obj).bitLength();
        long result = 40 + alignMemory(4 + (bitLength >> 3));
        return result;
    } else if (obj instanceof Iterable<?>) {
        Iterable<?> i = (Iterable<?>) obj;
        long total = 16;
        for (Object object : i) {
            total += getSize(object, false) + REFERENCE_SIZE;
        }
        return total;
    } else if (clazz.isArray() || obj instanceof ArrayImpl) {
        int overhead = 0;
        if (obj instanceof ArrayImpl) {
            obj = ((ArrayImpl) obj).getValues();
            clazz = obj.getClass();
            overhead += 2 * REFERENCE_SIZE;
        }
        Class<?> componentType = clazz.getComponentType();
        if (!componentType.isPrimitive()) {
            Object[] rows = (Object[]) obj;
            // Array overhead
            long total = overhead + 16 + alignMemory(rows.length * REFERENCE_SIZE);
            for (int i = 0; i < rows.length; i++) {
                total += getSize(rows[i], false);
            }
            return total;
        }
        int length = Array.getLength(obj);
        int primitiveSize = 8;
        if (componentType == boolean.class) {
            primitiveSize = 4;
        } else if (componentType == byte.class) {
            primitiveSize = 1;
        } else if (componentType == short.class) {
            primitiveSize = 2;
        } else if (componentType == int.class || componentType == float.class) {
            primitiveSize = 4;
        }
        return overhead + alignMemory(length * primitiveSize) + 16;
    } else if (obj instanceof Streamable<?>) {
        try {
            Streamable<?> s = (Streamable) obj;
            Object o = s.getReference();
            if (o instanceof BaseLob) {
                InputStreamFactory isf = ((BaseLob) o).getStreamFactory();
                if (isf.getStorageMode() == StorageMode.MEMORY) {
                    long length = isf.getLength();
                    if (length >= 0) {
                        return 40 + alignMemory(length);
                    }
                } else if (isf.getStorageMode() == StorageMode.PERSISTENT) {
                    long length = isf.getLength();
                    return 40 + alignMemory(Math.min(DataTypeManager.MAX_LOB_MEMORY_BYTES, length));
                }
            }
        } catch (Exception e) {
        }
    } else {
        if (SIZE_ESTIMATES.containsKey(clazz)) {
            return getSize(accountForValueCache, clazz);
        }
        // assume we can get a plausable estimate from the serialized size
        if (obj instanceof Serializable) {
            // we're ignoring classloader differences here
            ClassStats stats = objectEstimates.get(clazz.getName());
            if (stats == null) {
                stats = new ClassStats();
                objectEstimates.put(clazz.getName(), stats);
            }
            int samples = stats.samples.getAndIncrement();
            if (samples < 1000 || (samples & 1023) == 1023) {
                try {
                    DummyOutputStream os = new DummyOutputStream();
                    ObjectOutputStream oos = new ObjectOutputStream(os) {

                        @Override
                        protected void writeClassDescriptor(ObjectStreamClass desc) throws IOException {
                        }

                        @Override
                        protected void writeStreamHeader() throws IOException {
                        }
                    };
                    oos.writeObject(obj);
                    oos.close();
                    int result = (int) alignMemory(os.getBytes() * 3);
                    if (result > stats.averageSize) {
                        stats.averageSize = (stats.averageSize + result * 2) / 3;
                    } else {
                        stats.averageSize = (stats.averageSize + result) / 2;
                    }
                    return result;
                } catch (Exception e) {
                }
            }
            return stats.averageSize;
        }
    }
    return getSize(accountForValueCache, clazz);
}
Also used : Serializable(java.io.Serializable) ArrayImpl(org.teiid.core.types.ArrayImpl) BaseLob(org.teiid.core.types.BaseLob) InputStreamFactory(org.teiid.core.types.InputStreamFactory) ObjectOutputStream(java.io.ObjectOutputStream) IOException(java.io.IOException) ObjectStreamClass(java.io.ObjectStreamClass) Streamable(org.teiid.core.types.Streamable) ObjectStreamClass(java.io.ObjectStreamClass)

Example 8 with InputStreamFactory

use of org.teiid.core.types.InputStreamFactory in project teiid by teiid.

the class LobManager method persistLob.

public static void persistLob(final Streamable<?> lob, final FileStore store, byte[] bytes, boolean inlineLobs, int maxMemoryBytes) throws TeiidComponentException {
    long byteLength = Integer.MAX_VALUE;
    try {
        byteLength = lob.length() * (lob instanceof ClobType ? 2 : 1);
    } catch (SQLException e) {
    // just ignore for now - for a single read resource computing the length invalidates
    // TODO - inline small persisted lobs
    }
    try {
        // inline
        if (lob.getReferenceStreamId() == null || (inlineLobs && (byteLength <= maxMemoryBytes))) {
            lob.setReferenceStreamId(null);
            if (InputStreamFactory.getStorageMode(lob) == StorageMode.MEMORY) {
                return;
            }
            if (lob instanceof BlobType) {
                BlobType b = (BlobType) lob;
                byte[] blobBytes = b.getBytes(1, (int) byteLength);
                b.setReference(new SerialBlob(blobBytes));
            } else if (lob instanceof ClobType) {
                ClobType c = (ClobType) lob;
                // $NON-NLS-1$
                String s = "";
                // some clob impls return null for 0 length
                if (byteLength != 0) {
                    s = c.getSubString(1, (int) (byteLength >>> 1));
                }
                c.setReference(new ClobImpl(s));
            } else {
                XMLType x = (XMLType) lob;
                String s = x.getString();
                x.setReference(new SQLXMLImpl(s));
            }
            return;
        }
        InputStream is = null;
        if (lob instanceof BlobType) {
            is = new BlobInputStreamFactory((Blob) lob).getInputStream();
        } else if (lob instanceof ClobType) {
            is = new ClobInputStreamFactory((Clob) lob).getInputStream();
        } else {
            is = new SQLXMLInputStreamFactory((SQLXML) lob).getInputStream();
        }
        long offset = store.getLength();
        OutputStream fsos = store.createOutputStream();
        byteLength = ObjectConverterUtil.write(fsos, is, bytes, -1);
        // re-construct the new lobs based on the file store
        final long lobOffset = offset;
        final long lobLength = byteLength;
        /*
			 * Using an inner class here will hold a reference to the LobManager
			 * which prevents the removal of the FileStore until all of the
			 * lobs have been gc'd
			 */
        InputStreamFactory isf = new InputStreamFactory() {

            @Override
            public InputStream getInputStream() throws IOException {
                return store.createInputStream(lobOffset, lobLength);
            }

            @Override
            public StorageMode getStorageMode() {
                return StorageMode.PERSISTENT;
            }
        };
        isf.setLength(byteLength);
        if (lob instanceof BlobType) {
            ((BlobType) lob).setReference(new BlobImpl(isf));
        } else if (lob instanceof ClobType) {
            long length = -1;
            try {
                length = ((ClobType) lob).length();
            } catch (SQLException e) {
            // could be streaming
            }
            ((ClobType) lob).setReference(new ClobImpl(isf, length));
        } else {
            ((XMLType) lob).setReference(new SQLXMLImpl(isf));
        }
    } catch (SQLException e) {
        throw new TeiidComponentException(QueryPlugin.Event.TEIID30037, e);
    } catch (IOException e) {
        throw new TeiidComponentException(QueryPlugin.Event.TEIID30036, e);
    }
}
Also used : SQLXMLInputStreamFactory(org.teiid.core.types.InputStreamFactory.SQLXMLInputStreamFactory) SQLXMLImpl(org.teiid.core.types.SQLXMLImpl) ClobInputStreamFactory(org.teiid.core.types.InputStreamFactory.ClobInputStreamFactory) SQLException(java.sql.SQLException) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) SerialBlob(javax.sql.rowset.serial.SerialBlob) IOException(java.io.IOException) InputStreamFactory(org.teiid.core.types.InputStreamFactory) ClobInputStreamFactory(org.teiid.core.types.InputStreamFactory.ClobInputStreamFactory) BlobInputStreamFactory(org.teiid.core.types.InputStreamFactory.BlobInputStreamFactory) SQLXMLInputStreamFactory(org.teiid.core.types.InputStreamFactory.SQLXMLInputStreamFactory) BlobInputStreamFactory(org.teiid.core.types.InputStreamFactory.BlobInputStreamFactory) ClobType(org.teiid.core.types.ClobType) XMLType(org.teiid.core.types.XMLType) BlobType(org.teiid.core.types.BlobType) SQLXML(java.sql.SQLXML) TeiidComponentException(org.teiid.core.TeiidComponentException) Clob(java.sql.Clob) ClobImpl(org.teiid.core.types.ClobImpl) BlobImpl(org.teiid.core.types.BlobImpl)

Example 9 with InputStreamFactory

use of org.teiid.core.types.InputStreamFactory in project teiid by teiid.

the class MongoDBExecutionFactory method retrieveValue.

/**
 * @param field
 * @param expectedClass
 * @return
 * @throws TranslatorException
 */
public Object retrieveValue(Object value, Class<?> expectedClass, DB mongoDB, String fqn, String colName) throws TranslatorException {
    if (value == null) {
        return null;
    }
    if (value.getClass().equals(expectedClass)) {
        return value;
    }
    if (value instanceof DBRef) {
        Object obj = ((DBRef) value).getId();
        if (obj instanceof BasicDBObject) {
            BasicDBObject bdb = (BasicDBObject) obj;
            return bdb.get(colName);
        }
        return obj;
    } else if (value instanceof java.util.Date && expectedClass.equals(java.sql.Date.class)) {
        return new java.sql.Date(((java.util.Date) value).getTime());
    } else if (value instanceof java.util.Date && expectedClass.equals(java.sql.Timestamp.class)) {
        return new java.sql.Timestamp(((java.util.Date) value).getTime());
    } else if (value instanceof java.util.Date && expectedClass.equals(java.sql.Time.class)) {
        return new java.sql.Time(((java.util.Date) value).getTime());
    } else if (value instanceof String && expectedClass.equals(BigDecimal.class)) {
        return new BigDecimal((String) value);
    } else if (value instanceof String && expectedClass.equals(BigInteger.class)) {
        return new BigInteger((String) value);
    } else if (value instanceof String && expectedClass.equals(Character.class)) {
        return new Character(((String) value).charAt(0));
    } else if (value instanceof String && expectedClass.equals(BinaryType.class)) {
        return new BinaryType(((String) value).getBytes());
    } else if (value instanceof String && expectedClass.equals(Blob.class)) {
        GridFS gfs = new GridFS(mongoDB, fqn);
        final GridFSDBFile resource = gfs.findOne((String) value);
        if (resource == null) {
            return null;
        }
        return new BlobImpl(new InputStreamFactory() {

            @Override
            public InputStream getInputStream() throws IOException {
                return resource.getInputStream();
            }
        });
    } else if (value instanceof String && expectedClass.equals(Clob.class)) {
        GridFS gfs = new GridFS(mongoDB, fqn);
        final GridFSDBFile resource = gfs.findOne((String) value);
        if (resource == null) {
            return null;
        }
        return new ClobImpl(new InputStreamFactory() {

            @Override
            public InputStream getInputStream() throws IOException {
                return resource.getInputStream();
            }
        }, -1);
    } else if (value instanceof String && expectedClass.equals(SQLXML.class)) {
        GridFS gfs = new GridFS(mongoDB, fqn);
        final GridFSDBFile resource = gfs.findOne((String) value);
        if (resource == null) {
            return null;
        }
        return new SQLXMLImpl(new InputStreamFactory() {

            @Override
            public InputStream getInputStream() throws IOException {
                return resource.getInputStream();
            }
        });
    } else if (value instanceof BasicDBList) {
        BasicDBList arrayValues = (BasicDBList) value;
        // array
        if (expectedClass.isArray() && !(arrayValues.get(0) instanceof BasicDBObject)) {
            Class arrayType = expectedClass.getComponentType();
            Object array = Array.newInstance(arrayType, arrayValues.size());
            for (int i = 0; i < arrayValues.size(); i++) {
                Object arrayItem = retrieveValue(arrayValues.get(i), arrayType, mongoDB, fqn, colName);
                Array.set(array, i, arrayItem);
            }
            value = array;
        }
    } else if (value instanceof org.bson.types.ObjectId) {
        org.bson.types.ObjectId id = (org.bson.types.ObjectId) value;
        value = id.toHexString();
    } else {
        Transform transform = DataTypeManager.getTransform(value.getClass(), expectedClass);
        if (transform != null) {
            try {
                value = transform.transform(value, expectedClass);
            } catch (TransformationException e) {
                throw new TranslatorException(e);
            }
        }
    }
    return value;
}
Also used : InputStreamFactory(org.teiid.core.types.InputStreamFactory) BasicDBObject(com.mongodb.BasicDBObject) BlobImpl(org.teiid.core.types.BlobImpl) ClobImpl(org.teiid.core.types.ClobImpl) Blob(java.sql.Blob) SQLXMLImpl(org.teiid.core.types.SQLXMLImpl) TransformationException(org.teiid.core.types.TransformationException) BinaryType(org.teiid.core.types.BinaryType) InputStream(java.io.InputStream) DBRef(com.mongodb.DBRef) IOException(java.io.IOException) GridFS(com.mongodb.gridfs.GridFS) BigDecimal(java.math.BigDecimal) BasicDBList(com.mongodb.BasicDBList) SQLXML(java.sql.SQLXML) GridFSDBFile(com.mongodb.gridfs.GridFSDBFile) BigInteger(java.math.BigInteger) BasicDBObject(com.mongodb.BasicDBObject) Transform(org.teiid.core.types.Transform)

Example 10 with InputStreamFactory

use of org.teiid.core.types.InputStreamFactory in project teiid by teiid.

the class WSProcedureExecution method getOutputParameterValues.

@Override
public List<?> getOutputParameterValues() throws TranslatorException {
    Object result = returnValue;
    if (returnValue != null && (returnValue instanceof StAXSource) && procedure.getArguments().size() > 4 && procedure.getArguments().get(4).getDirection() == Direction.IN && Boolean.TRUE.equals(procedure.getArguments().get(4).getArgumentValue().getValue())) {
        SQLXMLImpl sqlXml = new StAXSQLXML((StAXSource) returnValue);
        XMLType xml = new XMLType(sqlXml);
        xml.setType(Type.DOCUMENT);
        result = xml;
    } else if (returnValue != null && returnValue instanceof DOMSource) {
        final DOMSource xmlSource = (DOMSource) returnValue;
        SQLXMLImpl sqlXml = new SQLXMLImpl(new InputStreamFactory() {

            @Override
            public InputStream getInputStream() throws IOException {
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                Result outputTarget = new StreamResult(outputStream);
                try {
                    TransformerFactory.newInstance().newTransformer().transform(xmlSource, outputTarget);
                } catch (Exception e) {
                    throw new IOException(e);
                }
                return new ByteArrayInputStream(outputStream.toByteArray());
            }
        });
        XMLType xml = new XMLType(sqlXml);
        xml.setType(Type.DOCUMENT);
        result = xml;
    }
    return Arrays.asList(result);
}
Also used : SQLXMLImpl(org.teiid.core.types.SQLXMLImpl) DOMSource(javax.xml.transform.dom.DOMSource) StreamResult(javax.xml.transform.stream.StreamResult) StAXSource(javax.xml.transform.stax.StAXSource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) InputStreamFactory(org.teiid.core.types.InputStreamFactory) TransformerException(javax.xml.transform.TransformerException) SQLException(java.sql.SQLException) XMLStreamException(javax.xml.stream.XMLStreamException) TranslatorException(org.teiid.translator.TranslatorException) DataNotAvailableException(org.teiid.translator.DataNotAvailableException) IOException(java.io.IOException) WebServiceException(javax.xml.ws.WebServiceException) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) XMLType(org.teiid.core.types.XMLType) ByteArrayInputStream(java.io.ByteArrayInputStream) StAXSQLXML(org.teiid.util.StAXSQLXML)

Aggregations

InputStreamFactory (org.teiid.core.types.InputStreamFactory)27 ByteArrayInputStream (java.io.ByteArrayInputStream)13 IOException (java.io.IOException)12 BlobImpl (org.teiid.core.types.BlobImpl)12 InputStream (java.io.InputStream)11 ClobImpl (org.teiid.core.types.ClobImpl)11 Test (org.junit.Test)10 BlobType (org.teiid.core.types.BlobType)8 SQLException (java.sql.SQLException)7 SQLXMLImpl (org.teiid.core.types.SQLXMLImpl)7 ClobType (org.teiid.core.types.ClobType)6 Blob (java.sql.Blob)5 ArrayList (java.util.ArrayList)5 FileStoreInputStreamFactory (org.teiid.common.buffer.FileStoreInputStreamFactory)4 StringReader (java.io.StringReader)3 BinaryType (org.teiid.core.types.BinaryType)3 ReaderInputStream (org.teiid.core.util.ReaderInputStream)3 N1qlQueryRow (com.couchbase.client.java.query.N1qlQueryRow)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 BigInteger (java.math.BigInteger)2