Skip to content

Commit

Permalink
COH-28552: Improve filter re-ordering algorithm for composite filters
Browse files Browse the repository at this point in the history
[git-p4: depot-paths = "//dev/coherence-ce/release/coherence-ce-v23.09/": change = 105341]
  • Loading branch information
aseovic committed Dec 12, 2023
1 parent 922e82e commit e61297d
Show file tree
Hide file tree
Showing 30 changed files with 560 additions and 269 deletions.
14 changes: 12 additions & 2 deletions prj/coherence-core/src/main/java/com/tangosol/util/Filter.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
* Copyright (c) 2000, 2020, Oracle and/or its affiliates.
* Copyright (c) 2000, 2023, Oracle and/or its affiliates.
*
* Licensed under the Universal Permissive License v 1.0 as shown at
* http://oss.oracle.com/licenses/upl.
* https://oss.oracle.com/licenses/upl.
*/
package com.tangosol.util;

Expand Down Expand Up @@ -49,6 +49,16 @@ public interface Filter<T>

// ---- Filter methods --------------------------------------------------

/**
* Return a string expression for this filter.
*
* @return a string expression for this filter
*/
public default String toExpression()
{
return toString();
}

/**
* Return a composed filter that represents a short-circuiting logical
* AND of this filter and another. When evaluating the composed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ public boolean evaluate(Object o)
return true;
}


// ----- IndexAwareFilter interface -------------------------------------

/**
Expand Down Expand Up @@ -184,4 +183,22 @@ else if (cFilters == 1)
listFilter.toArray(new Filter[cFilters]));
}
}

protected String getName()
{
switch (m_aFilter.length)
{
case 1:
return m_aFilter[0].getClass().getSimpleName();
case 2:
return "AndFilter";
default:
return super.getName();
}
}

protected String getOperator()
{
return "AND";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public boolean evaluate(T o)
return true;
}

public String toExpression()
{
return "TRUE";
}

// ----- EntryFilter interface ------------------------------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,22 @@ else if (cFilters == 1 && cMatches == 0)
return new AnyFilter((Filter[]) listFilter.toArray(new Filter[cFilters]));
}
}

protected String getName()
{
switch (m_aFilter.length)
{
case 1:
return m_aFilter[0].getClass().getSimpleName();
case 2:
return "OrFilter";
default:
return super.getName();
}
}

protected String getOperator()
{
return "OR";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.tangosol.util.QueryContext;

import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

Expand Down Expand Up @@ -70,6 +71,35 @@ public ArrayFilter(Filter[] aFilter)
}


// ----- Filter interface -----------------------------------------------

public String toExpression()
{
String sOperator = getOperator();
StringBuilder sb = new StringBuilder();

sb.append('(');

Filter[] aFilter = m_aFilter;
for (int i = 0, c = aFilter.length; i < c; i++)
{
if (i > 0)
{
sb.append(' ').append(sOperator).append(' ');
}
sb.append(aFilter[i].toExpression());
}

sb.append(')');

return sb.toString();
}

protected String getOperator()
{
throw new UnsupportedOperationException();
}

// ----- EntryFilter interface ------------------------------------------

/**
Expand Down Expand Up @@ -224,7 +254,7 @@ protected void optimizeFilterOrder(Map mapIndexes, Set setKeys)
return;
}

Set<Filter<?>> setFilter = simplifyFilters();
Set<Filter> setFilter = new LinkedHashSet<>(Arrays.asList(m_aFilter));
int cFilters = setFilter.size();
WeightedFilter[] aWeighted = new WeightedFilter[cFilters];
Filter<?>[] aFilter = new Filter[cFilters];
Expand Down Expand Up @@ -370,9 +400,8 @@ public int hashCode()
*/
public String toString()
{
String sClass = getClass().getName();
StringBuilder sb = new StringBuilder(
sClass.substring(sClass.lastIndexOf('.') + 1));
String sName = getName();
StringBuilder sb = new StringBuilder(sName);

sb.append('(');

Expand All @@ -391,6 +420,10 @@ public String toString()
return sb.toString();
}

protected String getName()
{
return getClass().getSimpleName();
}

// ----- ExternalizableLite interface -----------------------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,14 @@ public boolean evaluate(Object oTarget)
return evaluateExtracted(getValueExtractor().extract(oTarget));
}

public String toExpression()
{
return getValueExtractor().getCanonicalName() + " BETWEEN " +
(isLowerBoundInclusive() ? '[' : '(') +
getLowerBound() + ", " + getUpperBound() +
(isUpperBoundInclusive() ? ']' : ')');
}

// ----- EntryFilter interface ------------------------------------------

/**
Expand Down Expand Up @@ -428,12 +436,7 @@ protected boolean evaluateExtracted(Object oExtracted)

int cu = ((Comparable) oExtracted).compareTo(oUpperBound);

if ((fIncludeUpperBound && cu > 0) || (!fIncludeUpperBound && cu >= 0))
{
return false;
}

return true;
return (!fIncludeUpperBound || cu <= 0) && (fIncludeUpperBound || cu < 0);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@
import com.tangosol.io.pof.PofReader;
import com.tangosol.io.pof.PofWriter;

import com.tangosol.util.MapIndex;
import com.tangosol.util.ValueExtractor;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import java.util.Collection;
import java.util.Map;
import java.util.Set;

import jakarta.json.bind.annotation.JsonbProperty;

Expand Down Expand Up @@ -70,6 +69,26 @@ public ComparisonFilter(String sMethod, C value)
}


// ----- Filter interface -----------------------------------------------

public String toExpression()
{
C value = getValue();
String sValue = value instanceof Number
|| value instanceof Collection
|| value instanceof Map
|| value.getClass().isArray()
? toStringValue() : "'" + value + "'";
return getValueExtractor().getCanonicalName() + " " + getOperator() + " " + sValue;
}

protected String getOperator()
{
// it should really be abstract, but that would break custom filters that extend it,
// se let's just return an "unknown" operator
return "?";
}

// ----- accessors ------------------------------------------------------

/**
Expand Down Expand Up @@ -111,7 +130,7 @@ public boolean equals(Object o)
if (o instanceof ComparisonFilter)
{
ComparisonFilter that = (ComparisonFilter) o;
return this.getClass() == that.getClass()
return this.getClass() == that.getClass()
&& equals(this.m_extractor, that.m_extractor)
&& equals(this.m_value, that.m_value)
;
Expand All @@ -138,9 +157,7 @@ public int hashCode()
*/
public String toString()
{
String sClass = getClass().getName();

return sClass.substring(sClass.lastIndexOf('.') + 1) +
return getClass().getSimpleName() +
'(' + getValueExtractor() + ", " + toStringValue() + ')';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ public ContainsAllFilter(String sMethod, Set<?> setValues)
super(sMethod, new HashSet<>(setValues));
}

// ----- Filter interface -----------------------------------------------

protected String getOperator()
{
return "CONTAINS ALL";
}

// ----- ExtractorFilter methods ----------------------------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ public ContainsAnyFilter(String sMethod, Set<?> setValues)
super(sMethod, new HashSet<>(setValues));
}

// ----- Filter interface -----------------------------------------------

protected String getOperator()
{
return "CONTAINS ANY";
}

// ----- ExtractorFilter methods ----------------------------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ public ContainsFilter(String sMethod, E value)
super(sMethod, value);
}

// ----- Filter interface -----------------------------------------------

protected String getOperator()
{
return "CONTAINS";
}

// ----- ExtractorFilter methods ----------------------------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ public EqualsFilter(String sMethod, E value)
super(sMethod, value);
}

// ----- Filter interface -----------------------------------------------

protected String getOperator()
{
return "==";
}

// ----- ExtractorFilter methods ----------------------------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ public GreaterEqualsFilter(String sMethod, E value)
super(sMethod, value);
}

// ----- Filter interface -----------------------------------------------

protected String getOperator()
{
return ">=";
}

// ----- ExtractorFilter methods ----------------------------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ public GreaterFilter(String sMethod, E value)
super(sMethod, value);
}

// ----- Filter interface -----------------------------------------------

protected String getOperator()
{
return ">";
}

// ----- ExtractorFilter methods ----------------------------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ public InFilter(String sMethod, Set<? extends E> setValues)
super(sMethod, new HashSet<>(setValues));
}

// ----- Filter interface -----------------------------------------------

protected String getOperator()
{
return "IN";
}

// ----- ExtractorFilter methods ----------------------------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,12 @@ public boolean evaluate(T o)
throw new UnsupportedOperationException();
}

public String toExpression()
{
return "IN KEYSET " + m_setKeys;
}

// ----- EntryFilter interface ------------------------------------------
// ----- EntryFilter interface ------------------------------------------

/**
* {@inheritDoc}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
* Copyright (c) 2000, 2020, Oracle and/or its affiliates.
* Copyright (c) 2000, 2023, Oracle and/or its affiliates.
*
* Licensed under the Universal Permissive License v 1.0 as shown at
* http://oss.oracle.com/licenses/upl.
* https://oss.oracle.com/licenses/upl.
*/

package com.tangosol.util.filter;
Expand Down Expand Up @@ -47,4 +47,11 @@ public IsNotNullFilter(String sMethod)
{
super(sMethod, null);
}

// ----- Filter interface -----------------------------------------------

public String toExpression()
{
return "IS NOT NULL";
}
}
Loading

0 comments on commit e61297d

Please sign in to comment.