| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.util; |
| 7 |
|
| 8 |
import java.util.ArrayList; |
| 9 |
import java.util.Collections; |
| 10 |
import java.util.Map; |
| 11 |
import org.h2.engine.Constants; |
| 12 |
import org.h2.engine.SysProperties; |
| 13 |
import org.h2.message.DbException; |
| 14 |
|
| 15 |
/** |
| 16 |
* A cache implementation based on the last recently used (LRU) algorithm. |
| 17 |
*/ |
| 18 |
public class CacheLRU implements Cache { |
| 19 |
|
| 20 |
static final String TYPE_NAME = "LRU"; |
| 21 |
|
| 22 |
private final CacheWriter writer; |
| 23 |
|
| 24 |
/** |
| 25 |
* Use First-In-First-Out (don't move recently used items to the front of |
| 26 |
* the queue). |
| 27 |
*/ |
| 28 |
private final boolean fifo; |
| 29 |
|
| 30 |
private final CacheObject head = new CacheHead(); |
| 31 |
private final int mask; |
| 32 |
private CacheObject[] values; |
| 33 |
private int recordCount; |
| 34 |
|
| 35 |
/** |
| 36 |
* The number of cache buckets. |
| 37 |
*/ |
| 38 |
private final int len; |
| 39 |
|
| 40 |
/** |
| 41 |
* The maximum memory, in words (4 bytes each). |
| 42 |
*/ |
| 43 |
private long maxMemory; |
| 44 |
|
| 45 |
/** |
| 46 |
* The current memory used in this cache, in words (4 bytes each). |
| 47 |
*/ |
| 48 |
private long memory; |
| 49 |
|
| 50 |
CacheLRU(CacheWriter writer, int maxMemoryKb, boolean fifo) { |
| 51 |
this.writer = writer; |
| 52 |
this.fifo = fifo; |
| 53 |
this.setMaxMemory(maxMemoryKb); |
| 54 |
try { |
| 55 |
// Since setMaxMemory() ensures that maxMemory is >=0, |
| 56 |
// we don't have to worry about an underflow. |
| 57 |
long tmpLen = maxMemory / 64; |
| 58 |
if (tmpLen > Integer.MAX_VALUE) { |
| 59 |
throw new IllegalArgumentException(); |
| 60 |
} |
| 61 |
this.len = MathUtils.nextPowerOf2((int) tmpLen); |
| 62 |
} catch (IllegalArgumentException e) { |
| 63 |
throw new IllegalStateException("This much cache memory is not supported: " + maxMemoryKb + "kb", e); |
| 64 |
} |
| 65 |
this.mask = len - 1; |
| 66 |
clear(); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Create a cache of the given type and size. |
| 71 |
* |
| 72 |
* @param writer the cache writer |
| 73 |
* @param cacheType the cache type |
| 74 |
* @param cacheSize the size |
| 75 |
* @return the cache object |
| 76 |
*/ |
| 77 |
public static Cache getCache(CacheWriter writer, String cacheType, |
| 78 |
int cacheSize) { |
| 79 |
Map<Integer, CacheObject> secondLevel = null; |
| 80 |
if (cacheType.startsWith("SOFT_")) { |
| 81 |
secondLevel = new SoftValuesHashMap<>(); |
| 82 |
cacheType = cacheType.substring("SOFT_".length()); |
| 83 |
} |
| 84 |
Cache cache; |
| 85 |
if (CacheLRU.TYPE_NAME.equals(cacheType)) { |
| 86 |
cache = new CacheLRU(writer, cacheSize, false); |
| 87 |
} else if (CacheTQ.TYPE_NAME.equals(cacheType)) { |
| 88 |
cache = new CacheTQ(writer, cacheSize); |
| 89 |
} else { |
| 90 |
throw DbException.getInvalidValueException("CACHE_TYPE", cacheType); |
| 91 |
} |
| 92 |
if (secondLevel != null) { |
| 93 |
cache = new CacheSecondLevel(cache, secondLevel); |
| 94 |
} |
| 95 |
return cache; |
| 96 |
} |
| 97 |
|
| 98 |
@Override |
| 99 |
public void clear() { |
| 100 |
head.cacheNext = head.cachePrevious = head; |
| 101 |
// first set to null - avoiding out of memory |
| 102 |
values = null; |
| 103 |
values = new CacheObject[len]; |
| 104 |
recordCount = 0; |
| 105 |
memory = len * (long)Constants.MEMORY_POINTER; |
| 106 |
} |
| 107 |
|
| 108 |
@Override |
| 109 |
public void put(CacheObject rec) { |
| 110 |
if (SysProperties.CHECK) { |
| 111 |
int pos = rec.getPos(); |
| 112 |
CacheObject old = find(pos); |
| 113 |
if (old != null) { |
| 114 |
throw DbException.getInternalError("try to add a record twice at pos " + pos); |
| 115 |
} |
| 116 |
} |
| 117 |
int index = rec.getPos() & mask; |
| 118 |
rec.cacheChained = values[index]; |
| 119 |
values[index] = rec; |
| 120 |
recordCount++; |
| 121 |
memory += rec.getMemory(); |
| 122 |
addToFront(rec); |
| 123 |
removeOldIfRequired(); |
| 124 |
} |
| 125 |
|
| 126 |
@Override |
| 127 |
public CacheObject update(int pos, CacheObject rec) { |
| 128 |
CacheObject old = find(pos); |
| 129 |
if (old == null) { |
| 130 |
put(rec); |
| 131 |
} else { |
| 132 |
if (old != rec) { |
| 133 |
throw DbException.getInternalError("old!=record pos:" + pos + " old:" + old + " new:" + rec); |
| 134 |
} |
| 135 |
if (!fifo) { |
| 136 |
removeFromLinkedList(rec); |
| 137 |
addToFront(rec); |
| 138 |
} |
| 139 |
} |
| 140 |
return old; |
| 141 |
} |
| 142 |
|
| 143 |
private void removeOldIfRequired() { |
| 144 |
// a small method, to allow inlining |
| 145 |
if (memory >= maxMemory) { |
| 146 |
removeOld(); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
private void removeOld() { |
| 151 |
int i = 0; |
| 152 |
ArrayList<CacheObject> changed = new ArrayList<>(); |
| 153 |
long mem = memory; |
| 154 |
int rc = recordCount; |
| 155 |
boolean flushed = false; |
| 156 |
CacheObject next = head.cacheNext; |
| 157 |
while (true) { |
| 158 |
if (rc <= Constants.CACHE_MIN_RECORDS) { |
| 159 |
break; |
| 160 |
} |
| 161 |
if (changed.isEmpty()) { |
| 162 |
if (mem <= maxMemory) { |
| 163 |
break; |
| 164 |
} |
| 165 |
} else { |
| 166 |
if (mem * 4 <= maxMemory * 3) { |
| 167 |
break; |
| 168 |
} |
| 169 |
} |
| 170 |
CacheObject check = next; |
| 171 |
next = check.cacheNext; |
| 172 |
i++; |
| 173 |
if (i >= recordCount) { |
| 174 |
if (!flushed) { |
| 175 |
writer.flushLog(); |
| 176 |
flushed = true; |
| 177 |
i = 0; |
| 178 |
} else { |
| 179 |
// can't remove any record, because the records can not be |
| 180 |
// removed hopefully this does not happen frequently, but it |
| 181 |
// can happen |
| 182 |
writer.getTrace() |
| 183 |
.info("cannot remove records, cache size too small? records:" + |
| 184 |
recordCount + " memory:" + memory); |
| 185 |
break; |
| 186 |
} |
| 187 |
} |
| 188 |
if (check == head) { |
| 189 |
throw DbException.getInternalError("try to remove head"); |
| 190 |
} |
| 191 |
// we are not allowed to remove it if the log is not yet written |
| 192 |
// (because we need to log before writing the data) |
| 193 |
// also, can't write it if the record is pinned |
| 194 |
if (!check.canRemove()) { |
| 195 |
removeFromLinkedList(check); |
| 196 |
addToFront(check); |
| 197 |
continue; |
| 198 |
} |
| 199 |
rc--; |
| 200 |
mem -= check.getMemory(); |
| 201 |
if (check.isChanged()) { |
| 202 |
changed.add(check); |
| 203 |
} else { |
| 204 |
remove(check.getPos()); |
| 205 |
} |
| 206 |
} |
| 207 |
if (!changed.isEmpty()) { |
| 208 |
if (!flushed) { |
| 209 |
writer.flushLog(); |
| 210 |
} |
| 211 |
Collections.sort(changed); |
| 212 |
long max = maxMemory; |
| 213 |
int size = changed.size(); |
| 214 |
try { |
| 215 |
// temporary disable size checking, |
| 216 |
// to avoid stack overflow |
| 217 |
maxMemory = Long.MAX_VALUE; |
| 218 |
for (i = 0; i < size; i++) { |
| 219 |
CacheObject rec = changed.get(i); |
| 220 |
writer.writeBack(rec); |
| 221 |
} |
| 222 |
} finally { |
| 223 |
maxMemory = max; |
| 224 |
} |
| 225 |
for (i = 0; i < size; i++) { |
| 226 |
CacheObject rec = changed.get(i); |
| 227 |
remove(rec.getPos()); |
| 228 |
if (rec.cacheNext != null) { |
| 229 |
throw DbException.getInternalError(); |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
private void addToFront(CacheObject rec) { |
| 236 |
if (rec == head) { |
| 237 |
throw DbException.getInternalError("try to move head"); |
| 238 |
} |
| 239 |
rec.cacheNext = head; |
| 240 |
rec.cachePrevious = head.cachePrevious; |
| 241 |
rec.cachePrevious.cacheNext = rec; |
| 242 |
head.cachePrevious = rec; |
| 243 |
} |
| 244 |
|
| 245 |
private void removeFromLinkedList(CacheObject rec) { |
| 246 |
if (rec == head) { |
| 247 |
throw DbException.getInternalError("try to remove head"); |
| 248 |
} |
| 249 |
rec.cachePrevious.cacheNext = rec.cacheNext; |
| 250 |
rec.cacheNext.cachePrevious = rec.cachePrevious; |
| 251 |
// TODO cache: mystery: why is this required? needs more memory if we |
| 252 |
// don't do this |
| 253 |
rec.cacheNext = null; |
| 254 |
rec.cachePrevious = null; |
| 255 |
} |
| 256 |
|
| 257 |
@Override |
| 258 |
public boolean remove(int pos) { |
| 259 |
int index = pos & mask; |
| 260 |
CacheObject rec = values[index]; |
| 261 |
if (rec == null) { |
| 262 |
return false; |
| 263 |
} |
| 264 |
if (rec.getPos() == pos) { |
| 265 |
values[index] = rec.cacheChained; |
| 266 |
} else { |
| 267 |
CacheObject last; |
| 268 |
do { |
| 269 |
last = rec; |
| 270 |
rec = rec.cacheChained; |
| 271 |
if (rec == null) { |
| 272 |
return false; |
| 273 |
} |
| 274 |
} while (rec.getPos() != pos); |
| 275 |
last.cacheChained = rec.cacheChained; |
| 276 |
} |
| 277 |
recordCount--; |
| 278 |
memory -= rec.getMemory(); |
| 279 |
removeFromLinkedList(rec); |
| 280 |
if (SysProperties.CHECK) { |
| 281 |
rec.cacheChained = null; |
| 282 |
CacheObject o = find(pos); |
| 283 |
if (o != null) { |
| 284 |
throw DbException.getInternalError("not removed: " + o); |
| 285 |
} |
| 286 |
} |
| 287 |
return true; |
| 288 |
} |
| 289 |
|
| 290 |
@Override |
| 291 |
public CacheObject find(int pos) { |
| 292 |
CacheObject rec = values[pos & mask]; |
| 293 |
while (rec != null && rec.getPos() != pos) { |
| 294 |
rec = rec.cacheChained; |
| 295 |
} |
| 296 |
return rec; |
| 297 |
} |
| 298 |
|
| 299 |
@Override |
| 300 |
public CacheObject get(int pos) { |
| 301 |
CacheObject rec = find(pos); |
| 302 |
if (rec != null) { |
| 303 |
if (!fifo) { |
| 304 |
removeFromLinkedList(rec); |
| 305 |
addToFront(rec); |
| 306 |
} |
| 307 |
} |
| 308 |
return rec; |
| 309 |
} |
| 310 |
|
| 311 |
// private void testConsistency() { |
| 312 |
// int s = size; |
| 313 |
// HashSet set = new HashSet(); |
| 314 |
// for(int i=0; i<values.length; i++) { |
| 315 |
// Record rec = values[i]; |
| 316 |
// if(rec == null) { |
| 317 |
// continue; |
| 318 |
// } |
| 319 |
// set.add(rec); |
| 320 |
// while(rec.chained != null) { |
| 321 |
// rec = rec.chained; |
| 322 |
// set.add(rec); |
| 323 |
// } |
| 324 |
// } |
| 325 |
// Record rec = head.next; |
| 326 |
// while(rec != head) { |
| 327 |
// set.add(rec); |
| 328 |
// rec = rec.next; |
| 329 |
// } |
| 330 |
// rec = head.previous; |
| 331 |
// while(rec != head) { |
| 332 |
// set.add(rec); |
| 333 |
// rec = rec.previous; |
| 334 |
// } |
| 335 |
// if(set.size() != size) { |
| 336 |
// System.out.println("size="+size+" but el.size="+set.size()); |
| 337 |
// } |
| 338 |
// } |
| 339 |
|
| 340 |
@Override |
| 341 |
public ArrayList<CacheObject> getAllChanged() { |
| 342 |
// if(Database.CHECK) { |
| 343 |
// testConsistency(); |
| 344 |
// } |
| 345 |
ArrayList<CacheObject> list = new ArrayList<>(); |
| 346 |
CacheObject rec = head.cacheNext; |
| 347 |
while (rec != head) { |
| 348 |
if (rec.isChanged()) { |
| 349 |
list.add(rec); |
| 350 |
} |
| 351 |
rec = rec.cacheNext; |
| 352 |
} |
| 353 |
return list; |
| 354 |
} |
| 355 |
|
| 356 |
@Override |
| 357 |
public void setMaxMemory(int maxKb) { |
| 358 |
long newSize = maxKb * 1024L / 4; |
| 359 |
maxMemory = newSize < 0 ? 0 : newSize; |
| 360 |
// can not resize, otherwise existing records are lost |
| 361 |
// resize(maxSize); |
| 362 |
removeOldIfRequired(); |
| 363 |
} |
| 364 |
|
| 365 |
@Override |
| 366 |
public int getMaxMemory() { |
| 367 |
return (int) (maxMemory * 4L / 1024); |
| 368 |
} |
| 369 |
|
| 370 |
@Override |
| 371 |
public int getMemory() { |
| 372 |
// CacheObject rec = head.cacheNext; |
| 373 |
// while (rec != head) { |
| 374 |
// System.out.println(rec.getMemory() + " " + |
| 375 |
// MemoryFootprint.getObjectSize(rec) + " " + rec); |
| 376 |
// rec = rec.cacheNext; |
| 377 |
// } |
| 378 |
return (int) (memory * 4L / 1024); |
| 379 |
} |
| 380 |
|
| 381 |
} |