01: /*
02: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
03: * (http://h2database.com/html/license.html).
04: * Initial Developer: H2 Group
05: */
06: package org.h2.util;
07:
08: import java.util.Comparator;
09:
10: import org.h2.constant.SysProperties;
11: import org.h2.message.Message;
12: import org.h2.store.DiskFile;
13:
14: /**
15: * The base object for all cached objects.
16: */
17: public abstract class CacheObject {
18:
19: /**
20: * The number of blocks occupied by this object.
21: */
22: protected int blockCount;
23: public CacheObject previous, next, chained;
24: public int cacheQueue;
25: private int pos;
26: private boolean changed;
27:
28: /**
29: * Check if the object can be removed from the cache.
30: * For example pinned objects can not be removed.
31: *
32: * @return true if it can be removed
33: */
34: public abstract boolean canRemove();
35:
36: public static void sort(ObjectArray recordList) {
37: recordList.sort(new Comparator() {
38: public int compare(Object a, Object b) {
39: int pa = ((CacheObject) a).getPos();
40: int pb = ((CacheObject) b).getPos();
41: return pa == pb ? 0 : (pa < pb ? -1 : 1);
42: }
43: });
44: }
45:
46: public void setBlockCount(int size) {
47: this .blockCount = size;
48: }
49:
50: public int getBlockCount() {
51: return blockCount;
52: }
53:
54: public void setPos(int pos) {
55: if (SysProperties.CHECK
56: && (previous != null || next != null || chained != null)) {
57: throw Message.getInternalError("setPos too late");
58: }
59: this .pos = pos;
60: }
61:
62: public int getPos() {
63: return pos;
64: }
65:
66: public boolean isChanged() {
67: return changed;
68: }
69:
70: public void setChanged(boolean b) {
71: changed = b;
72: }
73:
74: public boolean isPinned() {
75: return false;
76: }
77:
78: /**
79: * Get the estimated memory size.
80: *
81: * @return number of double words (4 bytes)
82: */
83: public int getMemorySize() {
84: return blockCount * (DiskFile.BLOCK_SIZE / 4);
85: }
86:
87: }
|