Search in sources :

Example 1 with BlobImpl

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

the class AccumuloDataTypeManager method deserialize.

public static Object deserialize(final byte[] value, final Class<?> expectedType) {
    if (value == null || Arrays.equals(value, EMPTY_BYTES)) {
        return null;
    }
    try {
        if (expectedType.isAssignableFrom(Clob.class)) {
            return new ClobImpl(new InputStreamFactory() {

                @Override
                public InputStream getInputStream() throws IOException {
                    return ObjectConverterUtil.convertToInputStream(value);
                }
            }, -1);
        } else if (expectedType.isAssignableFrom(Blob.class)) {
            return new BlobType(new BlobImpl(new InputStreamFactory() {

                @Override
                public InputStream getInputStream() throws IOException {
                    return ObjectConverterUtil.convertToInputStream(value);
                }
            }));
        } else if (expectedType.isAssignableFrom(SQLXML.class)) {
            return new SQLXMLImpl(new InputStreamFactory() {

                @Override
                public InputStream getInputStream() throws IOException {
                    return ObjectConverterUtil.convertToInputStream(value);
                }
            });
        } else if (expectedType.isAssignableFrom(BinaryType.class)) {
            return new BinaryType(value);
        } else if (expectedType.isAssignableFrom(GeometryType.class)) {
            GeometryType result = new GeometryType(Arrays.copyOf(value, value.length - 4));
            int srid = (((value[value.length - 4] & 0xff) << 24) + ((value[value.length - 3] & 0xff) << 16) + ((value[value.length - 2] & 0xff) << 8) + ((value[value.length - 1] & 0xff) << 0));
            result.setSrid(srid);
            return result;
        } else if (expectedType.isAssignableFrom(byte[].class)) {
            return value;
        } else if (expectedType.isAssignableFrom(String.class) || expectedType.isAssignableFrom(Boolean.class) || expectedType.isAssignableFrom(Boolean.class) || expectedType.isAssignableFrom(Byte.class) || expectedType.isAssignableFrom(Short.class) || expectedType.isAssignableFrom(Character.class) || expectedType.isAssignableFrom(Integer.class) || expectedType.isAssignableFrom(Long.class) || expectedType.isAssignableFrom(BigInteger.class) || expectedType.isAssignableFrom(BigDecimal.class) || expectedType.isAssignableFrom(Float.class) || expectedType.isAssignableFrom(Double.class) || expectedType.isAssignableFrom(Date.class) || expectedType.isAssignableFrom(Time.class) || expectedType.isAssignableFrom(Timestamp.class)) {
            return DataTypeManager.transformValue(new String(value, UTF_8), expectedType);
        } else {
            ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(value));
            Object obj = ois.readObject();
            ois.close();
            return obj;
        }
    } catch (ClassNotFoundException e) {
        throw new TeiidRuntimeException(e);
    } catch (IOException e) {
        throw new TeiidRuntimeException(e);
    } catch (TransformationException e) {
        throw new TeiidRuntimeException(e);
    }
}
Also used : TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) InputStreamFactory(org.teiid.core.types.InputStreamFactory) Timestamp(java.sql.Timestamp) GeometryType(org.teiid.core.types.GeometryType) ClobImpl(org.teiid.core.types.ClobImpl) BlobImpl(org.teiid.core.types.BlobImpl) Blob(java.sql.Blob) SQLXMLImpl(org.teiid.core.types.SQLXMLImpl) TransformationException(org.teiid.core.types.TransformationException) BinaryType(org.teiid.core.types.BinaryType) ObjectInputStream(java.io.ObjectInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) Date(java.sql.Date) BigInteger(java.math.BigInteger) BlobType(org.teiid.core.types.BlobType) ByteArrayInputStream(java.io.ByteArrayInputStream) BigInteger(java.math.BigInteger) ObjectInputStream(java.io.ObjectInputStream)

Example 2 with BlobImpl

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

the class FunctionMethods method toBytes.

@TeiidFunction(category = FunctionCategoryConstants.CONVERSION, name = "to_bytes")
public static BlobType toBytes(ClobType value, String encoding, boolean wellFormed) throws IOException, SQLException {
    Charset cs = getCharset(encoding);
    ClobInputStreamFactory cisf = new ClobInputStreamFactory(value.getReference());
    cisf.setCharset(cs);
    if (!wellFormed || CharsetUtils.BASE64_NAME.equalsIgnoreCase(encoding) || CharsetUtils.HEX_NAME.equalsIgnoreCase(encoding)) {
        // validate that the binary conversion is possible
        // TODO: cache the result in a filestore
        InputStream is = new ReaderInputStream(value.getCharacterStream(), cs.newEncoder().onMalformedInput(CodingErrorAction.REPORT).onUnmappableCharacter(CodingErrorAction.REPORT));
        try {
            while (is.read() != -1) {
            }
        } catch (IOException e) {
            CharacterCodingException cce = ExceptionUtil.getExceptionOfType(e, CharacterCodingException.class);
            if (cce != null) {
                throw new IOException(CorePlugin.Util.gs(CorePlugin.Event.TEIID10083, cs.displayName()), cce);
            }
            throw e;
        } finally {
            is.close();
        }
    }
    return new BlobType(new BlobImpl(cisf));
}
Also used : ReaderInputStream(org.teiid.core.util.ReaderInputStream) BlobType(org.teiid.core.types.BlobType) ClobInputStreamFactory(org.teiid.core.types.InputStreamFactory.ClobInputStreamFactory) ReaderInputStream(org.teiid.core.util.ReaderInputStream) InputStream(java.io.InputStream) Charset(java.nio.charset.Charset) IOException(java.io.IOException) CharacterCodingException(java.nio.charset.CharacterCodingException) BlobImpl(org.teiid.core.types.BlobImpl)

Example 3 with BlobImpl

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

the class GeometryUtils method geometryToEwkb.

/**
 * We'll take the wkb format and add the extended flag/srid
 * @param geometry
 * @return
 */
public static BlobType geometryToEwkb(final GeometryType geometry) {
    final Blob b = geometry.getReference();
    BlobImpl blobImpl = new BlobImpl(new InputStreamFactory() {

        @Override
        public InputStream getInputStream() throws IOException {
            PushbackInputStream pbis;
            try {
                pbis = new PushbackInputStream(b.getBinaryStream(), 9);
            } catch (SQLException e) {
                throw new IOException(e);
            }
            int byteOrder = pbis.read();
            if (byteOrder == -1) {
                return pbis;
            }
            byte[] typeInt = new byte[4];
            int bytesRead = pbis.read(typeInt);
            if (bytesRead == 4) {
                int srid = geometry.getSrid();
                byte[] sridInt = new byte[4];
                ByteOrderValues.putInt(srid, sridInt, byteOrder == 0 ? ByteOrderValues.BIG_ENDIAN : ByteOrderValues.LITTLE_ENDIAN);
                pbis.unread(sridInt);
                typeInt[byteOrder == 0 ? 0 : 3] |= 0x20;
            }
            pbis.unread(typeInt, 0, bytesRead);
            pbis.unread(byteOrder);
            return pbis;
        }
    });
    return new BlobType(blobImpl);
}
Also used : Blob(java.sql.Blob) BlobType(org.teiid.core.types.BlobType) PushbackInputStream(java.io.PushbackInputStream) SQLException(java.sql.SQLException) PushbackInputStream(java.io.PushbackInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) InputStreamFactory(org.teiid.core.types.InputStreamFactory) BlobImpl(org.teiid.core.types.BlobImpl)

Example 4 with BlobImpl

use of org.teiid.core.types.BlobImpl 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 5 with BlobImpl

use of org.teiid.core.types.BlobImpl 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)

Aggregations

BlobImpl (org.teiid.core.types.BlobImpl)15 InputStreamFactory (org.teiid.core.types.InputStreamFactory)12 BlobType (org.teiid.core.types.BlobType)11 IOException (java.io.IOException)9 ClobImpl (org.teiid.core.types.ClobImpl)8 InputStream (java.io.InputStream)6 SQLException (java.sql.SQLException)6 ClobType (org.teiid.core.types.ClobType)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 Blob (java.sql.Blob)5 SQLXMLImpl (org.teiid.core.types.SQLXMLImpl)5 ArrayList (java.util.ArrayList)3 SerialBlob (javax.sql.rowset.serial.SerialBlob)3 FileStoreOutputStream (org.teiid.common.buffer.FileStore.FileStoreOutputStream)3 BinaryType (org.teiid.core.types.BinaryType)3 GeometryType (org.teiid.core.types.GeometryType)3 N1qlQueryRow (com.couchbase.client.java.query.N1qlQueryRow)2 BigInteger (java.math.BigInteger)2 SQLXML (java.sql.SQLXML)2 Test (org.junit.Test)2