Search in sources :

Example 1 with BufferManager

use of org.teiid.common.buffer.BufferManager in project teiid by teiid.

the class TestConnectorWorkItem method testTypeConversionClob.

@Test
public void testTypeConversionClob() throws Exception {
    BufferManager bm = BufferManagerFactory.getStandaloneBufferManager();
    String str = "hello world";
    Clob clob = (Clob) ConnectorWorkItem.convertToRuntimeType(bm, new InputStreamFactory() {

        @Override
        public InputStream getInputStream() throws IOException {
            return new ByteArrayInputStream(str.getBytes(Streamable.CHARSET));
        }
    }, DataTypeManager.DefaultDataClasses.CLOB, null);
    assertEquals(str, clob.getSubString(1, str.length()));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) BufferManager(org.teiid.common.buffer.BufferManager) Clob(java.sql.Clob) InputStreamFactory(org.teiid.core.types.InputStreamFactory) Test(org.junit.Test)

Example 2 with BufferManager

use of org.teiid.common.buffer.BufferManager 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 3 with BufferManager

use of org.teiid.common.buffer.BufferManager in project teiid by teiid.

the class TestConnectorWorkItem method testConversionError.

@Test
public void testConversionError() throws Exception {
    BufferManager bm = BufferManagerFactory.getStandaloneBufferManager();
    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 {
            List<String> list1 = new ArrayList<String>();
            list1.add("1");
            List<String> list2 = new ArrayList<String>();
            list2.add("a");
            final Iterator<List<String>> iter = Arrays.asList(list1, list2).iterator();
            return new ResultSetExecution() {

                @Override
                public void execute() throws TranslatorException {
                }

                @Override
                public void close() {
                }

                @Override
                public void cancel() throws TranslatorException {
                }

                @Override
                public List<?> next() throws TranslatorException, DataNotAvailableException {
                    if (iter.hasNext()) {
                        return iter.next();
                    }
                    return null;
                }
            };
        }
    };
    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 intkey FROM bqt1.smalla", EXAMPLE_BQT));
    requestMsg.setBufferManager(bm);
    ConnectorWorkItem cwi = new ConnectorWorkItem(requestMsg, cm);
    cwi.execute();
    AtomicResultsMessage message = cwi.more();
    List[] results = message.getResults();
    assertEquals(1, results.length);
    List<?> tuple = results[0];
    assertEquals(1, tuple.get(0));
    assertEquals(-1, message.getFinalRow());
    try {
        cwi.more();
        fail();
    } catch (TranslatorException e) {
    // should throw the conversion error
    }
}
Also used : ArrayList(java.util.ArrayList) ExecutionFactory(org.teiid.translator.ExecutionFactory) BufferManager(org.teiid.common.buffer.BufferManager) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) ResultSetExecution(org.teiid.translator.ResultSetExecution) ExecutionContext(org.teiid.translator.ExecutionContext) List(java.util.List) ArrayList(java.util.ArrayList) AtomicResultsMessage(org.teiid.dqp.message.AtomicResultsMessage) TranslatorException(org.teiid.translator.TranslatorException) QueryExpression(org.teiid.language.QueryExpression) AtomicRequestMessage(org.teiid.dqp.message.AtomicRequestMessage) Test(org.junit.Test)

Example 4 with BufferManager

use of org.teiid.common.buffer.BufferManager in project teiid by teiid.

the class TestConnectorWorkItem method testUnmodifibleList.

@Test
public void testUnmodifibleList() throws Exception {
    BufferManager bm = BufferManagerFactory.getStandaloneBufferManager();
    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 {
            List<String> list1 = Collections.singletonList("1");
            final Iterator<List<String>> iter = Arrays.asList(list1).iterator();
            return new ResultSetExecution() {

                @Override
                public void execute() throws TranslatorException {
                }

                @Override
                public void close() {
                }

                @Override
                public void cancel() throws TranslatorException {
                }

                @Override
                public List<?> next() throws TranslatorException, DataNotAvailableException {
                    if (iter.hasNext()) {
                        return iter.next();
                    }
                    return null;
                }
            };
        }
    };
    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();
    AtomicRequestMessage requestMsg = createNewAtomicRequestMessage(1, 1);
    // $NON-NLS-1$
    requestMsg.setCommand(helpGetCommand("SELECT intkey FROM bqt1.smalla", EXAMPLE_BQT));
    requestMsg.setBufferManager(bm);
    ConnectorWorkItem cwi = new ConnectorWorkItem(requestMsg, cm);
    cwi.execute();
    AtomicResultsMessage message = cwi.more();
    List[] results = message.getResults();
    assertEquals(1, results.length);
    List<?> tuple = results[0];
    assertEquals(1, tuple.get(0));
    assertEquals(1, message.getFinalRow());
}
Also used : ExecutionFactory(org.teiid.translator.ExecutionFactory) BufferManager(org.teiid.common.buffer.BufferManager) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) ResultSetExecution(org.teiid.translator.ResultSetExecution) ExecutionContext(org.teiid.translator.ExecutionContext) List(java.util.List) ArrayList(java.util.ArrayList) AtomicResultsMessage(org.teiid.dqp.message.AtomicResultsMessage) QueryExpression(org.teiid.language.QueryExpression) AtomicRequestMessage(org.teiid.dqp.message.AtomicRequestMessage) Test(org.junit.Test)

Example 5 with BufferManager

use of org.teiid.common.buffer.BufferManager in project teiid by teiid.

the class TestBufferManagerImpl method testProcessorBatchSize.

@Test
public void testProcessorBatchSize() {
    BufferManager bm = BufferManagerFactory.createBufferManager();
    int processorBatchSize = bm.getProcessorBatchSize();
    List<ElementSymbol> elements = new ArrayList<ElementSymbol>();
    ElementSymbol a = new ElementSymbol("a");
    a.setType(DataTypeManager.DefaultDataClasses.INTEGER);
    // we use a somewhat high estimate of string size
    ElementSymbol b = new ElementSymbol("b");
    b.setType(DataTypeManager.DefaultDataClasses.STRING);
    elements.add(a);
    // fixed/small
    assertEquals(processorBatchSize * 8, bm.getProcessorBatchSize(elements));
    elements.add(b);
    // small
    assertEquals(processorBatchSize * 4, bm.getProcessorBatchSize(elements));
    elements.add(b);
    // moderately small
    assertEquals(processorBatchSize * 2, bm.getProcessorBatchSize(elements));
    elements.add(b);
    elements.add(b);
    // "normal"
    assertEquals(processorBatchSize, bm.getProcessorBatchSize(elements));
    elements.addAll(Collections.nCopies(28, b));
    // large
    assertEquals(processorBatchSize / 2, bm.getProcessorBatchSize(elements));
    elements.addAll(Collections.nCopies(100, b));
    // huge
    assertEquals(processorBatchSize / 4, bm.getProcessorBatchSize(elements));
    elements.addAll(Collections.nCopies(375, b));
    // extreme
    assertEquals(processorBatchSize / 8, bm.getProcessorBatchSize(elements));
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) ArrayList(java.util.ArrayList) BufferManager(org.teiid.common.buffer.BufferManager) Test(org.junit.Test)

Aggregations

BufferManager (org.teiid.common.buffer.BufferManager)46 Test (org.junit.Test)32 List (java.util.List)23 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)22 CommandContext (org.teiid.query.util.CommandContext)18 ArrayList (java.util.ArrayList)17 TupleBuffer (org.teiid.common.buffer.TupleBuffer)16 FakeRelationalNode (org.teiid.query.processor.relational.FakeRelationalNode)8 BlockedException (org.teiid.common.buffer.BlockedException)6 BigDecimal (java.math.BigDecimal)5 TupleSource (org.teiid.common.buffer.TupleSource)5 CachedResults (org.teiid.dqp.internal.process.CachedResults)5 SessionAwareCache (org.teiid.dqp.internal.process.SessionAwareCache)5 FakeTupleSource (org.teiid.query.processor.FakeTupleSource)5 AggregateSymbol (org.teiid.query.sql.symbol.AggregateSymbol)5 TupleBatch (org.teiid.common.buffer.TupleBatch)4 TempTableDataManager (org.teiid.query.tempdata.TempTableDataManager)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ClobImpl (org.teiid.core.types.ClobImpl)2 ClobType (org.teiid.core.types.ClobType)2