View Javadoc

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