Skip to content

Commit

Permalink
Cater for queries of multitenancy / softdelete candidates. Fixes #42
Browse files Browse the repository at this point in the history
  • Loading branch information
andyjefferson committed Aug 30, 2021
1 parent 40bf1de commit 1e4ad24
Showing 1 changed file with 44 additions and 14 deletions.
Expand Up @@ -48,7 +48,9 @@
import org.datanucleus.store.query.expression.OrderExpression;
import org.datanucleus.store.query.expression.ParameterExpression;
import org.datanucleus.store.query.expression.PrimaryExpression;
import org.datanucleus.store.schema.table.Column;
import org.datanucleus.store.schema.table.MemberColumnMapping;
import org.datanucleus.store.schema.table.SurrogateColumnType;
import org.datanucleus.store.schema.table.Table;
import org.datanucleus.util.NucleusLogger;
import org.datanucleus.util.StringUtils;
Expand Down Expand Up @@ -214,25 +216,55 @@ else if (query.getType() == QueryType.BULK_DELETE)
*/
protected String compileFilter()
{
if (compilation.getExprFilter() != null)
Column multitenancyCol = table.getSurrogateColumn(SurrogateColumnType.MULTITENANCY);
Column softDeleteCol = table.getSurrogateColumn(SurrogateColumnType.SOFTDELETE);

if (compilation.getExprFilter() != null || multitenancyCol != null || softDeleteCol != null)
{
compileComponent = CompilationComponent.FILTER;

String cqlString = null;
try

CassandraExpression filterCqlExpr = null;
if (multitenancyCol != null)
{
compilation.getExprFilter().evaluate(this);
CassandraExpression filterExpr = stack.pop();
cqlString = ((CassandraBooleanExpression) filterExpr).getCQL();
CassandraFieldExpression multitenancyColExpr = new CassandraFieldExpression(multitenancyCol.getName(), null);
CassandraLiteral multitenancyColValue = new CassandraLiteral(ec.getTenantId());
CassandraBooleanExpression multitenancyExpr = new CassandraBooleanExpression(multitenancyColExpr, multitenancyColValue, Expression.OP_EQ);
filterCqlExpr = multitenancyExpr;
}
catch (Exception e)

if (softDeleteCol != null)
{
// Impossible to compile all to run in the datastore, so just exit
if (NucleusLogger.QUERY.isDebugEnabled())
CassandraFieldExpression softDeleteColExpr = new CassandraFieldExpression(softDeleteCol.getName(), null);
CassandraLiteral softDeleteColValue = new CassandraLiteral(Boolean.FALSE);
CassandraBooleanExpression softDeleteExpr = new CassandraBooleanExpression(softDeleteColExpr, softDeleteColValue, Expression.OP_EQ);
filterCqlExpr = (filterCqlExpr != null) ? new CassandraBooleanExpression(filterCqlExpr, softDeleteExpr, Expression.OP_AND) : softDeleteExpr;
}

if (compilation.getExprFilter() != null)
{
// User provided filter
try
{
NucleusLogger.QUERY.debug("Compilation of filter to be evaluated completely in-datastore was impossible : " + e.getMessage());
compilation.getExprFilter().evaluate(this);
CassandraExpression filterExpr = stack.pop();
filterCqlExpr = (filterCqlExpr != null) ? new CassandraBooleanExpression(filterCqlExpr, filterExpr, Expression.OP_AND) : filterExpr;
}
filterComplete = false;
catch (Exception e)
{
// Impossible to compile all to run in the datastore, so just exit
if (NucleusLogger.QUERY.isDebugEnabled())
{
NucleusLogger.QUERY.debug("Compilation of filter to be evaluated completely in-datastore was impossible : " + e.getMessage());
}
filterComplete = false;
}
}

if (filterCqlExpr != null)
{
cqlString = ((CassandraBooleanExpression)filterCqlExpr).getCQL();
}

compileComponent = null;
Expand Down Expand Up @@ -437,8 +469,6 @@ protected Object processEqExpression(Expression expr)
{
CassandraExpression right = stack.pop();
CassandraExpression left = stack.pop();
NucleusLogger.GENERAL.info(">> processEq left=" + StringUtils.toJVMIDString(left) + left +
" right=" + StringUtils.toJVMIDString(right) + " " + right);
CassandraBooleanExpression boolExpr = new CassandraBooleanExpression(left, right, expr.getOperator());
stack.push(boolExpr);
return boolExpr;
Expand Down Expand Up @@ -563,7 +593,7 @@ else if (compileComponent == CompilationComponent.RESULT)
protected CassandraExpression getExpressionForPrimary(PrimaryExpression primExpr)
{
List<String> tuples = primExpr.getTuples();
NucleusLogger.GENERAL.info(">> getExprForPrim " + primExpr);
NucleusLogger.GENERAL.debug(">> getExprForPrim " + primExpr);
if (tuples == null || tuples.isEmpty())
{
return null;
Expand All @@ -587,7 +617,7 @@ protected CassandraExpression getExpressionForPrimary(PrimaryExpression primExpr
if (mmd != null)
{
RelationType relationType = mmd.getRelationType(ec.getClassLoaderResolver());
NucleusLogger.GENERAL.info(">> getExprForPrim name=" + name + " mmd=" + mmd.getFullFieldName() + " relType=" + relationType);
NucleusLogger.GENERAL.debug(">> getExprForPrim name=" + name + " mmd=" + mmd.getFullFieldName() + " relType=" + relationType);
if (relationType == RelationType.NONE)
{
if (iter.hasNext())
Expand Down

0 comments on commit 1e4ad24

Please sign in to comment.