use of org.teiid.common.buffer.TupleBuffer in project teiid by teiid.
the class TempTableDataManager method handleCachedProcedure.
private TupleSource handleCachedProcedure(final CommandContext context, StoredProcedure proc) throws TeiidComponentException, QueryMetadataException, TeiidProcessingException {
String fullName = context.getMetadata().getFullName(proc.getProcedureID());
// $NON-NLS-1$
LogManager.logDetail(LogConstants.CTX_DQP, "processing cached procedure request for", fullName);
LinkedList<Object> vals = new LinkedList<Object>();
for (SPParameter param : proc.getInputParameters()) {
vals.add(((Constant) param.getExpression()).getValue());
}
// collapse the hash to single byte for the key to restrict the possible results to 256
int hash = vals.hashCode();
hash |= (hash >>> 16);
hash |= (hash >>> 8);
hash &= 0x000000ff;
final CacheID cid = new CacheID(new ParseInfo(), fullName + hash, context.getVdbName(), context.getVdbVersion(), context.getConnectionId(), context.getUserName());
cid.setParameters(vals);
CachedResults results = cache.get(cid);
if (results != null) {
TupleBuffer buffer = results.getResults();
return buffer.createIndexedTupleSource();
}
// construct a query with a no cache hint
final CacheHint hint = proc.getCacheHint();
proc.setCacheHint(null);
Option option = new Option();
option.setNoCache(true);
option.addNoCacheGroup(fullName);
proc.setOption(option);
StoredProcedure cloneProc = (StoredProcedure) proc.clone();
int i = 0;
for (SPParameter param : cloneProc.getInputParameters()) {
param.setExpression(new Reference(i++));
}
final QueryProcessor qp = context.getQueryProcessorFactory().createQueryProcessor(cloneProc.toString(), fullName.toUpperCase(), context, vals.toArray());
final BatchCollector bc = qp.createBatchCollector();
return new ProxyTupleSource() {
boolean success = false;
@Override
protected TupleSource createTupleSource() throws TeiidComponentException, TeiidProcessingException {
TupleBuffer tb = bc.collectTuples();
CachedResults cr = new CachedResults();
cr.setResults(tb, qp.getProcessorPlan());
Determinism determinismLevel = qp.getContext().getDeterminismLevel();
if (hint != null && hint.getDeterminism() != null) {
// $NON-NLS-1$ //$NON-NLS-2$
LogManager.logTrace(LogConstants.CTX_DQP, new Object[] { "Cache hint modified the query determinism from ", determinismLevel, " to ", hint.getDeterminism() });
determinismLevel = hint.getDeterminism();
}
cache.put(cid, determinismLevel, cr, hint != null ? hint.getTtl() : null);
context.setDeterminismLevel(determinismLevel);
success = true;
return tb.createIndexedTupleSource();
}
@Override
public void closeSource() {
super.closeSource();
qp.closeProcessing();
if (!success && bc.getTupleBuffer() != null) {
bc.getTupleBuffer().remove();
}
}
};
}
use of org.teiid.common.buffer.TupleBuffer in project teiid by teiid.
the class TestBufferManagerImpl method testTupleBufferSessionMax.
@Test
public void testTupleBufferSessionMax() throws Exception {
BufferManagerImpl bufferManager = new BufferManagerImpl();
bufferManager.setCache(new MemoryStorageManager() {
@Override
public long getMaxStorageSpace() {
return 64000;
}
});
bufferManager.setMaxReserveKB(10);
bufferManager.setMaxActivePlans(10);
bufferManager.setOptions(new Options().maxSessionBufferSizeEstimate(100000));
bufferManager.initialize();
CommandContext context = new CommandContext();
context.setSession(new SessionMetadata());
CommandContext.pushThreadLocalContext(context);
try {
List<TupleBuffer> tupleBuffers = new ArrayList<TupleBuffer>();
for (int i = 0; i < 36; i++) {
TupleBuffer tb = bufferManager.createTupleBuffer(Arrays.asList(new ElementSymbol("x", null, String.class)), "x", TupleSourceType.PROCESSOR);
try {
for (int j = 0; j < 50; j++) {
tb.addTuple(Arrays.asList("a"));
}
tb.saveBatch();
if (i % 2 == 0) {
tb.remove();
}
} catch (TeiidComponentException e) {
assertEquals(34, i);
return;
}
tupleBuffers.add(tb);
}
} finally {
CommandContext.popThreadLocalContext();
}
fail();
}
use of org.teiid.common.buffer.TupleBuffer in project teiid by teiid.
the class TestBufferManagerImpl method testTupleBufferMax.
@Test(expected = TeiidComponentException.class)
public void testTupleBufferMax() throws Exception {
BufferManagerImpl bufferManager = new BufferManagerImpl();
bufferManager.setCache(new MemoryStorageManager() {
@Override
public long getMaxStorageSpace() {
return 640;
}
});
bufferManager.setMaxReserveKB(10);
bufferManager.setMaxActivePlans(20);
bufferManager.initialize();
TupleBuffer tb = bufferManager.createTupleBuffer(Arrays.asList(new ElementSymbol("x", null, String.class)), "x", TupleSourceType.PROCESSOR);
// fill one batch, which should then exceed the max
for (int i = 0; i < 1024; i++) {
tb.addTuple(Arrays.asList("a"));
}
}
use of org.teiid.common.buffer.TupleBuffer 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));
}
use of org.teiid.common.buffer.TupleBuffer in project teiid by teiid.
the class TestBatchIterator method testNoSaveForwardOnly.
@Test
public void testNoSaveForwardOnly() throws Exception {
BatchIterator bi = new BatchIterator(new FakeRelationalNode(1, new List[] { Arrays.asList(1), Arrays.asList(1), Arrays.asList(1), Arrays.asList(1) }, 2) {
@Override
public TupleBatch nextBatchDirect() throws BlockedException, TeiidComponentException, TeiidProcessingException {
TupleBatch tb = super.nextBatchDirect();
tb.setRowOffset(tb.getBeginRow() + 3);
return tb;
}
});
BufferManager bm = BufferManagerFactory.getStandaloneBufferManager();
TupleBuffer tb = bm.createTupleBuffer(Arrays.asList(new ElementSymbol("x", null, DataTypeManager.DefaultDataClasses.INTEGER)), "test", TupleSourceType.PROCESSOR);
tb.setForwardOnly(true);
// $NON-NLS-1$
bi.setBuffer(tb, false);
tb.addTuple(Arrays.asList(2));
tb.addTuple(Arrays.asList(2));
tb.addTuple(Arrays.asList(2));
assertEquals(3, bi.getBuffer().getManagedRowCount());
bi.nextTuple();
// pull the first batch
assertEquals(2, bi.available());
assertEquals(0, bi.getBuffer().getManagedRowCount());
for (int i = 0; i < 2; i++) {
assertNotNull(bi.nextTuple());
assertEquals(0, bi.getBuffer().getManagedRowCount());
}
bi.readAhead(3);
assertEquals(2, bi.getBuffer().getManagedRowCount());
for (int i = 0; i < 4; i++) {
assertNotNull(bi.nextTuple());
assertEquals(0, bi.getBuffer().getManagedRowCount());
}
assertNull(bi.nextTuple());
assertEquals(0, bi.getBuffer().getManagedRowCount());
}
Aggregations