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.bytes.TCByteBuffer;
008: import com.tc.io.TCByteBufferInputStream;
009: import com.tc.net.groups.NodeID;
010: import com.tc.object.ObjectID;
011: import com.tc.object.dmi.DmiDescriptor;
012: import com.tc.object.dna.impl.DNAImpl;
013: import com.tc.object.dna.impl.ObjectStringSerializer;
014: import com.tc.object.gtx.GlobalTransactionIDGenerator;
015: import com.tc.object.lockmanager.api.LockID;
016: import com.tc.object.lockmanager.api.Notify;
017: import com.tc.object.tx.TransactionID;
018: import com.tc.object.tx.TxnBatchID;
019: import com.tc.object.tx.TxnType;
020: import com.tc.util.SequenceID;
021:
022: import java.io.IOException;
023: import java.util.ArrayList;
024: import java.util.HashMap;
025: import java.util.LinkedList;
026: import java.util.List;
027: import java.util.Map;
028:
029: public class TransactionBatchReaderImpl implements
030: TransactionBatchReader {
031:
032: private final TCByteBufferInputStream in;
033: private final TxnBatchID batchID;
034: private final NodeID source;
035: private int numTxns;
036: private ObjectStringSerializer serializer;
037: private final GlobalTransactionIDGenerator gtxm;
038: private final ServerTransactionFactory txnFactory;
039:
040: public TransactionBatchReaderImpl(
041: GlobalTransactionIDGenerator gtxm, TCByteBuffer[] data,
042: NodeID nodeID, ObjectStringSerializer serializer,
043: ServerTransactionFactory txnFactory) throws IOException {
044: this .gtxm = gtxm;
045: this .txnFactory = txnFactory;
046: this .in = new TCByteBufferInputStream(data);
047: this .source = nodeID;
048: this .batchID = new TxnBatchID(in.readLong());
049: this .numTxns = in.readInt();
050: this .serializer = serializer;
051: }
052:
053: public NodeID getNodeID() {
054: return this .source;
055: }
056:
057: public ServerTransaction getNextTransaction() throws IOException {
058: if (numTxns == 0) {
059: int bytesRemaining = in.available();
060: if (bytesRemaining != 0) {
061: throw new IOException(bytesRemaining
062: + " bytes remaining (expecting 0)");
063: }
064: return null;
065: }
066:
067: // TODO: use factory to avoid dupe instances
068: TransactionID txnID = new TransactionID(in.readLong());
069: TxnType txnType = TxnType.typeFor(in.readByte());
070:
071: SequenceID sequenceID = new SequenceID(in.readLong());
072:
073: final int numLocks = in.readInt();
074: LockID[] locks = new LockID[numLocks];
075: for (int i = 0; i < numLocks; i++) {
076: // TODO: use factory to avoid dupe instances
077: locks[i] = new LockID(in.readString());
078: }
079:
080: Map newRoots = new HashMap();
081: final int numNewRoots = in.readInt();
082: for (int i = 0; i < numNewRoots; i++) {
083: String name = in.readString();
084:
085: // TODO: use factory to avoid dupe instances
086: ObjectID id = new ObjectID(in.readLong());
087: newRoots.put(name, id);
088: }
089:
090: List notifies = new LinkedList();
091: final int numNotifies = in.readInt();
092: for (int i = 0; i < numNotifies; i++) {
093: Notify n = new Notify();
094: n.deserializeFrom(in);
095: notifies.add(n);
096: }
097:
098: final int dmiCount = in.readInt();
099: final DmiDescriptor[] dmis = new DmiDescriptor[dmiCount];
100: for (int i = 0; i < dmiCount; i++) {
101: DmiDescriptor dd = new DmiDescriptor();
102: dd.deserializeFrom(in);
103: dmis[i] = dd;
104: }
105:
106: List dnas = new ArrayList();
107: final int numDNA = in.readInt();
108: for (int i = 0; i < numDNA; i++) {
109: DNAImpl dna = new DNAImpl(serializer, true);
110: dna.deserializeFrom(in);
111: dnas.add(dna);
112: }
113:
114: numTxns--;
115: return txnFactory.createServerTransaction(gtxm, getBatchID(),
116: txnID, sequenceID, locks, source, dnas, serializer,
117: newRoots, txnType, notifies, dmis);
118: }
119:
120: public TxnBatchID getBatchID() {
121: return this .batchID;
122: }
123:
124: public int getNumTxns() {
125: return this.numTxns;
126: }
127: }
|