1 package com.quadcap.sql.file; 2 3 40 41 import java.io.IOException ; 42 import java.io.PrintWriter ; 43 44 import java.util.Enumeration ; 45 import java.util.Iterator ; 46 import java.util.Hashtable ; 47 48 import com.quadcap.util.Debug; 49 import com.quadcap.util.DList; 50 import com.quadcap.util.DListItem; 51 import com.quadcap.util.ListException; 52 53 import com.quadcap.util.collections.LongMap; 54 55 63 public abstract class Cache { 64 Object store; 65 Object lock; 66 int size; 67 boolean readOnly; 68 69 72 LongMap t; 73 74 77 DList lru = new DList(); 78 79 85 public void init(Object store, int size) { 86 this.store = store; 87 this.size = size; 88 if (this.lock == null) this.lock = this; 89 lru.resize(size); 90 t = new LongMap(size); 91 } 92 93 96 public boolean isReadOnly() { return readOnly; } 97 98 101 public void setReadOnly(boolean v) { this.readOnly = v; } 102 103 110 public void setLock(Object lock) { 111 this.lock = lock; 112 } 113 114 122 public Cacheable getCacheable(long key) throws IOException { 123 synchronized (lock) { 124 Cacheable c = (Cacheable)t.get(key); 126 if (c == null) { 127 c = getCacheable(); 130 c.init(store, key); 131 c.setReadOnly(readOnly); 132 t.put(key, c); 133 } 134 135 try { 137 lru.moveFront(c.getDListItem()); 138 } catch (ListException e) { 139 Debug.print(e); 141 } 143 c.incrRefCount(); 144 if (lru.size() != size) throw new RuntimeException ("cache size!"); 145 return c; 147 } 148 } 149 150 164 public Object get(int key) throws IOException { 165 Object data = null; 166 synchronized (lock) { 167 Cacheable c = getCacheable(key); 168 try { 169 data = c.getData(); 170 } finally { 171 c.decrRefCount(); 172 } 173 } 174 return data; 175 } 176 177 181 public void put(int key, Object val) throws IOException { 182 synchronized (lock) { 183 Cacheable c = getCacheable(key); 184 try { 185 c.setData(val); 186 } finally { 187 c.decrRefCount(); 188 } 189 } 190 } 191 192 197 abstract public Cacheable makeCacheable(); 198 199 205 private final Cacheable getCacheable() throws IOException { 206 Cacheable c = null; 207 DListItem d = lru.tail(); 208 209 while (d != null) { 210 c = (Cacheable)d.obj; 211 if (c != null && c.getRefCount() > 0) { 212 if (d == lru.head()) { 213 throw new RuntimeException ("no free cache item: cache size = " + lru.size() + "(" + size + ")"); 214 } 215 d = d.prev; 216 } else { 217 break; } 219 } 220 221 if (c == null) { 222 d.obj = c = makeCacheable(); 223 c.setDListItem(d); 224 } else { 225 long key = c.getKey(); 226 t.remove(key); 227 if (c.isDirty()) c.flush(); 228 } 229 return c; 230 } 231 232 235 public void flush() throws IOException { 236 synchronized (lock) { 237 boolean started = false; 238 for (DListItem d = lru.head(); !started || d != lru.head(); 239 d = d.next) { 240 started = true; 241 Cacheable c = (Cacheable)d.obj; 242 if (c != null) { 243 c.flush(); 244 } 245 } 246 } 247 } 248 249 public void revert() { 250 synchronized (lock) { 251 lru = new DList(); 252 init(store, size); 253 } 254 } 255 256 public void show(PrintWriter os) { 257 synchronized (lock) { 258 lru.show(os, "\n"); 259 } 260 } 261 } 262 | Popular Tags |