KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > core > storage > FixedSizeDelayCache


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Core License version 1 published by ozone-db.org.
3
//
4
// Copyright (C) 2003-@year@, Leo Mekenkamp. All rights reserved.
5
//
6
// $Id: FixedSizeDelayCache.java,v 1.1.2.2 2004/04/10 10:06:51 per_nyfelt Exp $
7

8 package org.ozoneDB.core.storage;
9
10 import java.util.Collection JavaDoc;
11 import java.util.Comparator JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.LinkedHashMap JavaDoc;
14 import java.util.LinkedHashSet JavaDoc;
15 import java.util.LinkedList JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.Properties JavaDoc;
19 import java.util.Set JavaDoc;
20 import java.util.Map.Entry;
21 import java.util.SortedSet JavaDoc;
22 import java.util.TreeMap JavaDoc;
23 import java.util.TreeSet JavaDoc;
24 import java.util.logging.Level JavaDoc;
25 import java.util.logging.Logger JavaDoc;
26 import org.ozoneDB.OzoneInternalException;
27 import org.ozoneDB.core.ConfigurationException;
28
29
30 /**
31  * <p>A cache with a fixed maximum number of objects in it. This cache grows
32  * until it has reached its maximum, which can be set by maxCapacity. Once that
33  * maximum has been reached, the least recently used object is thrown away when
34  * a new object is put in the cache. This cache has, next to that maximum number
35  * of elements, also a maximum time an element will remain in this cache. If
36  * that time has expired the element will be removed from this cache also.
37  * Reinserting an element (using the same key) causes the time for that element
38  * to be reset.</p>
39  *
40  * <p>Note: every instance creates its own thread for asynchronous removal of
41  * items past their 'best before' time. The trimmed() method on the TrimHandler
42  * is often but not always called asynchronously from that thread.</p>
43  *
44  * @author <a HREF="mailto:leoATmekenkampD0Tcom">Leo Mekenkamp (mind the anti sp@m)</a>
45  * @version $Id: FixedSizeDelayCache.java,v 1.1.2.2 2004/04/10 10:06:51 per_nyfelt Exp $
46  */

47 public class FixedSizeDelayCache extends DelayCache {
48     
49     private static final Logger JavaDoc log = Logger.getLogger(FixedSizeDelayCache.class.getName());
50
51     public static final PropertyInfo MAXCAPACITY = new PropertyInfo(
52         ".maxCapacity",
53         "int",
54         "1",
55         "number of objects that this cache should hold as a maximum",
56         new String JavaDoc[] {"100", "100000"}
57     );
58
59     private int maxCapacity;
60     
61     public FixedSizeDelayCache(Properties JavaDoc properties, String JavaDoc prefix) {
62         super(properties, prefix);
63         try {
64             setMaxCapacity(Integer.parseInt(properties.getProperty(prefix + MAXCAPACITY.getKey(), MAXCAPACITY.getDefaultValue())));
65             log.config(getPrefix() + " using a max capacity of " + getMaxCapacity());
66         } catch (NumberFormatException JavaDoc e) {
67             throw new ConfigurationException(e);
68         }
69     }
70
71     public final void setMaxCapacity(int maxCapacity) {
72         synchronized(getSynchronizer()) {
73             this.maxCapacity = maxCapacity;
74             ourTrim();
75         }
76     }
77     
78     public final int getMaxCapacity() {
79         // maxCapacity is an int so synchronization is not needed
80
return maxCapacity;
81     }
82     
83     public Collection JavaDoc getPropertyInfos() {
84         Collection JavaDoc result = super.getPropertyInfos();
85         result.add(MAXCAPACITY);
86         return result;
87     }
88     
89     public void put(Object JavaDoc key, Object JavaDoc value) {
90         synchronized(getSynchronizer()) {
91             super.put(key, value);
92             ourTrim();
93         }
94     }
95
96     private void ourTrim() {
97         while (size() > getMaxCapacity()) {
98             TimedEntry last = (TimedEntry) getEntries().last();
99             if (getTrimHandler() != null) {
100                 getTrimHandler().trimming(last.key, last.value);
101             }
102             getEntries().remove(last);
103             getKeysToEntries().remove(last.key);
104         }
105     }
106     
107 }
108
Popular Tags