comparison src/org/dancres/blitz/arc/Lru.java @ 0:3dc0c5604566

Initial checkin of blitz 2.0 fcs - no installer yet.
author Dan Creswell <dan.creswell@gmail.com>
date Sat, 21 Mar 2009 11:00:06 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:3dc0c5604566
1 package org.dancres.blitz.arc;
2
3 import java.io.IOException;
4
5 import java.util.logging.*;
6
7 import org.dancres.struct.LinkedInstances;
8
9 class Lru {
10 static final int T1 = 0;
11 static final int T2 = 1;
12 static final int B1 = 2;
13 static final int B2 = 3;
14
15 private static final String[] NAMES = {"T1", "T2", "B1", "B2"};
16
17 private LinkedInstances theEntries = new LinkedInstances();
18
19 private int theId;
20
21 Lru(int anId) {
22 theId = anId;
23 }
24
25 synchronized void remove(CacheBlockDescriptor aDesc) {
26 if (aDesc.getWhere() != theId)
27 throw new RuntimeException("I don't own this descriptor");
28
29 theEntries.remove(aDesc);
30 }
31
32 synchronized void mruInsert(CacheBlockDescriptor aDesc) {
33 theEntries.insert(aDesc);
34 aDesc.setWhere(theId);
35 }
36
37 synchronized void lruInsert(CacheBlockDescriptor aDesc) {
38 theEntries.add(aDesc);
39 aDesc.setWhere(theId);
40 }
41
42 synchronized CacheBlockDescriptor lruRemove() {
43 return (CacheBlockDescriptor) theEntries.removeLast();
44 }
45
46 synchronized int length() {
47 return theEntries.getSize();
48 }
49
50 synchronized void dump() {
51 Logger myLogger = ArcCache.theLogger;
52
53 myLogger.log(Level.FINE, "AC: " + NAMES[theId]);
54
55 CacheBlockDescriptor myCBD =
56 (CacheBlockDescriptor) theEntries.getHead();
57
58 while (myCBD != null) {
59
60 try {
61 myCBD.acquire();
62
63 myLogger.log(Level.FINE, myCBD.getId() + ", " +
64 myCBD.getId().hashCode() + ", ");
65
66 myCBD.release();
67 } catch (InterruptedException anIE) {
68 myLogger.log(Level.FINE, "Interrupted locking CBD");
69 }
70
71 myCBD = (CacheBlockDescriptor) myCBD.getNext();
72 }
73 }
74
75 synchronized void sync(BackingStore aStore) throws IOException {
76 CacheBlockDescriptor myCBD =
77 (CacheBlockDescriptor) theEntries.getHead();
78
79 while (myCBD != null) {
80
81 try {
82 myCBD.acquire();
83
84 aStore.save(myCBD.getContent());
85
86 myCBD.release();
87 } catch (InterruptedException anIE) {
88 throw new IOException();
89 }
90
91 myCBD = (CacheBlockDescriptor) myCBD.getNext();
92 }
93 }
94 }