KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > graph > NodeCache


1 /*
2   (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP, all rights reserved.
3   [See end of file]
4   $Id: NodeCache.java,v 1.2 2005/02/21 11:51:56 andy_seaborne Exp $
5 */

6
7 package com.hp.hpl.jena.graph;
8
9 /**
10     A NodeCache caches nodes according to their labels, to reduce store turnover
11     at the expense of some additional computation. The cache is represented as an
12     array indexed by the reduced hashcode of the labels of the nodes it contains.
13     Only the most recent node with any given reduced hash is kept. This tactic
14     means that we don't need to have any explicit cache-clearing code in normal
15     oepration.
16      
17     @author kers
18  */

19 public class NodeCache
20     {
21     /**
22         The defined size of the cache; 5000 is mostly guesswork. (It didn't *quite*
23         fill up when running the tests and had about an 85% hit-rate).
24     */

25     private static int SIZE = 5000;
26     
27     /**
28         The cache nodes, indexed by their label's reduced hash.
29     */

30     private Node [] nodes = new Node [SIZE];
31     
32     /**
33         Wipe the cache of all entries.
34     */

35     public void clear()
36         { for (int i = 0; i < SIZE; i += 1) nodes[i] = null; }
37     
38     public int size()
39         { return 0; }
40     
41     private int hits = 0;
42     private int misses = 0;
43     
44     /**
45         Answer the number of used slots in the cache
46     */

47     private int count()
48         {
49         int result = 0;
50         for (int i = 0; i < SIZE; i += 1) if (nodes[i] != null) result += 1;
51         return result;
52         }
53     
54     /**
55         Answer the node with the given <code>label</code> in the cache, or
56         <code>null</code> if there isn't one. Selects the slot in the cache by the
57         reduced hash of the label, and confirms that the Node is the right one using
58         .equals() on this label and that node's label.
59     */

60     public Node get( Object JavaDoc label )
61         {
62         Node present = nodes[(label.hashCode() & 0x7fffffff) % SIZE];
63 // if (present == null || !label.equals( present.label )) misses+= 1; else hits += 1;
64
// if ((misses + hits) %100 == 0) System.err.println( ">> hits: " + hits + ", misses: " + misses + ", occ: " + count() + "/" + SIZE );
65
return present == null || !label.equals( present.label ) ? null : present;
66         }
67     
68     /**
69          Record in the cache the designated Node, using the given label (which must
70          be .equals() to the Node's label).
71     */

72     public void put( Object JavaDoc label, Node cached )
73         {
74         nodes[(label.hashCode() & 0x7fffffff) % SIZE] = cached;
75         }
76     }
77
78 /*
79     (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP
80     All rights reserved.
81
82     Redistribution and use in source and binary forms, with or without
83     modification, are permitted provided that the following conditions
84     are met:
85
86     1. Redistributions of source code must retain the above copyright
87        notice, this list of conditions and the following disclaimer.
88
89     2. Redistributions in binary form must reproduce the above copyright
90        notice, this list of conditions and the following disclaimer in the
91        documentation and/or other materials provided with the distribution.
92
93     3. The name of the author may not be used to endorse or promote products
94        derived from this software without specific prior written permission.
95
96     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
97     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
98     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
99     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
100     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
101     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
102     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
103     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
104     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
105     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
106 */
Popular Tags