Skip to content

Commit

Permalink
compacted query and count methods for endpointinforservice considerin…
Browse files Browse the repository at this point in the history
…g redundant code
  • Loading branch information
angelo.andreussi authored and Coduz committed May 22, 2023
1 parent e2cac00 commit a923963
Showing 1 changed file with 35 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;

/**
* {@link EndpointInfoService} implementation.
Expand Down Expand Up @@ -212,13 +213,17 @@ public EndpointInfo find(KapuaId scopeId, KapuaId endpointInfoId)
}
}

@Override
public EndpointInfoListResult query(KapuaQuery query) throws KapuaException {
return query(query, EndpointInfo.ENDPOINT_TYPE_RESOURCE);
}

@Override
public EndpointInfoListResult query(KapuaQuery query, String section)
/**
* Traverse the account hierarchy bottom-up to search for {@link EndpointInfo} respecting the given query,
* performing for each layer the given operationToPerform until the given emptinessPredicate dictates to stop OR when endpoints of the same section are found in one layer
* In other terms, this method applies a given function to the "nearest usable endpoints", aka the ones that I see in a given scopeID
*
* @param query The query to filter the {@link EndpointInfo}s.
* @param section section of {@link EndpointInfo} where we want to search the information
* @param operationToPerform function to apply at each layer
* @param emptinessPredicate predicate that dictates to stop the traversal when false
*/
public Object traverse(KapuaQuery query, String section, myBifunction<EntityManager, KapuaQuery, Object, KapuaException> operationToPerform, Predicate<Object> emptinessPredicate)
throws KapuaException {
ArgumentValidator.notNull(query, "query");

Expand All @@ -232,14 +237,14 @@ public EndpointInfoListResult query(KapuaQuery query, String section)
//
// Do Query
return entityManagerSession.doAction(em -> {
EndpointInfoListResult endpointInfoListResult = EndpointInfoDAO.query(em, query);
Object res = operationToPerform.apply(em, query);

if (endpointInfoListResult.isEmpty() && query.getScopeId() != null) { //query did not find results
if (emptinessPredicate.test(res) && query.getScopeId() != null) { //query did not find results

KapuaId originalScopeId = query.getScopeId();

do {
// First check if there are any endpoint AT ALL specified in this scope
// First check if there are any endpoint AT ALL specified in this scope/layer
if (countAllEndpointsInScope(em, query.getScopeId(), section)) {
// There are endpoints (even not matching the query), exit because I found the "nearest usable" endpoints which don't have what I'm searching
break;
Expand All @@ -255,14 +260,14 @@ public EndpointInfoListResult query(KapuaQuery query, String section)
break;
}
query.setScopeId(account.getScopeId());
endpointInfoListResult = EndpointInfoDAO.query(em, query);
res = operationToPerform.apply(em, query);
}
while (query.getScopeId() != null && endpointInfoListResult.isEmpty());
while (query.getScopeId() != null && emptinessPredicate.test(res));

query.setScopeId(originalScopeId);
}

return endpointInfoListResult;
return res;
});
}

Expand All @@ -272,59 +277,30 @@ public long count(KapuaQuery query) throws KapuaException {
}

@Override
public long count(KapuaQuery query, String section)
throws KapuaException {
ArgumentValidator.notNull(query, "query");

//
// Check Access
AUTHORIZATION_SERVICE.checkPermission(
PERMISSION_FACTORY.newPermission(EndpointInfoDomains.ENDPOINT_INFO_DOMAIN, Actions.read, query.getScopeId())
);
addSectionToPredicate(query, section);

//
// Do count
return entityManagerSession.doAction(em -> {
long endpointInfoCount = EndpointInfoDAO.count(em, query);

if (endpointInfoCount == 0 && query.getScopeId() != null) {

KapuaId originalScopeId = query.getScopeId();

do {
// First check if there are any endpoint AT ALL specified in this scope
if (countAllEndpointsInScope(em, query.getScopeId(), section)) {
// There are endpoints (even not matching the query), exit because I found the "nearest usable" endpoints which don't have what I'm searching
break;
}
Account account = KapuaSecurityUtils.doPrivileged(() -> ACCOUNT_SERVICE.find(query.getScopeId()));

if (account == null) {
throw new KapuaEntityNotFoundException(Account.TYPE, query.getScopeId());
}
if (account.getScopeId() == null) {
// Query was originally on root account, and querying on parent scope id would mean querying in null,
// i.e. querying on all accounts. Since that's not what we want, break away.
break;
}

query.setScopeId(account.getScopeId());
endpointInfoCount = EndpointInfoDAO.count(em, query);
}
while (query.getScopeId() != null && endpointInfoCount == 0);
public long count(KapuaQuery query, String section) throws KapuaException {
return (long) traverse(query, section, EndpointInfoDAO::count, a -> (long)a == 0);
}

query.setScopeId(originalScopeId);
}
@Override
public EndpointInfoListResult query(KapuaQuery query) throws KapuaException {
return query(query, EndpointInfo.ENDPOINT_TYPE_RESOURCE);
}

return endpointInfoCount;
});
public EndpointInfoListResult query(KapuaQuery query, String section) throws KapuaException {
return (EndpointInfoListResult) traverse(query, section, EndpointInfoDAO::query, a -> ((EndpointInfoListResult)a).isEmpty());
}

//
// Private methods
// Private methods and interfaces
//

//this interface is created solely because java complains if you pass lambdas throwing checked exception
//to overcome this, I created this interface which is a custom form of a Bifunction throwing the checked KapuaException
@FunctionalInterface
private interface myBifunction<A, B, C, E extends KapuaException> {
C apply(A input1, B input2) throws E;
}

/**
* Checks whether another {@link EndpointInfo} already exists with the given values.
*
Expand Down

0 comments on commit a923963

Please sign in to comment.