use of org.teiid.common.buffer.BufferManager in project teiid by teiid.
the class TestBatchIterator method testReset1.
@Test
public void testReset1() throws Exception {
BatchIterator bi = new BatchIterator(new FakeRelationalNode(1, new List[] { Arrays.asList(1), Arrays.asList(2), Arrays.asList(3) }, 2));
BufferManager bm = BufferManagerFactory.getStandaloneBufferManager();
TupleBuffer tb = bm.createTupleBuffer(Arrays.asList(new ElementSymbol("x", null, DataTypeManager.DefaultDataClasses.INTEGER)), "test", TupleSourceType.PROCESSOR);
// $NON-NLS-1$
bi.setBuffer(tb, true);
bi.nextTuple();
bi.mark();
bi.nextTuple();
bi.reset();
assertEquals(2, bi.getCurrentIndex());
assertEquals(2, bi.nextTuple().get(0));
}
use of org.teiid.common.buffer.BufferManager in project teiid by teiid.
the class TestBatchIterator method testReset2.
@Test
public void testReset2() throws Exception {
BatchIterator bi = new BatchIterator(new FakeRelationalNode(1, new List[] { Arrays.asList(1), Arrays.asList(2) }, 2));
BufferManager bm = BufferManagerFactory.getStandaloneBufferManager();
TupleBuffer tb = bm.createTupleBuffer(Arrays.asList(new ElementSymbol("x", null, DataTypeManager.DefaultDataClasses.INTEGER)), "test", TupleSourceType.PROCESSOR);
// $NON-NLS-1$
bi.setBuffer(tb, true);
bi.hasNext();
bi.mark();
bi.nextTuple();
bi.nextTuple();
assertNull(bi.nextTuple());
bi.reset();
bi.hasNext();
assertEquals(1, bi.getCurrentIndex());
assertEquals(1, bi.nextTuple().get(0));
}
use of org.teiid.common.buffer.BufferManager in project teiid by teiid.
the class FunctionMethods method concat.
public static ClobType concat(CommandContext context, ClobType str1, ClobType str2) throws IOException, SQLException {
BufferManager bm = context.getBufferManager();
// $NON-NLS-1$
FileStore fs = bm.createFileStore("clob");
FileStoreInputStreamFactory fsisf = new FileStoreInputStreamFactory(fs, Streamable.ENCODING);
boolean remove = true;
try (Reader characterStream = str1.getCharacterStream();
Reader characterStream2 = str2.getCharacterStream()) {
Writer writer = fsisf.getWriter();
int chars = ObjectConverterUtil.write(writer, characterStream, -1, false);
chars += ObjectConverterUtil.write(writer, characterStream2, -1, false);
writer.close();
if (fsisf.getStorageMode() == StorageMode.MEMORY) {
// detach if just in memory
byte[] bytes = fsisf.getMemoryBytes();
return new ClobType(new ClobImpl(new String(bytes, Streamable.ENCODING)));
}
remove = false;
context.addCreatedLob(fsisf);
return new ClobType(new ClobImpl(fsisf, chars));
} finally {
if (remove) {
fs.remove();
}
}
}
use of org.teiid.common.buffer.BufferManager 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());
}
use of org.teiid.common.buffer.BufferManager in project teiid by teiid.
the class TestCachedResults method testCaching.
@Test
public void testCaching() throws Exception {
FakeBufferService fbs = new FakeBufferService(true);
// $NON-NLS-1$
ElementSymbol x = new ElementSymbol("x");
x.setType(DataTypeManager.DefaultDataClasses.INTEGER);
List<ElementSymbol> schema = Arrays.asList(x);
// $NON-NLS-1$
TupleBuffer tb = BufferManagerFactory.getStandaloneBufferManager().createTupleBuffer(schema, "x", TupleSourceType.PROCESSOR);
tb.setForwardOnly(false);
tb.addTuple(Arrays.asList(1));
tb.addTuple(Arrays.asList(2));
tb.addTuple(Arrays.asList(3));
tb.addTuple(Arrays.asList(4));
tb.addTuple(Arrays.asList(5));
tb.addTuple(Arrays.asList(6));
tb.addTuple(Arrays.asList(7));
tb.addTuple(Arrays.asList(8));
tb.addTuple(Arrays.asList(9));
tb.addTuple(Arrays.asList(10));
tb.close();
BufferManager bm = fbs.getBufferManager();
CachedResults results = new CachedResults();
ProcessorPlan plan = new FakeProcessorPlan(0);
CommandContext cc = new CommandContext();
Table t = RealMetadataFactory.exampleBQT().getGroupID("bqt1.smalla");
cc.accessedDataObject(t);
plan.setContext(cc);
results.setResults(tb, plan);
results.setCommand(new Query());
// Cache cache = new DefaultCache("dummy"); //$NON-NLS-1$
long ts = results.getAccessInfo().getCreationTime();
// in cache
for (int row = 1; row <= tb.getRowCount(); row += 4) {
// cache.put(results.getId()+","+row, tb.getBatch(row), null); //$NON-NLS-1$
}
results.prepare(bm);
// simulate distribute
TupleBuffer distributedTb = bm.getTupleBuffer(results.getId());
CachedResults cachedResults = UnitTestUtil.helpSerialize(results);
RealMetadataFactory.buildWorkContext(RealMetadataFactory.exampleBQT());
BufferManager bm2 = fbs.getBufferManager();
bm2.distributeTupleBuffer(results.getId(), distributedTb);
assertTrue(cachedResults.restore(bm2));
// since restored, simulate a async cache flush
// cache.clear();
TupleBuffer cachedTb = cachedResults.getResults();
assertTrue(cachedTb.isFinal());
assertEquals(tb.getRowCount(), cachedTb.getRowCount());
assertEquals(tb.getBatchSize(), cachedTb.getBatchSize());
assertArrayEquals(tb.getBatch(1).getAllTuples(), cachedTb.getBatch(1).getAllTuples());
assertArrayEquals(tb.getBatch(9).getAllTuples(), cachedTb.getBatch(9).getAllTuples());
assertTrue(ts - cachedResults.getAccessInfo().getCreationTime() <= 5000);
// ensure that an incomplete load fails ( is this still valid use case?)
// bm2.getTupleBuffer(results.getId()).remove();
// cachedResults = UnitTestUtil.helpSerialize(results);
// assertFalse(cachedResults.restore(cache, bm2));
}
Aggregations