KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP, all rights reserved.
3   [See end of file]
4   $Id: TripleCache.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     TripleCache caches triples according to their SPO members, to reduce store
11     turnover at the expense of some added computation. The cache is implemented
12     as an array indexed by the (reduced) hashCode of the triples it stores.
13     Each slot is treated independantly and only the most recent stored triple is
14     remembered - there is no weighting, LRU, or anything like that.
15
16     @author kers
17 */

18 public class TripleCache
19     {
20     /**
21          The size of the cache array. 1000 gets 80% hits when running the Jena
22          test suite and all the slots get used. 10000 gets about 83% and *almost* all
23          the slots get used. Absent real performance indicators, 1000 will do for
24          now.
25     */

26     public static int SIZE = 1000;
27     
28     /**
29          The array holding the cached triples.
30     */

31     private Triple [] triples = new Triple[SIZE];
32     
33     /**
34          Cache the triple <code>t</code> by storing it in the slot with the its reduced
35          hash. Any triple already in that slot vanishes. Answer that triple.
36     */

37     public Triple put( Triple t )
38         { triples[(t.hashCode() & 0x7fffffff) % SIZE] = t; return t; }
39     
40     private static int count = 0;
41     private int id = ++count;
42     private int hits = 0;
43     private int misses = 0;
44         
45     /**
46          Answer the number of occupied slots in the cache array.
47     */

48     private int count()
49         {
50         int result = 0;
51         for (int i = 0; i < SIZE; i += 1) if (triples[i] != null) result += 1;
52         return result;
53         }
54     
55     /**
56          Answer any triple in the cache with subject <code>s</code>, predicate
57          <code>p</code>, and object <code>o</code>, or <code>null</code> if
58          no such triple exists.
59      <p>
60          The implementation looks in the slot with the same reduced hashCode as
61          the SPO combination would have. If the triple there has the same SPO,
62          it is returned; otherwise <code>null</code> is returned.
63     */

64     public Triple get( Node s, Node p, Node o )
65         {
66         Triple already = triples[(Triple.hashCode( s, p, o ) & 0x7fffffff) % SIZE];
67 // if (already == null || !already.sameAs( s, p, o )) misses += 1; else hits += 1;
68
// if ((hits + misses) % 1000 == 0) System.err.println( ">> cache [" + id + "] hits: " + hits + ", misses: " + misses + ", occ: " + count() + "/" + SIZE );
69
return already == null || !already.sameAs( s, p, o ) ? null : already;
70         }
71     }
72 /*
73     (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP
74     All rights reserved.
75
76     Redistribution and use in source and binary forms, with or without
77     modification, are permitted provided that the following conditions
78     are met:
79
80     1. Redistributions of source code must retain the above copyright
81        notice, this list of conditions and the following disclaimer.
82
83     2. Redistributions in binary form must reproduce the above copyright
84        notice, this list of conditions and the following disclaimer in the
85        documentation and/or other materials provided with the distribution.
86
87     3. The name of the author may not be used to endorse or promote products
88        derived from this software without specific prior written permission.
89
90     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
91     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
92     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
93     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
94     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
95     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
96     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
97     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
98     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
99     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
100 */
Popular Tags