Search in sources :

Example 11 with InputStreamFactory

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

the class CommandContext method close.

public void close() {
    synchronized (this.globalState) {
        if (this.globalState.reservedBuffers > 0) {
            long toRelease = this.globalState.reservedBuffers;
            this.globalState.reservedBuffers = 0;
            this.globalState.bufferManager.releaseOrphanedBuffers(toRelease);
        }
        if (this.globalState.reusableExecutions != null) {
            for (List<ReusableExecution<?>> reusableExecutions : this.globalState.reusableExecutions.values()) {
                for (ReusableExecution<?> reusableExecution : reusableExecutions) {
                    try {
                        reusableExecution.dispose();
                    } catch (Exception e) {
                        LogManager.logWarning(LogConstants.CTX_DQP, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30030));
                    }
                }
            }
            this.globalState.reusableExecutions.clear();
        }
        if (this.globalState.commandListeners != null) {
            for (CommandListener listener : this.globalState.commandListeners) {
                try {
                    listener.commandClosed(this);
                } catch (Exception e) {
                    LogManager.logWarning(LogConstants.CTX_DQP, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30031));
                }
            }
            this.globalState.commandListeners.clear();
        }
        if (this.globalState.lookups != null) {
            for (TupleSource ts : this.globalState.lookups.values()) {
                ts.closeSource();
            }
            this.globalState.lookups = null;
        }
        if (this.globalState.created != null) {
            for (InputStreamFactory isf : this.globalState.created) {
                try {
                    isf.free();
                } catch (IOException e) {
                }
            }
            this.globalState.created.clear();
        }
    }
}
Also used : CommandListener(org.teiid.CommandListener) TupleSource(org.teiid.common.buffer.TupleSource) ReusableExecution(org.teiid.translator.ReusableExecution) IOException(java.io.IOException) InputStreamFactory(org.teiid.core.types.InputStreamFactory) QueryProcessingException(org.teiid.api.exception.query.QueryProcessingException) TeiidComponentException(org.teiid.core.TeiidComponentException) TeiidException(org.teiid.core.TeiidException) TeiidSQLException(org.teiid.jdbc.TeiidSQLException) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Example 12 with InputStreamFactory

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

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

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

the class PreparedStatementRequest method createStreamCopy.

/**
 * embedded lobs can be sent with just a reference to a stream,
 * create a copy instead
 * @param context
 * @param i
 * @param param
 * @param value
 * @throws QueryResolverException
 */
private static void createStreamCopy(CommandContext context, int i, Reference param, Object value) throws QueryResolverException {
    try {
        InputStreamFactory isf = ((BaseLob) value).getStreamFactory();
        InputStream initial = isf.getInputStream();
        InputStream other = isf.getInputStream();
        if (initial == other) {
            // this violates the expectation that the inputstream is a new instance,
            // $NON-NLS-1$
            FileStore fs = context.getBufferManager().createFileStore("bytes");
            FileStoreInputStreamFactory fsisf = new FileStoreInputStreamFactory(fs, Streamable.ENCODING);
            SaveOnReadInputStream is = new SaveOnReadInputStream(initial, fsisf);
            context.addCreatedLob(fsisf);
            ((BaseLob) value).setStreamFactory(is.getInputStreamFactory());
        } else {
            initial.close();
            other.close();
        }
    } catch (SQLException e) {
        // $NON-NLS-1$
        String msg = QueryPlugin.Util.getString("QueryUtil.Error_executing_conversion_function_to_convert_value", i + 1, value, value.getClass(), DataTypeManager.getDataTypeName(param.getType()));
        throw new QueryResolverException(QueryPlugin.Event.TEIID30557, e, msg);
    } catch (IOException e) {
        // $NON-NLS-1$
        String msg = QueryPlugin.Util.getString("QueryUtil.Error_executing_conversion_function_to_convert_value", i + 1, value, value.getClass(), DataTypeManager.getDataTypeName(param.getType()));
        throw new QueryResolverException(QueryPlugin.Event.TEIID30557, e, msg);
    }
}
Also used : FileStore(org.teiid.common.buffer.FileStore) FileStoreInputStreamFactory(org.teiid.common.buffer.FileStoreInputStreamFactory) SQLException(java.sql.SQLException) InputStream(java.io.InputStream) BaseLob(org.teiid.core.types.BaseLob) IOException(java.io.IOException) InputStreamFactory(org.teiid.core.types.InputStreamFactory) FileStoreInputStreamFactory(org.teiid.common.buffer.FileStoreInputStreamFactory) QueryResolverException(org.teiid.api.exception.query.QueryResolverException)

Example 15 with InputStreamFactory

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

the class TestLobManager method testInlining.

@Test
public void testInlining() 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("small"), Charset.forName(Streamable.ENCODING));
        }
    }, 5));
    assertEquals(StorageMode.OTHER, InputStreamFactory.getStorageMode(clob));
    LobManager lobManager = new LobManager(new int[] { 0 }, fs);
    lobManager.updateReferences(Arrays.asList(clob), ReferenceMode.CREATE);
    assertEquals(StorageMode.MEMORY, InputStreamFactory.getStorageMode(clob));
}
Also used : ClobType(org.teiid.core.types.ClobType) ReaderInputStream(org.teiid.core.util.ReaderInputStream) StringReader(java.io.StringReader) InputStreamFactory(org.teiid.core.types.InputStreamFactory) ClobImpl(org.teiid.core.types.ClobImpl) Test(org.junit.Test)

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