Search in sources :

Example 1 with JavaQueryInMemoryEvaluator

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();
    }
}
Also used : Table(org.datanucleus.store.schema.table.Table) JavaQueryInMemoryEvaluator(org.datanucleus.store.query.inmemory.JavaQueryInMemoryEvaluator) ArrayList(java.util.ArrayList) JDOQLInMemoryEvaluator(org.datanucleus.store.query.inmemory.JDOQLInMemoryEvaluator) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) HBaseBooleanExpression(org.datanucleus.store.hbase.query.expression.HBaseBooleanExpression) StoreData(org.datanucleus.store.StoreData) Filter(org.apache.hadoop.hbase.filter.Filter) Column(org.datanucleus.store.schema.table.Column) Collection(java.util.Collection) HBaseManagedConnection(org.datanucleus.store.hbase.HBaseManagedConnection) ArrayList(java.util.ArrayList) List(java.util.List) NucleusException(org.datanucleus.exceptions.NucleusException)

Example 2 with JavaQueryInMemoryEvaluator

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();
    }
}
Also used : Table(org.datanucleus.store.schema.table.Table) JavaQueryInMemoryEvaluator(org.datanucleus.store.query.inmemory.JavaQueryInMemoryEvaluator) ArrayList(java.util.ArrayList) JPQLInMemoryEvaluator(org.datanucleus.store.query.inmemory.JPQLInMemoryEvaluator) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) HBaseBooleanExpression(org.datanucleus.store.hbase.query.expression.HBaseBooleanExpression) StoreData(org.datanucleus.store.StoreData) Filter(org.apache.hadoop.hbase.filter.Filter) Column(org.datanucleus.store.schema.table.Column) Collection(java.util.Collection) HBaseManagedConnection(org.datanucleus.store.hbase.HBaseManagedConnection) ArrayList(java.util.ArrayList) List(java.util.List) NucleusException(org.datanucleus.exceptions.NucleusException)

Example 3 with JavaQueryInMemoryEvaluator

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();
    }
}
Also used : JavaQueryInMemoryEvaluator(org.datanucleus.store.query.inmemory.JavaQueryInMemoryEvaluator) ArrayList(java.util.ArrayList) JDOQLInMemoryEvaluator(org.datanucleus.store.query.inmemory.JDOQLInMemoryEvaluator) Collection(java.util.Collection) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) ArrayList(java.util.ArrayList) List(java.util.List) Properties(java.util.Properties) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData)

Example 4 with JavaQueryInMemoryEvaluator

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();
    }
}
Also used : JDOQLCompiler(org.datanucleus.store.query.compiler.JDOQLCompiler) Query(org.datanucleus.store.query.Query) JDOQuery(org.datanucleus.api.jdo.JDOQuery) PersistenceManager(javax.jdo.PersistenceManager) HashMap(java.util.HashMap) JavaQueryInMemoryEvaluator(org.datanucleus.store.query.inmemory.JavaQueryInMemoryEvaluator) ArrayList(java.util.ArrayList) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) JDOQLInMemoryEvaluator(org.datanucleus.store.query.inmemory.JDOQLInMemoryEvaluator) JDOQuery(org.datanucleus.api.jdo.JDOQuery) JavaQueryCompiler(org.datanucleus.store.query.compiler.JavaQueryCompiler) Transaction(javax.jdo.Transaction) ArrayList(java.util.ArrayList) List(java.util.List) QueryCompilation(org.datanucleus.store.query.compiler.QueryCompilation) Person(org.datanucleus.samples.models.company.Person)

Example 5 with JavaQueryInMemoryEvaluator

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();
    }
}
Also used : JDOQLCompiler(org.datanucleus.store.query.compiler.JDOQLCompiler) GroupMember(org.datanucleus.samples.one_many.unidir_2.GroupMember) Query(org.datanucleus.store.query.Query) JDOQuery(org.datanucleus.api.jdo.JDOQuery) PersistenceManager(javax.jdo.PersistenceManager) HashMap(java.util.HashMap) JavaQueryInMemoryEvaluator(org.datanucleus.store.query.inmemory.JavaQueryInMemoryEvaluator) ArrayList(java.util.ArrayList) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) JDOQLInMemoryEvaluator(org.datanucleus.store.query.inmemory.JDOQLInMemoryEvaluator) JDOQuery(org.datanucleus.api.jdo.JDOQuery) UserGroup(org.datanucleus.samples.one_many.unidir_2.UserGroup) JavaQueryCompiler(org.datanucleus.store.query.compiler.JavaQueryCompiler) Transaction(javax.jdo.Transaction) ArrayList(java.util.ArrayList) List(java.util.List) QueryCompilation(org.datanucleus.store.query.compiler.QueryCompilation)

Aggregations

ArrayList (java.util.ArrayList)24 List (java.util.List)24 JavaQueryInMemoryEvaluator (org.datanucleus.store.query.inmemory.JavaQueryInMemoryEvaluator)24 JDOQLInMemoryEvaluator (org.datanucleus.store.query.inmemory.JDOQLInMemoryEvaluator)20 HashMap (java.util.HashMap)14 PersistenceManager (javax.jdo.PersistenceManager)14 Transaction (javax.jdo.Transaction)14 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)14 JDOQuery (org.datanucleus.api.jdo.JDOQuery)14 Query (org.datanucleus.store.query.Query)14 JDOQLCompiler (org.datanucleus.store.query.compiler.JDOQLCompiler)14 JavaQueryCompiler (org.datanucleus.store.query.compiler.JavaQueryCompiler)14 QueryCompilation (org.datanucleus.store.query.compiler.QueryCompilation)14 Collection (java.util.Collection)10 Person (org.datanucleus.samples.models.company.Person)10 NucleusException (org.datanucleus.exceptions.NucleusException)8 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)8 ManagedConnection (org.datanucleus.store.connection.ManagedConnection)8 Iterator (java.util.Iterator)4 ManagedConnectionResourceListener (org.datanucleus.store.connection.ManagedConnectionResourceListener)4