1 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 private static final int GLOBAL_TRESHOLD = 768; 28 private static final int LOCAL_TRESHOLD = 512; 29 30 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 private static int cacheSize = 0; 37 private int localCacheSize = 0; 38 private boolean tresholdReached = false; 39 40 private boolean dataCommited = false; 42 43 private LinkedList commitedOperations = new LinkedList (); 45 private LinkedList operations = new LinkedList (); 47 48 public TransactionCache() { 49 } 50 51 54 public boolean tresholdReached () { 55 return tresholdReached; 56 } 57 58 61 public boolean containsCommitedData () { 62 return dataCommited; 63 } 64 65 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 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 97 public void addDeleted (MOFID id) { 98 if (!tresholdReached) { 99 operations.addLast ((new Record (OP_DELETE, id, null))); 100 incrementCacheSize(); 101 } 102 } 103 104 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 115 public void commit () { 116 if (!operations.isEmpty()) 117 commitedOperations.addLast (operations); 118 119 operations = new LinkedList (); 120 dataCommited = true; 121 } 122 123 126 public CacheIterator iterator () { 127 return new CacheIterator (); 128 } 129 130 132 public static class Record { 133 byte op; MOFID id; byte [] value; 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 146 public class CacheIterator { 147 148 private Iterator primar; 149 private Iterator secondar = null; 150 private Object 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 temp = nextItem; 179 fetchNext (); 180 return (Record) temp; 181 } 182 183 } 184 185 } | Popular Tags |