00001
package com.quadcap.sql.file;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
import java.io.IOException;
00042
import java.io.PrintWriter;
00043
00044
import java.util.Enumeration;
00045
import java.util.Iterator;
00046
import java.util.Hashtable;
00047
00048
import com.quadcap.util.Debug;
00049
import com.quadcap.util.DList;
00050
import com.quadcap.util.DListItem;
00051
import com.quadcap.util.ListException;
00052
00053
import com.quadcap.util.collections.LongMap;
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063 public abstract class Cache {
00064 Object
store;
00065 Object
lock;
00066 int size;
00067 boolean readOnly;
00068
00069
00070
00071
00072 LongMap
t;
00073
00074
00075
00076
00077 DList
lru =
new DList();
00078
00079
00080
00081
00082
00083
00084
00085 public void init(Object store,
int size) {
00086
this.store = store;
00087
this.size = size;
00088
if (
this.lock == null)
this.lock =
this;
00089
lru.resize(size);
00090
t =
new LongMap(size);
00091 }
00092
00093
00094
00095
00096 public boolean isReadOnly() {
return readOnly; }
00097
00098
00099
00100
00101 public void setReadOnly(
boolean v) {
this.readOnly = v; }
00102
00103
00104
00105
00106
00107
00108
00109
00110 public void setLock(Object lock) {
00111
this.lock = lock;
00112 }
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122 public Cacheable getCacheable(
long key)
throws IOException {
00123
synchronized (
lock) {
00124
00125
Cacheable c = (
Cacheable)
t.get(key);
00126
if (c == null) {
00127
00128
00129 c =
getCacheable();
00130 c.
init(
store, key);
00131 c.
setReadOnly(
readOnly);
00132
t.put(key, c);
00133 }
00134
00135
00136
try {
00137
lru.moveFront(c.
getDListItem());
00138 }
catch (
ListException e) {
00139
00140
Debug.print(e);
00141
00142 }
00143 c.
incrRefCount();
00144
if (
lru.size() !=
size)
throw new RuntimeException(
"cache size!");
00145
00146
return c;
00147 }
00148 }
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164 public Object
get(
int key)
throws IOException {
00165 Object data = null;
00166
synchronized (
lock) {
00167
Cacheable c =
getCacheable(key);
00168
try {
00169 data = c.
getData();
00170 } finally {
00171 c.
decrRefCount();
00172 }
00173 }
00174
return data;
00175 }
00176
00177
00178
00179
00180
00181 public void put(
int key, Object val)
throws IOException {
00182
synchronized (
lock) {
00183
Cacheable c =
getCacheable(key);
00184
try {
00185 c.
setData(val);
00186 } finally {
00187 c.
decrRefCount();
00188 }
00189 }
00190 }
00191
00192
00193
00194
00195
00196
00197
abstract public Cacheable makeCacheable();
00198
00199
00200
00201
00202
00203
00204
00205 private final Cacheable getCacheable() throws IOException {
00206
Cacheable c = null;
00207
DListItem d =
lru.tail();
00208
00209
while (d != null) {
00210 c = (
Cacheable)d.
obj;
00211
if (c != null && c.
getRefCount() > 0) {
00212
if (d ==
lru.head()) {
00213
throw new RuntimeException(
"no free cache item: cache size = " +
lru.size() +
"(" +
size +
")");
00214 }
00215 d = d.
prev;
00216 }
else {
00217
break;
00218 }
00219 }
00220
00221
if (c == null) {
00222 d.
obj = c =
makeCacheable();
00223 c.
setDListItem(d);
00224 }
else {
00225
long key = c.
getKey();
00226
t.remove(key);
00227
if (c.
isDirty()) c.
flush();
00228 }
00229
return c;
00230 }
00231
00232
00233
00234
00235 public void flush() throws IOException {
00236
synchronized (
lock) {
00237
boolean started =
false;
00238
for (
DListItem d =
lru.head(); !started || d !=
lru.head();
00239 d = d.
next) {
00240 started =
true;
00241
Cacheable c = (
Cacheable)d.
obj;
00242
if (c != null) {
00243 c.
flush();
00244 }
00245 }
00246 }
00247 }
00248
00249 public void revert() {
00250
synchronized (
lock) {
00251
lru =
new DList();
00252 init(
store,
size);
00253 }
00254 }
00255
00256 public void show(PrintWriter os) {
00257
synchronized (
lock) {
00258
lru.show(os,
"\n");
00259 }
00260 }
00261 }