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