File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\util\CacheTQ.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.util;
8
9 import java.util.ArrayList;
10
11 /**
12 * An alternative cache implementation. This implementation uses two caches: a
13 * LRU cache and a FIFO cache. Entries are first kept in the FIFO cache, and if
14 * referenced again then marked in a hash set. If referenced again, they are
15 * moved to the LRU cache. Stream pages are never added to the LRU cache. It is
16 * supposed to be more or less scan resistant, and it doesn't cache large rows
17 * in the LRU cache.
18 */
19 public class CacheTQ implements Cache {
20
21 static final String TYPE_NAME = "TQ";
22
23 private final Cache lru;
24 private final Cache fifo;
25 private final SmallLRUCache<Integer, Object> recentlyUsed = SmallLRUCache.newInstance(1024);
26 private int lastUsed = -1;
27
28 private int maxMemory;
29
30 CacheTQ(CacheWriter writer, int maxMemoryKb) {
31 this.maxMemory = maxMemoryKb;
32 lru = new CacheLRU(writer, (int) (maxMemoryKb * 0.8), false);
33 fifo = new CacheLRU(writer, (int) (maxMemoryKb * 0.2), true);
34 setMaxMemory(4 * maxMemoryKb);
35 }
36
37 public void clear() {
38 lru.clear();
39 fifo.clear();
40 recentlyUsed.clear();
41 lastUsed = -1;
42 }
43
44 public CacheObject find(int pos) {
45 CacheObject r = lru.find(pos);
46 if (r == null) {
47 r = fifo.find(pos);
48 }
49 return r;
50 }
51
52 public CacheObject get(int pos) {
53 CacheObject r = lru.find(pos);
54 if (r != null) {
55 return r;
56 }
57 r = fifo.find(pos);
58 if (r != null && !r.isStream()) {
59 if (recentlyUsed.get(pos) != null) {
60 if (lastUsed != pos) {
61 fifo.remove(pos);
62 lru.put(r);
63 }
64 } else {
65 recentlyUsed.put(pos, this);
66 }
67 lastUsed = pos;
68 }
69 return r;
70 }
71
72 public ArrayList<CacheObject> getAllChanged() {
73 ArrayList<CacheObject> changed = New.arrayList();
74 changed.addAll(lru.getAllChanged());
75 changed.addAll(fifo.getAllChanged());
76 return changed;
77 }
78
79 public int getMaxMemory() {
80 return maxMemory;
81 }
82
83 public int getMemory() {
84 return lru.getMemory() + fifo.getMemory();
85 }
86
87 public void put(CacheObject r) {
88 if (r.isStream()) {
89 fifo.put(r);
90 } else if (recentlyUsed.get(r.getPos()) != null) {
91 lru.put(r);
92 } else {
93 fifo.put(r);
94 lastUsed = r.getPos();
95 }
96 }
97
98 public boolean remove(int pos) {
99 boolean result = lru.remove(pos);
100 if (!result) {
101 result = fifo.remove(pos);
102 }
103 recentlyUsed.remove(pos);
104 return result;
105 }
106
107 public void setMaxMemory(int maxMemoryKb) {
108 this.maxMemory = maxMemoryKb;
109 lru.setMaxMemory((int) (maxMemoryKb * 0.8));
110 fifo.setMaxMemory((int) (maxMemoryKb * 0.2));
111 recentlyUsed.setMaxSize(4 * maxMemoryKb);
112 }
113
114 public CacheObject update(int pos, CacheObject record) {
115 if (lru.find(pos) != null) {
116 return lru.update(pos, record);
117 }
118 return fifo.update(pos, record);
119 }
120
121 }
122