KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > persistence > memoryimpl > TransactionLog


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.mdr.persistence.memoryimpl;
20
21 import java.util.*;
22 import org.netbeans.mdr.persistence.*;
23
24 public class TransactionLog {
25
26     // operations codes
27
private static final byte OP_NONE = 0;
28     private static final byte OP_ADD = 1;
29     private static final byte OP_REMOVE = 2;
30     private static final byte OP_REPLACE = 3;
31
32     private HashMap log = new HashMap ();
33     private SinglevaluedIndex index;
34     private boolean rollingBack = false;
35
36     // ..........................................................................
37

38     public TransactionLog (SinglevaluedIndex index) {
39         this.index = index;
40     }
41     
42     // ..........................................................................
43

44     /**
45      * Clears the transaction log.
46      */

47     public void clear () {
48         log.clear ();
49     }
50
51     /**
52      * Logs add operation.
53      */

54     public void logAdd (Object JavaDoc key) {
55         if (rollingBack)
56             return;
57         Record rec = (Record) log.get(key);
58         if (rec == null) {
59             rec = new Record (OP_REMOVE, null);
60             log.put (key, rec);
61         } else {
62             rec.opCode = OP_REPLACE;
63         }
64     }
65
66     /**
67      * Logs remove operation.
68      */

69     public void logRemove (Object JavaDoc key, Object JavaDoc value) {
70         if (rollingBack)
71             return;
72         Record rec = (Record) log.get(key);
73         if (rec == null) {
74             rec = new Record (OP_ADD, value);
75             log.put (key, rec);
76         } else {
77             log.remove (key);
78         }
79     }
80
81     /**
82      * Logs replace operation.
83      */

84     public void logReplace (Object JavaDoc key, Object JavaDoc oldValue) {
85         if (rollingBack)
86             return;
87         Record rec = (Record) log.get(key);
88         if (rec == null) {
89             rec = new Record (OP_REPLACE, oldValue);
90             log.put (key, rec);
91         }
92     }
93
94     public void logValue (Object JavaDoc key, Object JavaDoc value) {
95         if (rollingBack)
96             return;
97         log.put (key, new Record (OP_NONE, value));
98     }
99     
100     public boolean isLogged (Object JavaDoc key) {
101         return log.get (key) != null;
102     }
103     
104     public void setDirty (Object JavaDoc key) {
105         if (rollingBack)
106             return;
107         Record rec = (Record) log.get(key);
108         if (rec.opCode == OP_NONE)
109             rec.opCode = OP_REPLACE;
110     }
111     
112     /**
113      * Rolls back all changes performed over the index during the recorded transaction.
114      */

115     public void rollBack () throws StorageException {
116         rollingBack = true;
117         Iterator iter = log.keySet().iterator();
118         while(iter.hasNext()) {
119             Object JavaDoc key = iter.next();
120             Record rec = (Record) log.get(key);
121             if (rec.opCode == OP_NONE)
122                 continue;
123             Object JavaDoc value;
124             if (rec.value instanceof ValueLog) {
125                 value = ((ValueLog) rec.value).resolveOriginalValue ();
126             } else {
127                 value = rec.value;
128             }
129             switch (rec.opCode) {
130                 case OP_ADD:
131                     index.add (key, value);
132                 break;
133                 case OP_REMOVE:
134                     index.remove (key);
135                 break;
136                 case OP_REPLACE:
137                     index.replace (key, value);
138                 break;
139             } // switch
140
}
141         rollingBack = false;
142     }
143
144     // ValueLog .................................................................
145

146     public interface ValueLog {
147         
148         public Object JavaDoc resolveOriginalValue () throws StorageException;
149         
150     } // ValueLog
151

152     // Record ...................................................................
153

154     private static class Record {
155         
156         byte opCode; // operation code
157
Object JavaDoc value;
158
159         public Record (byte opCode, Object JavaDoc value) {
160             this.opCode = opCode;
161             this.value = value;
162         }
163         
164     } // Record
165

166 }
167
Popular Tags