use of org.teiid.query.parser.ParseInfo 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.query.parser.ParseInfo in project teiid by teiid.
the class CommandContext method putPlan.
public void putPlan(String key, PreparedPlan plan, Determinism determinismLevel) {
if (this.globalState.planCache == null) {
return;
}
CacheID id = new CacheID(new ParseInfo(), key, getVdbName(), getVdbVersion(), getConnectionId(), getUserName());
this.globalState.planCache.put(id, determinismLevel, plan, null);
}
use of org.teiid.query.parser.ParseInfo in project teiid by teiid.
the class TestSessionAwareCache method testVDBRemoval.
@Test
public void testVDBRemoval() {
SessionAwareCache<Cachable> cache = new SessionAwareCache<Cachable>("resultset", DefaultCacheFactory.INSTANCE, SessionAwareCache.Type.RESULTSET, 0);
CacheID id = new CacheID(buildWorkContext(), new ParseInfo(), "SELECT * FROM FOO");
Cachable result = Mockito.mock(Cachable.class);
Mockito.stub(result.prepare((BufferManager) anyObject())).toReturn(true);
Mockito.stub(result.restore((BufferManager) anyObject())).toReturn(true);
id = new CacheID(buildWorkContext(), new ParseInfo(), "SELECT * FROM FOO");
cache.put(id, Determinism.VDB_DETERMINISTIC, result, null);
Object c = cache.get(id);
assertTrue(result == c);
cache.clearForVDB("vdb-name", "1");
assertNull(cache.get(id));
}
use of org.teiid.query.parser.ParseInfo in project teiid by teiid.
the class TestSessionAwareCache method testRemove.
@Test
public void testRemove() {
SessionAwareCache<Cachable> cache = new SessionAwareCache<Cachable>("resultset", DefaultCacheFactory.INSTANCE, SessionAwareCache.Type.RESULTSET, 0);
CacheID id = new CacheID(buildWorkContext(), new ParseInfo(), "SELECT * FROM FOO");
Cachable result = Mockito.mock(Cachable.class);
Mockito.stub(result.prepare((BufferManager) anyObject())).toReturn(true);
Mockito.stub(result.restore((BufferManager) anyObject())).toReturn(true);
id = new CacheID(buildWorkContext(), new ParseInfo(), "SELECT * FROM FOO");
cache.put(id, Determinism.VDB_DETERMINISTIC, result, null);
Object c = cache.get(id);
assertTrue(result == c);
assertTrue(cache.remove(id, Determinism.VDB_DETERMINISTIC) != null);
assertNull(cache.get(id));
// session scope
cache.put(id, Determinism.SESSION_DETERMINISTIC, result, null);
assertTrue(cache.get(id) != null);
assertTrue(cache.remove(id, Determinism.SESSION_DETERMINISTIC) != null);
assertNull(cache.get(id));
}
use of org.teiid.query.parser.ParseInfo in project teiid by teiid.
the class TestODataSQLBuilder method helpTest.
public QueryState helpTest(String url, String sqlExpected, Integer skip, Integer top, Boolean count) throws Exception {
QueryState state = setup(url);
Client client = state.client;
ArgumentCaptor<Query> arg1 = ArgumentCaptor.forClass(Query.class);
ArgumentCaptor<EntityCollectionResponse> arg6 = ArgumentCaptor.forClass(EntityCollectionResponse.class);
List<SQLParameter> parameters = new ArrayList<SQLParameter>();
if (sqlExpected != null) {
Query actualCommand = (Query) QueryParser.getQueryParser().parseCommand(sqlExpected, new ParseInfo());
Mockito.verify(client).executeSQL(arg1.capture(), Mockito.eq(parameters), Mockito.eq(count), (Integer) Mockito.eq(skip), (Integer) Mockito.eq(top), (String) Mockito.eq(null), Mockito.anyInt(), arg6.capture());
Assert.assertEquals(actualCommand.toString(), arg1.getValue().toString());
}
state.parameters = parameters;
state.arg1 = arg1;
state.arg6 = arg6;
return state;
}
Aggregations