File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\fulltext\FullTextSettings.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.fulltext;
8
9 import java.sql.Connection;
10 import java.sql.PreparedStatement;
11 import java.sql.ResultSet;
12 import java.sql.SQLException;
13 import java.sql.Statement;
14 import java.util.HashMap;
15 import java.util.HashSet;
16 import org.h2.util.New;
17 import org.h2.util.SoftHashMap;
18
19 /**
20 * The global settings of a full text search.
21 */
22 class FullTextSettings {
23
24 /**
25 * The settings of open indexes.
26 */
27 private static final HashMap<String, FullTextSettings> SETTINGS = New.hashMap();
28
29 /**
30 * Whether this instance has been initialized.
31 */
32 private boolean initialized;
33
34 /**
35 * The set of words not to index (stop words).
36 */
37 private final HashSet<String> ignoreList = New.hashSet();
38
39 /**
40 * The set of words / terms.
41 */
42 private final HashMap<String, Integer> words = New.hashMap();
43
44 /**
45 * The set of indexes in this database.
46 */
47 private final HashMap<Integer, IndexInfo> indexes = New.hashMap();
48
49 /**
50 * The prepared statement cache.
51 */
52 private final SoftHashMap<Connection, SoftHashMap<String, PreparedStatement>> cache =
53 new SoftHashMap<Connection, SoftHashMap<String, PreparedStatement>>();
54
55 /**
56 * The whitespace characters.
57 */
58 private String whitespaceChars = " \t\n\r\f+\"*%&/()=?'!,.;:-_#@|^~`{}[]<>\\";
59
60 /**
61 * Create a new instance.
62 */
63 protected FullTextSettings() {
64 // don't allow construction
65 }
66
67 /**
68 * Get the ignore list.
69 *
70 * @return the ignore list
71 */
72 protected HashSet<String> getIgnoreList() {
73 return ignoreList;
74 }
75
76 /**
77 * Get the word list.
78 *
79 * @return the word list
80 */
81 protected HashMap<String, Integer> getWordList() {
82 return words;
83 }
84
85 /**
86 * Get the index information for the given index id.
87 *
88 * @param indexId the index id
89 * @return the index info
90 */
91 protected IndexInfo getIndexInfo(int indexId) {
92 return indexes.get(indexId);
93 }
94
95 /**
96 * Add an index.
97 *
98 * @param index the index
99 */
100 protected void addIndexInfo(IndexInfo index) {
101 indexes.put(index.id, index);
102 }
103
104 /**
105 * Convert a word to uppercase. This method returns null if the word is in
106 * the ignore list.
107 *
108 * @param word the word to convert and check
109 * @return the uppercase version of the word or null
110 */
111 protected String convertWord(String word) {
112 // TODO this is locale specific, document
113 word = word.toUpperCase();
114 if (ignoreList.contains(word)) {
115 return null;
116 }
117 return word;
118 }
119
120 /**
121 * Get or create the fulltext settings for this database.
122 *
123 * @param conn the connection
124 * @return the settings
125 */
126 protected static FullTextSettings getInstance(Connection conn) throws SQLException {
127 String path = getIndexPath(conn);
128 FullTextSettings setting = SETTINGS.get(path);
129 if (setting == null) {
130 setting = new FullTextSettings();
131 SETTINGS.put(path, setting);
132 }
133 return setting;
134 }
135
136 /**
137 * Get the file system path.
138 *
139 * @param conn the connection
140 * @return the file system path
141 */
142 protected static String getIndexPath(Connection conn) throws SQLException {
143 Statement stat = conn.createStatement();
144 ResultSet rs = stat.executeQuery("CALL IFNULL(DATABASE_PATH(), 'MEM:' || DATABASE())");
145 rs.next();
146 String path = rs.getString(1);
147 if ("MEM:UNNAMED".equals(path)) {
148 throw FullText.throwException("Fulltext search for private (unnamed) in-memory databases is not supported.");
149 }
150 rs.close();
151 return path;
152 }
153
154 /**
155 * Prepare a statement. The statement is cached in a soft reference cache.
156 *
157 * @param conn the connection
158 * @param sql the statement
159 * @return the prepared statement
160 */
161 protected synchronized PreparedStatement prepare(Connection conn, String sql) throws SQLException {
162 SoftHashMap<String, PreparedStatement> c = cache.get(conn);
163 if (c == null) {
164 c = new SoftHashMap<String, PreparedStatement>();
165 cache.put(conn, c);
166 }
167 PreparedStatement prep = c.get(sql);
168 if (prep != null && prep.getConnection().isClosed()) {
169 prep = null;
170 }
171 if (prep == null) {
172 prep = conn.prepareStatement(sql);
173 c.put(sql, prep);
174 }
175 return prep;
176 }
177
178 /**
179 * Remove all indexes from the settings.
180 */
181 protected void removeAllIndexes() {
182 indexes.clear();
183 }
184
185 /**
186 * Remove an index from the settings.
187 *
188 * @param index the index to remove
189 */
190 protected void removeIndexInfo(IndexInfo index) {
191 indexes.remove(index.id);
192 }
193
194 /**
195 * Set the initialized flag.
196 *
197 * @param b the new value
198 */
199 protected void setInitialized(boolean b) {
200 this.initialized = b;
201 }
202
203 /**
204 * Get the initialized flag.
205 *
206 * @return whether this instance is initialized
207 */
208 protected boolean isInitialized() {
209 return initialized;
210 }
211
212 /**
213 * Close all fulltext settings, freeing up memory.
214 */
215 protected static void closeAll() {
216 SETTINGS.clear();
217 }
218
219 protected void setWhitespaceChars(String whitespaceChars) {
220 this.whitespaceChars = whitespaceChars;
221 }
222
223 protected String getWhitespaceChars() {
224 return whitespaceChars;
225 }
226
227 }
228