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

SystemScopeService: Optimize SQL access #1502

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 @@ -38,11 +38,13 @@
@Table(name = "system_scope")
@NamedQueries({
@NamedQuery(name = SystemScope.QUERY_ALL, query = "select s from SystemScope s ORDER BY s.id"),
@NamedQuery(name = SystemScope.QUERY_BY_VALUE, query = "select s from SystemScope s WHERE s.value = :" + SystemScope.PARAM_VALUE)
@NamedQuery(name = SystemScope.QUERY_BY_VALUE, query = "select s from SystemScope s WHERE s.value = :" + SystemScope.PARAM_VALUE),
@NamedQuery(name = SystemScope.QUERY_BY_VALUES, query = "select s from SystemScope s WHERE s.value in :" + SystemScope.PARAM_VALUE)
})
public class SystemScope {

public static final String QUERY_BY_VALUE = "SystemScope.getByValue";
public static final String QUERY_BY_VALUES = "SystemScope.getByValues";
public static final String QUERY_ALL = "SystemScope.findAll";

public static final String PARAM_VALUE = "value";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public interface SystemScopeRepository {

public SystemScope getByValue(String value);

public Set<SystemScope> getByValues(Set<String> values);

public void remove(SystemScope scope);

public SystemScope save(SystemScope scope);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import static org.mitre.util.jpa.JpaUtil.getSingleResult;
import static org.mitre.util.jpa.JpaUtil.saveOrUpdate;

import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

Expand Down Expand Up @@ -76,6 +78,17 @@ public SystemScope getByValue(String value) {
return getSingleResult(query.getResultList());
}

@Override
public Set<SystemScope> getByValues(Set<String> values) {
if(values.isEmpty()) {
return Collections.emptySet();
}

TypedQuery<SystemScope> query = em.createNamedQuery(SystemScope.QUERY_BY_VALUES, SystemScope.class);
query.setParameter(SystemScope.PARAM_VALUE, values);
return new HashSet<>(query.getResultList());
}

/* (non-Javadoc)
* @see org.mitre.oauth2.repository.SystemScopeRepository#remove(org.mitre.oauth2.model.SystemScope)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@
*/
package org.mitre.oauth2.service.impl;

import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import org.mitre.oauth2.model.SystemScope;
import org.mitre.oauth2.repository.SystemScopeRepository;
Expand All @@ -32,7 +35,6 @@
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.base.Strings;
import com.google.common.collect.Collections2;
import com.google.common.collect.Sets;

Expand Down Expand Up @@ -67,24 +69,6 @@ public boolean apply(SystemScope input) {
}
};

private Function<String, SystemScope> stringToSystemScope = new Function<String, SystemScope>() {
@Override
public SystemScope apply(String input) {
if (Strings.isNullOrEmpty(input)) {
return null;
} else {
// get the real scope if it's available
SystemScope s = getByValue(input);
if (s == null) {
// make a fake one otherwise
s = new SystemScope(input);
}

return s;
}
}
};

private Function<SystemScope, String> systemScopeToString = new Function<SystemScope, String>() {
@Override
public String apply(SystemScope input) {
Expand Down Expand Up @@ -120,6 +104,10 @@ public SystemScope getByValue(String value) {
return repository.getByValue(value);
}

private Set<SystemScope> getByValues(Set<String> values) {
return repository.getByValues(values);
}

/* (non-Javadoc)
* @see org.mitre.oauth2.service.SystemScopeService#remove(org.mitre.oauth2.model.SystemScope)
*/
Expand Down Expand Up @@ -149,7 +137,19 @@ public Set<SystemScope> fromStrings(Set<String> scope) {
if (scope == null) {
return null;
} else {
return new LinkedHashSet<>(Collections2.filter(Collections2.transform(scope, stringToSystemScope), Predicates.notNull()));
Set<String> scopeValues = scope.stream().filter(Objects::nonNull).collect(Collectors.toSet());
Set<SystemScope> scopesFromDB = getByValues(scopeValues);
Set<String> scopesFromDBValues = scopesFromDB.stream().map(SystemScope::getValue).collect(Collectors.toSet());
Set<SystemScope> missingScopesFromDB = scopeValues
.stream()
.filter(sv -> !scopesFromDBValues.contains(sv))
.map(sv -> new SystemScope(sv))
.collect(Collectors.toSet());

Set<SystemScope> allScopes = new HashSet<SystemScope>();
allScopes.addAll(scopesFromDB);
allScopes.addAll(missingScopesFromDB);
return allScopes;
}
}

Expand Down