Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CARBON-161 add requestId to requestScope #1329

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.function.Function;

import javax.ws.rs.core.MultivaluedHashMap;
Expand Down Expand Up @@ -77,6 +78,7 @@ public class RequestScope implements com.yahoo.elide.security.RequestScope {
@Getter private final int updateStatusCode;

@Getter private final MultipleFilterDialect filterDialect;
private String requestId;
AshishAg24 marked this conversation as resolved.
Show resolved Hide resolved
private final Map<String, FilterExpression> expressionsByType;

private PublishSubject<CRUDEvent> lifecycleEvents;
Expand Down Expand Up @@ -126,6 +128,7 @@ public RequestScope(String path,
this.newPersistentResources = new LinkedHashSet<>();
this.dirtyResources = new LinkedHashSet<>();
this.deletedResources = new LinkedHashSet<>();
this.requestId = UUID.randomUUID().toString();

Function<RequestScope, PermissionExecutor> permissionExecutorGenerator = elideSettings.getPermissionExecutor();
this.permissionExecutor = (permissionExecutorGenerator == null)
Expand Down Expand Up @@ -216,6 +219,7 @@ protected RequestScope(String path, JsonApiDocument jsonApiDocument, RequestScop
this.lifecycleEvents = outerRequestScope.lifecycleEvents;
this.distinctLifecycleEvents = outerRequestScope.distinctLifecycleEvents;
this.queuedLifecycleEvents = outerRequestScope.queuedLifecycleEvents;
this.requestId = outerRequestScope.requestId;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public abstract class AbstractHQLQueryBuilder {

protected static final boolean USE_ALIAS = true;
protected static final boolean NO_ALIAS = false;
protected Set<String> alreadyJoined = new HashSet<>();

/**
* Represents a relationship between two entities.
Expand Down Expand Up @@ -118,14 +119,22 @@ protected void supplyFilterQueryParameters(Query query, Collection<FilterPredica
* @return an HQL join clause
*/
protected String getJoinClauseFromFilters(FilterExpression filterExpression) {
return getJoinClauseFromFilters(filterExpression, false);
}

/**
* Extracts all the HQL JOIN clauses from given filter expression.
* @param filterExpression the filter expression to extract a join clause from
* @param skipFetches JOIN but don't FETCH JOIN a relationship.
* @return an HQL join clause
*/
protected String getJoinClauseFromFilters(FilterExpression filterExpression, boolean skipFetches) {
PredicateExtractionVisitor visitor = new PredicateExtractionVisitor(new ArrayList<>());
Collection<FilterPredicate> predicates = filterExpression.accept(visitor);

Set<String> alreadyJoined = new HashSet<>();

return predicates.stream()
.map(predicate -> extractJoinClause(predicate, alreadyJoined))
.collect(Collectors.joining(SPACE));
.map(predicate -> extractJoinClause(predicate, skipFetches))
.collect(Collectors.joining(SPACE));
}

/**
Expand All @@ -143,10 +152,10 @@ protected void addPaginationToQuery(Query query) {
/**
* Extracts a join clause from a filter predicate (if it exists).
* @param predicate The predicate to examine
* @param alreadyJoined A set of joins that have already been computed.
* @param skipFetches Don't fetch join
* @return A HQL string representing the join
*/
private String extractJoinClause(FilterPredicate predicate, Set<String> alreadyJoined) {
private String extractJoinClause(FilterPredicate predicate, boolean skipFetches) {
StringBuilder joinClause = new StringBuilder();

String previousAlias = null;
Expand All @@ -163,18 +172,28 @@ private String extractJoinClause(FilterPredicate predicate, Set<String> alreadyJ

String alias = typeAlias + UNDERSCORE + fieldName;

String joinFragment;
String joinKey;

//This is the first path element
if (previousAlias == null) {
joinFragment = LEFT + JOIN + typeAlias + PERIOD + fieldName + SPACE + alias + SPACE;
joinKey = typeAlias + PERIOD + fieldName;
} else {
joinFragment = LEFT + JOIN + previousAlias + PERIOD + fieldName + SPACE + alias + SPACE;
joinKey = previousAlias + PERIOD + fieldName;
}

if (!alreadyJoined.contains(joinFragment)) {
String fetch = "";
RelationshipType type = dictionary.getRelationshipType(pathElement.getType(), fieldName);

//This is a to-One relationship belonging to the collection being retrieved.
if (!skipFetches && type.isToOne() && !type.isComputed() && previousAlias == null) {
fetch = "FETCH ";

}
String joinFragment = LEFT + JOIN + fetch + joinKey + SPACE + alias + SPACE;

if (!alreadyJoined.contains(joinKey)) {
joinClause.append(joinFragment);
alreadyJoined.add(joinFragment);
alreadyJoined.add(joinKey);
}

previousAlias = alias;
Expand Down Expand Up @@ -203,10 +222,14 @@ protected String extractToOneMergeJoins(Class<?> entityClass, String alias,
if (skipRelation.apply(relationshipName)) {
continue;
}
String joinKey = alias + PERIOD + relationshipName;

if (alreadyJoined.contains(joinKey)) {
continue;
}

joinString.append(" LEFT JOIN FETCH ");
joinString.append(alias);
joinString.append(PERIOD);
joinString.append(relationshipName);
joinString.append(joinKey);
joinString.append(SPACE);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public Query build() {
filterClause = new FilterTranslator().apply(filterExpression.get(), USE_ALIAS);

//Build the JOIN clause
joinClause = getJoinClauseFromFilters(filterExpression.get());
joinClause = getJoinClauseFromFilters(filterExpression.get(), true);

} else {
predicates = new HashSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public Query build() {
FilterExpression joinedExpression = new AndFilterExpression(scoped, idExpression);

//Build the JOIN clause from the filter predicate
joinClause = getJoinClauseFromFilters(joinedExpression);
joinClause = getJoinClauseFromFilters(joinedExpression, true);

//Build the WHERE clause
filterClause = new FilterTranslator().apply(joinedExpression, USE_ALIAS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public void testSubCollectionFetchWithJoinFilter() {

String expected = "SELECT example_Book FROM example.Author example_Author__fetch "
+ "JOIN example_Author__fetch.books example_Book "
+ "LEFT JOIN example_Book.publisher example_Book_publisher LEFT JOIN FETCH example_Book.publisher "
+ "LEFT JOIN FETCH example_Book.publisher example_Book_publisher "
+ "WHERE example_Book_publisher.name IN (:books_publisher_name_XXX) AND example_Author__fetch=:example_Author__fetch ";
String actual = query.getQueryText();
actual = actual.replaceFirst(":publisher_name_\\w+_\\w+", ":books_publisher_name_XXX");
Expand Down Expand Up @@ -188,7 +188,7 @@ public void testSubCollectionFetchWithSortingAndFilters() {

String expected = "SELECT example_Book FROM example.Author example_Author__fetch "
+ "JOIN example_Author__fetch.books example_Book "
+ "LEFT JOIN example_Book.publisher example_Book_publisher LEFT JOIN FETCH example_Book.publisher "
+ "LEFT JOIN FETCH example_Book.publisher example_Book_publisher "
+ "WHERE example_Book_publisher.name IN (:publisher_name_XXX) AND example_Author__fetch=:example_Author__fetch order by example_Book.title asc";

String actual = query.getQueryText();
Expand Down