View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
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   * Simple one RFile soft reference cache.
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     * Constructor
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     * @return the size
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(); // clear out some crap.
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      // noop
97    }
98  
99    @Override
100   public CacheStats getStats() {
101     // TODO: implement this if we ever actually use this block cache
102     return null;
103   }
104 
105   @Override
106   public long getFreeSize() {
107     // TODO: implement this if we ever actually use this block cache
108     return 0;
109   }
110 
111   @Override
112   public long getCurrentSize() {
113     // TODO: implement this if we ever actually use this block cache
114     return 0;
115   }
116 
117   @Override
118   public long getEvictedCount() {
119     // TODO: implement this if we ever actually use this block cache
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     // TODO: implement this if we ever actually use this block cache
136     return 0;
137   }
138 
139 }
140