Search in sources :

Example 1 with BlobType

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

the class S3ProcedureExecution method next.

@Override
public List<?> next() throws TranslatorException, DataNotAvailableException {
    if (this.command.getProcedureName().equalsIgnoreCase(S3ExecutionFactory.SAVEFILE) || this.command.getProcedureName().equalsIgnoreCase(S3ExecutionFactory.DELETEFILE)) {
        return null;
    }
    if (this.execution == null || this.execution.getResponseCode() < 200 || this.execution.getResponseCode() > 300) {
        return null;
    }
    Blob contents = (Blob) execution.getOutputParameterValues().get(0);
    BlobInputStreamFactory isf = new BlobInputStreamFactory(contents);
    String length = getHeader("Content-Length");
    if (length != null) {
        isf.setLength(Long.parseLong(length));
    }
    Object value = null;
    if (isText) {
        ClobImpl clob = new ClobImpl(isf, -1);
        clob.setCharset(Charset.forName(this.ef.getEncoding()));
        value = new ClobType(clob);
        if (!streaming) {
            value = new InputStreamFactory.ClobInputStreamFactory(clob);
        }
    } else {
        if (streaming) {
            value = new BlobType(contents);
        } else {
            value = isf;
        }
    }
    String lastModified = getHeader("Last-Modified");
    ArrayList<Object> result = new ArrayList<Object>(2);
    result.add(value);
    if (!isList) {
        result.add(endpoint);
        try {
            SimpleDateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss zzz");
            result.add(lastModified == null ? null : new Timestamp(df.parse(lastModified).getTime()));
        } catch (ParseException e) {
            result.add(null);
        }
        result.add(getHeader("ETag"));
        result.add(length);
    }
    this.execution = null;
    return result;
}
Also used : Blob(java.sql.Blob) ArrayList(java.util.ArrayList) InputStreamFactory(org.teiid.core.types.InputStreamFactory) BlobInputStreamFactory(org.teiid.core.types.InputStreamFactory.BlobInputStreamFactory) Timestamp(java.sql.Timestamp) BlobInputStreamFactory(org.teiid.core.types.InputStreamFactory.BlobInputStreamFactory) ClobType(org.teiid.core.types.ClobType) BlobType(org.teiid.core.types.BlobType) ParseException(java.text.ParseException) ClobImpl(org.teiid.core.types.ClobImpl) SimpleDateFormat(java.text.SimpleDateFormat)

Example 2 with BlobType

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

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

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

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

the class TestDQPCore method testLobConcurrency.

@Test
public void testLobConcurrency() throws Exception {
    RequestMessage reqMsg = exampleRequestMessage("select to_bytes(stringkey, 'utf-8') FROM BQT1.SmallA");
    reqMsg.setTxnAutoWrapMode(RequestMessage.TXN_WRAP_OFF);
    agds.setSleep(100);
    ResultsFuture<ResultsMessage> message = core.executeRequest(reqMsg.getExecutionId(), reqMsg);
    final LobThread t = new LobThread(reqMsg);
    t.start();
    message.addCompletionListener(new ResultsFuture.CompletionListener<ResultsMessage>() {

        @Override
        public void onCompletion(ResultsFuture<ResultsMessage> future) {
            try {
                final BlobType bt = (BlobType) future.get().getResultsList().get(0).get(0);
                synchronized (t) {
                    t.bt = bt;
                    t.workContext = DQPWorkContext.getWorkContext();
                    t.notify();
                }
                // give the Thread a chance to run
                Thread.sleep(100);
            } catch (Exception e) {
                t.interrupt();
                throw new RuntimeException(e);
            }
        }
    });
    message.get();
    t.join();
    assertNotNull(t.chunkFuture.get().getBytes());
}
Also used : ResultsFuture(org.teiid.client.util.ResultsFuture) BlobType(org.teiid.core.types.BlobType) ResultsMessage(org.teiid.client.ResultsMessage) RequestMessage(org.teiid.client.RequestMessage) TimeoutException(java.util.concurrent.TimeoutException) TeiidProcessingException(org.teiid.core.TeiidProcessingException) QueryResolverException(org.teiid.api.exception.query.QueryResolverException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Aggregations

BlobType (org.teiid.core.types.BlobType)23 BlobImpl (org.teiid.core.types.BlobImpl)11 InputStreamFactory (org.teiid.core.types.InputStreamFactory)10 Test (org.junit.Test)9 ByteArrayInputStream (java.io.ByteArrayInputStream)7 IOException (java.io.IOException)7 SerialBlob (javax.sql.rowset.serial.SerialBlob)7 Clob (java.sql.Clob)6 SQLException (java.sql.SQLException)6 ClobImpl (org.teiid.core.types.ClobImpl)6 ClobType (org.teiid.core.types.ClobType)6 XMLType (org.teiid.core.types.XMLType)6 InputStream (java.io.InputStream)5 Blob (java.sql.Blob)5 ArrayList (java.util.ArrayList)4 ReaderInputStream (org.teiid.core.util.ReaderInputStream)4 SerialClob (javax.sql.rowset.serial.SerialClob)3 FileStoreOutputStream (org.teiid.common.buffer.FileStore.FileStoreOutputStream)3 SQLXMLImpl (org.teiid.core.types.SQLXMLImpl)3 N1qlQueryRow (com.couchbase.client.java.query.N1qlQueryRow)2