Search in sources :

Example 6 with BlobImpl

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

the class MySQLExecutionFactory method toGeometryType.

/**
 * It appears that mysql will actually return a byte array or a blob backed by a byte array
 * but just to be safe we'll assume that there may be true blob and that we should back the
 * geometry value with that blob.
 * @param val
 * @return
 * @throws SQLException
 */
GeometryType toGeometryType(final Blob val) throws SQLException {
    if (val == null) {
        return null;
    }
    // create a wrapper for that will handle the srid
    long length = val.length() - 4;
    InputStreamFactory streamFactory = new InputStreamFactory() {

        @Override
        public InputStream getInputStream() throws IOException {
            InputStream is;
            try {
                is = val.getBinaryStream();
            } catch (SQLException e) {
                throw new IOException(e);
            }
            for (int i = 0; i < 4; i++) {
                is.read();
            }
            return is;
        }
    };
    // read the little endian srid
    InputStream is = val.getBinaryStream();
    int srid = 0;
    try {
        for (int i = 0; i < 4; i++) {
            try {
                int b = is.read();
                srid += (b << i * 8);
            } catch (IOException e) {
                // could not determine srid
                srid = GeometryType.UNKNOWN_SRID;
            }
        }
    } finally {
        try {
            is.close();
        } catch (IOException e) {
        // i
        }
    }
    streamFactory.setLength(length);
    Blob b = new BlobImpl(streamFactory);
    GeometryType geom = new GeometryType(b);
    geom.setSrid(srid);
    return geom;
}
Also used : GeometryType(org.teiid.core.types.GeometryType) Blob(java.sql.Blob) SQLException(java.sql.SQLException) InputStream(java.io.InputStream) IOException(java.io.IOException) InputStreamFactory(org.teiid.core.types.InputStreamFactory) BlobImpl(org.teiid.core.types.BlobImpl)

Example 7 with BlobImpl

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

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

the class StringAgg method getResult.

/**
 * @see org.teiid.query.function.aggregate.AggregateFunction#getResult(CommandContext)
 */
public Object getResult(CommandContext commandContext) throws TeiidProcessingException {
    if (this.result == null) {
        this.result = buildResult(commandContext);
    }
    try {
        this.result.getWriter().close();
        FileStoreOutputStream fs = this.result.getOuputStream();
        fs.close();
        if (binary) {
            if (fs.bytesWritten()) {
                return new BlobType(new BlobImpl(result));
            }
            return new BlobType(new SerialBlob(Arrays.copyOf(fs.getBuffer(), fs.getCount())));
        }
        if (fs.bytesWritten()) {
            return new ClobType(new ClobImpl(result, -1));
        }
        return new ClobType(new ClobImpl(new String(Arrays.copyOf(fs.getBuffer(), fs.getCount()), Streamable.ENCODING)));
    } catch (IOException e) {
        throw new TeiidProcessingException(QueryPlugin.Event.TEIID30422, e);
    } catch (SQLException e) {
        throw new TeiidProcessingException(QueryPlugin.Event.TEIID30423, e);
    }
}
Also used : ClobType(org.teiid.core.types.ClobType) BlobType(org.teiid.core.types.BlobType) SQLException(java.sql.SQLException) SerialBlob(javax.sql.rowset.serial.SerialBlob) FileStoreOutputStream(org.teiid.common.buffer.FileStore.FileStoreOutputStream) IOException(java.io.IOException) BlobImpl(org.teiid.core.types.BlobImpl) ClobImpl(org.teiid.core.types.ClobImpl) TeiidProcessingException(org.teiid.core.TeiidProcessingException)

Example 9 with BlobImpl

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

the class TextAgg method getResult.

/**
 * @see org.teiid.query.function.aggregate.AggregateFunction#getResult(CommandContext)
 */
public Object getResult(CommandContext commandContext) throws TeiidProcessingException {
    if (this.result == null) {
        this.result = buildResult(commandContext);
    }
    try {
        FileStoreOutputStream fs = this.result.getOuputStream();
        fs.close();
        if (fs.bytesWritten()) {
            return new BlobType(new BlobImpl(result));
        }
        return new BlobType(new SerialBlob(Arrays.copyOf(fs.getBuffer(), fs.getCount())));
    } catch (IOException e) {
        throw new TeiidProcessingException(QueryPlugin.Event.TEIID30422, e);
    } catch (SQLException e) {
        throw new TeiidProcessingException(QueryPlugin.Event.TEIID30423, e);
    }
}
Also used : BlobType(org.teiid.core.types.BlobType) SQLException(java.sql.SQLException) SerialBlob(javax.sql.rowset.serial.SerialBlob) FileStoreOutputStream(org.teiid.common.buffer.FileStore.FileStoreOutputStream) IOException(java.io.IOException) BlobImpl(org.teiid.core.types.BlobImpl) TeiidProcessingException(org.teiid.core.TeiidProcessingException)

Example 10 with BlobImpl

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

the class TestLobManager method testLobPeristence.

@Test
public void testLobPeristence() throws Exception {
    BufferManager buffMgr = BufferManagerFactory.getStandaloneBufferManager();
    FileStore fs = buffMgr.createFileStore("temp");
    ClobType clob = new ClobType(new ClobImpl(new InputStreamFactory() {

        @Override
        public InputStream getInputStream() throws IOException {
            return new ReaderInputStream(new StringReader("Clob contents One"), Charset.forName(Streamable.ENCODING));
        }
    }, -1));
    BlobType blob = new BlobType(new BlobImpl(new InputStreamFactory() {

        @Override
        public InputStream getInputStream() throws IOException {
            return new ReaderInputStream(new StringReader("Blob contents Two"), Charset.forName(Streamable.ENCODING));
        }
    }));
    BlobType blobEmpty = new BlobType(new BlobImpl(new InputStreamFactory() {

        @Override
        public InputStream getInputStream() throws IOException {
            return new ByteArrayInputStream(new byte[0]);
        }
    }));
    FileStore fs1 = buffMgr.createFileStore("blob");
    FileStoreInputStreamFactory fsisf = new FileStoreInputStreamFactory(fs1, Streamable.ENCODING);
    FileStoreOutputStream fsos = fsisf.getOuputStream();
    byte[] b = new byte[DataTypeManager.MAX_LOB_MEMORY_BYTES + 1];
    fsos.write(b);
    fsos.close();
    BlobType blob1 = new BlobType(new BlobImpl(fsisf));
    assertNotNull(blob1.getReferenceStreamId());
    LobManager lobManager = new LobManager(new int[] { 0, 1, 2, 3 }, fs);
    lobManager.setMaxMemoryBytes(4);
    List<?> tuple = Arrays.asList(clob, blob, blob1, blobEmpty);
    lobManager.updateReferences(tuple, ReferenceMode.CREATE);
    assertNotNull(blob1.getReferenceStreamId());
    lobManager.persist();
    Streamable<?> lob = lobManager.getLobReference(clob.getReferenceStreamId());
    assertTrue(lob.getClass().isAssignableFrom(ClobType.class));
    ClobType clobRead = (ClobType) lob;
    assertEquals(ClobType.getString(clob), ClobType.getString(clobRead));
    assertTrue(clobRead.length() != -1);
    lob = lobManager.getLobReference(blob.getReferenceStreamId());
    assertTrue(lob.getClass().isAssignableFrom(BlobType.class));
    BlobType blobRead = (BlobType) lob;
    assertTrue(Arrays.equals(ObjectConverterUtil.convertToByteArray(blob.getBinaryStream()), ObjectConverterUtil.convertToByteArray(blobRead.getBinaryStream())));
    lobManager.updateReferences(tuple, ReferenceMode.REMOVE);
    assertEquals(0, lobManager.getLobCount());
}
Also used : FileStoreOutputStream(org.teiid.common.buffer.FileStore.FileStoreOutputStream) InputStreamFactory(org.teiid.core.types.InputStreamFactory) ClobType(org.teiid.core.types.ClobType) ReaderInputStream(org.teiid.core.util.ReaderInputStream) BlobType(org.teiid.core.types.BlobType) ByteArrayInputStream(java.io.ByteArrayInputStream) StringReader(java.io.StringReader) ClobImpl(org.teiid.core.types.ClobImpl) BlobImpl(org.teiid.core.types.BlobImpl) Test(org.junit.Test)

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