Search in sources :

Example 6 with BlobType

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

the class TestConnectorWorkItem method testTypeConversion.

@Test
public void testTypeConversion() throws Exception {
    BufferManager bm = BufferManagerFactory.getStandaloneBufferManager();
    String str = "hello world";
    Object source = new StreamSource(new StringReader(str));
    XMLType xml = (XMLType) ConnectorWorkItem.convertToRuntimeType(bm, source, DataTypeManager.DefaultDataClasses.XML, null);
    assertEquals(str, xml.getString());
    source = new StAXSource(XMLType.getXmlInputFactory().createXMLEventReader(new StringReader("<a/>")));
    xml = (XMLType) ConnectorWorkItem.convertToRuntimeType(bm, source, DataTypeManager.DefaultDataClasses.XML, null);
    XMLInputFactory in = XMLType.getXmlInputFactory();
    XMLStreamReader reader = in.createXMLStreamReader(new StringReader(xml.getString()));
    assertEquals(XMLEvent.START_DOCUMENT, reader.getEventType());
    assertEquals(XMLEvent.START_ELEMENT, reader.next());
    assertEquals("a", reader.getLocalName());
    assertEquals(XMLEvent.END_ELEMENT, reader.next());
    byte[] bytes = str.getBytes(Streamable.ENCODING);
    source = new InputStreamFactory.BlobInputStreamFactory(BlobType.createBlob(bytes));
    BlobType blob = (BlobType) ConnectorWorkItem.convertToRuntimeType(bm, source, DataTypeManager.DefaultDataClasses.BLOB, null);
    assertArrayEquals(bytes, ObjectConverterUtil.convertToByteArray(blob.getBinaryStream()));
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) StreamSource(javax.xml.transform.stream.StreamSource) StAXSource(javax.xml.transform.stax.StAXSource) BufferManager(org.teiid.common.buffer.BufferManager) InputStreamFactory(org.teiid.core.types.InputStreamFactory) XMLType(org.teiid.core.types.XMLType) BlobType(org.teiid.core.types.BlobType) StringReader(java.io.StringReader) XMLInputFactory(javax.xml.stream.XMLInputFactory) Test(org.junit.Test)

Example 7 with BlobType

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

the class TeiidServiceHandler method handleLobResult.

private void handleLobResult(String charSet, Object result, ServiceResponse response) throws SQLException {
    if (result == null) {
        // or should this be an empty result?
        return;
    }
    if (result instanceof SQLXML) {
        if (charSet != null) {
            XMLSerialize serialize = new XMLSerialize();
            // $NON-NLS-1$
            serialize.setTypeString("blob");
            serialize.setDeclaration(true);
            serialize.setEncoding(charSet);
            serialize.setDocument(true);
            try {
                InputStream content = ((BlobType) XMLSystemFunctions.serialize(serialize, new XMLType((SQLXML) result))).getBinaryStream();
                response.writeContent(content, 200, false);
                response.writeOK(ContentType.APPLICATION_OCTET_STREAM);
            } catch (TransformationException e) {
                throw new SQLException(e);
            }
        } else {
            InputStream content = ((SQLXML) result).getBinaryStream();
            response.writeContent(content, 200, false);
            response.writeOK(ContentType.APPLICATION_XML);
        }
    } else if (result instanceof Blob) {
        InputStream content = ((Blob) result).getBinaryStream();
        response.writeContent(content, 200, false);
        response.writeOK(ContentType.APPLICATION_OCTET_STREAM);
    } else if (result instanceof Clob) {
        InputStream content = new ReaderInputStream(((Clob) result).getCharacterStream(), charSet == null ? Charset.defaultCharset() : Charset.forName(charSet));
        response.writeContent(content, 200, false);
        response.writeOK(ContentType.TEXT_PLAIN);
    } else {
        InputStream content = new ByteArrayInputStream(result.toString().getBytes(charSet == null ? Charset.defaultCharset() : Charset.forName(charSet)));
        response.writeContent(content, 200, false);
        response.writeOK(ContentType.APPLICATION_OCTET_STREAM);
    }
}
Also used : XMLType(org.teiid.core.types.XMLType) SQLXML(java.sql.SQLXML) XMLSerialize(org.teiid.query.sql.symbol.XMLSerialize) BlobType(org.teiid.core.types.BlobType) TransformationException(org.teiid.core.types.TransformationException) Blob(java.sql.Blob) ReaderInputStream(org.teiid.core.util.ReaderInputStream) SQLException(java.sql.SQLException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ReaderInputStream(org.teiid.core.util.ReaderInputStream) InputStream(java.io.InputStream) Clob(java.sql.Clob)

Example 8 with BlobType

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

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

the class MongoDBDirectQueryExecution method next.

@Override
public List<?> next() throws TranslatorException, DataNotAvailableException {
    final DBObject value = nextRow();
    if (value == null) {
        return null;
    }
    BlobType result = new BlobType(new BlobImpl(new InputStreamFactory() {

        @Override
        public InputStream getInputStream() throws IOException {
            return new ByteArrayInputStream(JSON.serialize(value).getBytes(Streamable.CHARSET));
        }
    }));
    if (returnsArray) {
        List<Object[]> row = new ArrayList<Object[]>(1);
        row.add(new Object[] { result });
        return row;
    }
    return Arrays.asList(result);
}
Also used : BlobType(org.teiid.core.types.BlobType) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) DBObject(com.mongodb.DBObject) InputStreamFactory(org.teiid.core.types.InputStreamFactory) DBObject(com.mongodb.DBObject) BlobImpl(org.teiid.core.types.BlobImpl)

Example 10 with BlobType

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

the class LobWorkItem method createLobStream.

/**
 * Create a object which can create a sequence of LobChunk objects on a given
 * LOB object
 * @throws SQLException
 */
private ByteLobChunkStream createLobStream(String referenceStreamId) throws TeiidComponentException, SQLException {
    // get the reference object in the buffer manager, and try to stream off
    // the original sources.
    Streamable<?> streamable = parent.resultsBuffer.getLobReference(referenceStreamId);
    if (streamable instanceof XMLType) {
        XMLType xml = (XMLType) streamable;
        return new ByteLobChunkStream(xml.getBinaryStream(), chunkSize);
    } else if (streamable instanceof ClobType) {
        ClobType clob = (ClobType) streamable;
        return new ByteLobChunkStream(new ReaderInputStream(clob.getCharacterStream(), Charset.forName(Streamable.ENCODING)), chunkSize);
    }
    BlobType blob = (BlobType) streamable;
    return new ByteLobChunkStream(blob.getBinaryStream(), chunkSize);
}
Also used : ClobType(org.teiid.core.types.ClobType) XMLType(org.teiid.core.types.XMLType) ReaderInputStream(org.teiid.core.util.ReaderInputStream) BlobType(org.teiid.core.types.BlobType)

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