01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
03: * notice. All rights reserved.
04: */
05: package com.tc.objectserver.persistence.impl;
06:
07: import com.tc.memorydatastore.client.MemoryDataStoreClient;
08: import com.tc.memorydatastore.message.TCByteArrayKeyValuePair;
09: 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.TransactionPersistor;
14: import com.tc.util.Conversion;
15:
16: import java.util.Collection;
17: import java.util.HashSet;
18: import java.util.Iterator;
19:
20: class MemoryStoreTransactionPersistor implements TransactionPersistor {
21:
22: private final MemoryDataStoreClient db;
23:
24: public MemoryStoreTransactionPersistor(MemoryDataStoreClient db) {
25: this .db = db;
26: }
27:
28: public Collection loadAllGlobalTransactionDescriptors() {
29: Collection rv = new HashSet();
30: Collection txns = db.getAll();
31:
32: for (Iterator i = txns.iterator(); i.hasNext();) {
33: TCByteArrayKeyValuePair pair = (TCByteArrayKeyValuePair) i
34: .next();
35: rv.add(new GlobalTransactionDescriptor(
36: bytes2ServerTxnID(pair.getKey()),
37: bytes2GlobalTxnID(pair.getValue())));
38: }
39: return rv;
40: }
41:
42: public void saveGlobalTransactionDescriptor(
43: PersistenceTransaction tx, GlobalTransactionDescriptor gtx) {
44: this .db.put(serverTxnID2Bytes(gtx.getServerTransactionID()),
45: globalTxnID2Bytes(gtx.getGlobalTransactionID()));
46: }
47:
48: private GlobalTransactionID bytes2GlobalTxnID(byte[] data) {
49: return new GlobalTransactionID(Conversion.bytes2Long(data));
50: }
51:
52: private byte[] globalTxnID2Bytes(
53: GlobalTransactionID globalTransactionID) {
54: return Conversion.long2Bytes(globalTransactionID.toLong());
55: }
56:
57: private byte[] serverTxnID2Bytes(
58: ServerTransactionID serverTransactionID) {
59: return serverTransactionID.getBytes();
60: }
61:
62: private ServerTransactionID bytes2ServerTxnID(byte[] data) {
63: return ServerTransactionID.createFrom(data);
64: }
65:
66: public void deleteAllGlobalTransactionDescriptors(
67: PersistenceTransaction tx, Collection toDelete) {
68: for (Iterator i = toDelete.iterator(); i.hasNext();) {
69: ServerTransactionID stxID = (ServerTransactionID) i.next();
70: db.remove(serverTxnID2Bytes(stxID));
71: }
72: }
73: }
|