| 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 org.h2.message.DbException; |
| 9 |
|
| 10 |
/** |
| 11 |
* The base object for all cached objects. |
| 12 |
*/ |
| 13 |
public abstract class CacheObject implements Comparable<CacheObject> { |
| 14 |
|
| 15 |
/** |
| 16 |
* The previous element in the LRU linked list. If the previous element is |
| 17 |
* the head, then this element is the most recently used object. |
| 18 |
*/ |
| 19 |
public CacheObject cachePrevious; |
| 20 |
|
| 21 |
/** |
| 22 |
* The next element in the LRU linked list. If the next element is the head, |
| 23 |
* then this element is the least recently used object. |
| 24 |
*/ |
| 25 |
public CacheObject cacheNext; |
| 26 |
|
| 27 |
/** |
| 28 |
* The next element in the hash chain. |
| 29 |
*/ |
| 30 |
public CacheObject cacheChained; |
| 31 |
|
| 32 |
private int pos; |
| 33 |
private boolean changed; |
| 34 |
|
| 35 |
/** |
| 36 |
* Check if the object can be removed from the cache. |
| 37 |
* For example pinned objects can not be removed. |
| 38 |
* |
| 39 |
* @return true if it can be removed |
| 40 |
*/ |
| 41 |
public abstract boolean canRemove(); |
| 42 |
|
| 43 |
/** |
| 44 |
* Get the estimated used memory. |
| 45 |
* |
| 46 |
* @return number of words (one word is 4 bytes) |
| 47 |
*/ |
| 48 |
public abstract int getMemory(); |
| 49 |
|
| 50 |
public void setPos(int pos) { |
| 51 |
if (cachePrevious != null || cacheNext != null || cacheChained != null) { |
| 52 |
throw DbException.getInternalError("setPos too late"); |
| 53 |
} |
| 54 |
this.pos = pos; |
| 55 |
} |
| 56 |
|
| 57 |
public int getPos() { |
| 58 |
return pos; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Check if this cache object has been changed and thus needs to be written |
| 63 |
* back to the storage. |
| 64 |
* |
| 65 |
* @return if it has been changed |
| 66 |
*/ |
| 67 |
public boolean isChanged() { |
| 68 |
return changed; |
| 69 |
} |
| 70 |
|
| 71 |
public void setChanged(boolean b) { |
| 72 |
changed = b; |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
public int compareTo(CacheObject other) { |
| 77 |
return Integer.compare(getPos(), other.getPos()); |
| 78 |
} |
| 79 |
|
| 80 |
public boolean isStream() { |
| 81 |
return false; |
| 82 |
} |
| 83 |
|
| 84 |
} |