-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add query search operators to support more complex index matching
- Loading branch information
Showing
40 changed files
with
1,529 additions
and
414 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
api/src/main/java/run/halo/app/extension/index/query/All.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package run.halo.app.extension.index.query; | ||
|
||
import java.util.NavigableSet; | ||
|
||
public class All extends SimpleQuery { | ||
|
||
public All(String fieldName) { | ||
super(fieldName, null); | ||
} | ||
|
||
@Override | ||
public NavigableSet<String> matches(QueryIndexView indexView) { | ||
return indexView.getAllIdsForField(fieldName); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
api/src/main/java/run/halo/app/extension/index/query/And.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package run.halo.app.extension.index.query; | ||
|
||
import com.google.common.collect.Sets; | ||
import java.util.Collection; | ||
import java.util.NavigableSet; | ||
|
||
public class And extends LogicalQuery { | ||
|
||
/** | ||
* Creates a new And query with the given child queries. | ||
* | ||
* @param childQueries The child queries | ||
*/ | ||
public And(Collection<Query> childQueries) { | ||
super(childQueries); | ||
if (this.size < 2) { | ||
throw new IllegalStateException( | ||
"An 'And' query cannot have fewer than 2 child queries, " + childQueries.size() | ||
+ " were supplied"); | ||
} | ||
} | ||
|
||
@Override | ||
public NavigableSet<String> matches(QueryIndexView indexView) { | ||
NavigableSet<String> resultSet = null; | ||
for (Query query : childQueries) { | ||
NavigableSet<String> currentResult = query.matches(indexView); | ||
indexView.removeAllFieldValuesByIdNotIn(currentResult); | ||
if (resultSet == null) { | ||
resultSet = Sets.newTreeSet(currentResult); | ||
} else { | ||
resultSet.retainAll(currentResult); | ||
} | ||
} | ||
return resultSet == null ? Sets.newTreeSet() : resultSet; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
api/src/main/java/run/halo/app/extension/index/query/Between.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package run.halo.app.extension.index.query; | ||
|
||
import com.google.common.collect.Sets; | ||
import java.util.NavigableSet; | ||
|
||
public class Between extends SimpleQuery { | ||
private final String lowerValue; | ||
private final boolean lowerInclusive; | ||
private final String upperValue; | ||
private final boolean upperInclusive; | ||
|
||
public Between(String fieldName, String lowerValue, boolean lowerInclusive, | ||
String upperValue, boolean upperInclusive) { | ||
// value and isFieldRef are not used in Between | ||
super(fieldName, null, false); | ||
this.lowerValue = lowerValue; | ||
this.lowerInclusive = lowerInclusive; | ||
this.upperValue = upperValue; | ||
this.upperInclusive = upperInclusive; | ||
} | ||
|
||
|
||
@Override | ||
public NavigableSet<String> matches(QueryIndexView indexView) { | ||
NavigableSet<String> allValues = indexView.getAllValuesForField(fieldName); | ||
// get all values in the specified range | ||
var subSet = allValues.subSet(lowerValue, lowerInclusive, upperValue, upperInclusive); | ||
|
||
var resultSet = Sets.<String>newTreeSet(); | ||
for (String val : subSet) { | ||
resultSet.addAll(indexView.getIdsForFieldValue(fieldName, val)); | ||
} | ||
return resultSet; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
api/src/main/java/run/halo/app/extension/index/query/EqualQuery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package run.halo.app.extension.index.query; | ||
|
||
import java.util.NavigableSet; | ||
|
||
public class EqualQuery extends SimpleQuery { | ||
|
||
public EqualQuery(String fieldName, String value) { | ||
super(fieldName, value); | ||
} | ||
|
||
public EqualQuery(String fieldName, String value, boolean isFieldRef) { | ||
super(fieldName, value, isFieldRef); | ||
} | ||
|
||
@Override | ||
public NavigableSet<String> matches(QueryIndexView indexView) { | ||
if (isFieldRef) { | ||
return resultSetForRefValue(indexView); | ||
} | ||
return resultSetForExactValue(indexView); | ||
} | ||
|
||
private NavigableSet<String> resultSetForRefValue(QueryIndexView indexView) { | ||
return indexView.findIdsForFieldValueEqual(fieldName, value); | ||
} | ||
|
||
private NavigableSet<String> resultSetForExactValue(QueryIndexView indexView) { | ||
return indexView.getIdsForFieldValue(fieldName, value); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
api/src/main/java/run/halo/app/extension/index/query/GreaterThanQuery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package run.halo.app.extension.index.query; | ||
|
||
import com.google.common.collect.Sets; | ||
import java.util.NavigableSet; | ||
|
||
public class GreaterThanQuery extends SimpleQuery { | ||
private final boolean orEqual; | ||
|
||
public GreaterThanQuery(String fieldName, String value, boolean orEqual) { | ||
this(fieldName, value, orEqual, false); | ||
} | ||
|
||
public GreaterThanQuery(String fieldName, String value, boolean orEqual, boolean isFieldRef) { | ||
super(fieldName, value, isFieldRef); | ||
this.orEqual = orEqual; | ||
} | ||
|
||
@Override | ||
public NavigableSet<String> matches(QueryIndexView indexView) { | ||
if (isFieldRef) { | ||
return resultSetForRefValue(indexView); | ||
} | ||
return resultSetForExtractValue(indexView); | ||
} | ||
|
||
private NavigableSet<String> resultSetForRefValue(QueryIndexView indexView) { | ||
return indexView.findIdsForFieldValueGreaterThan(fieldName, value, orEqual); | ||
} | ||
|
||
private NavigableSet<String> resultSetForExtractValue(QueryIndexView indexView) { | ||
var resultSet = Sets.<String>newTreeSet(); | ||
var allValues = indexView.getAllValuesForField(fieldName); | ||
NavigableSet<String> tailSet = | ||
orEqual ? allValues.tailSet(value, true) : allValues.tailSet(value, false); | ||
|
||
for (String val : tailSet) { | ||
resultSet.addAll(indexView.getIdsForFieldValue(fieldName, val)); | ||
} | ||
return resultSet; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
api/src/main/java/run/halo/app/extension/index/query/InQuery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package run.halo.app.extension.index.query; | ||
|
||
import com.google.common.collect.Sets; | ||
import java.util.NavigableSet; | ||
import java.util.Set; | ||
|
||
public class InQuery extends SimpleQuery { | ||
private final Set<String> values; | ||
|
||
public InQuery(String columnName, Set<String> values) { | ||
super(columnName, null); | ||
this.values = values; | ||
} | ||
|
||
@Override | ||
public NavigableSet<String> matches(QueryIndexView indexView) { | ||
NavigableSet<String> resultSet = Sets.newTreeSet(); | ||
for (String val : values) { | ||
resultSet.addAll(indexView.getIdsForFieldValue(fieldName, val)); | ||
} | ||
return resultSet; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
api/src/main/java/run/halo/app/extension/index/query/LessThanQuery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package run.halo.app.extension.index.query; | ||
|
||
import com.google.common.collect.Sets; | ||
import java.util.NavigableSet; | ||
|
||
public class LessThanQuery extends SimpleQuery { | ||
private final boolean orEqual; | ||
|
||
public LessThanQuery(String fieldName, String value, boolean orEqual) { | ||
this(fieldName, value, orEqual, false); | ||
} | ||
|
||
public LessThanQuery(String fieldName, String value, boolean orEqual, boolean isFieldRef) { | ||
super(fieldName, value, isFieldRef); | ||
this.orEqual = orEqual; | ||
} | ||
|
||
@Override | ||
public NavigableSet<String> matches(QueryIndexView indexView) { | ||
if (isFieldRef) { | ||
return resultSetForRefValue(indexView); | ||
} | ||
return resultSetForExactValue(indexView); | ||
} | ||
|
||
private NavigableSet<String> resultSetForRefValue(QueryIndexView indexView) { | ||
return indexView.findIdsForFieldValueLessThan(fieldName, value, orEqual); | ||
} | ||
|
||
private NavigableSet<String> resultSetForExactValue(QueryIndexView indexView) { | ||
var resultSet = Sets.<String>newTreeSet(); | ||
var allValues = indexView.getAllValuesForField(fieldName); | ||
var headSet = orEqual ? allValues.headSet(value, true) | ||
: allValues.headSet(value, false); | ||
|
||
for (String val : headSet) { | ||
resultSet.addAll(indexView.getIdsForFieldValue(fieldName, val)); | ||
} | ||
return resultSet; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
api/src/main/java/run/halo/app/extension/index/query/LogicalQuery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package run.halo.app.extension.index.query; | ||
|
||
import java.util.Collection; | ||
import java.util.Objects; | ||
|
||
public abstract class LogicalQuery implements Query { | ||
protected final Collection<Query> childQueries; | ||
protected final int size; | ||
|
||
/** | ||
* Creates a new logical query with the given child queries. | ||
* | ||
* @param childQueries with the given child queries. | ||
*/ | ||
public LogicalQuery(Collection<Query> childQueries) { | ||
Objects.requireNonNull(childQueries, | ||
"The child queries supplied to a logical query cannot be null"); | ||
for (Query query : childQueries) { | ||
if (!isValid(query)) { | ||
throw new IllegalStateException("Unexpected type of query: " + (query == null ? null | ||
: query + ", " + query.getClass())); | ||
} | ||
} | ||
this.size = childQueries.size(); | ||
this.childQueries = childQueries; | ||
} | ||
|
||
boolean isValid(Query query) { | ||
return query instanceof LogicalQuery || query instanceof SimpleQuery; | ||
} | ||
} |
Oops, something went wrong.