Debian Codegraph @

src/main/org/h2/fulltext/FullTextSettings.java

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