Skip to content

Commit

Permalink
Merge af9c9e9 into dcfacfe
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesagnew committed Mar 11, 2019
2 parents dcfacfe + af9c9e9 commit 171db6c
Show file tree
Hide file tree
Showing 18 changed files with 443 additions and 294 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ca.uhn.fhir.rest.param;

import ca.uhn.fhir.model.api.IQueryParameterOr;
import ca.uhn.fhir.util.CoverageIgnore;

/*
Expand Down Expand Up @@ -30,11 +31,14 @@ StringOrListParam newInstance() {
return new StringOrListParam();
}

@CoverageIgnore
@Override
public StringAndListParam addAnd(StringOrListParam theValue) {
addValue(theValue);
return this;
}

public StringAndListParam addAnd(StringParam theValue) {
addValue(new StringOrListParam().addOr(theValue));
return this;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ca.uhn.fhir.rest.param;

import ca.uhn.fhir.util.CoverageIgnore;
import org.apache.commons.lang3.Validate;

/*
* #%L
Expand Down Expand Up @@ -30,11 +30,15 @@ TokenOrListParam newInstance() {
return new TokenOrListParam();
}

@CoverageIgnore
@Override
public TokenAndListParam addAnd(TokenOrListParam theValue) {
addValue(theValue);
return this;
}

public TokenAndListParam addAnd(TokenParam theValue) {
Validate.notNull(theValue, "theValue must not be null");
addValue(new TokenOrListParam().add(theValue));
return this;
}
}
20 changes: 6 additions & 14 deletions hapi-fhir-jpaserver-base/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -126,23 +126,16 @@
<version>${project.version}</version>
</dependency>

<!--
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<groupId>net.ttddyy</groupId>
<artifactId>datasource-proxy</artifactId>
</dependency>
-->

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.ttddyy</groupId>
<artifactId>datasource-proxy</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.javassist</groupId>
Expand Down Expand Up @@ -460,6 +453,10 @@
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
</dependency>

<dependency>
<groupId>org.eclipse.jetty</groupId>
Expand Down Expand Up @@ -523,11 +520,6 @@
<artifactId>greenmail-spring</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
import java.math.MathContext;
import java.util.*;
import java.util.Map.Entry;
import java.util.function.Function;
import java.util.stream.Collectors;

import static org.apache.commons.lang3.StringUtils.*;
Expand Down Expand Up @@ -193,7 +194,7 @@ private void addPredicateComposite(String theResourceName, RuntimeSearchParam th

private void addPredicateDate(String theResourceName, String theParamName, List<? extends IQueryParameterType> theList) {

Join<ResourceTable, ResourceIndexedSearchParamDate> join = createOrReuseJoin(JoinEnum.DATE, theParamName);
Join<ResourceTable, ResourceIndexedSearchParamDate> join = createJoin(JoinEnum.DATE, theParamName);

if (theList.get(0).getMissing() != null) {
Boolean missing = theList.get(0).getMissing();
Expand Down Expand Up @@ -301,7 +302,7 @@ private void addPredicateLanguage(List<List<? extends IQueryParameterType>> theL

private void addPredicateNumber(String theResourceName, String theParamName, List<? extends IQueryParameterType> theList) {

Join<ResourceTable, ResourceIndexedSearchParamNumber> join = createOrReuseJoin(JoinEnum.NUMBER, theParamName);
Join<ResourceTable, ResourceIndexedSearchParamNumber> join = createJoin(JoinEnum.NUMBER, theParamName);

if (theList.get(0).getMissing() != null) {
addPredicateParamMissing(theResourceName, theParamName, theList.get(0).getMissing(), join);
Expand Down Expand Up @@ -361,7 +362,7 @@ private void addPredicateParamMissing(String theResourceName, String theParamNam
}

private void addPredicateQuantity(String theResourceName, String theParamName, List<? extends IQueryParameterType> theList) {
Join<ResourceTable, ResourceIndexedSearchParamQuantity> join = createOrReuseJoin(JoinEnum.QUANTITY, theParamName);
Join<ResourceTable, ResourceIndexedSearchParamQuantity> join = createJoin(JoinEnum.QUANTITY, theParamName);

if (theList.get(0).getMissing() != null) {
addPredicateParamMissing(theResourceName, theParamName, theList.get(0).getMissing(), join);
Expand Down Expand Up @@ -389,7 +390,7 @@ private void addPredicateReference(String theResourceName, String theParamName,
return;
}

Join<ResourceTable, ResourceLink> join = createOrReuseJoin(JoinEnum.REFERENCE, theParamName);
Join<ResourceTable, ResourceLink> join = createJoin(JoinEnum.REFERENCE, theParamName);

List<Predicate> codePredicates = new ArrayList<>();

Expand Down Expand Up @@ -683,7 +684,7 @@ private void addPredicateResourceId(List<List<? extends IQueryParameterType>> th

private void addPredicateString(String theResourceName, String theParamName, List<? extends IQueryParameterType> theList) {

Join<ResourceTable, ResourceIndexedSearchParamString> join = createOrReuseJoin(JoinEnum.STRING, theParamName);
Join<ResourceTable, ResourceIndexedSearchParamString> join = createJoin(JoinEnum.STRING, theParamName);

if (theList.get(0).getMissing() != null) {
addPredicateParamMissing(theResourceName, theParamName, theList.get(0).getMissing(), join);
Expand Down Expand Up @@ -831,13 +832,12 @@ private void addPredicateTag(List<List<? extends IQueryParameterType>> theList,
private void addPredicateToken(String theResourceName, String theParamName, List<? extends IQueryParameterType> theList) {

if (theList.get(0).getMissing() != null) {
Join<ResourceTable, ResourceIndexedSearchParamToken> join = createOrReuseJoin(JoinEnum.TOKEN, theParamName);
Join<ResourceTable, ResourceIndexedSearchParamToken> join = createJoin(JoinEnum.TOKEN, theParamName);
addPredicateParamMissing(theResourceName, theParamName, theList.get(0).getMissing(), join);
return;
}

List<Predicate> codePredicates = new ArrayList<>();
Join<ResourceTable, ResourceIndexedSearchParamToken> join = null;
List<IQueryParameterType> tokens = new ArrayList<>();
for (IQueryParameterType nextOr : theList) {

Expand All @@ -849,17 +849,14 @@ private void addPredicateToken(String theResourceName, String theParamName, List
}
}

if (join == null) {
join = createOrReuseJoin(JoinEnum.TOKEN, theParamName);
}

tokens.add(nextOr);
}

if (tokens.isEmpty()) {
return;
}

Join<ResourceTable, ResourceIndexedSearchParamToken> join = createJoin(JoinEnum.TOKEN, theParamName);
List<Predicate> singleCode = createPredicateToken(tokens, theResourceName, theParamName, myBuilder, join);
codePredicates.addAll(singleCode);

Expand All @@ -869,7 +866,7 @@ private void addPredicateToken(String theResourceName, String theParamName, List

private void addPredicateUri(String theResourceName, String theParamName, List<? extends IQueryParameterType> theList) {

Join<ResourceTable, ResourceIndexedSearchParamUri> join = createOrReuseJoin(JoinEnum.URI, theParamName);
Join<ResourceTable, ResourceIndexedSearchParamUri> join = createJoin(JoinEnum.URI, theParamName);

if (theList.get(0).getMissing() != null) {
addPredicateParamMissing(theResourceName, theParamName, theList.get(0).getMissing(), join);
Expand Down Expand Up @@ -1018,33 +1015,36 @@ private Predicate createCompositeParamPart(String theResourceName, Root<Resource
@SuppressWarnings("unchecked")
private <T> Join<ResourceTable, T> createOrReuseJoin(JoinEnum theType, String theSearchParameterName) {
JoinKey key = new JoinKey(theSearchParameterName, theType);
return (Join<ResourceTable, T>) myIndexJoins.computeIfAbsent(key, k -> {
Join<ResourceTable, ResourceIndexedSearchParamDate> join = null;
switch (theType) {
case DATE:
join = myResourceTableRoot.join("myParamsDate", JoinType.LEFT);
break;
case NUMBER:
join = myResourceTableRoot.join("myParamsNumber", JoinType.LEFT);
break;
case QUANTITY:
join = myResourceTableRoot.join("myParamsQuantity", JoinType.LEFT);
break;
case REFERENCE:
join = myResourceTableRoot.join("myResourceLinks", JoinType.LEFT);
break;
case STRING:
join = myResourceTableRoot.join("myParamsString", JoinType.LEFT);
break;
case URI:
join = myResourceTableRoot.join("myParamsUri", JoinType.LEFT);
break;
case TOKEN:
join = myResourceTableRoot.join("myParamsToken", JoinType.LEFT);
break;
}
return join;
});
return (Join<ResourceTable, T>) myIndexJoins.computeIfAbsent(key, k -> createJoin(theType, theSearchParameterName));
}

@SuppressWarnings("unchecked")
private <T> Join<ResourceTable, T> createJoin(JoinEnum theType, String theSearchParameterName) {
Join<ResourceTable, ResourceIndexedSearchParamDate> join = null;
switch (theType) {
case DATE:
join = myResourceTableRoot.join("myParamsDate", JoinType.LEFT);
break;
case NUMBER:
join = myResourceTableRoot.join("myParamsNumber", JoinType.LEFT);
break;
case QUANTITY:
join = myResourceTableRoot.join("myParamsQuantity", JoinType.LEFT);
break;
case REFERENCE:
join = myResourceTableRoot.join("myResourceLinks", JoinType.LEFT);
break;
case STRING:
join = myResourceTableRoot.join("myParamsString", JoinType.LEFT);
break;
case URI:
join = myResourceTableRoot.join("myParamsUri", JoinType.LEFT);
break;
case TOKEN:
join = myResourceTableRoot.join("myParamsToken", JoinType.LEFT);
break;
}
return (Join<ResourceTable, T>) join;
}

private Predicate createPredicateDate(IQueryParameterType theParam, String theResourceName, String theParamName, CriteriaBuilder theBuilder, From<?, ResourceIndexedSearchParamDate> theFrom) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package ca.uhn.fhir.jpa.util;

import net.ttddyy.dsproxy.ExecutionInfo;
import net.ttddyy.dsproxy.QueryInfo;
import net.ttddyy.dsproxy.proxy.ParameterSetOperation;
import net.ttddyy.dsproxy.support.ProxyDataSourceBuilder;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.engine.jdbc.internal.BasicFormatterImpl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Queue;
import java.util.stream.Collectors;

public abstract class BaseCaptureQueriesListener implements ProxyDataSourceBuilder.SingleQueryExecution {

@Override
public void execute(ExecutionInfo theExecutionInfo, List<QueryInfo> theQueryInfoList) {
final Queue<Query> queryList = provideQueryList();
for (QueryInfo next : theQueryInfoList) {
String sql = StringUtils.trim(next.getQuery());
List<String> params;
if (next.getParametersList().size() > 0 && next.getParametersList().get(0).size() > 0) {
List<ParameterSetOperation> values = next
.getParametersList()
.get(0);
params = values.stream()
.map(t -> t.getArgs()[1])
.map(t -> t != null ? t.toString() : "NULL")
.collect(Collectors.toList());
} else {
params = Collections.emptyList();
}

long elapsedTime = theExecutionInfo.getElapsedTime();
long startTime = System.currentTimeMillis() - elapsedTime;
queryList.add(new Query(sql, params, startTime, elapsedTime));
}
}

protected abstract Queue<Query> provideQueryList();

public static class Query {
private final String myThreadName = Thread.currentThread().getName();
private final String mySql;
private final List<String> myParams;
private final long myQueryTimestamp;
private final long myElapsedTime;

Query(String theSql, List<String> theParams, long theQueryTimestamp, long theElapsedTime) {
mySql = theSql;
myParams = Collections.unmodifiableList(theParams);
myQueryTimestamp = theQueryTimestamp;
myElapsedTime = theElapsedTime;
}

public long getQueryTimestamp() {
return myQueryTimestamp;
}

public long getElapsedTime() {
return myElapsedTime;
}

public String getThreadName() {
return myThreadName;
}

public String getSql(boolean theInlineParams, boolean theFormat) {
String retVal = mySql;
if (theFormat) {
retVal = new BasicFormatterImpl().format(retVal);

// BasicFormatterImpl annoyingly adds a newline at the very start of its output
while (retVal.startsWith("\n")) {
retVal = retVal.substring(1);
}
}

if (theInlineParams) {
List<String> nextParams = new ArrayList<>(myParams);
while (retVal.contains("?") && nextParams.size() > 0) {
int idx = retVal.indexOf("?");
retVal = retVal.substring(0, idx) + "'" + nextParams.remove(0) + "'" + retVal.substring(idx + 1);
}
}

return retVal;

}

}

}
Loading

0 comments on commit 171db6c

Please sign in to comment.