1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.io.hfile;
20
21 import java.lang.ref.ReferenceQueue;
22 import java.lang.ref.SoftReference;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26
27 import org.apache.hadoop.classification.InterfaceAudience;
28 import org.apache.hadoop.conf.Configuration;
29
30
31
32
33
34 @InterfaceAudience.Private
35 public class SimpleBlockCache implements BlockCache {
36 private static class Ref extends SoftReference<Cacheable> {
37 public BlockCacheKey blockId;
38 public Ref(BlockCacheKey blockId, Cacheable block, ReferenceQueue q) {
39 super(block, q);
40 this.blockId = blockId;
41 }
42 }
43 private Map<BlockCacheKey,Ref> cache =
44 new HashMap<BlockCacheKey,Ref>();
45
46 private ReferenceQueue q = new ReferenceQueue();
47 public int dumps = 0;
48
49
50
51
52 public SimpleBlockCache() {
53 super();
54 }
55
56 void processQueue() {
57 Ref r;
58 while ( (r = (Ref)q.poll()) != null) {
59 cache.remove(r.blockId);
60 dumps++;
61 }
62 }
63
64
65
66
67 public synchronized long size() {
68 processQueue();
69 return cache.size();
70 }
71
72 public synchronized Cacheable getBlock(BlockCacheKey cacheKey, boolean caching, boolean repeat,
73 boolean updateCacheMetrics) {
74 processQueue();
75 Ref ref = cache.get(cacheKey);
76 if (ref == null)
77 return null;
78 return ref.get();
79 }
80
81 public synchronized void cacheBlock(BlockCacheKey cacheKey, Cacheable block) {
82 cache.put(cacheKey, new Ref(cacheKey, block, q));
83 }
84
85 public synchronized void cacheBlock(BlockCacheKey cacheKey, Cacheable block,
86 boolean inMemory) {
87 cache.put(cacheKey, new Ref(cacheKey, block, q));
88 }
89
90 @Override
91 public boolean evictBlock(BlockCacheKey cacheKey) {
92 return cache.remove(cacheKey) != null;
93 }
94
95 public void shutdown() {
96
97 }
98
99 @Override
100 public CacheStats getStats() {
101
102 return null;
103 }
104
105 @Override
106 public long getFreeSize() {
107
108 return 0;
109 }
110
111 @Override
112 public long getCurrentSize() {
113
114 return 0;
115 }
116
117 @Override
118 public long getEvictedCount() {
119
120 return 0;
121 }
122
123 @Override
124 public int evictBlocksByHfileName(String string) {
125 throw new UnsupportedOperationException();
126 }
127
128 @Override
129 public List<BlockCacheColumnFamilySummary> getBlockCacheColumnFamilySummaries(Configuration conf) {
130 throw new UnsupportedOperationException();
131 }
132
133 @Override
134 public long getBlockCount() {
135
136 return 0;
137 }
138
139 }
140