use of com.hazelcast.sql.impl.optimizer.SqlPlan in project hazelcast by hazelcast.
the class PlanCacheTest method testBasicOperations.
@Test
public void testBasicOperations() {
PlanCache cache = new PlanCache(10);
PlanKey key = createKey("sql");
// Put plan
SqlPlan plan1 = createPlan(key, PART_MAP_1);
cache.put(key, plan1);
assertEquals(1, cache.size());
assertSame(plan1, cache.get(key));
// Overwrite plan
SqlPlan plan2 = createPlan(key, PART_MAP_2);
cache.put(key, plan2);
assertEquals(1, cache.size());
assertSame(plan2, cache.get(key));
// Invalidate the plan that is no longer cached
cache.invalidate(plan1);
assertEquals(1, cache.size());
assertSame(plan2, cache.get(key));
// Invalidate cache plan
cache.invalidate(plan2);
assertEquals(0, cache.size());
assertNull(cache.get(key));
// Clear
cache.put(key, plan1);
assertEquals(1, cache.size());
assertSame(plan1, cache.get(key));
cache.clear();
assertEquals(0, cache.size());
assertNull(cache.get(key));
}
use of com.hazelcast.sql.impl.optimizer.SqlPlan in project hazelcast by hazelcast.
the class SqlServiceImpl method prepare.
private SqlPlan prepare(String schema, String sql, List<Object> arguments, SqlExpectedResultType expectedResultType) {
List<List<String>> searchPaths = prepareSearchPaths(schema);
PlanKey planKey = new PlanKey(searchPaths, sql);
SqlPlan plan = planCache.get(planKey);
if (plan == null) {
SqlCatalog catalog = new SqlCatalog(optimizer.tableResolvers());
plan = optimizer.prepare(new OptimizationTask(sql, arguments, searchPaths, catalog));
if (plan.isCacheable()) {
planCache.put(planKey, plan);
}
}
checkReturnType(plan, expectedResultType);
return plan;
}
use of com.hazelcast.sql.impl.optimizer.SqlPlan in project hazelcast by hazelcast.
the class SqlServiceImpl method query0.
private SqlResult query0(QueryId queryId, String schema, String sql, List<Object> args, long timeout, int pageSize, SqlExpectedResultType expectedResultType, SqlSecurityContext securityContext) {
// Validate and normalize
if (sql == null || sql.isEmpty()) {
throw QueryException.error("SQL statement cannot be empty.");
}
List<Object> args0 = new ArrayList<>(args);
if (timeout < 0) {
throw QueryException.error("Timeout cannot be negative: " + timeout);
}
if (pageSize <= 0) {
throw QueryException.error("Page size must be positive: " + pageSize);
}
// Prepare and execute
SqlPlan plan = prepare(schema, sql, args0, expectedResultType);
if (securityContext.isSecurityEnabled()) {
plan.checkPermissions(securityContext);
}
// TODO: pageSize ?
return plan.execute(queryId, args0, timeout);
}
use of com.hazelcast.sql.impl.optimizer.SqlPlan in project hazelcast by hazelcast.
the class PlanCache method shrinkIfNeeded.
private void shrinkIfNeeded() {
int oversize = plans.size() - maxSize;
if (oversize <= 0) {
return;
}
// Sort plans according to their last used timestamps
TreeMap<Long, SqlPlan> sorted = new TreeMap<>();
for (SqlPlan plan : plans.values()) {
sorted.put(plan.getPlanLastUsed(), plan);
}
// Remove oldest plans
for (SqlPlan plan : sorted.values()) {
boolean removed = remove(plan);
if (removed) {
if (--oversize == 0) {
break;
}
}
}
}
use of com.hazelcast.sql.impl.optimizer.SqlPlan in project hazelcast by hazelcast.
the class PlanCacheTestSupport method createPlan.
public static SqlPlan createPlan(PlanKey key, Map<UUID, PartitionIdSet> partitions, int... objectKeys) {
Set<PlanObjectKey> objectKeys0 = new HashSet<>();
if (objectKeys != null) {
for (int objectId : objectKeys) {
objectKeys0.add(createObjectId(objectId));
}
}
SqlPlan plan = new TestPlan(key, objectKeys0, partitions);
assertEquals(key, plan.getPlanKey());
assertEquals(0L, plan.getPlanLastUsed());
return plan;
}
Aggregations