From jbosscache-commits at lists.jboss.org Wed Oct 8 04:46:07 2008 Content-Type: multipart/mixed; boundary="===============4001920010093926110==" MIME-Version: 1.0 From: jbosscache-commits at lists.jboss.org To: jbosscache-commits at lists.jboss.org Subject: [jbosscache-commits] JBoss Cache SVN: r6856 - core/branches/flat/src/main/java/org/jboss/starobrno/context. Date: Wed, 08 Oct 2008 04:46:07 -0400 Message-ID: --===============4001920010093926110== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Author: manik.surtani(a)jboss.com Date: 2008-10-08 04:46:07 -0400 (Wed, 08 Oct 2008) New Revision: 6856 Added: core/branches/flat/src/main/java/org/jboss/starobrno/context/EntryLookup= .java core/branches/flat/src/main/java/org/jboss/starobrno/context/Transaction= Context.java core/branches/flat/src/main/java/org/jboss/starobrno/context/Transaction= ContextImpl.java Modified: core/branches/flat/src/main/java/org/jboss/starobrno/context/InvocationC= ontext.java core/branches/flat/src/main/java/org/jboss/starobrno/context/InvocationC= ontextImpl.java Log: Contexts Added: core/branches/flat/src/main/java/org/jboss/starobrno/context/EntryLo= okup.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- core/branches/flat/src/main/java/org/jboss/starobrno/context/EntryLooku= p.java (rev 0) +++ core/branches/flat/src/main/java/org/jboss/starobrno/context/EntryLooku= p.java 2008-10-08 08:46:07 UTC (rev 6856) @@ -0,0 +1,46 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2008, Red Hat Middleware LLC, and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.starobrno.context; + +import org.jboss.starobrno.mvcc.MVCCEntry; + +import java.util.Map; +import java.util.Set; + +/** + * // TODO: MANIK: Document this + * + * @author Manik Surtani (manik(a)jbo= ss.org) + * @since 3.0 + */ +public interface EntryLookup +{ + MVCCEntry lookupEntry(Object key); + + Map getLookedUpEntries(); + + void putLookedUpEntry(MVCCEntry e); + + void putLookedUpEntries(Set lookedUpEntries); + + void clearLookedUpEntries(); +} Modified: core/branches/flat/src/main/java/org/jboss/starobrno/context/Invo= cationContext.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- core/branches/flat/src/main/java/org/jboss/starobrno/context/Invocation= Context.java 2008-10-08 08:22:02 UTC (rev 6855) +++ core/branches/flat/src/main/java/org/jboss/starobrno/context/Invocation= Context.java 2008-10-08 08:46:07 UTC (rev 6856) @@ -24,13 +24,9 @@ import org.jboss.cache.commands.VisitableCommand; import org.jboss.cache.config.Option; import org.jboss.cache.transaction.GlobalTransaction; -import org.jboss.cache.transaction.TransactionContext; -import org.jboss.starobrno.mvcc.MVCCEntry; = import javax.transaction.Transaction; import java.util.List; -import java.util.Map; -import java.util.Set; = /** * // TODO: MANIK: Document this @@ -38,18 +34,8 @@ * @author Manik Surtani (manik(a)jbo= ss.org) * @since 3.0 */ -public interface InvocationContext +public interface InvocationContext extends EntryLookup { - MVCCEntry lookupEntry(Object key); - - Map getLookedUpEntries(); - - void putLookedUpEntry(MVCCEntry e); - - void putLookedUpEntries(Set lookedUpEntries); - - void clearLookedUpEntries(); - void setLocalRollbackOnly(boolean localRollbackOnly); = Transaction getTransaction(); Modified: core/branches/flat/src/main/java/org/jboss/starobrno/context/Invo= cationContextImpl.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- core/branches/flat/src/main/java/org/jboss/starobrno/context/Invocation= ContextImpl.java 2008-10-08 08:22:02 UTC (rev 6855) +++ core/branches/flat/src/main/java/org/jboss/starobrno/context/Invocation= ContextImpl.java 2008-10-08 08:46:07 UTC (rev 6856) @@ -29,7 +29,6 @@ import org.jboss.cache.config.Option; import org.jboss.cache.marshall.MethodCall; import org.jboss.cache.transaction.GlobalTransaction; -import org.jboss.cache.transaction.TransactionContext; import org.jboss.cache.transaction.TransactionTable; import org.jboss.cache.util.Immutables; import org.jboss.starobrno.mvcc.MVCCEntry; @@ -86,10 +85,8 @@ */ public MVCCEntry lookupEntry(Object k) { - // TODO: Fix this after we have a Transaction context that works. -// if (transactionContext !=3D null) return transactionContext.lookup= Entry(k); -// return lookedUpEntries =3D=3D null ? null : lookedUpEntries.get(k); - return null; + if (transactionContext !=3D null) return transactionContext.lookupEn= try(k); + return lookedUpEntries =3D=3D null ? null : lookedUpEntries.get(k); } = /** @@ -103,29 +100,26 @@ */ public void putLookedUpEntry(MVCCEntry e) { - // TODO: Fix this after we have a Transaction context that works. - -// if (transactionContext !=3D null) -// transactionContext.putLookedUpEntry(e); -// else -// { -// if (lookedUpEntries =3D=3D null) lookedUpEntries =3D new HashMa= p(4); -// lookedUpEntries.put(e.getKey(), e); -// } + if (transactionContext !=3D null) + transactionContext.putLookedUpEntry(e); + else + { + if (lookedUpEntries =3D=3D null) lookedUpEntries =3D new HashMap<= Object, MVCCEntry>(4); + lookedUpEntries.put(e.getKey(), e); + } } = public void putLookedUpEntries(Set lookedUpEntries) { - // TODO: Fix this after we have a Transaction context that works. -// if (transactionContext !=3D null) -// transactionContext.putLookedUpEntries(lookedUpEntries); -// else -// { -// if (this.lookedUpEntries =3D=3D null) -// this.lookedUpEntries =3D new HashMap(); -// -// for (MVCCEntry e: lookedUpEntries) this.lookedUpEntries.put(e.g= etKey(), e); -// } + if (transactionContext !=3D null) + transactionContext.putLookedUpEntries(lookedUpEntries); + else + { + if (this.lookedUpEntries =3D=3D null) + this.lookedUpEntries =3D new HashMap(); + + for (MVCCEntry e : lookedUpEntries) this.lookedUpEntries.put(e.ge= tKey(), e); + } } = /** @@ -135,12 +129,9 @@ */ public void clearLookedUpEntries() { - // TODO: see if we can reinstate common behaviour once we have the I= CI calling ctx.reset() instead of ctx.clearLookedUpNodes() -// if (transactionContext !=3D null) -// transactionContext.clearLookedUpNodes(); -// else - - if (lookedUpEntries !=3D null) lookedUpEntries.clear(); + if (transactionContext !=3D null) + transactionContext.clearLookedUpEntries(); + else if (lookedUpEntries !=3D null) lookedUpEntries.clear(); } = /** @@ -154,10 +145,8 @@ @SuppressWarnings("unchecked") public Map getLookedUpEntries() { - // TODO: Fix this after we have a Transaction context that works. -// if (transactionContext !=3D null) return transactionContext.getLoo= kedUpNodes(); -// return (Map) (lookedUpEntries =3D=3D null ? Collecti= ons.emptyMap() : lookedUpEntries); - return null; + if (transactionContext !=3D null) return transactionContext.getLooke= dUpEntries(); + return (Map) (lookedUpEntries =3D=3D null ? Colle= ctions.emptyMap() : lookedUpEntries); } = @SuppressWarnings("unchecked") @@ -299,7 +288,7 @@ public List getKeysLocked() { // first check transactional scope - if (transactionContext !=3D null) return transactionContext.getLocks= (); + if (transactionContext !=3D null) return transactionContext.getKeysL= ocked(); return invocationLocks =3D=3D null || invocationLocks.isEmpty() ? Co= llections.emptyList() : Immutables.immutableListConvert(invocationLocks); } = @@ -316,12 +305,12 @@ * @param locks locks to add */ @SuppressWarnings("unchecked") - public void addAllKeysLocked(List locks) + public void addAllKeysLocked(List locks) { // first check transactional scope if (transactionContext !=3D null) { - transactionContext.addAllLocks(locks); + transactionContext.addAllKeysLocked(locks); } else { @@ -349,7 +338,7 @@ // first check transactional scope if (transactionContext !=3D null) { - transactionContext.addLock(lock); + transactionContext.addKeyLocked(lock); } else { @@ -377,7 +366,7 @@ // first check transactional scope if (transactionContext !=3D null) { - transactionContext.removeLock(lock); + transactionContext.removeKeyLocked(lock); } else { @@ -401,7 +390,7 @@ // first check transactional scope if (transactionContext !=3D null) { - transactionContext.clearLocks(); + transactionContext.clearKeysLocked(); } else { @@ -422,7 +411,7 @@ // first check transactional scope if (transactionContext !=3D null) { - return transactionContext.hasLock(lock); + return transactionContext.hasLockedKey(lock); } else { Added: core/branches/flat/src/main/java/org/jboss/starobrno/context/Transac= tionContext.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- core/branches/flat/src/main/java/org/jboss/starobrno/context/Transactio= nContext.java (rev 0) +++ core/branches/flat/src/main/java/org/jboss/starobrno/context/Transactio= nContext.java 2008-10-08 08:46:07 UTC (rev 6856) @@ -0,0 +1,283 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2000 - 2008, Red Hat Middleware LLC, and individual contribut= ors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.starobrno.context; + +import org.jboss.cache.Fqn; +import org.jboss.cache.commands.WriteCommand; +import org.jboss.cache.config.Option; +import org.jboss.cache.interceptors.OrderedSynchronizationHandler; + +import javax.transaction.Transaction; +import java.util.List; + +/** + * Captures information pertaining to a specific JTA transaction. + *

+ * This was a concrete class called TransactionEntry prior to 3.0. + *

+ * + * @author Manik Surtani (manik(a)jbo= ss.org) + * @see org.jboss.cache.InvocationContext + */ +public interface TransactionContext extends EntryLookup +{ + /** + * Adds a modification to the modification list. + * + * @param command modification + */ + void addModification(WriteCommand command); + + /** + * Returns all modifications. If there are no modifications in this tr= ansaction this method will return an empty list. + * + * @return list of modifications. + */ + List getModifications(); + + /** + * Adds a modification to the local modification list. + * + * @param command command to add to list. Should not be null. + * @throws NullPointerException if the command to be added is null. + */ + void addLocalModification(WriteCommand command); + + /** + * Returns all modifications that have been invoked with the LOCAL cach= e mode option. These will also be in the standard modification list. + * + * @return list of LOCAL modifications, or an empty list. + */ + List getLocalModifications(); + + /** + * Adds the node that has been removed in the scope of the current tran= saction. + * + * @param fqn fqn that has been removed. + * @throws NullPointerException if the Fqn is null. + */ + void addRemovedNode(Fqn fqn); + + /** + * Gets the list of removed nodes. + * + * @return list of nodes removed in the current transaction scope. Not= e that this method will return an empty list if nothing has been removed. = The list returned is defensively copied. + */ + List getRemovedNodes(); + + /** + * Sets the local transaction to be associated with this transaction co= ntext. + * + * @param tx JTA transaction to associate with. + */ + void setTransaction(Transaction tx); + + /** + * Returns a local transaction associated with this context. + * + * @return a JTA transaction + */ + Transaction getTransaction(); + + /** + * Adds a lock to the currently maintained collection of locks acquired. + *

+ * Most code could not use this method directly, but use {@link org.jbo= ss.cache.InvocationContext#addLock(Object)} instead, + * which would delegate to this method if a transaction is in scope or = otherwise use invocation-specific locks. + *

+ * Note that currently (as of 3.0.0) this lock is weakly typed. This i= s to allow support for both MVCC (which uses {@link org.jboss.cache.Fqn}s a= s locks) + * as well as legacy Optimistic and Pessimistic Locking schemes (which = use {@link org.jboss.cache.lock.NodeLock} as locks). Once support for + * legacy node locking schemes are dropped, this method will be more st= rongly typed to accept {@link org.jboss.cache.Fqn}. + * + * @param lock lock to add + * @see org.jboss.cache.InvocationContext#addLock(Object) + */ + @SuppressWarnings("unchecked") + void addKeyLocked(Object lock); + + /** + * Removes a lock from the currently maintained collection of locks acq= uired. + *

+ * Most code could not use this method directly, but use {@link org.jbo= ss.cache.InvocationContext#removeLock(Object)} instead, + * which would delegate to this method if a transaction is in scope or = otherwise use invocation-specific locks. + *

+ * Note that currently (as of 3.0.0) this lock is weakly typed. This i= s to allow support for both MVCC (which uses {@link org.jboss.cache.Fqn}s a= s locks) + * as well as legacy Optimistic and Pessimistic Locking schemes (which = use {@link org.jboss.cache.lock.NodeLock} as locks). Once support for + * legacy node locking schemes are dropped, this method will be more st= rongly typed to accept {@link org.jboss.cache.Fqn}. + * + * @param lock lock to remove + * @see org.jboss.cache.InvocationContext#removeLock(Object) + */ + @SuppressWarnings("unchecked") + void removeKeyLocked(Object lock); + + /** + * Clears all locks from the currently maintained collection of locks a= cquired. + *

+ * Most code could not use this method directly, but use {@link org.jbo= ss.cache.InvocationContext#clearLocks()} instead, + * which would delegate to this method if a transaction is in scope or = otherwise use invocation-specific locks. + *

+ * Note that currently (as of 3.0.0) this lock is weakly typed. This i= s to allow support for both MVCC (which uses {@link org.jboss.cache.Fqn}s a= s locks) + * as well as legacy Optimistic and Pessimistic Locking schemes (which = use {@link org.jboss.cache.lock.NodeLock} as locks). Once support for + * legacy node locking schemes are dropped, this method will be more st= rongly typed to accept {@link org.jboss.cache.Fqn}. + * + * @see org.jboss.cache.InvocationContext#clearLocks() + */ + void clearKeysLocked(); + + /** + * Adds a List of locks to the currently maintained collection of locks= acquired. + *

+ * Most code could not use this method directly, but use {@link org.jbo= ss.cache.InvocationContext#addAllLocks(java.util.List)} instead, + * which would delegate to this method if a transaction is in scope or = otherwise use invocation-specific locks. + *

+ * Note that currently (as of 3.0.0) this list is unchecked. This is t= o allow support for both MVCC (which uses Fqns as locks) + * as well as legacy Optimistic and Pessimistic Locking schemes (which = use {@link org.jboss.cache.lock.NodeLock} as locks). Once support for + * legacy node locking schemes are dropped, this method will be more st= rongly typed to accept List. + * + * @param newLocks locks to add + * @see org.jboss.cache.InvocationContext#addAllLocks(java.util.List) + */ + @SuppressWarnings("unchecked") + void addAllKeysLocked(List newLocks); + + /** + * Returns an immutable, defensive copy of the List of locks currently= maintained for the transaction. + *

+ * Most code could not use this method directly, but use {@link org.jbo= ss.cache.InvocationContext#getLocks()} instead, + * which would delegate to this method if a transaction is in scope or = otherwise use invocation-specific locks. + *

+ * Note that currently (as of 3.0.0) this list is unchecked. This is t= o allow support for both MVCC (which uses Fqns as locks) + * as well as legacy Optimistic and Pessimistic Locking schemes (which = use {@link org.jboss.cache.lock.NodeLock} as locks). Once support for + * legacy node locking schemes are dropped, this method will be more st= rongly typed to return List. + * + * @return locks held in current scope. + * @see org.jboss.cache.InvocationContext#getLocks() + */ + @SuppressWarnings("unchecked") + List getKeysLocked(); + + /** + * Most code could not use this method directly, but use {@link org.jbo= ss.cache.InvocationContext#hasLock(Object)} ()} instead, + * which would delegate to this method if a transaction is in scope or = otherwise use invocation-specific locks. + * + * @param lock lock to test + * @return true if the lock being tested is already held in the current= scope, false otherwise. + */ + boolean hasLockedKey(Object lock); + + /** + * Gets the value of the forceAsyncReplication flag. Used by Replicati= onInterceptor and OptimisticReplicationInterceptor + * when dealing with {@link org.jboss.cache.Cache#putForExternalRead(or= g.jboss.cache.Fqn,Object,Object)} within + * a transactional context. + * + * @return true if the forceAsyncReplication flag is set to true. + */ + boolean isForceAsyncReplication(); + + /** + * Sets the value of the forceAsyncReplication flag. Used by Replicati= onInterceptor and OptimisticReplicationInterceptor + * when dealing with {@link org.jboss.cache.Cache#putForExternalRead(or= g.jboss.cache.Fqn,Object,Object)} within + * a transactional context. Also used by OptimisticReplicationIntercept= or when dealing + * with {@link org.jboss.cache.config.Option#setForceAsynchronous(boole= an)} in a + * non-transactional context (i.e. with an implicit transaction). + * + * @param forceAsyncReplication value of forceAsyncReplication + */ + void setForceAsyncReplication(boolean forceAsyncReplication); + + /** + * Gets the value of the forceSyncReplication flag. Used by Replicatio= nInterceptor and OptimisticReplicationInterceptor + * when dealing with {@link org.jboss.cache.Cache#putForExternalRead(or= g.jboss.cache.Fqn,Object,Object)} within + * a transactional context. + * + * @return true if the forceAsyncReplication flag is set to true. + */ + boolean isForceSyncReplication(); + + /** + * Sets the value of the forceSyncReplication flag. Used by Replicatio= nInterceptor and OptimisticReplicationInterceptor + * when dealing with {@link org.jboss.cache.Cache#putForExternalRead(or= g.jboss.cache.Fqn,Object,Object)} within + * a transactional context. + * + * @param forceSyncReplication value of forceSyncReplication + */ + void setForceSyncReplication(boolean forceSyncReplication); + + /** + * Adds an Fqn to the list of uninitialized nodes created by the cache = loader. + * + * @param fqn fqn to add. Must not be null. + */ + void addDummyNodeCreatedByCacheLoader(Fqn fqn); + + /** + * @return a list of uninitialized nodes created by the cache loader, o= r an empty list. + */ + List getDummyNodesCreatedByCacheLoader(); + + /** + * Sets a transaction-scope option override + * + * @param o option to set + */ + void setOption(Option o); + + /** + * Retrieves a transaction scope option override + * + * @return option + */ + Option getOption(); + + /** + * @return the ordered sync handler associated with this transaction + */ + OrderedSynchronizationHandler getOrderedSynchronizationHandler(); + + /** + * Associates an ordered sync handler with this transaction. + * + * @param orderedSynchronizationHandler to set + */ + void setOrderedSynchronizationHandler(OrderedSynchronizationHandler ord= eredSynchronizationHandler); + + /** + * @return true if modifications were registered. + */ + boolean hasModifications(); + + /** + * @return true if any modifications have been invoked with cache mode = being LOCAL. + */ + boolean hasLocalModifications(); + + /** + * @return true if either there are modifications or local modification= s that are not for replicating. + */ + boolean hasAnyModifications(); + + /** + * Cleans up internal state, freeing up references. + */ + void reset(); +} Added: core/branches/flat/src/main/java/org/jboss/starobrno/context/Transac= tionContextImpl.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- core/branches/flat/src/main/java/org/jboss/starobrno/context/Transactio= nContextImpl.java (rev 0) +++ core/branches/flat/src/main/java/org/jboss/starobrno/context/Transactio= nContextImpl.java 2008-10-08 08:46:07 UTC (rev 6856) @@ -0,0 +1,346 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2000 - 2008, Red Hat Middleware LLC, and individual contribut= ors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.starobrno.context; + +import org.jboss.cache.Fqn; +import org.jboss.cache.commands.WriteCommand; +import org.jboss.cache.config.Option; +import org.jboss.cache.interceptors.OrderedSynchronizationHandler; +import org.jboss.cache.util.Immutables; +import org.jboss.starobrno.mvcc.MVCCEntry; + +import javax.transaction.RollbackException; +import javax.transaction.SystemException; +import javax.transaction.Transaction; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedHashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * A transaction context specially geared to dealing with MVCC. + * + * @author Manik Surtani (manik(a)jbo= ss.org) + * @since 3.0 + */ +public class TransactionContextImpl implements TransactionContext +{ + /** + * Local transaction + */ + private Transaction ltx =3D null; + private Option option; + private OrderedSynchronizationHandler orderedSynchronizationHandler; + + private boolean forceAsyncReplication =3D false; + private boolean forceSyncReplication =3D false; + + /** + * List<ReversibleCommand> of modifications ({@link org.jboss.cac= he.commands.WriteCommand}). They will be replicated on TX commit + */ + private List modificationList; + /** + * A list of modifications that have been encountered with a LOCAL mode= option. These will be removed from the modification list during replicati= on. + */ + private List localModifications; + + /** + * LinkedHashSet of locks acquired by the transaction. We use a LinkedH= ashSet because we need efficient Set semantics + * but also need guaranteed ordering for use by lock release code (see = JBCCACHE-874). + *

+ * This needs to be unchecked since we support both MVCC (Fqns held her= e) or legacy Opt/Pess locking (NodeLocks held here). + * once we drop support for opt/pess locks we can genericise this to co= ntain Fqns. - Manik Surtani, June 2008 + */ + private LinkedHashSet transactionLocks; + + /** + * A list of dummy uninitialised nodes created by the cache loader inte= rceptor to load data for a + * given node in this tx. + */ + private List dummyNodesCreatedByCacheLoader; + + /** + * List of nodes that have been removed by the transaction + */ + private List removedNodes =3D null; + + private final Map lookedUpEntries =3D new HashMap(8); + + public TransactionContextImpl(Transaction tx) throws SystemException, R= ollbackException + { + ltx =3D tx; + orderedSynchronizationHandler =3D new OrderedSynchronizationHandler(= tx); + } + + /** + * Retrieves a node from the registry of looked up nodes in the current= scope. + *

+ * This is not normally called directly since {@link org.jboss.cache.In= vocationContext#lookUpNode(org.jboss.cache.Fqn)} + * would delegate to this method if a transaction is in scope. + *

+ * + * @param fqn fqn to look up + * @return a node, or null if it cannot be found. + */ + public MVCCEntry lookupEntry(Object key) + { + return lookedUpEntries.get(key); + } + + /** + * Puts an entry in the registry of looked up nodes in the current scop= e. + *

+ * This is not normally called directly since {@link org.jboss.cache.In= vocationContext#putLookedUpNode(org.jboss.cache.Fqn, org.jboss.cache.NodeSP= I)} + * would delegate to this method if a transaction is in scope. + *

+ * + * @param f fqn to add + * @param n node to add + */ + public void putLookedUpEntry(MVCCEntry entry) + { + lookedUpEntries.put(entry.getKey(), entry); + } + + /** + * Clears the registry of looked up nodes. + *

+ * This is not normally called directly since {@link org.jboss.cache.In= vocationContext#clearLookedUpNodes()} + * would delegate to this method if a transaction is in scope. + *

+ */ + public void clearLookedUpEntries() + { + lookedUpEntries.clear(); + } + + /** + * Retrieves a map of nodes looked up within the current invocation's s= cope. + *

+ * This is not normally called directly since {@link org.jboss.cache.In= vocationContext#getLookedUpNodes()} + * would delegate to this method if a transaction is in scope. + *

+ * + * @return a map of looked up nodes. + */ + public Map getLookedUpEntries() + { + return lookedUpEntries; + } + + public void reset() + { + orderedSynchronizationHandler =3D null; + modificationList =3D null; + localModifications =3D null; + option =3D null; + if (transactionLocks !=3D null) transactionLocks.clear(); + if (dummyNodesCreatedByCacheLoader !=3D null) dummyNodesCreatedByCac= heLoader.clear(); + if (removedNodes !=3D null) removedNodes.clear(); + lookedUpEntries.clear(); + } + + public void putLookedUpEntries(Set entries) + { + for (MVCCEntry e : entries) lookedUpEntries.put(e.getKey(), e); + } + + public void addModification(WriteCommand command) + { + if (command =3D=3D null) return; + if (modificationList =3D=3D null) modificationList =3D new LinkedLis= t(); + modificationList.add(command); + } + + public List getModifications() + { + if (modificationList =3D=3D null) return Collections.emptyList(); + return modificationList; + } + + public void addLocalModification(WriteCommand command) + { + if (command =3D=3D null) throw new NullPointerException("Command is = null!"); + if (localModifications =3D=3D null) localModifications =3D new Linke= dList(); + localModifications.add(command); + } + + public List getLocalModifications() + { + if (localModifications =3D=3D null) return Collections.emptyList(); + return localModifications; + } + + + public void addRemovedNode(Fqn fqn) + { + if (fqn =3D=3D null) throw new NullPointerException("Fqn is null!"); + if (removedNodes =3D=3D null) removedNodes =3D new LinkedList(); + removedNodes.add(fqn); + } + + public List getRemovedNodes() + { + if (removedNodes =3D=3D null) return Collections.emptyList(); + return new ArrayList(removedNodes); + } + + public void setTransaction(Transaction tx) + { + ltx =3D tx; + } + + public Transaction getTransaction() + { + return ltx; + } + + @SuppressWarnings("unchecked") + public void addKeyLocked(Object lock) + { + // no need to worry about concurrency here - a context is only valid= for a single thread. + if (transactionLocks =3D=3D null) transactionLocks =3D new LinkedHas= hSet(5); + transactionLocks.add(lock); + } + + @SuppressWarnings("unchecked") + public void removeKeyLocked(Object lock) + { + // no need to worry about concurrency here - a context is only valid= for a single thread. + if (transactionLocks !=3D null) transactionLocks.remove(lock); + } + + public void clearKeysLocked() + { + if (transactionLocks !=3D null) transactionLocks.clear(); + } + + @SuppressWarnings("unchecked") + public void addAllKeysLocked(List newLocks) + { + // no need to worry about concurrency here - a context is only valid= for a single thread. + if (transactionLocks =3D=3D null) transactionLocks =3D new LinkedHas= hSet(5); + transactionLocks.addAll(newLocks); + } + + @SuppressWarnings("unchecked") + public List getKeysLocked() + { + return transactionLocks =3D=3D null || transactionLocks.isEmpty() ? = Collections.emptyList() : Immutables.immutableListConvert(transactionLocks); + } + + public boolean hasLockedKey(Object lock) + { + return transactionLocks !=3D null && transactionLocks.contains(lock); + } + + public boolean isForceAsyncReplication() + { + return forceAsyncReplication; + } + + public void setForceAsyncReplication(boolean forceAsyncReplication) + { + this.forceAsyncReplication =3D forceAsyncReplication; + if (forceAsyncReplication) + { + forceSyncReplication =3D false; + } + } + + public boolean isForceSyncReplication() + { + return forceSyncReplication; + } + + public void setForceSyncReplication(boolean forceSyncReplication) + { + this.forceSyncReplication =3D forceSyncReplication; + if (forceSyncReplication) + { + forceAsyncReplication =3D false; + } + } + + /** + * Returns debug information about this transaction. + */ + @Override + public String toString() + { + StringBuilder sb =3D new StringBuilder(); + sb.append("TransactionEntry\nmodificationList: ").append(modificatio= nList); + return sb.toString(); + } + + public void addDummyNodeCreatedByCacheLoader(Fqn fqn) + { + if (dummyNodesCreatedByCacheLoader =3D=3D null) + dummyNodesCreatedByCacheLoader =3D new LinkedList(); + dummyNodesCreatedByCacheLoader.add(fqn); + } + + public List getDummyNodesCreatedByCacheLoader() + { + if (dummyNodesCreatedByCacheLoader =3D=3D null) return Collections.e= mptyList(); + return dummyNodesCreatedByCacheLoader; + } + + public void setOption(Option o) + { + this.option =3D o; + } + + public Option getOption() + { + return this.option; + } + + public OrderedSynchronizationHandler getOrderedSynchronizationHandler() + { + return orderedSynchronizationHandler; + } + + public void setOrderedSynchronizationHandler(OrderedSynchronizationHand= ler orderedSynchronizationHandler) + { + this.orderedSynchronizationHandler =3D orderedSynchronizationHandler; + } + + public boolean hasModifications() + { + return modificationList !=3D null && !modificationList.isEmpty(); + } + + public boolean hasLocalModifications() + { + return localModifications !=3D null && !localModifications.isEmpty(); + } + + public boolean hasAnyModifications() + { + return hasModifications() || hasLocalModifications(); + } +} --===============4001920010093926110==--