use of org.datanucleus.store.query.inmemory.JavaQueryInMemoryEvaluator in project datanucleus-hbase by datanucleus.
the class JDOQLQuery method performExecute.
protected Object performExecute(Map parameters) {
HBaseManagedConnection mconn = (HBaseManagedConnection) getStoreManager().getConnectionManager().getConnection(ec);
try {
long startTime = System.currentTimeMillis();
if (NucleusLogger.QUERY.isDebugEnabled()) {
NucleusLogger.QUERY.debug(Localiser.msg("021046", Query.LANGUAGE_JDOQL, getSingleStringQuery(), null));
}
boolean filterInMemory = true;
List candidates = null;
if (candidateCollection != null) {
candidates = new ArrayList(candidateCollection);
} else {
// Generate the HBase Filter, from the query filter plus any necessary discriminator restriction
Filter filter = null;
HBaseBooleanExpression filterExpr = null;
AbstractClassMetaData cmd = getCandidateClassMetaData();
StoreData sd = storeMgr.getStoreDataForClass(cmd.getFullClassName());
if (sd == null) {
storeMgr.manageClasses(clr, cmd.getFullClassName());
sd = storeMgr.getStoreDataForClass(cmd.getFullClassName());
}
Table table = (sd != null ? sd.getTable() : null);
if (table != null) {
Column multitenancyCol = table.getSurrogateColumn(SurrogateColumnType.MULTITENANCY);
if (multitenancyCol != null) {
String value = ec.getTenantId();
if (value != null) {
// Filter on discriminator for this tenant
String familyName = HBaseUtils.getFamilyNameForColumn(multitenancyCol);
String qualifName = HBaseUtils.getQualifierNameForColumn(multitenancyCol);
HBaseBooleanExpression multitenantFilterExpr = new HBaseBooleanExpression(familyName, qualifName, value, Expression.OP_EQ);
filterExpr = multitenantFilterExpr;
}
}
Column softDeleteCol = table.getSurrogateColumn(SurrogateColumnType.SOFTDELETE);
if (softDeleteCol != null) {
// Filter on soft-delete flag
String familyName = HBaseUtils.getFamilyNameForColumn(softDeleteCol);
String qualifName = HBaseUtils.getQualifierNameForColumn(softDeleteCol);
HBaseBooleanExpression softDeleteFilterExpr = new HBaseBooleanExpression(familyName, qualifName, Boolean.FALSE, Expression.OP_EQ);
if (filterExpr != null) {
filterExpr = new HBaseBooleanExpression(filterExpr, softDeleteFilterExpr, Expression.OP_AND);
} else {
filterExpr = softDeleteFilterExpr;
}
}
}
if (datastoreCompilation != null && datastoreCompilation.isFilterComplete()) {
HBaseBooleanExpression userFilterExpr = datastoreCompilation.getFilterExpression();
if (filterExpr == null) {
filterExpr = userFilterExpr;
} else if (userFilterExpr == null) {
// Nothing to do
} else {
filterExpr = new HBaseBooleanExpression(filterExpr, userFilterExpr, Expression.OP_AND);
}
}
if (filterExpr != null) {
filter = filterExpr.getFilter();
}
candidates = HBaseQueryUtils.getObjectsOfCandidateType(ec, mconn, candidateClass, subclasses, ignoreCache, getFetchPlan(), filter, storeMgr);
if (filter != null && datastoreCompilation != null && datastoreCompilation.isFilterComplete()) {
filterInMemory = false;
}
}
// Apply any other restrictions not handled in the datastore
JavaQueryInMemoryEvaluator resultMapper = new JDOQLInMemoryEvaluator(this, candidates, compilation, parameters, ec.getClassLoaderResolver());
Collection results = resultMapper.execute(filterInMemory, true, true, true, true);
if (NucleusLogger.QUERY.isDebugEnabled()) {
NucleusLogger.QUERY.debug(Localiser.msg("021074", Query.LANGUAGE_JDOQL, "" + (System.currentTimeMillis() - startTime)));
}
if (type == QueryType.BULK_DELETE) {
ec.deleteObjects(results.toArray());
return Long.valueOf(results.size());
} else if (type == QueryType.BULK_UPDATE) {
// TODO Support BULK UPDATE
throw new NucleusException("Bulk Update is not yet supported");
} else {
return results;
}
} finally {
mconn.release();
}
}
use of org.datanucleus.store.query.inmemory.JavaQueryInMemoryEvaluator in project datanucleus-hbase by datanucleus.
the class JPQLQuery method performExecute.
protected Object performExecute(Map parameters) {
HBaseManagedConnection mconn = (HBaseManagedConnection) getStoreManager().getConnectionManager().getConnection(ec);
try {
long startTime = System.currentTimeMillis();
if (NucleusLogger.QUERY.isDebugEnabled()) {
NucleusLogger.QUERY.debug(Localiser.msg("021046", Query.LANGUAGE_JPQL, getSingleStringQuery(), null));
}
boolean filterInMemory = true;
List candidates = null;
if (candidateCollection != null) {
candidates = new ArrayList(candidateCollection);
} else {
// Generate the HBase Filter, from the query filter plus any necessary discriminator restriction
Filter filter = null;
HBaseBooleanExpression filterExpr = null;
AbstractClassMetaData cmd = getCandidateClassMetaData();
StoreData sd = storeMgr.getStoreDataForClass(cmd.getFullClassName());
if (sd == null) {
storeMgr.manageClasses(clr, cmd.getFullClassName());
sd = storeMgr.getStoreDataForClass(cmd.getFullClassName());
}
Table table = (sd != null) ? sd.getTable() : null;
if (table != null) {
Column multitenancyCol = table.getSurrogateColumn(SurrogateColumnType.MULTITENANCY);
if (multitenancyCol != null) {
// Filter on multi-tenant discriminator
String familyName = HBaseUtils.getFamilyNameForColumn(multitenancyCol);
String qualifName = HBaseUtils.getQualifierNameForColumn(multitenancyCol);
String value = ec.getTenantId();
filterExpr = new HBaseBooleanExpression(familyName, qualifName, value, Expression.OP_EQ);
}
Column softDeleteCol = table.getSurrogateColumn(SurrogateColumnType.SOFTDELETE);
if (softDeleteCol != null) {
// Filter on soft-delete flag
String familyName = HBaseUtils.getFamilyNameForColumn(softDeleteCol);
String qualifName = HBaseUtils.getQualifierNameForColumn(softDeleteCol);
HBaseBooleanExpression softDeleteFilterExpr = new HBaseBooleanExpression(familyName, qualifName, Boolean.FALSE, Expression.OP_EQ);
if (filterExpr != null) {
filterExpr = new HBaseBooleanExpression(filterExpr, softDeleteFilterExpr, Expression.OP_AND);
} else {
filterExpr = softDeleteFilterExpr;
}
}
}
if (datastoreCompilation != null && datastoreCompilation.isFilterComplete()) {
HBaseBooleanExpression userFilterExpr = datastoreCompilation.getFilterExpression();
if (filterExpr == null) {
filterExpr = userFilterExpr;
} else if (userFilterExpr == null) {
// Nothing to do
} else {
filterExpr = new HBaseBooleanExpression(filterExpr, userFilterExpr, Expression.OP_AND);
}
}
if (filterExpr != null) {
filter = filterExpr.getFilter();
}
candidates = HBaseQueryUtils.getObjectsOfCandidateType(ec, mconn, candidateClass, subclasses, ignoreCache, getFetchPlan(), filter, storeMgr);
if (filter != null && datastoreCompilation != null && datastoreCompilation.isFilterComplete()) {
filterInMemory = false;
}
}
// Apply any result restrictions to the results
JavaQueryInMemoryEvaluator resultMapper = new JPQLInMemoryEvaluator(this, candidates, compilation, parameters, ec.getClassLoaderResolver());
Collection results = resultMapper.execute(filterInMemory, true, true, true, true);
if (NucleusLogger.QUERY.isDebugEnabled()) {
NucleusLogger.QUERY.debug(Localiser.msg("021074", Query.LANGUAGE_JPQL, "" + (System.currentTimeMillis() - startTime)));
}
if (type == QueryType.BULK_DELETE) {
ec.deleteObjects(results.toArray());
return Long.valueOf(results.size());
} else if (type == QueryType.BULK_UPDATE) {
// TODO Support BULK UPDATE
throw new NucleusException("Bulk Update is not yet supported");
} else {
return results;
}
} finally {
mconn.release();
}
}
use of org.datanucleus.store.query.inmemory.JavaQueryInMemoryEvaluator in project datanucleus-json by datanucleus.
the class JDOQLQuery method performExecute.
protected Object performExecute(Map parameters) {
AbstractClassMetaData cmd = ec.getMetaDataManager().getMetaDataForClass(candidateClass, ec.getClassLoaderResolver());
Properties options = new Properties();
options.put(ConnectionFactoryImpl.STORE_JSON_URL, ((JsonPersistenceHandler) getStoreManager().getPersistenceHandler()).getURLPathForQuery(cmd));
ManagedConnection mconn = getStoreManager().getConnectionManager().getConnection(ec, options);
try {
long startTime = System.currentTimeMillis();
if (NucleusLogger.QUERY.isDebugEnabled()) {
NucleusLogger.QUERY.debug(Localiser.msg("021046", Query.LANGUAGE_JDOQL, getSingleStringQuery(), null));
}
List candidates = null;
if (candidateCollection == null) {
candidates = ((JsonPersistenceHandler) getStoreManager().getPersistenceHandler()).getObjectsOfCandidateType(ec, mconn, candidateClass, subclasses, ignoreCache, options);
} else {
candidates = new ArrayList(candidateCollection);
}
JavaQueryInMemoryEvaluator resultMapper = new JDOQLInMemoryEvaluator(this, candidates, compilation, parameters, ec.getClassLoaderResolver());
Collection results = resultMapper.execute(true, true, true, true, true);
if (NucleusLogger.QUERY.isDebugEnabled()) {
NucleusLogger.QUERY.debug(Localiser.msg("021074", Query.LANGUAGE_JDOQL, "" + (System.currentTimeMillis() - startTime)));
}
return results;
} finally {
mconn.release();
}
}
use of org.datanucleus.store.query.inmemory.JavaQueryInMemoryEvaluator in project tests by datanucleus.
the class JDOQLEvaluatorTest method testFilterGreaterThanLessThan.
/**
* Test of filter with ">", "<" and "+".
*/
public void testFilterGreaterThanLessThan() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
// Create some instances to query over
List<Person> instances = new ArrayList<>();
Person p1 = new Person(101, "Mickey", "Mouse", "mickey.mouse@warnerbros.com");
p1.setAge(34);
Person p2 = new Person(102, "Donald", "Duck", "donald.duck@warnerbros.com");
p2.setAge(38);
Person p3 = new Person(103, "Minnie", "Mouse", "minnie.mouse@warnerbros.com");
p3.setAge(31);
instances.add(p1);
instances.add(p2);
instances.add(p3);
// Compile the query
JDOQuery q = (JDOQuery) pm.newQuery(Person.class, "age > 34");
Query query = q.getInternalQuery();
ClassLoaderResolver clr = query.getExecutionContext().getClassLoaderResolver();
JavaQueryCompiler compiler = new JDOQLCompiler(query.getExecutionContext().getNucleusContext(), clr, null, query.getCandidateClass(), null, query.getFilter(), query.getParsedImports(), query.getOrdering(), query.getResult(), query.getGrouping(), query.getHaving(), query.getExplicitParametersDeclaration(), query.getExplicitVariablesDeclaration(), null);
QueryCompilation compilation = compiler.compile(new HashMap(), null);
// Execute the query
JavaQueryInMemoryEvaluator eval = new JDOQLInMemoryEvaluator(query, instances, compilation, null, clr);
List results = (List) eval.execute(true, true, true, true, true);
assertEquals("Number of result instances was wrong", 1, results.size());
Person p = (Person) results.get(0);
assertEquals("Result instance has wrong first name", "Donald", p.getFirstName());
assertEquals("Result instance has wrong last name", "Duck", p.getLastName());
assertEquals("Person number of result instance is wrong", 102, p.getPersonNum());
assertEquals("Age of result instance is wrong", 38, p.getAge());
// Compile the query
q = (JDOQuery) pm.newQuery(Person.class, "age+2 < 35");
query = q.getInternalQuery();
clr = query.getExecutionContext().getClassLoaderResolver();
compiler = new JDOQLCompiler(query.getExecutionContext().getNucleusContext(), clr, null, query.getCandidateClass(), null, query.getFilter(), query.getParsedImports(), query.getOrdering(), query.getResult(), query.getGrouping(), query.getHaving(), query.getExplicitParametersDeclaration(), query.getExplicitVariablesDeclaration(), null);
compilation = compiler.compile(new HashMap(), null);
// Execute the query
eval = new JDOQLInMemoryEvaluator(query, instances, compilation, null, clr);
results = (List) eval.execute(true, true, true, true, true);
assertEquals("Number of result instances was wrong", 1, results.size());
p = (Person) results.get(0);
assertEquals("Result instance has wrong first name", "Minnie", p.getFirstName());
assertEquals("Result instance has wrong last name", "Mouse", p.getLastName());
assertEquals("Person number of result instance is wrong", 103, p.getPersonNum());
assertEquals("Age of result instance is wrong", 31, p.getAge());
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown during query execution " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of org.datanucleus.store.query.inmemory.JavaQueryInMemoryEvaluator in project tests by datanucleus.
the class JDOQLEvaluatorTest method testFilterCollectionContains.
/**
* Test of filter with collectionField.contains(element).
*/
public void testFilterCollectionContains() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
// Create some instances to query over
List<UserGroup> instances = new ArrayList<>();
UserGroup grp1 = new UserGroup(101, "First Group");
UserGroup grp2 = new UserGroup(102, "Second Group");
UserGroup grp3 = new UserGroup(103, "Third Group");
GroupMember mem1 = new GroupMember(201, "Donald Duck");
GroupMember mem2 = new GroupMember(202, "Mickey Mouse");
GroupMember mem3 = new GroupMember(203, "Minnie Mouse");
grp1.getMembers().add(mem1);
grp2.getMembers().add(mem1);
grp2.getMembers().add(mem2);
grp3.getMembers().add(mem2);
grp3.getMembers().add(mem3);
instances.add(grp1);
instances.add(grp2);
instances.add(grp3);
// Compile the query
JDOQuery q = (JDOQuery) pm.newQuery(UserGroup.class, "members.contains(el) && el.name == 'Minnie Mouse'");
q.declareVariables(GroupMember.class.getName() + " el");
Query query = q.getInternalQuery();
ClassLoaderResolver clr = query.getExecutionContext().getClassLoaderResolver();
JavaQueryCompiler compiler = new JDOQLCompiler(query.getExecutionContext().getNucleusContext(), clr, null, query.getCandidateClass(), null, query.getFilter(), query.getParsedImports(), query.getOrdering(), query.getResult(), query.getGrouping(), query.getHaving(), query.getExplicitParametersDeclaration(), query.getExplicitVariablesDeclaration(), null);
QueryCompilation compilation = compiler.compile(new HashMap(), null);
// Execute the query
JavaQueryInMemoryEvaluator eval = new JDOQLInMemoryEvaluator(query, instances, compilation, null, clr);
List results = (List) eval.execute(true, true, true, true, true);
assertEquals("Number of result instances was wrong", 1, results.size());
UserGroup grp = (UserGroup) results.get(0);
assertEquals("Result instance has wrong name", "Third Group", grp.getName());
tx.commit();
} catch (Exception e) {
LOG.info(">> Unexpected exception thrown during test", e);
fail("Exception thrown during query execution " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
Aggregations