Checkstyle

This commit is contained in:
Egor Cherniak 2022-01-13 16:12:46 +03:00
parent d8aed54f24
commit 74cee2fc36
No known key found for this signature in database
GPG Key ID: 26F47333B7BE4ED9
23 changed files with 275 additions and 240 deletions

View File

@ -14,7 +14,7 @@ jobs:
uses: actions/checkout@v2
- name: Deploy package
uses: valitydev/action-deploy-jdk-package@v1.0.7
uses: valitydev/action-deploy-jdk-package@v1.0.9
with:
server-username: ${{ secrets.OSSRH_USERNAME }}
server-password: ${{ secrets.OSSRH_TOKEN }}

View File

@ -7,22 +7,13 @@ import java.util.function.Function;
import java.util.stream.Collectors;
public class BaseCompositeQuery<T, CT> extends BaseQuery<T, CT> implements CompositeQuery<T, CT> {
private List<Query> queries;
private final Function<QueryContext, QueryResult<T, CT>> execFunction;
private final BiFunction<QueryContext, List<QueryResult>, QueryResult<T, CT>> parallelExecFunction;
private List<Query> queries;
public static <T, CT> BaseCompositeQuery<T, CT> newInstance(Object descriptor, QueryParameters params, List<Query> queries, Function<QueryContext, QueryResult<T, CT>> execFunction) {
return newInstance(descriptor, params, queries, execFunction, null);
}
public static <T, CT> BaseCompositeQuery<T, CT> newInstance(Object descriptor, QueryParameters params, List<Query> queries, Function<QueryContext, QueryResult<T, CT>> execFunction, BiFunction<QueryContext, List<QueryResult>, QueryResult<T, CT>> parallelExecFunction) {
BaseCompositeQuery<T, CT> compositeQuery = new BaseCompositeQuery<>(descriptor, params, execFunction, parallelExecFunction);
compositeQuery.setChildQueries(queries);
return compositeQuery;
}
public BaseCompositeQuery(Object descriptor, QueryParameters params, Function<QueryContext, QueryResult<T, CT>> execFunction, BiFunction<QueryContext, List<QueryResult>, QueryResult<T, CT>> parallelExecFunction) {
public BaseCompositeQuery(Object descriptor, QueryParameters params,
Function<QueryContext, QueryResult<T, CT>> execFunction,
BiFunction<QueryContext, List<QueryResult>, QueryResult<T, CT>> parallelExecFunction) {
super(descriptor, params);
if (execFunction == null) {
throw new NullPointerException("Null exec function is not allowed");
@ -31,10 +22,32 @@ public class BaseCompositeQuery<T, CT> extends BaseQuery<T, CT> implements Compo
this.parallelExecFunction = parallelExecFunction;
}
public static <T, CT> BaseCompositeQuery<T, CT> newInstance(Object descriptor, QueryParameters params,
List<Query> queries,
Function<QueryContext,
QueryResult<T, CT>> execFunction) {
return newInstance(descriptor, params, queries, execFunction, null);
}
public static <T, CT> BaseCompositeQuery<T, CT> newInstance(Object descriptor, QueryParameters params,
List<Query> queries,
Function<QueryContext, QueryResult<T, CT>> execFunction,
BiFunction<QueryContext, List<QueryResult>,
QueryResult<T, CT>> parallelExecFunction) {
BaseCompositeQuery<T, CT> compositeQuery =
new BaseCompositeQuery<>(descriptor, params, execFunction, parallelExecFunction);
compositeQuery.setChildQueries(queries);
return compositeQuery;
}
public List<Query> getChildQueries() {
return queries;
}
protected void setChildQueries(List<Query> queries) {
this.queries = queries.stream().peek(query -> query.setParentQuery(this)).collect(Collectors.toList());
}
public boolean isParallel() {
return parallelExecFunction != null;
}
@ -45,15 +58,12 @@ public class BaseCompositeQuery<T, CT> extends BaseQuery<T, CT> implements Compo
}
@Override
public QueryResult<T, CT> execute(QueryContext context, List<QueryResult> collectedResults) throws QueryExecutionException {
public QueryResult<T, CT> execute(QueryContext context, List<QueryResult> collectedResults)
throws QueryExecutionException {
if (parallelExecFunction != null) {
return parallelExecFunction.apply(context, collectedResults);
} else {
return CompositeQuery.super.execute(context, collectedResults);
}
}
protected void setChildQueries(List<Query> queries) {
this.queries = queries.stream().peek(query -> query.setParentQuery(this)).collect(Collectors.toList());
}
}

View File

@ -21,9 +21,11 @@ public abstract class BaseQueryValidator implements QueryValidator {
}
}
protected <T extends QueryParameters> T checkParamsType(QueryParameters parameters, Class<? extends QueryParameters> expectedType) {
protected <T extends QueryParameters> T checkParamsType(QueryParameters parameters,
Class<? extends QueryParameters> expectedType) {
if (!expectedType.isAssignableFrom(parameters.getClass())) {
checkParamsResult(true, "Parameters has wrong type:" + parameters.getClass() + ", expected: " + expectedType);
checkParamsResult(true,
"Parameters has wrong type:" + parameters.getClass() + ", expected: " + expectedType);
}
return (T) parameters;
}

View File

@ -9,7 +9,8 @@ public interface CompositeQuery<T, CT> extends Query<T, CT> {
return false;
}
default QueryResult<T, CT> execute(QueryContext context, List<QueryResult> collectedResults) throws QueryExecutionException {
default QueryResult<T, CT> execute(QueryContext context, List<QueryResult> collectedResults)
throws QueryExecutionException {
throw new UnsupportedOperationException("Explicit implementation required");
}

View File

@ -7,7 +7,7 @@ public abstract class PagedBaseFunction<T, CT> extends BaseFunction<T, CT> {
public static final int MAX_SIZE_VALUE = 1000;
private String continuationToken;
private final String continuationToken;
public PagedBaseFunction(Object descriptor, QueryParameters params, String name, String continuationToken) {
super(descriptor, params, name);
@ -33,8 +33,7 @@ public abstract class PagedBaseFunction<T, CT> extends BaseFunction<T, CT> {
}
public Integer getSize() {
return Optional.ofNullable(getIntParameter(Parameters.SIZE_PARAMETER, true))
.orElse(MAX_SIZE_VALUE);
return Optional.ofNullable(getIntParameter(Parameters.SIZE_PARAMETER, true)).orElse(MAX_SIZE_VALUE);
}
}
@ -44,7 +43,8 @@ public abstract class PagedBaseFunction<T, CT> extends BaseFunction<T, CT> {
public void validateQuery(Query query) throws IllegalArgumentException {
super.validateQuery(query);
if (query instanceof PagedBaseFunction) {
validateContinuationToken(query.getQueryParameters(), ((PagedBaseFunction) query).getContinuationToken());
validateContinuationToken(query.getQueryParameters(),
((PagedBaseFunction) query).getContinuationToken());
}
}
@ -53,15 +53,12 @@ public abstract class PagedBaseFunction<T, CT> extends BaseFunction<T, CT> {
super.validateParameters(parameters);
PagedBaseParameters pagedBaseParameters = super.checkParamsType(parameters, PagedBaseParameters.class);
checkParamsResult(pagedBaseParameters.getSize() > MAX_SIZE_VALUE,
String.format(
"Size must be less or equals to %d but was %d",
MAX_SIZE_VALUE,
pagedBaseParameters.getSize()
)
);
String.format("Size must be less or equals to %d but was %d", MAX_SIZE_VALUE,
pagedBaseParameters.getSize()));
}
private void validateContinuationToken(QueryParameters queryParameters, String continuationToken) throws BadTokenException {
private void validateContinuationToken(QueryParameters queryParameters, String continuationToken)
throws BadTokenException {
try {
TokenUtil.validateToken(queryParameters, continuationToken);
} catch (IllegalArgumentException ex) {

View File

@ -16,7 +16,8 @@ public class QueryExecutionException extends QueryProcessingException {
super(cause);
}
public QueryExecutionException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
public QueryExecutionException(String message, Throwable cause, boolean enableSuppression,
boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}

View File

@ -9,10 +9,6 @@ import java.util.Map;
import java.util.Objects;
public class QueryParameters {
public interface QueryParametersRef<T extends QueryParameters> {
T newInstance(Map<String, Object> parameters, QueryParameters derivedParameters);
}
private final Map<String, Object> parameters;
private final QueryParameters derivedParameters;
@ -26,7 +22,8 @@ public class QueryParameters {
}
public Object getParameter(String key, boolean deepSearch) {
return parameters.getOrDefault(key, deepSearch && derivedParameters != null ? derivedParameters.getParameter(key, deepSearch) : null);
return parameters.getOrDefault(key,
deepSearch && derivedParameters != null ? derivedParameters.getParameter(key, deepSearch) : null);
}
public <T extends QueryParameters> T removeParameters(QueryParametersRef<T> parametersRef, String... keys) {
@ -99,11 +96,15 @@ public class QueryParameters {
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
QueryParameters that = (QueryParameters) o;
return Objects.equals(parameters, that.parameters) &&
Objects.equals(derivedParameters, that.derivedParameters);
return Objects.equals(parameters, that.parameters)
&& Objects.equals(derivedParameters, that.derivedParameters);
}
@Override
@ -113,9 +114,11 @@ public class QueryParameters {
@Override
public String toString() {
return "QueryParameters{" +
"parameters=" + parameters +
", derivedParameters=" + (derivedParameters == null ? "null" : "notnull") +
'}';
return "QueryParameters{" + "parameters=" + parameters + ", derivedParameters=" +
(derivedParameters == null ? "null" : "notnull") + '}';
}
public interface QueryParametersRef<T extends QueryParameters> {
T newInstance(Map<String, Object> parameters, QueryParameters derivedParameters);
}
}

View File

@ -16,7 +16,8 @@ public class QueryProcessingException extends RuntimeException {
super(cause);
}
public QueryProcessingException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
public QueryProcessingException(String message, Throwable cause, boolean enableSuppression,
boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}

View File

@ -63,14 +63,14 @@ public class RootQuery extends BaseQuery {
if (query instanceof RootQuery) {
Query childQuery = ((RootQuery) query).childQuery;
if (childQuery instanceof CompositeQuery) {
Optional<? extends Collection> childQueries = Optional.ofNullable(((CompositeQuery) childQuery).getChildQueries());
checkParamsResult(
!childQueries.isPresent() || childQueries.get().isEmpty(),
"Request must contain at least one query"
);
Optional<? extends Collection> childQueries =
Optional.ofNullable(((CompositeQuery) childQuery).getChildQueries());
checkParamsResult(!childQueries.isPresent() || childQueries.get().isEmpty(),
"Request must contain at least one query");
}
} else if (query instanceof CompositeQuery) {
checkParamsResult(true, "Request can't hold more than one query, received count: " + ((CompositeQuery) query).getChildQueries().size());
checkParamsResult(true, "Request can't hold more than one query, received count: " +
((CompositeQuery) query).getChildQueries().size());
} else {
assert false : "No other types expected here";
}
@ -78,14 +78,19 @@ public class RootQuery extends BaseQuery {
}
public static class RootParser extends AbstractQueryParser {
private RootValidator validator = new RootValidator();
private final RootValidator validator = new RootValidator();
public static String getMainDescriptor() {
return Parameters.QUERY_PARAMETER;
}
@Override
public List<QueryPart> parseQuery(Map<String, Object> source, QueryPart parent) throws QueryParserException {
RootParameters parameters = getValidatedParameters(source, parent, RootParameters::new, validator);
return Stream.of(
new QueryPart(getMainDescriptor(), new QueryParameters(parameters.getQuery(), parameters), parent),
new QueryPart(getMainDescriptor(),
new QueryParameters(parameters.getQuery(), parameters), parent),
new QueryPart(QueryPart.DEFAULT_DESCRIPTOR, parameters.getRestParameters(), parent))
.collect(Collectors.toList());
}
@ -94,24 +99,23 @@ public class RootQuery extends BaseQuery {
public boolean apply(Map source, QueryPart parent) {
return parent == null;
}
public static String getMainDescriptor() {
return Parameters.QUERY_PARAMETER;
}
}
public static class RootBuilder extends AbstractQueryBuilder {
private RootValidator validator = new RootValidator();
private final RootValidator validator = new RootValidator();
@Override
public Query buildQuery(List<QueryPart> queryParts, String continuationToken, QueryPart parentQueryPart, QueryBuilder baseBuilder) throws QueryBuilderException {
Query resultQuery = buildSingleQuery(RootParser.getMainDescriptor(), queryParts, queryPart -> createQuery(queryPart, continuationToken, baseBuilder));
public Query buildQuery(List<QueryPart> queryParts, String continuationToken, QueryPart parentQueryPart,
QueryBuilder baseBuilder) throws QueryBuilderException {
Query resultQuery = buildSingleQuery(RootParser.getMainDescriptor(), queryParts,
queryPart -> createQuery(queryPart, continuationToken, baseBuilder));
validator.validateQuery(resultQuery);
return resultQuery;
}
private RootQuery createQuery(QueryPart queryPart, String continuationToken, QueryBuilder baseBuilder) {
Query childQuery = baseBuilder.buildQuery(queryPart.getChildren(), continuationToken, queryPart, baseBuilder);
Query childQuery =
baseBuilder.buildQuery(queryPart.getChildren(), continuationToken, queryPart, baseBuilder);
RootQuery rootQuery = new RootQuery(queryPart.getDescriptor(), queryPart.getParameters(), childQuery);
childQuery.setParentQuery(rootQuery);
return rootQuery;

View File

@ -17,54 +17,48 @@ public abstract class AbstractQueryBuilder implements QueryBuilder {
}
protected <T, CT> CompositeQuery<T, CT> createCompositeQuery(
Object descriptor,
QueryParameters derivedParameters,
protected <T, CT> CompositeQuery<T, CT> createCompositeQuery(Object descriptor, QueryParameters derivedParameters,
List<Query> childQueries,
Function<QueryContext, QueryResult<T, CT>> execFunction,
BiFunction<QueryContext, List<QueryResult>, QueryResult<T, CT>> parallelExecFunction
) {
return BaseCompositeQuery.newInstance(descriptor, new QueryParameters(Collections.emptyMap(), derivedParameters), childQueries, execFunction, parallelExecFunction);
Function<QueryContext,
QueryResult<T, CT>> execFunction,
BiFunction<QueryContext, List<QueryResult>,
QueryResult<T, CT>> parallelExecFunction) {
return BaseCompositeQuery.newInstance(descriptor,
new QueryParameters(Collections.emptyMap(), derivedParameters), childQueries, execFunction,
parallelExecFunction);
}
protected CompositeQuery createCompositeQuery(
Object descriptor,
QueryParameters derivedParameters,
List<Query> childQueries
) {
return createCompositeQuery(
descriptor, derivedParameters, childQueries,
context -> new BaseQueryResult<>(
() -> childQueries.stream().map(query -> query.execute(context)),
() -> childQueries.stream().map(query -> query.execute(context)).collect(Collectors.toList())
),
(context, queryResults) -> new BaseQueryResult<>(
() -> queryResults.stream(),
() -> queryResults
)
protected CompositeQuery createCompositeQuery(Object descriptor, QueryParameters derivedParameters,
List<Query> childQueries) {
return createCompositeQuery(descriptor, derivedParameters, childQueries,
context -> new BaseQueryResult<>(() -> childQueries.stream().map(query -> query.execute(context)),
() -> childQueries.stream().map(query -> query.execute(context)).collect(Collectors.toList())),
(context, queryResults) -> new BaseQueryResult<>(() -> queryResults.stream(), () -> queryResults)
);
}
protected <T, CT> CompositeQuery<T, CT> createCompositeQuery(
Object descriptor,
List<Query> childQueries,
Function<QueryContext, QueryResult<T, CT>> execFunction,
BiFunction<QueryContext, List<QueryResult>, QueryResult<T, CT>> parallelExecFunction
) {
return createCompositeQuery(descriptor, new QueryParameters(Collections.emptyMap(), null), childQueries, execFunction, parallelExecFunction);
protected <T, CT> CompositeQuery<T, CT> createCompositeQuery(Object descriptor, List<Query> childQueries,
Function<QueryContext,
QueryResult<T, CT>> execFunction,
BiFunction<QueryContext, List<QueryResult>,
QueryResult<T, CT>> parallelExecFunction) {
return createCompositeQuery(descriptor, new QueryParameters(Collections.emptyMap(), null),
childQueries, execFunction, parallelExecFunction);
}
protected QueryParameters getParameters(QueryPart queryPart) {
return queryPart == null ? null : queryPart.getParameters();
}
protected List<Query> buildQueries(Object matchDescriptor, List<QueryPart> queryParts, Function<QueryPart, Query> queryCreator) {
protected List<Query> buildQueries(Object matchDescriptor, List<QueryPart> queryParts,
Function<QueryPart, Query> queryCreator) {
List<QueryPart> matchedParts = getMatchedPartsStream(matchDescriptor, queryParts).collect(Collectors.toList());
return matchedParts.stream().map(queryPart -> queryCreator.apply(queryPart)).collect(Collectors.toList());
}
protected Query buildSingleQuery(Object matchDescriptor, List<QueryPart> queryParts, Function<QueryPart, Query> queryCreator) {
protected Query buildSingleQuery(Object matchDescriptor, List<QueryPart> queryParts,
Function<QueryPart, Query> queryCreator) {
List<Query> queries = buildQueries(matchDescriptor, queryParts, queryCreator);
if (queries.size() == 0) {
throw new QueryBuilderException("No queries found in referred data");
@ -75,15 +69,14 @@ public abstract class AbstractQueryBuilder implements QueryBuilder {
}
}
protected <T, CT> Query<T, CT> buildAndWrapQueries(
Object matchDescriptor,
List<QueryPart> queryParts,
protected <T, CT> Query<T, CT> buildAndWrapQueries(Object matchDescriptor, List<QueryPart> queryParts,
Function<QueryPart, Query> queryCreator,
QueryParameters parentParameters,
Function<QueryContext, QueryResult<T, CT>> execFunction,
BiFunction<QueryContext, List<QueryResult>, QueryResult<T, CT>> parallelExecFunction
) {
BiFunction<QueryContext, List<QueryResult>,
QueryResult<T, CT>> parallelExecFunction) {
List<Query> queries = buildQueries(matchDescriptor, queryParts, queryCreator);
return createCompositeQuery(queries.get(0).getDescriptor(), parentParameters, queries, execFunction, parallelExecFunction);
return createCompositeQuery(queries.get(0).getDescriptor(), parentParameters, queries, execFunction,
parallelExecFunction);
}
}

View File

@ -22,15 +22,13 @@ public abstract class BaseQueryBuilder implements QueryBuilder {
}
@Override
public Query buildQuery(List<QueryPart> queryParts, String continuationToken, QueryPart parentQueryPart, QueryBuilder baseBuilder) throws QueryBuilderException {
public Query buildQuery(List<QueryPart> queryParts, String continuationToken, QueryPart parentQueryPart,
QueryBuilder baseBuilder) throws QueryBuilderException {
try {
List<Query> queries = queryParts.isEmpty() ? Collections.emptyList() : builders.stream()
.filter(
builder -> builder.apply(queryParts, parentQueryPart))
.map(
builder -> builder.buildQuery(queryParts, continuationToken, parentQueryPart, baseBuilder == null ? this : baseBuilder)
)
.collect(Collectors.toList());
List<Query> queries = queryParts.isEmpty() ? Collections.emptyList() :
builders.stream().filter(builder -> builder.apply(queryParts, parentQueryPart))
.map(builder -> builder.buildQuery(queryParts, continuationToken, parentQueryPart,
baseBuilder == null ? this : baseBuilder)).collect(Collectors.toList());
if (queries.size() > 1) {
throw new QueryBuilderException("Build result has more than one query");
} else if (queries.size() == 1) {

View File

@ -7,7 +7,8 @@ import java.util.List;
public interface QueryBuilder {
Query buildQuery(List<QueryPart> queryParts, String continuationToken, QueryPart parentQueryPart, QueryBuilder baseBuilder) throws QueryBuilderException;
Query buildQuery(List<QueryPart> queryParts, String continuationToken, QueryPart parentQueryPart,
QueryBuilder baseBuilder) throws QueryBuilderException;
boolean apply(List<QueryPart> queryParts, QueryPart parent);
}

View File

@ -18,7 +18,8 @@ public class QueryBuilderException extends QueryProcessingException {
super(cause);
}
public QueryBuilderException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
public QueryBuilderException(String message, Throwable cause, boolean enableSuppression,
boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}

View File

@ -11,7 +11,10 @@ public abstract class AbstractQueryParser implements QueryParser<Map<String, Obj
return queryPart == null ? null : queryPart.getParameters();
}
protected <T extends QueryParameters> T getValidatedParameters(Map<String, Object> source, QueryPart parent, QueryParameters.QueryParametersRef<T> parametersRef, QueryValidator validator) throws QueryParserException {
protected <T extends QueryParameters> T getValidatedParameters(Map<String, Object> source, QueryPart parent,
QueryParameters.QueryParametersRef<T> parametersRef,
QueryValidator validator)
throws QueryParserException {
try {
T parameters = parametersRef.newInstance(source, getParameters(parent));
validator.validateParameters(parameters);

View File

@ -15,7 +15,7 @@ public class JsonQueryParser implements QueryParser<String> {
private QueryParser<Map<String, Object>> queryPartParser;
public JsonQueryParser withQueryParser(QueryParser<Map<String, Object>> queryPartParser){
public JsonQueryParser withQueryParser(QueryParser<Map<String, Object>> queryPartParser) {
this.queryPartParser = queryPartParser;
return this;
}
@ -32,7 +32,7 @@ public class JsonQueryParser implements QueryParser<String> {
return queryPartParser.parseQuery(jsonMap, parent);
} catch (IOException e) {
throw new QueryParserException("Failed to parse received json request: "+e.getMessage(), e);
throw new QueryParserException("Failed to parse received json request: " + e.getMessage(), e);
}
}
@ -47,6 +47,7 @@ public class JsonQueryParser implements QueryParser<String> {
}
protected Map<String, Object> parseJsonMap(String data) throws IOException {
return getMapper().readerFor(new TypeReference<Map<String, Object>>() {}).readValue(data);
return getMapper().readerFor(new TypeReference<Map<String, Object>>() {
}).readValue(data);
}
}

View File

@ -3,7 +3,8 @@ package dev.vality.magista.dsl.parser;
import java.util.List;
public interface QueryParser<Src> {
List<QueryPart> parseQuery(Src source, QueryPart parent) throws QueryParserException;
boolean apply(Src source, QueryPart parent);
public interface QueryParser<T> {
List<QueryPart> parseQuery(T source, QueryPart parent) throws QueryParserException;
boolean apply(T source, QueryPart parent);
}

View File

@ -18,7 +18,8 @@ public class QueryParserException extends QueryExecutionException {
super(cause);
}
public QueryParserException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
public QueryParserException(String message, Throwable cause, boolean enableSuppression,
boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}

View File

@ -8,8 +8,8 @@ public class QueryPart {
public static final Object DEFAULT_DESCRIPTOR = "default_descriptor";
private final Object descriptor;
private final QueryParameters parameters;
private List<QueryPart> children;
private final QueryPart parent;
private List<QueryPart> children;
public QueryPart(Object descriptor, QueryParameters parameters) {
this(descriptor, parameters, null);
@ -45,16 +45,13 @@ public class QueryPart {
}
public boolean isEmpty() {
return descriptor.equals(DEFAULT_DESCRIPTOR) && parameters.getParametersMap().isEmpty() && (children == null || children.isEmpty());
return descriptor.equals(DEFAULT_DESCRIPTOR) && parameters.getParametersMap().isEmpty()
&& (children == null || children.isEmpty());
}
@Override
public String toString() {
return "QueryPart{" +
"descriptor=" + descriptor +
", parameters=" + parameters +
", children=" + children +
", parent=" + (parent == null ? "null" : "notnull") +
'}';
return "QueryPart{" + "descriptor=" + descriptor + ", parameters=" + parameters + ", children=" + children +
", parent=" + (parent == null ? "null" : "notnull") + '}';
}
}

View File

@ -15,46 +15,53 @@ import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class DummiesFunction extends PagedBaseFunction<Map.Entry<Long, Dummy>, StatTestResponse> implements CompositeQuery<Map.Entry<Long, Dummy>, StatTestResponse> {
public class DummiesFunction extends PagedBaseFunction<Map.Entry<Long, Dummy>, StatTestResponse>
implements CompositeQuery<Map.Entry<Long, Dummy>, StatTestResponse> {
public static final String FUNC_NAME = "dummies";
private final CompositeQuery<QueryResult, List<QueryResult>> subquery;
private DummiesFunction(Object descriptor, QueryParameters params, String continuationToken, CompositeQuery<QueryResult, List<QueryResult>> subquery) {
private DummiesFunction(Object descriptor, QueryParameters params, String continuationToken,
CompositeQuery<QueryResult, List<QueryResult>> subquery) {
super(descriptor, params, FUNC_NAME, continuationToken);
this.subquery = subquery;
}
private static DummiesFunction createDummiesFunction(Object descriptor, QueryParameters queryParameters,
String continuationToken,
CompositeQuery<QueryResult, List<QueryResult>> subquery) {
DummiesFunction dummiesFunction = new DummiesFunction(descriptor, queryParameters, continuationToken, subquery);
subquery.setParentQuery(dummiesFunction);
return dummiesFunction;
}
@Override
public QueryResult<Map.Entry<Long, Dummy>, StatTestResponse> execute(QueryContext context) throws QueryExecutionException {
public QueryResult<Map.Entry<Long, Dummy>, StatTestResponse> execute(QueryContext context)
throws QueryExecutionException {
QueryResult<QueryResult, List<QueryResult>> collectedResults = subquery.execute(context);
return execute(context, collectedResults.getCollectedStream());
}
@Override
public QueryResult<Map.Entry<Long, Dummy>, StatTestResponse> execute(QueryContext context, List<QueryResult> collectedResults) throws QueryExecutionException {
QueryResult<Map.Entry<Long, Dummy>, List<Map.Entry<Long, Dummy>>> dummiesResult = (QueryResult<Map.Entry<Long, Dummy>, List<Map.Entry<Long, Dummy>>>) collectedResults.get(0);
public QueryResult<Map.Entry<Long, Dummy>, StatTestResponse> execute(QueryContext context,
List<QueryResult> collectedResults)
throws QueryExecutionException {
QueryResult<Map.Entry<Long, Dummy>, List<Map.Entry<Long, Dummy>>> dummiesResult =
(QueryResult<Map.Entry<Long, Dummy>, List<Map.Entry<Long, Dummy>>>) collectedResults.get(0);
return new BaseQueryResult<>(
() -> dummiesResult.getDataStream(),
() -> {
List<Dummy> statResponseData = dummiesResult.getDataStream()
.map(dummyEntry -> dummyEntry.getValue()).collect(Collectors.toList());
return new BaseQueryResult<>(() -> dummiesResult.getDataStream(), () -> {
List<Dummy> statResponseData =
dummiesResult.getDataStream().map(dummyEntry -> dummyEntry.getValue()).collect(Collectors.toList());
StatTestResponse statResponse = new StatTestResponse(statResponseData);
List<Map.Entry<Long, Dummy>> dummyStats = dummiesResult.getCollectedStream();
if (!dummiesResult.getCollectedStream().isEmpty() && getQueryParameters().getSize() == dummyStats.size()) {
statResponse.setContinuationToken(
TokenUtil.buildToken(
getQueryParameters(),
dummyStats.get(dummyStats.size() - 1).getKey()
)
);
TokenUtil.buildToken(getQueryParameters(), dummyStats.get(dummyStats.size() - 1).getKey()));
}
return statResponse;
}
);
});
}
@Override
@ -122,51 +129,47 @@ public class DummiesFunction extends PagedBaseFunction<Map.Entry<Long, Dummy>, S
}
public static class DummiesParser extends AbstractQueryParser {
private DummiesValidator validator = new DummiesValidator();
private final DummiesValidator validator = new DummiesValidator();
public static String getMainDescriptor() {
return FUNC_NAME;
}
@Override
public List<QueryPart> parseQuery(Map<String, Object> source, QueryPart parent) throws QueryParserException {
Map<String, Object> funcSource = (Map) source.get(FUNC_NAME);
TestParameters parameters = getValidatedParameters(funcSource, parent, TestParameters::new, validator);
return Stream.of(
new QueryPart(FUNC_NAME, parameters, parent)
)
.collect(Collectors.toList());
return Stream.of(new QueryPart(FUNC_NAME, parameters, parent)).collect(Collectors.toList());
}
@Override
public boolean apply(Map source, QueryPart parent) {
return parent != null
&& RootQuery.RootParser.getMainDescriptor().equals(parent.getDescriptor())
return parent != null && RootQuery.RootParser.getMainDescriptor().equals(parent.getDescriptor())
&& (source.get(FUNC_NAME) instanceof Map);
}
public static String getMainDescriptor() {
return FUNC_NAME;
}
}
public static class DummiesBuilder extends AbstractQueryBuilder {
private DummiesValidator validator = new DummiesValidator();
private final DummiesValidator validator = new DummiesValidator();
@Override
public Query buildQuery(List<QueryPart> queryParts, String continuationToken, QueryPart parentQueryPart, QueryBuilder baseBuilder) throws QueryBuilderException {
Query resultQuery = buildSingleQuery(DummiesParser.getMainDescriptor(), queryParts, queryPart -> createQuery(queryPart, continuationToken));
public Query buildQuery(List<QueryPart> queryParts, String continuationToken, QueryPart parentQueryPart,
QueryBuilder baseBuilder) throws QueryBuilderException {
Query resultQuery = buildSingleQuery(DummiesParser.getMainDescriptor(), queryParts,
queryPart -> createQuery(queryPart, continuationToken));
validator.validateQuery(resultQuery);
return resultQuery;
}
private CompositeQuery createQuery(QueryPart queryPart, String continuationToken) {
List<Query> queries = Arrays.asList(
new GetDataFunction(queryPart.getDescriptor() + ":" + GetDataFunction.FUNC_NAME, queryPart.getParameters(), continuationToken)
);
CompositeQuery<QueryResult, List<QueryResult>> compositeQuery = createCompositeQuery(
queryPart.getDescriptor(),
getParameters(queryPart.getParent()),
queries
);
return createDummiesFunction(queryPart.getDescriptor(), queryPart.getParameters(), continuationToken, compositeQuery);
new GetDataFunction(queryPart.getDescriptor() + ":" + GetDataFunction.FUNC_NAME,
queryPart.getParameters(), continuationToken));
CompositeQuery<QueryResult, List<QueryResult>> compositeQuery =
createCompositeQuery(queryPart.getDescriptor(), getParameters(queryPart.getParent()), queries);
return createDummiesFunction(queryPart.getDescriptor(), queryPart.getParameters(), continuationToken,
compositeQuery);
}
@Override
@ -175,13 +178,8 @@ public class DummiesFunction extends PagedBaseFunction<Map.Entry<Long, Dummy>, S
}
}
private static DummiesFunction createDummiesFunction(Object descriptor, QueryParameters queryParameters, String continuationToken, CompositeQuery<QueryResult, List<QueryResult>> subquery) {
DummiesFunction dummiesFunction = new DummiesFunction(descriptor, queryParameters, continuationToken, subquery);
subquery.setParentQuery(dummiesFunction);
return dummiesFunction;
}
private static class GetDataFunction extends PagedBaseFunction<Map.Entry<Long, Dummy>, Collection<Map.Entry<Long, Dummy>>> {
private static class GetDataFunction
extends PagedBaseFunction<Map.Entry<Long, Dummy>, Collection<Map.Entry<Long, Dummy>>> {
private static final String FUNC_NAME = DummiesFunction.FUNC_NAME + "_data";
public GetDataFunction(Object descriptor, QueryParameters params, String continuationToken) {
@ -193,12 +191,12 @@ public class DummiesFunction extends PagedBaseFunction<Map.Entry<Long, Dummy>, S
}
@Override
public QueryResult<Map.Entry<Long, Dummy>, Collection<Map.Entry<Long, Dummy>>> execute(QueryContext context) throws QueryExecutionException {
public QueryResult<Map.Entry<Long, Dummy>, Collection<Map.Entry<Long, Dummy>>> execute(QueryContext context)
throws QueryExecutionException {
TestQueryContext functionContext = getContext(context);
TestParameters parameters = new TestParameters(getQueryParameters(), getQueryParameters().getDerivedParameters());
Collection<Map.Entry<Long, Dummy>> result = functionContext.getSearchDao().getDummies(
parameters.getId()
);
TestParameters parameters =
new TestParameters(getQueryParameters(), getQueryParameters().getDerivedParameters());
Collection<Map.Entry<Long, Dummy>> result = functionContext.getSearchDao().getDummies(parameters.getId());
return new BaseQueryResult<>(() -> result.stream(), () -> result);
}

View File

@ -5,13 +5,14 @@ import java.util.stream.Collectors;
public class SearchDao {
public static final List<Dummy> allDummies = Arrays.asList(
new Dummy(1L, "kek", "2016-03-22T06:12:27Z", "2016-03-22T06:12:27Z"),
public static final List<Dummy> allDummies =
Arrays.asList(new Dummy(1L, "kek", "2016-03-22T06:12:27Z", "2016-03-22T06:12:27Z"),
new Dummy(2L, "kek", "2016-03-22T06:12:27Z", "2016-03-22T06:12:27Z"),
new Dummy(3L, "kek", "2016-03-22T06:12:27Z", "2016-03-22T06:12:27Z"),
new Dummy(4L, "kek", "2016-03-22T06:12:27Z", "2016-03-22T06:12:27Z")
);
new Dummy(4L, "kek", "2016-03-22T06:12:27Z", "2016-03-22T06:12:27Z"));
public Collection<Map.Entry<Long, Dummy>> getDummies(String id) {
return allDummies.stream().filter(d -> id.equals(d.getId().toString())).map(d -> new AbstractMap.SimpleEntry<>(d.getId(), d)).collect(Collectors.toList());
return allDummies.stream().filter(d -> id.equals(d.getId().toString()))
.map(d -> new AbstractMap.SimpleEntry<>(d.getId(), d)).collect(Collectors.toList());
}
}

View File

@ -25,14 +25,16 @@ public class QueryBuilderImplTest {
mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
return mapper;
}
}.withQueryParser(new BaseQueryParser(Arrays.asList(new RootQuery.RootParser(), new DummiesFunction.DummiesParser())) {
}.withQueryParser(
new BaseQueryParser(Arrays.asList(new RootQuery.RootParser(), new DummiesFunction.DummiesParser())) {
@Override
public boolean apply(Map<String, Object> source, QueryPart parent) {
return true;
}
});
private QueryBuilder builder = new BaseQueryBuilder(Arrays.asList(new RootQuery.RootBuilder(), new DummiesFunction.DummiesBuilder())) {
private final QueryBuilder builder =
new BaseQueryBuilder(Arrays.asList(new RootQuery.RootBuilder(), new DummiesFunction.DummiesBuilder())) {
@Override
public boolean apply(List<QueryPart> queryParts, QueryPart parent) {
return true;
@ -41,7 +43,9 @@ public class QueryBuilderImplTest {
@Test
public void test() {
String json = "{'query': {'dummies': {'id': '1','kek':'kek','from_time': '2016-03-22T00:12:00Z','to_time': '2016-03-22T01:12:00Z'}}}";
String json =
"{'query': {'dummies': {'id': '1','kek':'kek','from_time': '2016-03-22T00:12:00Z'," +
"'to_time': '2016-03-22T01:12:00Z'}}}";
Query query = buildQuery(json);
assertTrue(query instanceof RootQuery);
query.getDescriptor();

View File

@ -22,7 +22,8 @@ public class JsonQueryParserTest {
mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
return mapper;
}
}.withQueryParser(new BaseQueryParser(Arrays.asList(new RootQuery.RootParser(), new DummiesFunction.DummiesParser())) {
}.withQueryParser(
new BaseQueryParser(Arrays.asList(new RootQuery.RootParser(), new DummiesFunction.DummiesParser())) {
@Override
public boolean apply(Map<String, Object> source, QueryPart parent) {
return true;
@ -34,7 +35,8 @@ public class JsonQueryParserTest {
String json = "{'query': {'keksiki': {}}}";
List<QueryPart> queryParts = parser.parseQuery(json);
assertEquals("root query", 1, queryParts.size());
assertEquals("root query has 0 parameter - no recognized function names", 0, queryParts.get(0).getChildren().size());
assertEquals("root query has 0 parameter - no recognized function names", 0,
queryParts.get(0).getChildren().size());
}
@ -47,19 +49,25 @@ public class JsonQueryParserTest {
@Test(expected = QueryParserException.class)
public void testKekParseError() {
String json = "{'query': {'dummies': {'id': '1','kek': 'kekekekkekekekke','from_time': '2016-03-22T00:12:00Z','to_time': '2016-03-22T01:12:00Z'}}}";
String json =
"{'query': {'dummies': {'id': '1','kek': 'kekekekkekekekke','from_time': '2016-03-22T00:12:00Z'," +
"'to_time': '2016-03-22T01:12:00Z'}}}";
parser.parseQuery(json);
}
@Test
public void testHappyPath() {
String json = "{'query': {'dummies': {'id': '1','kek': 'kek','from_time': '2016-03-22T00:12:00Z','to_time': '2016-03-22T01:12:00Z'}}}";
String json =
"{'query': {'dummies': {'id': '1','kek': 'kek','from_time': '2016-03-22T00:12:00Z'," +
"'to_time': '2016-03-22T01:12:00Z'}}}";
parser.parseQuery(json);
}
@Test(expected = QueryParserException.class)
public void testTimeParseError() {
String json = "{'query': {'dummies': {'id': '1','kek': '2','from_time': '2016-03-22T00:12:00Z','to_time': '2016-03-22T00:00:00Z'}}}";
String json =
"{'query': {'dummies': {'id': '1','kek': '2','from_time': '2016-03-22T00:12:00Z'," +
"'to_time': '2016-03-22T00:00:00Z'}}}";
parser.parseQuery(json);
}
}

View File

@ -2,9 +2,9 @@ package dev.vality.magista.dsl.search;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import dev.vality.magista.dsl.*;
import dev.vality.magista.dsl.builder.BaseQueryBuilder;
import dev.vality.magista.dsl.builder.QueryBuilder;
import dev.vality.magista.dsl.*;
import dev.vality.magista.dsl.parser.*;
import org.junit.Test;
@ -17,13 +17,15 @@ import static org.junit.Assert.assertEquals;
public class DummiesSearchQueryTest {
private final BaseQueryParser queryPartParser = new BaseQueryParser(Arrays.asList(new RootQuery.RootParser(), new DummiesFunction.DummiesParser())) {
private final BaseQueryParser queryPartParser =
new BaseQueryParser(Arrays.asList(new RootQuery.RootParser(), new DummiesFunction.DummiesParser())) {
@Override
public boolean apply(Map<String, Object> source, QueryPart parent) {
return true;
}
};
private final BaseQueryBuilder baseQueryBuilder = new BaseQueryBuilder(Arrays.asList(new RootQuery.RootBuilder(), new DummiesFunction.DummiesBuilder())) {
private final BaseQueryBuilder baseQueryBuilder =
new BaseQueryBuilder(Arrays.asList(new RootQuery.RootBuilder(), new DummiesFunction.DummiesBuilder())) {
@Override
public boolean apply(List<QueryPart> queryParts, QueryPart parent) {
return true;
@ -38,11 +40,14 @@ public class DummiesSearchQueryTest {
}
}.withQueryParser(queryPartParser);
QueryProcessorImpl queryProcessor = new QueryProcessorImpl(jsonQueryParser, baseQueryBuilder, new TestQueryContext(new SearchDao()));
QueryProcessorImpl queryProcessor =
new QueryProcessorImpl(jsonQueryParser, baseQueryBuilder, new TestQueryContext(new SearchDao()));
@Test
public void testPayments() {
String json = "{'query': {'dummies': {'id': '1','kek': 'kek','from_time': '2016-03-22T00:12:00Z','to_time': '2016-03-22T01:12:00Z'}}}";
String json =
"{'query': {'dummies': {'id': '1','kek': 'kek','from_time': '2016-03-22T00:12:00Z'," +
"'to_time': '2016-03-22T01:12:00Z'}}}";
StatTestResponse statResponse = queryProcessor.processQuery(new StatTestRequest(json));
assertEquals(1, statResponse.getStatTestResponseData().getDummies().size());
}
@ -56,7 +61,9 @@ public class DummiesSearchQueryTest {
@Test(expected = BadTokenException.class)
public void testBadToken() {
String json = "{'query': {'dummies': {'id': '1','kek': 'kek','from_time': '2016-03-22T00:12:00Z','to_time': '2016-03-22T01:12:00Z'}}}";
String json =
"{'query': {'dummies': {'id': '1','kek': 'kek','from_time': '2016-03-22T00:12:00Z'," +
"'to_time': '2016-03-22T01:12:00Z'}}}";
StatTestRequest statRequest = new StatTestRequest(json);
statRequest.setContinuationToken(UUID.randomUUID().toString());
queryProcessor.processQuery(statRequest);
@ -64,18 +71,20 @@ public class DummiesSearchQueryTest {
class QueryProcessorImpl implements QueryProcessor<StatTestRequest, StatTestResponse> {
private QueryParser<String> sourceParser;
private QueryBuilder queryBuilder;
private QueryContext queryContext;
private final QueryParser<String> sourceParser;
private final QueryBuilder queryBuilder;
private final QueryContext queryContext;
public QueryProcessorImpl(QueryParser<String> sourceParser, QueryBuilder queryBuilder, QueryContext queryContext) {
public QueryProcessorImpl(QueryParser<String> sourceParser, QueryBuilder queryBuilder,
QueryContext queryContext) {
this.sourceParser = sourceParser;
this.queryBuilder = queryBuilder;
this.queryContext = queryContext;
}
@Override
public StatTestResponse processQuery(StatTestRequest source) throws BadTokenException, QueryProcessingException {
public StatTestResponse processQuery(StatTestRequest source)
throws BadTokenException, QueryProcessingException {
List<QueryPart> queryParts = sourceParser.parseQuery(source.getDsl(), null);
Query query = queryBuilder.buildQuery(queryParts, source.getContinuationToken(), null, null);
QueryResult queryResult = query.execute(queryContext);