001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.objectserver.tx;
006:
007: import com.tc.net.groups.NodeID;
008: import com.tc.object.dmi.DmiDescriptor;
009: import com.tc.object.dna.api.DNA;
010: import com.tc.object.dna.impl.ObjectStringSerializer;
011: import com.tc.object.gtx.GlobalTransactionID;
012: import com.tc.object.gtx.GlobalTransactionIDGenerator;
013: import com.tc.object.lockmanager.api.LockID;
014: import com.tc.object.tx.ServerTransactionID;
015: import com.tc.object.tx.TransactionID;
016: import com.tc.object.tx.TxnBatchID;
017: import com.tc.object.tx.TxnType;
018: import com.tc.util.Assert;
019: import com.tc.util.SequenceID;
020:
021: import java.util.Collection;
022: import java.util.HashSet;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Map;
026: import java.util.Set;
027:
028: /**
029: * Represents an atomic change to the states of objects on the server
030: *
031: * @author steve
032: */
033: public class ServerTransactionImpl implements ServerTransaction {
034: private final ServerTransactionID serverTxID;
035: private final SequenceID seqID;
036: private final List changes;
037: private final LockID[] lockIDs;
038: private final TransactionID txID;
039: private final Map newRoots;
040: private final NodeID sourceID;
041: private final TxnType transactionType;
042: private final ObjectStringSerializer serializer;
043: private final Collection notifies;
044: private final DmiDescriptor[] dmis;
045: private final Set objectIDs;
046: private final Set newObjectIDs;
047: private final TxnBatchID batchID;
048: private final GlobalTransactionID globalTxnID;
049:
050: public ServerTransactionImpl(GlobalTransactionIDGenerator gtxm,
051: TxnBatchID batchID, TransactionID txID,
052: SequenceID sequenceID, LockID[] lockIDs, NodeID source,
053: List dnas, ObjectStringSerializer serializer, Map newRoots,
054: TxnType transactionType, Collection notifies,
055: DmiDescriptor[] dmis) {
056: this .batchID = batchID;
057: this .txID = txID;
058: this .seqID = sequenceID;
059: this .lockIDs = lockIDs;
060: this .newRoots = newRoots;
061: this .sourceID = source;
062: this .serverTxID = new ServerTransactionID(source, txID);
063: // NOTE::XXX:: GlobalTransactionID is assigned in the process transaction stage. The transaction could be
064: // re-ordered before apply. This is not a problem because for an transaction to be re-ordered, it should not
065: // have any common objects between them. hence if g1 is the first txn and g2 is the second txn, g2 can be applied
066: // before g1 only when g2 has no common objects(or locks) with g1. If this is not true then we cant assign gid here.
067: this .globalTxnID = gtxm
068: .getOrCreateGlobalTransactionID(serverTxID);
069: this .transactionType = transactionType;
070: this .notifies = notifies;
071: this .dmis = dmis;
072: this .changes = dnas;
073: this .serializer = serializer;
074: Set ids = new HashSet(changes.size());
075: HashSet newIDs = new HashSet(changes.size());
076: boolean added = true;
077: for (Iterator i = changes.iterator(); i.hasNext();) {
078: DNA dna = (DNA) i.next();
079: added &= ids.add(dna.getObjectID());
080: if (!dna.isDelta()) {
081: newIDs.add(dna.getObjectID());
082: }
083: }
084: Assert.assertTrue(added);
085: this .objectIDs = ids;
086: this .newObjectIDs = newIDs;
087: }
088:
089: public ObjectStringSerializer getSerializer() {
090: return serializer;
091: }
092:
093: public LockID[] getLockIDs() {
094: return lockIDs;
095: }
096:
097: public NodeID getSourceID() {
098: return sourceID;
099: }
100:
101: public TransactionID getTransactionID() {
102: return txID;
103: }
104:
105: public SequenceID getClientSequenceID() {
106: return seqID;
107: }
108:
109: public List getChanges() {
110: return changes;
111: }
112:
113: public Map getNewRoots() {
114: return newRoots;
115: }
116:
117: public TxnType getTransactionType() {
118: return transactionType;
119: }
120:
121: public Set getObjectIDs() {
122: return this .objectIDs;
123: }
124:
125: public Set getNewObjectIDs() {
126: return this .newObjectIDs;
127: }
128:
129: public Collection getNotifies() {
130: return notifies;
131: }
132:
133: public DmiDescriptor[] getDmiDescriptors() {
134: return dmis;
135: }
136:
137: public String toString() {
138: return "ServerTransaction[" + seqID + " , " + txID + ","
139: + sourceID + "," + transactionType + "] = { changes = "
140: + changes.size() + ", notifies = " + notifies.size()
141: + ", newRoots = " + newRoots.size() + "}";
142: }
143:
144: public ServerTransactionID getServerTransactionID() {
145: return serverTxID;
146: }
147:
148: public TxnBatchID getBatchID() {
149: return batchID;
150: }
151:
152: public GlobalTransactionID getGlobalTransactionID() {
153: return this .globalTxnID;
154: }
155:
156: public boolean needsBroadcast() {
157: return true;
158: }
159: }
|