Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WELD-1430 Use optimized session keys if possible
- Loading branch information
Showing
5 changed files
with
245 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
impl/src/main/java/org/jboss/weld/context/beanstore/BeanIdentifierIndexNamingScheme.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* | ||
* JBoss, Home of Professional Open Source | ||
* Copyright 2014, Red Hat, Inc., and individual contributors | ||
* by the @authors tag. See the copyright.txt in the distribution for a | ||
* full listing of individual contributors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.jboss.weld.context.beanstore; | ||
|
||
import org.jboss.weld.bean.StringBeanIdentifier; | ||
import org.jboss.weld.exceptions.IllegalStateException; | ||
import org.jboss.weld.serialization.BeanIdentifierIndex; | ||
import org.jboss.weld.serialization.spi.BeanIdentifier; | ||
|
||
/** | ||
* A naming scheme which makes use of {@link BeanIdentifierIndex} if possible. | ||
* | ||
* @author Martin Kouba | ||
*/ | ||
public class BeanIdentifierIndexNamingScheme extends SimpleNamingScheme { | ||
|
||
private static final String FALLBACK_FLAG = "F_"; | ||
|
||
private final BeanIdentifierIndex index; | ||
|
||
public BeanIdentifierIndexNamingScheme(String prefix, BeanIdentifierIndex index) { | ||
super(prefix); | ||
this.index = index; | ||
} | ||
|
||
@Override | ||
public BeanIdentifier deprefix(String id) { | ||
String deprefixed = id.substring(getPrefix().length() + getDelimiter().length()); | ||
if (deprefixed.startsWith(FALLBACK_FLAG)) { | ||
return new StringBeanIdentifier(deprefixed.substring(FALLBACK_FLAG.length())); | ||
} | ||
try { | ||
return index.getIdentifier(Integer.valueOf(deprefixed)); | ||
} catch (NumberFormatException e) { | ||
throw new IllegalStateException("Unable to deprefix invalid identifier", e); | ||
} | ||
} | ||
|
||
@Override | ||
public String prefix(BeanIdentifier id) { | ||
Integer idx = index.getIndex(id); | ||
if (idx == null) { | ||
return getPrefix() + getDelimiter() + FALLBACK_FLAG + id.asString(); | ||
} | ||
return getPrefix() + getDelimiter() + idx; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
impl/src/main/java/org/jboss/weld/serialization/BeanIdentifierIndex.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source | ||
* Copyright 2014, Red Hat, Inc., and individual contributors | ||
* by the @authors tag. See the copyright.txt in the distribution for a | ||
* full listing of individual contributors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.jboss.weld.serialization; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import javax.enterprise.inject.spi.Bean; | ||
import javax.enterprise.inject.spi.PassivationCapable; | ||
|
||
import org.jboss.weld.bean.CommonBean; | ||
import org.jboss.weld.bean.StringBeanIdentifier; | ||
import org.jboss.weld.bootstrap.api.Service; | ||
import org.jboss.weld.exceptions.IllegalStateException; | ||
import org.jboss.weld.serialization.spi.BeanIdentifier; | ||
import org.jboss.weld.util.Preconditions; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
|
||
/** | ||
* The index holds identifiers for the specified set of beans (note that only instances of {@link CommonBean} and implementations of {@link PassivationCapable} | ||
* are included). Identifiers are sorted into ascending order, according to the {@link BeanIdentifier#asString()} natural ordering. | ||
* | ||
* @author Martin Kouba | ||
*/ | ||
public class BeanIdentifierIndex implements Service { | ||
|
||
private BeanIdentifier[] index; | ||
|
||
private Map<BeanIdentifier, Integer> reverseIndex; | ||
|
||
/** | ||
* | ||
* @param identifier | ||
* @return the index value for the given identifier or null if the index does not contain the given identifier | ||
*/ | ||
public Integer getIndex(BeanIdentifier identifier) { | ||
checkIsBuilt(); | ||
Preconditions.checkArgumentNotNull(identifier, "identifier"); | ||
return reverseIndex.get(identifier); | ||
} | ||
|
||
/** | ||
* | ||
* @param idx | ||
* @return the identifier for the given index | ||
*/ | ||
public BeanIdentifier getIdentifier(int idx) { | ||
checkIsBuilt(); | ||
return index[idx]; | ||
} | ||
|
||
/** | ||
* Note that the index can only be built once. | ||
* | ||
* @param beans The set of beans the index should be built from, only instances of {@link CommonBean} and implementations of {@link PassivationCapable} are | ||
* included | ||
* @throws IllegalStateException If the index is built already | ||
*/ | ||
public void build(Set<Bean<?>> beans) { | ||
|
||
if (index != null) { | ||
throw new IllegalStateException("BeanIdentifier index is already built!"); | ||
} | ||
|
||
if (beans.isEmpty()) { | ||
index = new BeanIdentifier[0]; | ||
reverseIndex = Collections.emptyMap(); | ||
return; | ||
} | ||
|
||
List<BeanIdentifier> tempIndex = new ArrayList<BeanIdentifier>(beans.size()); | ||
|
||
for (Bean<?> bean : beans) { | ||
if (bean instanceof CommonBean<?>) { | ||
tempIndex.add(((CommonBean<?>) bean).getIdentifier()); | ||
} else if (bean instanceof PassivationCapable) { | ||
tempIndex.add(new StringBeanIdentifier(((PassivationCapable) bean).getId())); | ||
} | ||
} | ||
|
||
Collections.sort(tempIndex, new Comparator<BeanIdentifier>() { | ||
@Override | ||
public int compare(BeanIdentifier o1, BeanIdentifier o2) { | ||
return o1.asString().compareTo(o2.asString()); | ||
} | ||
}); | ||
|
||
index = tempIndex.toArray(new BeanIdentifier[tempIndex.size()]); | ||
|
||
ImmutableMap.Builder<BeanIdentifier, Integer> builder = ImmutableMap.builder(); | ||
for (int i = 0; i < index.length; i++) { | ||
builder.put(index[i], i); | ||
} | ||
reverseIndex = builder.build(); | ||
} | ||
|
||
@Override | ||
public void cleanup() { | ||
index = null; | ||
} | ||
|
||
private void checkIsBuilt() { | ||
if (index == null) { | ||
throw new IllegalStateException("BeanIdentifier index not built!"); | ||
} | ||
} | ||
|
||
} |