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