Skip to content

Commit

Permalink
Cache hash-code in high-frequency invocation classes
Browse files Browse the repository at this point in the history
  • Loading branch information
valdas-s committed Jul 31, 2014
1 parent 8264555 commit 2a5bcbf
Show file tree
Hide file tree
Showing 18 changed files with 196 additions and 218 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http:https://www.gnu.org/licenses/lgpl-3.0.html>.
Expand Down
13 changes: 6 additions & 7 deletions xacml-core/src/main/java/org/xacml4j/v30/BagOfAttributeExp.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ public final class BagOfAttributeExp
{
private static final long serialVersionUID = -8197446176793438616L;

private BagOfAttributeExpType type;
private Multiset<AttributeExp> values;
private final BagOfAttributeExpType type;
private final Multiset<AttributeExp> values;
private final int hashCode;

/**
* Constructs bag of attributes.
Expand All @@ -66,13 +67,13 @@ public final class BagOfAttributeExp
}
this.type = type;
this.values = ImmutableMultiset.copyOf(attributes);

this.hashCode = Objects.hashCode(type, values);
}

private BagOfAttributeExp(Builder b){
this.type = b.bagType;
this.values = b.valuesBuilder.build();

this.hashCode = Objects.hashCode(type, values);
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -248,9 +249,7 @@ public String toString() {

@Override
public int hashCode(){
return Objects.hashCode(
type,
values);
return hashCode;
}

/**
Expand Down
119 changes: 11 additions & 108 deletions xacml-core/src/main/java/org/xacml4j/v30/RequestContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,20 @@
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http:https://www.gnu.org/licenses/lgpl-3.0.html>.
* #L%
*/

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

Expand All @@ -34,70 +32,25 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.Iterables;
import com.google.common.collect.LinkedListMultimap;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;

public class RequestContext
{
private boolean returnPolicyIdList;
private boolean combinedDecision;
private Multimap<CategoryId, Category> attributes;
private Map<String, Category> attributesByXmlId;
private Collection<RequestReference> requestReferences;
private RequestDefaults requestDefaults;
private final boolean returnPolicyIdList;
private final boolean combinedDecision;
private final Multimap<CategoryId, Category> attributes;
private final Map<String, Category> attributesByXmlId;
private final Collection<RequestReference> requestReferences;
private final RequestDefaults requestDefaults;

private transient int cachedHashCode;

/**
* Constructs request with a given arguments
*
* @param returnPolicyIdList a flag indicating
* that response should contains applicable
* evaluated policy or policy set identifiers
* list
* @param attributes a collection of request attributes
* @param requestReferences a request references
* @param requestDefaults a request defaults
*/
public RequestContext(
boolean returnPolicyIdList,
boolean combinedDecision,
Collection<Category> attributes,
Collection<RequestReference> requestReferences,
RequestDefaults requestDefaults)
{
this.returnPolicyIdList = returnPolicyIdList;
this.attributes = LinkedListMultimap.create();
this.requestReferences = (requestReferences == null)?
Collections.<RequestReference>emptyList():new ArrayList<RequestReference>(requestReferences);
this.attributesByXmlId = new HashMap<String, Category>();
this.requestDefaults = requestDefaults;
this.combinedDecision = combinedDecision;
if(attributes != null){
for(Category attr : attributes){
// index attributes by category
this.attributes.put(attr.getCategoryId(), attr);
// index attributes
// by id for fast lookup
if(attr.getId() != null){
this.attributesByXmlId.put(attr.getId(), attr);
}
}
}
this.cachedHashCode = Objects.hashCode(
this.returnPolicyIdList,
this.combinedDecision,
this.attributes,
this.requestReferences,
this.requestDefaults);
}
private final transient int cachedHashCode;

private RequestContext(Builder b)
{
this.returnPolicyIdList = b.returnPolicyIdList;
this.attributes = LinkedListMultimap.create();
this.requestReferences = b.reqRefs.build();
this.attributesByXmlId = new HashMap<String, Category>();
this.attributesByXmlId = Maps.newHashMap();
this.requestDefaults = b.reqDefaults;
this.combinedDecision = b.combinedDecision;
this.attributes = b.attrBuilder.build();
Expand All @@ -114,56 +67,6 @@ private RequestContext(Builder b)
this.requestDefaults);
}

/**
* Constructs a request with a given attributes
*
* @param attributes a collection of {@link Category}
* instances
*/
public RequestContext(boolean returnPolicyIdList,
boolean combinedDecision,
Collection<Category> attributes,
Collection<RequestReference> requestReferences)
{
this(returnPolicyIdList, combinedDecision, attributes,
requestReferences, new RequestDefaults());
}

public RequestContext(boolean returnPolicyIdList,
boolean combinedDecision,
Collection<Category> attributes)
{
this(returnPolicyIdList, combinedDecision, attributes,
Collections.<RequestReference>emptyList());
}

/**
* Constructs a request with a given attributes
*
* @param attributes a collection of {@link Category}
* instances
*/
public RequestContext(boolean returnPolicyIdList,
Collection<Category> attributes)
{
this(returnPolicyIdList, false, attributes,
Collections.<RequestReference>emptyList());
}

/**
* Constructs a request with a given attributes
*
* @param attributes a collection of {@link Category}
* instances
*/
public RequestContext(boolean returnPolicyIdList,
Collection<Category> attributes,
RequestDefaults requestDefaults)
{
this(returnPolicyIdList, false, attributes,
Collections.<RequestReference>emptyList(), requestDefaults);
}

public static Builder builder(){
return new Builder();
}
Expand Down
4 changes: 3 additions & 1 deletion xacml-core/src/main/java/org/xacml4j/v30/Status.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public final class Status
private final StatusCode code;
private final String message;
private final StatusDetail detail;
private final int hashCode;

/**
* Creates status with a given status
Expand All @@ -43,6 +44,7 @@ public Status(Builder b){
this.code = b.code;
this.message = b.message;
this.detail = b.detail;
this.hashCode = Objects.hashCode(code, message, detail);
}

public static Builder processingError(){
Expand Down Expand Up @@ -130,7 +132,7 @@ public boolean equals(Object o){

@Override
public int hashCode(){
return Objects.hashCode(code, message, detail);
return hashCode;
}

public static class Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public RequestContext create(RequestType req) throws XacmlSyntaxException
{
attributes.add(createEnvironment(req.getEnvironment()));
}
return new RequestContext(false, attributes);
return RequestContext.builder().returnPolicyIdList(false).attributes(attributes).build();
}

private Collection<Category> normalize(Multimap<CategoryId, Category> attributes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private RequestContext resolveAttributes(RequestContext req,
}
resolved.add(attributes);
}
return new RequestContext(req.isReturnPolicyIdList(), resolved);
return RequestContext.builder().attributes(resolved).returnPolicyIdList(req.isReturnPolicyIdList()).build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ public Collection<Result> handle(RequestContext request, PolicyDecisionPointCont
List<Result> results = new LinkedList<Result>();
for(List<Category> requestAttr : cartesian)
{
RequestContext req = new RequestContext(request.isReturnPolicyIdList(),
requestAttr, request.getRequestDefaults());
RequestContext req = RequestContext.builder().copyOf(request, requestAttr).build();
if(log.isDebugEnabled()){
log.debug("Created request=\"{}\"", req);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public Collection<Result> handle(RequestContext request, PolicyDecisionPointCont
Status
.syntaxError()
.message("Found more than AttributeId=\"%s\" " +
"value of type=\"%s\"", RESOURCE_ID_ATTRIBUTE,
"value of type=\"%s\"", RESOURCE_ID_ATTRIBUTE,
XacmlTypes.XPATH).build())
.includeInResultAttr(request.getIncludeInResultAttributes())
.build());
Expand Down Expand Up @@ -110,8 +110,9 @@ public Collection<Result> handle(RequestContext request, PolicyDecisionPointCont
}
attributes.add(attrs);
}
return handleNext(new RequestContext(
request.isReturnPolicyIdList(), attributes), context);
return handleNext(
RequestContext.builder().copyOf(request, attributes).build(),
context);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@

public final class AttributeSet
{
private long timestamp;
private ImmutableMap<String, BagOfAttributeExp> values;
private AttributeResolverDescriptor d;
private final long timestamp;
private final ImmutableMap<String, BagOfAttributeExp> values;
private final AttributeResolverDescriptor d;
private final int hashCode;

private AttributeSet(Builder b){
this.d = b.d;
this.values = b.mapBuilder.build();
this.timestamp = b.ticker.read();
this.hashCode = Objects.hashCode(d, values);
}

public static Builder builder(AttributeResolverDescriptor d){
Expand Down Expand Up @@ -149,7 +151,7 @@ public String toString(){

@Override
public int hashCode(){
return Objects.hashCode(d, values);
return hashCode;
}

public static class Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ public final class ResolverCacheKey implements Serializable
{
private static final long serialVersionUID = -6895205924708410228L;

private String resolverId;
private List<BagOfAttributeExp> keys;
private final String resolverId;
private final List<BagOfAttributeExp> keys;
private final int hashCode;

public ResolverCacheKey(Builder b){
public ResolverCacheKey(Builder b) {
this.resolverId = b.id;
this.keys = b.keysBuilder.build();
this.hashCode = Objects.hashCode(resolverId, keys);
}

public static Builder builder(){
Expand All @@ -56,19 +58,16 @@ public static Builder builder(){

@Override
public int hashCode(){
return Objects.hashCode(resolverId, keys);
return hashCode;
}

@Override
public boolean equals(Object o)
{
if(o == this){
if (o == this) {
return true;
}
if(o == null){
return false;
}
if(!(o instanceof ResolverCacheKey)){
if (!(o instanceof ResolverCacheKey)) {
return false;
}
ResolverCacheKey k = (ResolverCacheKey)o;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ public abstract class BaseAttributeExp<T>

private final T value;
private final AttributeExpType type;
private final int hashCode;

protected BaseAttributeExp(AttributeExpType attrType,
T attrValue) {
Preconditions.checkNotNull(attrType);
Preconditions.checkNotNull(attrValue);
this.type = attrType;
this.value = attrValue;
this.hashCode = Objects.hashCode(type, value);
}

@Override
Expand Down Expand Up @@ -79,8 +81,7 @@ public String toString() {

@Override
public int hashCode(){
return Objects.hashCode(
type, value);
return hashCode;
}

@Override
Expand All @@ -89,10 +90,7 @@ public BagOfAttributeExp toBag(){
}

@Override
public boolean equals(Object o){
if(o == null){
return false;
}
public boolean equals(Object o) {
if(o == this){
return true;
}
Expand Down
Loading

0 comments on commit 2a5bcbf

Please sign in to comment.