Author: manik.surtani(a)jboss.com
Date: 2008-04-09 05:08:28 -0400 (Wed, 09 Apr 2008)
New Revision: 5522
Modified:
core/trunk/src/main/java/org/jboss/cache/Fqn.java
core/trunk/src/main/java/org/jboss/cache/NodeFactory.java
core/trunk/src/main/java/org/jboss/cache/buddyreplication/BuddyManager.java
core/trunk/src/main/java/org/jboss/cache/interceptors/OptimisticInterceptor.java
core/trunk/src/main/java/org/jboss/cache/loader/CacheLoaderManager.java
Log:
Updated Fqn, and code that uses deprecated Fqn constructors
Modified: core/trunk/src/main/java/org/jboss/cache/Fqn.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/Fqn.java 2008-04-09 02:38:58 UTC (rev 5521)
+++ core/trunk/src/main/java/org/jboss/cache/Fqn.java 2008-04-09 09:08:28 UTC (rev 5522)
@@ -38,11 +38,10 @@
* </pre>
* Alternatively, the same Fqn could be constructed using an array:
* <pre>
- * Fqn<String> abc = new Fqn<String>(new String[] { "people",
"Smith", "Joe" });
+ * Fqn<String> abc = Fqn.fromElements("people", "Smith",
"Joe");
* </pre>
* This is a bit more efficient to construct.
* <p/>
- * <p/>
* Note that<br>
* <p/>
* <code>Fqn<String> f = new
Fqn<String>("/a/b/c");</code>
@@ -58,9 +57,28 @@
* Another way to look at it is that the "/" separarator is only parsed when it
forms
* part of a String passed in to Fqn.fromString() and not otherwise.
* <p/>
- * <B>NOTE</B> that support for custom object elements of Fqns will be
<i>dropped</i> from JBoss Cache 3.0.0 onwards. Consider
- * it's use in 2.x as a deprecated feature.. From 3.0.0 onwards, Fqns will only be
able to contain <tt>String</tt>s or
- * Java primitives (<tt>byte</tt>, <tt>short</tt>,
<tt>int</tt>, <tt>long</tt>, <tt>float</tt>,
<tt>double</tt>, <tt>char</tt> and <tt>boolean</tt>).
+ * <B>Best practices</B>: Always creating Fqns - even when using some factory
methods - can be expensive in the long run,
+ * and as far as possible we recommend that client code holds on to their Fqn references
and reuse them. E.g.:
+ * <code>
+ * // BAD!!
+ * for (int i=0; i<someBigNumber; i++)
+ * {
+ * cache.get(Fqn.fromString("/a/b/c"), "key" + i);
+ * }
+ * </code>
+ * instead, do:
+ * <code>
+ * // Much better
+ * Fqn f = Fqn.fromString("/a/b/c");
+ * for (int i=0; i<someBigNumber; i++)
+ * {
+ * cache.get(f, "key" + i);
+ * }
+ * </code>
+ * <p/>
+ * <B>NOTE</B> public constructors of this class are
<b>deprectaed</b> and will be removed in JBoss Cache 3.0.0. The
+ * factory methods provided are the correct way get an instance of an Fqn, since these
allow for numerous optimisations.
+ * <p/>
*
* @version $Revision$
*/
@@ -84,54 +102,92 @@
* Immutable root FQN.
*/
@SuppressWarnings("unchecked")
- public static final Fqn ROOT = new Fqn();
+ public static final Fqn ROOT = new Fqn(true);
/**
* A cached string representation of this Fqn, used by toString to it isn't
calculated again every time.
*/
private String cachedStringRep;
+ private Fqn(boolean internalMarker)
+ {
+ elements = Collections.emptyList();
+ size = 0;
+ }
+
+ private Fqn(boolean internalMarker, List<E> names, boolean safe)
+ {
+ if (names != null)
+ {
+ // if not safe make a defensive copy
+ elements = safe ? names : new ArrayList<E>(names);
+ size = elements.size();
+ }
+ else
+ {
+ elements = Collections.emptyList();
+ size = 0;
+ }
+ }
+
+ private Fqn(boolean internalMarker, Fqn<E> base, List<E> relative)
+ {
+ List<E> elements = new ArrayList<E>(base.elements.size() +
relative.size());
+ elements.addAll(base.elements);
+ elements.addAll(relative);
+ this.elements = elements;
+ size = elements.size();
+ }
+
/**
* Constructs a root Fqn
+ *
+ * @deprecated use {@link #ROOT} instead. This constructor will be removed in 3.0.0.
*/
+ @Deprecated
public Fqn()
{
- elements = Collections.emptyList();
- size = 0;
+ this(true);
}
/**
* Constructs a FQN from a list of names.
*
* @param names List of names
+ * @deprecated use {@link #fromList(java.util.List)} instead. This constructor will
be removed in 3.0.0.
*/
+ @Deprecated
public Fqn(List<E> names)
{
// the list is unsafe - may be referenced externally
- this(names, false);
+ this(true, names, false);
}
/**
+ * Retrieves an Fqn that represents the list of elements passed in.
+ *
+ * @param names list of elements that comprise the Fqn
+ * @return an Fqn
+ * @since 2.2.0
+ */
+ public static <E> Fqn<E> fromList(List<E> names)
+ {
+ return new Fqn<E>(true, names, false);
+ }
+
+ /**
* If safe is false, Collections.unmodifiableList() is used to wrap the list passed
in. This is an optimisation so
* Fqn.fromString(), probably the most frequently used factory method, doesn't end
up needing to use the unmodifiableList()
* since it creates the list internally.
*
* @param names List of names
* @param safe whether this list is referenced externally (safe = false) or not (safe
= true).
+ * @deprecated use {@link #fromList(java.util.List)} instead. The boolean
"safety" hint is calculated internally. This constructor will be removed in
3.0.0.
*/
+ @Deprecated
public Fqn(List<E> names, boolean safe)
{
- if (names != null)
- {
- // if not safe make a defensive copy
- elements = safe ? names : new ArrayList<E>(names);
- size = elements.size();
- }
- else
- {
- elements = Collections.emptyList();
- size = 0;
- }
+ this(true, names, safe);
}
@@ -139,55 +195,106 @@
* Constructs a Fqn from an array of names.
*
* @param names Names that comprose this Fqn
+ * @deprecated use {@link #fromElements(Object[])} instead. This constructor will be
removed in 3.0.0.
*/
+ @Deprecated
public Fqn(E... names)
{
// safe - the list is created here.
- this(Arrays.asList(names), true);
+ this(true, Arrays.asList(names), true);
}
/**
+ * Retrieves an Fqn that represents the array of elements passed in.
+ *
+ * @param elements array of elements that comprise the Fqn
+ * @return an Fqn
+ * @since 2.2.0
+ */
+ public static <E> Fqn<E> fromElements(E... elements)
+ {
+ return new Fqn<E>(true, Arrays.asList(elements), true);
+ }
+
+ /**
* Constructs a Fqn from a base and relative Fqn.
*
* @param base parent Fqn
* @param relative Sub-Fqn relative to the parent
+ * @deprecated use {@link #fromRelativeFqn(Fqn, Fqn)} instead. This constructor will
be removed in 3.0.0.
*/
+ @Deprecated
public Fqn(Fqn<E> base, Fqn<E> relative)
{
- this(base, relative.elements);
+ this(true, base, relative.elements);
}
/**
+ * Retrieves an Fqn that represents the absolute Fqn of the relative Fqn passed in.
+ *
+ * @param base base Fqn
+ * @param relative relative Fqn
+ * @return an Fqn
+ * @since 2.2.0
+ */
+ public static <E> Fqn<E> fromRelativeFqn(Fqn<E> base, Fqn<E>
relative)
+ {
+ return new Fqn<E>(true, base, relative.elements);
+ }
+
+ /**
* Constructs a Fqn from a base and a list of relative names.
*
* @param base parent Fqn
* @param relative List of elements that identify the child Fqn, relative to the
parent
+ * @deprecated use {@link #fromRelativeList(Fqn, java.util.List)} instead. This
constructor will be removed in 3.0.0.
*/
+ @Deprecated
public Fqn(Fqn<E> base, List<E> relative)
{
- List<E> elements = new ArrayList<E>(base.elements.size() +
relative.size());
- elements.addAll(base.elements);
- elements.addAll(relative);
- this.elements = elements;
- size = elements.size();
+ this(true, base, relative);
}
/**
+ * Retrieves an Fqn that represents the List of elements passed in, relative to the
base Fqn.
+ *
+ * @param base base Fqn
+ * @param relativeElements relative List of elements
+ * @return an Fqn
+ * @since 2.2.0
+ */
+ public static <E> Fqn<E> fromRelativeList(Fqn<E> base, List<E>
relativeElements)
+ {
+ return new Fqn<E>(true, base, relativeElements);
+ }
+
+ /**
* Constructs a Fqn from a base and two relative names.
*
* @param base parent Fqn
* @param childNames elements that denote the path to the Fqn, under the parent
+ * @deprecated use {@link #fromRelativeElements(Fqn, Object[])} instead. This
constructor will be removed in 3.0.0.
*/
+ @Deprecated
public Fqn(Fqn<E> base, E... childNames)
{
- List<E> elements = new ArrayList<E>(base.elements.size() +
childNames.length);
- elements.addAll(base.elements);
- elements.addAll(Arrays.asList(childNames));
- this.elements = elements;
- size = elements.size();
+ this(true, base, Arrays.asList(childNames));
}
/**
+ * Retrieves an Fqn that represents the array of elements passed in, relative to the
base Fqn.
+ *
+ * @param base base Fqn
+ * @param relativeElements relative elements
+ * @return an Fqn
+ * @since 2.2.0
+ */
+ public static <E> Fqn<E> fromRelativeElements(Fqn<E> base, E...
relativeElements)
+ {
+ return new Fqn<E>(true, base, Arrays.asList(relativeElements));
+ }
+
+ /**
* Returns a new Fqn from a string, where the elements are deliminated by
* one or more separator ({@link #SEPARATOR}) characters.<br><br>
* Example use:<br>
@@ -216,7 +323,7 @@
List<String> list = new ArrayList<String>();
StringTokenizer tok = new StringTokenizer(stringRepresentation, SEPARATOR);
while (tok.hasMoreTokens()) list.add(tok.nextToken());
- return new Fqn<String>(list, true);
+ return new Fqn<String>(true, list, true);
}
/**
@@ -253,7 +360,7 @@
*/
public Fqn<E> getSubFqn(int startIndex, int endIndex)
{
- return new Fqn<E>(elements.subList(startIndex, endIndex), true);
+ return new Fqn<E>(true, elements.subList(startIndex, endIndex), true);
}
/**
@@ -485,7 +592,7 @@
case 1:
return root();
default:
- return new Fqn<E>(elements.subList(0, size - 1));
+ return new Fqn<E>(true, elements.subList(0, size - 1), true);
}
}
Modified: core/trunk/src/main/java/org/jboss/cache/NodeFactory.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/NodeFactory.java 2008-04-09 02:38:58 UTC (rev
5521)
+++ core/trunk/src/main/java/org/jboss/cache/NodeFactory.java 2008-04-09 09:08:28 UTC (rev
5522)
@@ -106,7 +106,7 @@
public Node<K, V> createNodeOfType(Node<K, V> template, Object childName,
Node<K, V> parent, Map<K, V> data)
{
// not a workspace node.
- return createDataNode(childName, new Fqn(parent.getFqn(), childName),
(NodeSPI<K, V>) parent, data, false);
+ return createDataNode(childName, Fqn.fromRelativeElements(parent.getFqn(),
childName), (NodeSPI<K, V>) parent, data, false);
}
public WorkspaceNode<K, V> createWorkspaceNode(NodeSPI<K, V> dataNode,
TransactionWorkspace workspace)
Modified: core/trunk/src/main/java/org/jboss/cache/buddyreplication/BuddyManager.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/buddyreplication/BuddyManager.java 2008-04-09
02:38:58 UTC (rev 5521)
+++ core/trunk/src/main/java/org/jboss/cache/buddyreplication/BuddyManager.java 2008-04-09
09:08:28 UTC (rev 5522)
@@ -8,7 +8,13 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import org.jboss.cache.*;
+import org.jboss.cache.CacheException;
+import org.jboss.cache.CacheSPI;
+import org.jboss.cache.Fqn;
+import org.jboss.cache.Node;
+import org.jboss.cache.RPCManager;
+import org.jboss.cache.Region;
+import org.jboss.cache.RegionManager;
import org.jboss.cache.commands.CacheCommand;
import org.jboss.cache.commands.CacheCommandsFactory;
import org.jboss.cache.commands.remote.AnnounceBuddyPoolNameCommand;
@@ -38,7 +44,11 @@
import java.io.ByteArrayInputStream;
import java.util.*;
-import java.util.concurrent.*;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.TimeUnit;
/**
* Class that manages buddy replication groups.
@@ -491,7 +501,7 @@
// should be a LOCAL call.
cache.getInvocationContext().getOptionOverrides().setCacheModeLocal(true);
- cache.removeNode(new Fqn(BUDDY_BACKUP_SUBTREE_FQN, groupName));
+ cache.removeNode(Fqn.fromRelativeElements(BUDDY_BACKUP_SUBTREE_FQN,
groupName));
}
catch (CacheException e)
{
@@ -531,10 +541,8 @@
buddyGroupsIParticipateIn.put(newGroup.getDataOwner(), newGroup);
// Integrate state transfer from the data owner of the buddy group
- Fqn integrationBase = new Fqn(BuddyManager.BUDDY_BACKUP_SUBTREE_FQN,
- newGroup.getGroupName());
+ Fqn integrationBase = getBackupRoot(newGroup.getDataOwner());
-
if (state.isEmpty())
{
if (configuredToFetchState())
@@ -549,7 +557,7 @@
// FIXME -- restore the next line and remove the following one
// when JBCACHE-1265 is fixed
// root.addChild(new Fqn<String>(BUDDY_BACKUP_SUBTREE,
newGroup.getGroupName()));
- cache.put(new Fqn<String>(BUDDY_BACKUP_SUBTREE, newGroup.getGroupName()),
null);
+ cache.put(integrationBase, null);
}
else
{
@@ -559,7 +567,7 @@
if (!regionManager.isInactive(fqn))
{
//ClassLoader cl = (marshaller == null) ? null :
marshaller.getClassLoader(fqnS);
- Fqn integrationRoot = new Fqn(integrationBase, fqn);
+ Fqn integrationRoot = Fqn.fromRelativeFqn(integrationBase, fqn);
byte[] stateBuffer = entry.getValue();
MarshalledValueInputStream in = null;
@@ -642,7 +650,8 @@
try
{
return (CacheCommand) call.accept(null, fqnTransformer);
- } catch (Throwable throwable)
+ }
+ catch (Throwable throwable)
{
log.error("error while transforming an call", throwable);
throw new CacheException(throwable);
@@ -986,7 +995,6 @@
}
-
/**
* Assumes the backup Fqn if the current instance is the data owner
*
Modified:
core/trunk/src/main/java/org/jboss/cache/interceptors/OptimisticInterceptor.java
===================================================================
---
core/trunk/src/main/java/org/jboss/cache/interceptors/OptimisticInterceptor.java 2008-04-09
02:38:58 UTC (rev 5521)
+++
core/trunk/src/main/java/org/jboss/cache/interceptors/OptimisticInterceptor.java 2008-04-09
09:08:28 UTC (rev 5522)
@@ -11,8 +11,8 @@
import org.jboss.cache.InvocationContext;
import org.jboss.cache.NodeFactory;
import org.jboss.cache.NodeSPI;
-import org.jboss.cache.interceptors.base.ChainedInterceptor;
import org.jboss.cache.factories.annotations.Inject;
+import org.jboss.cache.interceptors.base.ChainedInterceptor;
import org.jboss.cache.lock.TimeoutException;
import org.jboss.cache.optimistic.TransactionWorkspace;
import org.jboss.cache.optimistic.WorkspaceNode;
@@ -56,12 +56,11 @@
/**
* Adds the Fqn of the node as well as all children and childrens children to the
list.
- *
*/
protected void greedyGetFqns(List<Fqn> list, NodeSPI<?, ?> n, Fqn
newBase)
{
list.add(n.getFqn());
- Fqn newFqn = new Fqn(newBase, n.getFqn().getLastElement());
+ Fqn newFqn = Fqn.fromRelativeElements(newBase, n.getFqn().getLastElement());
list.add(newFqn);
for (NodeSPI child : n.getChildrenDirect())
Modified: core/trunk/src/main/java/org/jboss/cache/loader/CacheLoaderManager.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/loader/CacheLoaderManager.java 2008-04-09
02:38:58 UTC (rev 5521)
+++ core/trunk/src/main/java/org/jboss/cache/loader/CacheLoaderManager.java 2008-04-09
09:08:28 UTC (rev 5522)
@@ -328,7 +328,7 @@
Fqn tmp_fqn = Fqn.ROOT;
for (int i = 0; i < fqn.size() - 1; i++)
{
- tmp_fqn = new Fqn(tmp_fqn, fqn.get(i));
+ tmp_fqn = Fqn.fromRelativeElements(tmp_fqn, fqn.get(i));
cache.get(tmp_fqn, "bla");
}
}
@@ -350,7 +350,7 @@
for (Object aChildren : children)
{
String child_name = (String) aChildren;
- Fqn child_fqn = new Fqn(fqn, child_name);
+ Fqn child_fqn = Fqn.fromRelativeElements(fqn, child_name);
preload(child_fqn, false, true);
}
}