KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > objectserver > persistence > impl > TestTransactionStore


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

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 JavaDoc;
17 import java.io.ByteArrayOutputStream JavaDoc;
18 import java.io.ObjectInputStream JavaDoc;
19 import java.io.ObjectOutputStream JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.SortedSet JavaDoc;
25 import java.util.TreeSet JavaDoc;
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 JavaDoc volatileMap = new HashMap JavaDoc();
35   private final SortedSet JavaDoc ids = new TreeSet JavaDoc(GlobalTransactionID.COMPARATOR);
36   private final Map JavaDoc durableMap = new HashMap JavaDoc();
37   private final Map JavaDoc sids2gid = new HashMap JavaDoc();
38   public long idSequence = 1;
39
40   public long nextGlobalTransactionIDBatch(int batchSize) {
41     throw new ImplementMe();
42   }
43
44   public void restart() throws Exception JavaDoc {
45     volatileMap.clear();
46     ids.clear();
47     sids2gid.clear();
48     volatileMap.putAll(durableMap);
49     for (Iterator JavaDoc 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 JavaDoc 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 JavaDoc[] { persistenceTransaction, txID });
72       if (!volatileMap.containsValue(txID)) throw new AssertionError JavaDoc();
73       ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
74       ObjectOutputStream JavaDoc out = new ObjectOutputStream JavaDoc(baos);
75       out.writeObject(txID);
76       out.flush();
77       out.close();
78       baos.flush();
79       basicPut(durableMap, (GlobalTransactionDescriptor) new ObjectInputStream JavaDoc(new ByteArrayInputStream JavaDoc(baos
80           .toByteArray())).readObject());
81
82     } catch (Exception JavaDoc 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 JavaDoc e) {
92       throw new TCRuntimeException(e);
93     }
94   }
95
96   public GlobalTransactionID getLeastGlobalTransactionID() {
97     leastContextQueue.put(new Object JavaDoc());
98     return (GlobalTransactionID) ids.first();
99   }
100
101   public void removeAllByServerTransactionID(PersistenceTransaction tx, Collection JavaDoc collection) {
102     for (Iterator JavaDoc 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