001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (http://h2database.com/html/license.html).
004: * Initial Developer: H2 Group
005: */
006: package org.h2.fulltext;
007:
008: import java.sql.Connection;
009: import java.sql.PreparedStatement;
010: import java.sql.ResultSet;
011: import java.sql.SQLException;
012: import java.sql.Statement;
013: import java.util.HashMap;
014: import java.util.HashSet;
015:
016: import org.h2.util.ObjectUtils;
017:
018: /**
019: * The global settings of a full text search.
020: */
021: public class FullTextSettings {
022:
023: private static HashMap settings = new HashMap();
024:
025: private HashSet ignoreList = new HashSet();
026: private HashMap words = new HashMap();
027: private HashMap indexes = new HashMap();
028: private PreparedStatement prepSelectMapByWordId;
029: private PreparedStatement prepSelectRowById;
030:
031: private FullTextSettings() {
032: }
033:
034: HashSet getIgnoreList() {
035: return ignoreList;
036: }
037:
038: public HashMap getWordList() {
039: return words;
040: }
041:
042: IndexInfo getIndexInfo(long indexId) {
043: return (IndexInfo) indexes.get(ObjectUtils.getLong(indexId));
044: }
045:
046: void addIndexInfo(IndexInfo index) {
047: indexes.put(ObjectUtils.getLong(index.id), index);
048: }
049:
050: public String convertWord(String word) {
051: // TODO this is locale specific, document
052: word = word.toUpperCase();
053: if (ignoreList.contains(word)) {
054: return null;
055: }
056: return word;
057: }
058:
059: static FullTextSettings getInstance(Connection conn)
060: throws SQLException {
061: String path = getIndexPath(conn);
062: FullTextSettings setting = (FullTextSettings) settings
063: .get(path);
064: if (setting == null) {
065: setting = new FullTextSettings();
066: settings.put(path, setting);
067: }
068: return setting;
069: }
070:
071: private static String getIndexPath(Connection conn)
072: throws SQLException {
073: Statement stat = conn.createStatement();
074: ResultSet rs = stat
075: .executeQuery("CALL IFNULL(DATABASE_PATH(), 'MEM:' || DATABASE())");
076: rs.next();
077: String path = rs.getString(1);
078: if ("MEM:UNNAMED".equals(path)) {
079: throw new SQLException("FULLTEXT",
080: "Fulltext search for private (unnamed) in-memory databases is not supported.");
081: }
082: rs.close();
083: return path;
084: }
085:
086: public PreparedStatement getPrepSelectMapByWordId() {
087: return prepSelectMapByWordId;
088: }
089:
090: public void setPrepSelectMapByWordId(
091: PreparedStatement prepSelectMapByWordId) {
092: this .prepSelectMapByWordId = prepSelectMapByWordId;
093: }
094:
095: public PreparedStatement getPrepSelectRowById() {
096: return prepSelectRowById;
097: }
098:
099: public void setPrepSelectRowById(PreparedStatement prepSelectRowById) {
100: this.prepSelectRowById = prepSelectRowById;
101: }
102:
103: }
|