KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > StatementCache


1 /**
2  * com.mckoi.database.StatementCache 15 Sep 2001
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database;
26
27 import com.mckoi.util.Cache;
28 import com.mckoi.database.global.ObjectTranslator;
29 import com.mckoi.database.global.ByteLongObject;
30 import com.mckoi.debug.*;
31
32 /**
33  * A cache that maintains a serialized set of StatementTree objects that can
34  * be deserialized on demand. The purpose of this cache is to improve the
35  * performance of queries that are run repeatedly (for example, multiple
36  * INSERT statements).
37  * <p>
38  * SYNCHRONIZATION: This object is safe to use over multiple threads.
39  *
40  * @author Tobias Downer
41  */

42
43 public final class StatementCache {
44
45   /**
46    * The DatabaseSystem of this cache.
47    */

48   private DatabaseSystem system;
49
50   /**
51    * The internal cache representation.
52    */

53   private Cache cache;
54
55   /**
56    * Constructs the cache.
57    */

58   public StatementCache(DatabaseSystem system,
59                         int hash_size, int max_size, int clean_percentage) {
60     this.system = system;
61     cache = new Cache(hash_size, max_size, clean_percentage);
62   }
63
64   /**
65    * Returns a DebugLogger object we can use to log debug messages.
66    */

67   public final DebugLogger Debug() {
68     return system.Debug();
69   }
70
71   /**
72    * Puts a new query string/StatementTree into the cache.
73    */

74   public synchronized void put(String JavaDoc query_string,
75                                StatementTree statement_tree) {
76     query_string = query_string.trim();
77     // Is this query string already in the cache?
78
if (cache.get(query_string) == null) {
79       try {
80         Object JavaDoc cloned_tree = statement_tree.clone();
81         cache.put(query_string, cloned_tree);
82       }
83       catch (CloneNotSupportedException JavaDoc e) {
84         Debug().writeException(e);
85         throw new Error JavaDoc("Unable to clone statement tree: " + e.getMessage());
86       }
87     }
88   }
89
90   /**
91    * Gets a StatementTree for the query string if it is stored in the cache.
92    * If it isn't stored in the cache returns null.
93    */

94   public synchronized StatementTree get(String JavaDoc query_string) {
95     query_string = query_string.trim();
96     Object JavaDoc ob = cache.get(query_string);
97     if (ob != null) {
98       try {
99 // System.out.println("CACHE HIT!");
100
// We found a cached version of this query so deserialize and return
101
// it.
102
StatementTree cloned_tree = (StatementTree) ob;
103         return (StatementTree) cloned_tree.clone();
104       }
105       catch (CloneNotSupportedException JavaDoc e) {
106         Debug().writeException(e);
107         throw new Error JavaDoc("Unable to clone statement tree: " + e.getMessage());
108       }
109     }
110     // Not found so return null
111
return null;
112   }
113
114 }
115
Popular Tags