KickJava   Java API By Example, From Geeks To Geeks.

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


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 MultivalueLog {
25
26     private static final byte OP_ADD = 0;
27     private static final byte OP_REMOVE = 1;
28     private static final byte OP_REPLACE = 2;
29     private static final byte OP_ADD_ALL = 3;
30
31     private static final int NULL_POSITION = -1;
32
33     private ArrayList log = new ArrayList ();
34     private MultivaluedIndexImpl index;
35     private boolean rollingBack = false;
36
37     // ..........................................................................
38

39     public MultivalueLog(MultivaluedIndexImpl index) {
40         this.index = index;
41     }
42     
43     // ..........................................................................
44

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

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

55     public void logAdd (Object JavaDoc key, Object JavaDoc value) {
56         if (rollingBack)
57             return;
58         Record rec = new Record (OP_REMOVE, key, value);
59         log.add (0, rec);
60     }
61
62     /**
63      * Logs add operation.
64      */

65     public void logAdd (Object JavaDoc key, Object JavaDoc value, int position) {
66         if (rollingBack)
67             return;
68         Record rec = new Record (OP_REMOVE, key, value, position);
69         log.add (0, rec);
70     }
71     
72     /**
73      * Logs remove operation.
74      */

75     public void logRemove (Object JavaDoc key, Object JavaDoc value) {
76         if (rollingBack)
77             return;
78         Record rec = new Record (OP_ADD, key, value);
79         log.add (0, rec);
80     }
81
82     /**
83      * Logs remove operation.
84      */

85     public void logRemove (Object JavaDoc key, Object JavaDoc value, int position) {
86         if (rollingBack)
87             return;
88         Record rec = new Record (OP_ADD, key, value, position);
89         log.add (0, rec);
90     }
91     
92     /**
93      * Logs remove operation.
94      */

95     public void logRemoveKey (Object JavaDoc key, Object JavaDoc value) {
96         if (rollingBack)
97             return;
98         Record rec = new Record (OP_ADD_ALL, key, value);
99         log.add (0, rec);
100     }
101     
102     /**
103      * Logs replace operation.
104      */

105     public void logReplace (Object JavaDoc key, Object JavaDoc oldValue, int position) {
106         if (rollingBack)
107             return;
108         Record rec = new Record (OP_REPLACE, key, oldValue, position);
109         log.add (0, rec);
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.iterator();
118         while(iter.hasNext()) {
119             Record rec = (Record) iter.next();
120             switch (rec.opCode) {
121                 case OP_ADD:
122                     if (rec.position == NULL_POSITION)
123                         index.add (rec.key, rec.value);
124                     else
125                         ((MultivaluedOrderedIndex) index).add (rec.key, rec.position, rec.value);
126                     break;
127                 case OP_REMOVE:
128                     if (rec.position == NULL_POSITION)
129                         index.remove (rec.key, rec.value);
130                     else
131                         ((MultivaluedOrderedIndex) index).remove (rec.key, rec.position);
132                     break;
133                 case OP_REPLACE:
134                     ((MultivaluedOrderedIndex) index).replace (rec.key, rec.position, rec.value);
135                     break;
136                 case OP_ADD_ALL:
137                     index.setKey (rec.key, rec.value);
138             }
139         }
140         rollingBack = false;
141     }
142
143     // Record ...................................................................
144

145     private static class Record {
146         
147         byte opCode; // operation code
148
Object JavaDoc key;
149         Object JavaDoc value;
150         int position;
151
152         public Record (byte opCode, Object JavaDoc key, Object JavaDoc value, int position) {
153             this.opCode = opCode;
154             this.key = key;
155             this.value = value;
156             this.position = position;
157         }
158         
159         public Record (byte opCode, Object JavaDoc key, Object JavaDoc value) {
160             this.opCode = opCode;
161             this.key = key;
162             this.value = value;
163             this.position = NULL_POSITION;
164         }
165         
166     } // Record
167

168 }
169
Popular Tags