KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > persistence > btreeimpl > btreestorage > TransactionCache


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.btreeimpl.btreestorage;
20
21 import java.util.*;
22 import org.netbeans.mdr.persistence.MOFID;
23
24 public class TransactionCache {
25
26     // max. number of cached operations
27
private static final int GLOBAL_TRESHOLD = 768;
28     private static final int LOCAL_TRESHOLD = 512;
29
30     // operations codes
31
public static final byte OP_INSERT = 0;
32     public static final byte OP_DELETE = 1;
33     public static final byte OP_REPLACE = 2;
34
35     // number of cached operations
36
private static int cacheSize = 0;
37     private int localCacheSize = 0;
38     private boolean tresholdReached = false;
39     
40     // true if transaction cache stores some commited operations
41
private boolean dataCommited = false;
42     
43     // list of lits containing commited operations (each list corresponds to one commit)
44
private LinkedList commitedOperations = new LinkedList ();
45     // cached operations that have not been commited yet
46
private LinkedList operations = new LinkedList ();
47             
48     public TransactionCache() {
49     }
50
51     /**
52      * @return true if transaction cache treshold has been reached
53      */

54     public boolean tresholdReached () {
55         return tresholdReached;
56     }
57     
58     /**
59      * @return true if cache stores some commited operations
60      */

61     public boolean containsCommitedData () {
62         return dataCommited;
63     }
64     
65     /**
66      * Resets cache (to store no operations).
67      */

68     public void clear () {
69         operations = new LinkedList ();
70         commitedOperations.clear ();
71         synchronized (TransactionCache.class) {
72             cacheSize -= localCacheSize;
73             localCacheSize = 0;
74             tresholdReached = false;
75         }
76         dataCommited = false;
77     }
78     
79     private void incrementCacheSize() {
80         synchronized (TransactionCache.class) {
81             localCacheSize++;
82             cacheSize++;
83      
84             tresholdReached |= cacheSize >= GLOBAL_TRESHOLD || localCacheSize >= LOCAL_TRESHOLD;
85         }
86     }
87     
88     /** adds one insert operation */
89     public void addInserted (MOFID id, byte [] value) {
90         if (!tresholdReached) {
91             operations.addLast (new Record (OP_INSERT, id, value));
92             incrementCacheSize();
93         }
94     }
95     
96     /** adds one delete operation */
97     public void addDeleted (MOFID id) {
98         if (!tresholdReached) {
99             operations.addLast ((new Record (OP_DELETE, id, null)));
100             incrementCacheSize();
101         }
102     }
103     
104     /** adds one replace operation */
105     public void addReplaced (MOFID id, byte [] value) {
106         if (!tresholdReached) {
107             operations.addLast (new Record (OP_REPLACE, id, value));
108             incrementCacheSize();
109         }
110     }
111     
112     /**
113      * Commits operations temporarly stored in @link #operations.
114      */

115     public void commit () {
116         if (!operations.isEmpty())
117             commitedOperations.addLast (operations);
118                 
119         operations = new LinkedList ();
120         dataCommited = true;
121     }
122
123     /**
124      * Returns cache iterator.
125      */

126     public CacheIterator iterator () {
127         return new CacheIterator ();
128     }
129     
130     // Record *******************************************************************
131

132     public static class Record {
133         byte op; // operation code
134
MOFID id; // key
135
byte [] value; // serialized value of a Streamable object
136

137         public Record (byte opId, MOFID id, byte [] value) {
138             this.op = opId;
139             this.id = id;
140             this.value = value;
141         }
142     }
143     
144     // CacheIterator ************************************************************
145

146     public class CacheIterator {
147         
148         private Iterator primar;
149         private Iterator secondar = null;
150         private Object JavaDoc nextItem = null;
151         
152         CacheIterator () {
153             primar = commitedOperations.iterator ();
154             if (primar.hasNext ()) {
155                 secondar = ((List)primar.next ()).iterator ();
156                 fetchNext();
157             }
158         }
159         
160         private void fetchNext () {
161             if (secondar.hasNext ()) {
162                 nextItem = secondar.next ();
163             } else {
164                 if (primar.hasNext ()) {
165                     secondar = ((List)primar.next()).iterator();
166                     nextItem = secondar.next ();
167                 } else {
168                     nextItem = null;
169                 }
170             }
171         }
172         
173         public boolean hasNext () {
174             return nextItem != null;
175         }
176         
177         public Record next () {
178             Object JavaDoc temp = nextItem;
179             fetchNext ();
180             return (Record) temp;
181         }
182                     
183     }
184
185 }
Popular Tags