Search in sources :

Example 16 with InputStreamFactory

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

Example 17 with InputStreamFactory

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

the class TestConnectorWorkItem method testLobs.

@Test
public void testLobs() throws Exception {
    BufferManager bm = BufferManagerFactory.getStandaloneBufferManager();
    final List<Object> result = Arrays.asList(AutoGenDataService.CLOB_VAL);
    final ExecutionFactory<Object, Object> ef = new ExecutionFactory<Object, Object>() {

        @Override
        public boolean isSourceRequired() {
            return false;
        }

        @Override
        public ResultSetExecution createResultSetExecution(QueryExpression command, ExecutionContext executionContext, RuntimeMetadata metadata, Object connection) throws TranslatorException {
            return new ResultSetExecution() {

                private boolean returned;

                @Override
                public void execute() throws TranslatorException {
                }

                @Override
                public void close() {
                }

                @Override
                public void cancel() throws TranslatorException {
                }

                @Override
                public List<?> next() throws TranslatorException, DataNotAvailableException {
                    if (returned) {
                        return null;
                    }
                    returned = true;
                    return result;
                }
            };
        }
    };
    ConnectorManager cm = new // $NON-NLS-1$ //$NON-NLS-2$
    ConnectorManager(// $NON-NLS-1$ //$NON-NLS-2$
    "FakeConnector", // $NON-NLS-1$ //$NON-NLS-2$
    "FakeConnector") {

        public ExecutionFactory getExecutionFactory() {
            return ef;
        }

        public Object getConnectionFactory() {
            return null;
        }
    };
    cm.start();
    ef.setCopyLobs(true);
    AtomicRequestMessage requestMsg = createNewAtomicRequestMessage(1, 1);
    // $NON-NLS-1$
    requestMsg.setCommand(helpGetCommand("SELECT CLOB_COLUMN FROM LOB_TESTING_ONE", EXAMPLE_BQT));
    requestMsg.setBufferManager(bm);
    ConnectorWorkItem cwi = new ConnectorWorkItem(requestMsg, cm);
    cwi.execute();
    AtomicResultsMessage message = cwi.more();
    List[] resutls = message.getResults();
    List<?> tuple = resutls[0];
    ClobType clob = (ClobType) tuple.get(0);
    assertEquals(StorageMode.MEMORY, InputStreamFactory.getStorageMode(clob));
    assertTrue(message.supportsImplicitClose());
    result.set(0, AutoGenDataService.CLOB_VAL);
    ef.setCopyLobs(false);
    cwi = new ConnectorWorkItem(requestMsg, cm);
    cwi.execute();
    message = cwi.more();
    resutls = message.getResults();
    tuple = resutls[0];
    clob = (ClobType) tuple.get(0);
    assertEquals(StorageMode.OTHER, InputStreamFactory.getStorageMode(clob));
    assertFalse(message.supportsImplicitClose());
    result.set(0, new ClobImpl(new InputStreamFactory() {

        @Override
        public InputStream getInputStream() throws IOException {
            return new ByteArrayInputStream(new byte[0]);
        }

        @Override
        public StorageMode getStorageMode() {
            // TODO: introduce an explicit streaming
            return StorageMode.FREE;
        }
    }, -1));
    requestMsg.setCopyStreamingLobs(true);
    cwi = new ConnectorWorkItem(requestMsg, cm);
    cwi.execute();
    message = cwi.more();
    resutls = message.getResults();
    tuple = resutls[0];
    clob = (ClobType) tuple.get(0);
    // switched from FREE to PERSISTENT
    assertEquals(StorageMode.PERSISTENT, InputStreamFactory.getStorageMode(clob));
    assertFalse(message.supportsImplicitClose());
}
Also used : ExecutionFactory(org.teiid.translator.ExecutionFactory) BufferManager(org.teiid.common.buffer.BufferManager) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) InputStreamFactory(org.teiid.core.types.InputStreamFactory) ClobType(org.teiid.core.types.ClobType) ResultSetExecution(org.teiid.translator.ResultSetExecution) ExecutionContext(org.teiid.translator.ExecutionContext) ByteArrayInputStream(java.io.ByteArrayInputStream) AtomicResultsMessage(org.teiid.dqp.message.AtomicResultsMessage) List(java.util.List) ArrayList(java.util.ArrayList) QueryExpression(org.teiid.language.QueryExpression) ClobImpl(org.teiid.core.types.ClobImpl) AtomicRequestMessage(org.teiid.dqp.message.AtomicRequestMessage) Test(org.junit.Test)

Example 18 with InputStreamFactory

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

the class TestSaveOnReadInputStream method testSave.

@Test
public void testSave() throws IOException {
    SaveOnReadInputStream soris = getSaveOnReadInputStream();
    InputStreamFactory isf = soris.getInputStreamFactory();
    InputStream is = isf.getInputStream();
    assertEquals("hello world", new String(ObjectConverterUtil.convertToByteArray(is), Streamable.CHARSET));
    InputStream is2 = isf.getInputStream();
    assertEquals("hello world", new String(ObjectConverterUtil.convertToByteArray(is2), Streamable.CHARSET));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) InputStreamFactory(org.teiid.core.types.InputStreamFactory) FileStoreInputStreamFactory(org.teiid.common.buffer.FileStoreInputStreamFactory) Test(org.junit.Test)

Example 19 with InputStreamFactory

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

the class PreparedStatementImpl method setClob.

void setClob(Object parameterIndex, final Reader reader) throws SQLException {
    if (reader == null) {
        this.setObject(parameterIndex, null);
        return;
    }
    final ReaderInputStream readerInputStream = new ReaderInputStream(reader, Charset.forName(Streamable.ENCODING));
    this.setObject(parameterIndex, new ClobImpl(new InputStreamFactory() {

        @Override
        public InputStream getInputStream() throws IOException {
            return readerInputStream;
        }
    }, -1));
}
Also used : ReaderInputStream(org.teiid.core.util.ReaderInputStream) InputStreamFactory(org.teiid.core.types.InputStreamFactory) ClobImpl(org.teiid.core.types.ClobImpl)

Example 20 with InputStreamFactory

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

the class ResultSetImpl method createInputStreamFactory.

private InputStreamFactory createInputStreamFactory(Streamable<?> type) {
    final StreamingLobChunckProducer.Factory factory = new StreamingLobChunckProducer.Factory(this.statement.getDQP(), this.requestID, type);
    InputStreamFactory isf = new InputStreamFactory() {

        @Override
        public InputStream getInputStream() throws IOException {
            return new LobChunkInputStream(factory.getLobChunkProducer());
        }
    };
    return isf;
}
Also used : StreamingLobChunckProducer(org.teiid.client.lob.StreamingLobChunckProducer) LobChunkInputStream(org.teiid.client.lob.LobChunkInputStream) InputStreamFactory(org.teiid.core.types.InputStreamFactory) InputStreamFactory(org.teiid.core.types.InputStreamFactory)

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