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 processQueue();
74 Ref ref = cache.get(cacheKey);
75 if (ref == null)
76 return null;
77 return ref.get();
78 }
79
80 public synchronized void cacheBlock(BlockCacheKey cacheKey, Cacheable block) {
81 cache.put(cacheKey, new Ref(cacheKey, block, q));
82 }
83
84 public synchronized void cacheBlock(BlockCacheKey cacheKey, Cacheable block,
85 boolean inMemory) {
86 cache.put(cacheKey, new Ref(cacheKey, block, q));
87 }
88
89 @Override
90 public boolean evictBlock(BlockCacheKey cacheKey) {
91 return cache.remove(cacheKey) != null;
92 }
93
94 public void shutdown() {
95
96 }
97
98 @Override
99 public CacheStats getStats() {
100
101 return null;
102 }
103
104 @Override
105 public long getFreeSize() {
106
107 return 0;
108 }
109
110 @Override
111 public long getCurrentSize() {
112
113 return 0;
114 }
115
116 @Override
117 public long getEvictedCount() {
118
119 return 0;
120 }
121
122 @Override
123 public int evictBlocksByHfileName(String string) {
124 throw new UnsupportedOperationException();
125 }
126
127 @Override
128 public List<BlockCacheColumnFamilySummary> getBlockCacheColumnFamilySummaries(Configuration conf) {
129 throw new UnsupportedOperationException();
130 }
131
132 @Override
133 public long getBlockCount() {
134
135 return 0;
136 }
137
138 }
139