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      processQueue(); // clear out some crap.
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      // noop
96    }
97  
98    @Override
99    public CacheStats getStats() {
100     // TODO: implement this if we ever actually use this block cache
101     return null;
102   }
103 
104   @Override
105   public long getFreeSize() {
106     // TODO: implement this if we ever actually use this block cache
107     return 0;
108   }
109 
110   @Override
111   public long getCurrentSize() {
112     // TODO: implement this if we ever actually use this block cache
113     return 0;
114   }
115 
116   @Override
117   public long getEvictedCount() {
118     // TODO: implement this if we ever actually use this block cache
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     // TODO: implement this if we ever actually use this block cache
135     return 0;
136   }
137 
138 }
139