1 4 package com.tc.objectserver.persistence.impl; 5 6 import com.tc.exception.ImplementMe; 7 import com.tc.exception.TCRuntimeException; 8 import com.tc.net.protocol.tcm.ChannelID; 9 import com.tc.object.gtx.GlobalTransactionID; 10 import com.tc.object.tx.ServerTransactionID; 11 import com.tc.objectserver.gtx.GlobalTransactionDescriptor; 12 import com.tc.objectserver.persistence.api.PersistenceTransaction; 13 import com.tc.objectserver.persistence.api.TransactionStore; 14 import com.tc.util.concurrent.NoExceptionLinkedQueue; 15 16 import java.io.ByteArrayInputStream ; 17 import java.io.ByteArrayOutputStream ; 18 import java.io.ObjectInputStream ; 19 import java.io.ObjectOutputStream ; 20 import java.util.Collection ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 import java.util.SortedSet ; 25 import java.util.TreeSet ; 26 27 public class TestTransactionStore implements TransactionStore { 28 29 public final NoExceptionLinkedQueue leastContextQueue = new NoExceptionLinkedQueue(); 30 public final NoExceptionLinkedQueue commitContextQueue = new NoExceptionLinkedQueue(); 31 public final NoExceptionLinkedQueue loadContextQueue = new NoExceptionLinkedQueue(); 32 public final NoExceptionLinkedQueue nextTransactionIDContextQueue = new NoExceptionLinkedQueue(); 33 34 private final Map volatileMap = new HashMap (); 35 private final SortedSet ids = new TreeSet (GlobalTransactionID.COMPARATOR); 36 private final Map durableMap = new HashMap (); 37 private final Map sids2gid = new HashMap (); 38 public long idSequence = 1; 39 40 public long nextGlobalTransactionIDBatch(int batchSize) { 41 throw new ImplementMe(); 42 } 43 44 public void restart() throws Exception { 45 volatileMap.clear(); 46 ids.clear(); 47 sids2gid.clear(); 48 volatileMap.putAll(durableMap); 49 for (Iterator i = volatileMap.values().iterator(); i.hasNext();) { 50 ServerTransactionID stxID = ((GlobalTransactionDescriptor) i.next()).getServerTransactionID(); 51 GlobalTransactionID gid = new GlobalTransactionID(idSequence++); 52 ids.add(gid); 53 sids2gid.put(stxID, gid); 54 } 55 } 56 57 public GlobalTransactionDescriptor createTransactionDescriptor(ServerTransactionID stxid) { 58 nextTransactionIDContextQueue.put(stxid); 59 GlobalTransactionDescriptor rv = new GlobalTransactionDescriptor(stxid); 60 basicPut(volatileMap, rv); 61 62 return rv; 63 } 64 65 private void basicPut(Map map, GlobalTransactionDescriptor txID) { 66 map.put(txID.getServerTransactionID(), txID); 67 } 68 69 public void commitTransactionDescriptor(PersistenceTransaction persistenceTransaction, GlobalTransactionDescriptor txID) { 70 try { 71 commitContextQueue.put(new Object [] { persistenceTransaction, txID }); 72 if (!volatileMap.containsValue(txID)) throw new AssertionError (); 73 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 74 ObjectOutputStream out = new ObjectOutputStream (baos); 75 out.writeObject(txID); 76 out.flush(); 77 out.close(); 78 baos.flush(); 79 basicPut(durableMap, (GlobalTransactionDescriptor) new ObjectInputStream (new ByteArrayInputStream (baos 80 .toByteArray())).readObject()); 81 82 } catch (Exception e) { 83 throw new TCRuntimeException(e); 84 } 85 } 86 87 public GlobalTransactionDescriptor getTransactionDescriptor(ServerTransactionID stxid) { 88 try { 89 loadContextQueue.put(stxid); 90 return (GlobalTransactionDescriptor) volatileMap.get(stxid); 91 } catch (Exception e) { 92 throw new TCRuntimeException(e); 93 } 94 } 95 96 public GlobalTransactionID getLeastGlobalTransactionID() { 97 leastContextQueue.put(new Object ()); 98 return (GlobalTransactionID) ids.first(); 99 } 100 101 public void removeAllByServerTransactionID(PersistenceTransaction tx, Collection collection) { 102 for (Iterator iter = collection.iterator(); iter.hasNext();) { 103 ServerTransactionID sid = (ServerTransactionID) iter.next(); 104 GlobalTransactionID gid = (GlobalTransactionID) sids2gid.remove(sid); 105 if (gid != null) { 106 ids.remove(gid); 107 } 108 volatileMap.remove(sid); 109 durableMap.remove(sid); 110 } 111 } 112 113 public GlobalTransactionID createGlobalTransactionID(ServerTransactionID stxnID) { 114 loadContextQueue.put(stxnID); 115 GlobalTransactionID gtxID = new GlobalTransactionID(idSequence++); 116 ids.add(gtxID); 117 sids2gid.put(stxnID, gtxID); 118 return gtxID; 119 } 120 121 public void shutdownClient(PersistenceTransaction transaction, ChannelID client) { 122 throw new ImplementMe(); 123 } 124 } | Popular Tags |