001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.lucene.store.jdbc.handler;
018:
019: import java.io.IOException;
020: import java.sql.PreparedStatement;
021: import java.sql.ResultSet;
022: import java.sql.Timestamp;
023:
024: import org.apache.lucene.store.IndexInput;
025: import org.apache.lucene.store.IndexOutput;
026: import org.apache.lucene.store.jdbc.JdbcDirectory;
027: import org.apache.lucene.store.jdbc.JdbcFileEntrySettings;
028: import org.apache.lucene.store.jdbc.JdbcStoreException;
029: import org.apache.lucene.store.jdbc.index.JdbcIndexConfigurable;
030: import org.apache.lucene.store.jdbc.support.JdbcTable;
031: import org.apache.lucene.store.jdbc.support.JdbcTemplate;
032:
033: /**
034: * A base file entry handler that supports most of the file entry base operations.
035: * <p/>
036: * Supports the creation of configurable <code>IndexInput</code> and <code>IndexOutput</code>,
037: * base on the {@link JdbcFileEntrySettings#INDEX_INPUT_TYPE_SETTING} and
038: * {@link JdbcFileEntrySettings#INDEX_OUTPUT_TYPE_SETTING}.
039: * <p/>
040: * Does not implement the deletion of files.
041: *
042: * @author kimchy
043: */
044: public abstract class AbstractFileEntryHandler implements
045: FileEntryHandler {
046:
047: protected JdbcDirectory jdbcDirectory;
048:
049: protected JdbcTable table;
050:
051: protected JdbcTemplate jdbcTemplate;
052:
053: public void configure(JdbcDirectory jdbcDirectory) {
054: this .jdbcDirectory = jdbcDirectory;
055: this .jdbcTemplate = jdbcDirectory.getJdbcTemplate();
056: this .table = jdbcDirectory.getTable();
057: }
058:
059: public boolean fileExists(final String name) throws IOException {
060: return ((Boolean) jdbcTemplate.executeSelect(table
061: .sqlSelectNameExists(),
062: new JdbcTemplate.ExecuteSelectCallback() {
063: public void fillPrepareStatement(
064: PreparedStatement ps) throws Exception {
065: ps.setFetchSize(1);
066: ps.setString(1, name);
067: }
068:
069: public Object execute(ResultSet rs)
070: throws Exception {
071: if (!rs.next()) {
072: return Boolean.FALSE;
073: }
074: return (rs.getBoolean(1)) ? Boolean.FALSE
075: : Boolean.TRUE;
076: }
077: })).booleanValue();
078: }
079:
080: public long fileModified(final String name) throws IOException {
081: return ((Long) jdbcTemplate.executeSelect(table
082: .sqlSelecltLastModifiedByName(),
083: new JdbcTemplate.ExecuteSelectCallback() {
084: public void fillPrepareStatement(
085: PreparedStatement ps) throws Exception {
086: ps.setFetchSize(1);
087: ps.setString(1, name);
088: }
089:
090: public Object execute(ResultSet rs)
091: throws Exception {
092: if (rs.next()) {
093: Timestamp ts = rs.getTimestamp(1);
094: return new Long(ts.getTime());
095: }
096: return new Long(0L);
097: }
098: })).longValue();
099: }
100:
101: public void touchFile(final String name) throws IOException {
102: jdbcTemplate.executeUpdate(table.sqlUpdateLastModifiedByName(),
103: new JdbcTemplate.PrepateStatementAwareCallback() {
104: public void fillPrepareStatement(
105: PreparedStatement ps) throws Exception {
106: ps.setFetchSize(1);
107: ps.setString(1, name);
108: }
109: });
110: }
111:
112: public void renameFile(final String from, final String to)
113: throws IOException {
114: // TODO find a way if it can be done in the same sql query
115: deleteFile(to);
116: jdbcTemplate.executeUpdate(table.sqlUpdateNameByName(),
117: new JdbcTemplate.PrepateStatementAwareCallback() {
118: public void fillPrepareStatement(
119: PreparedStatement ps) throws Exception {
120: ps.setFetchSize(1);
121: ps.setString(1, to);
122: ps.setString(2, from);
123: }
124: });
125: }
126:
127: public long fileLength(final String name) throws IOException {
128: return ((Long) jdbcTemplate.executeSelect(table
129: .sqlSelectSizeByName(),
130: new JdbcTemplate.ExecuteSelectCallback() {
131: public void fillPrepareStatement(
132: PreparedStatement ps) throws Exception {
133: ps.setFetchSize(1);
134: ps.setString(1, name);
135: }
136:
137: public Object execute(ResultSet rs)
138: throws Exception {
139: if (rs.next()) {
140: return new Long(rs.getLong(1));
141: }
142: return new Long(0L);
143: }
144: })).longValue();
145: }
146:
147: public IndexInput openInput(String name) throws IOException {
148: IndexInput indexInput;
149: JdbcFileEntrySettings settings = jdbcDirectory.getSettings()
150: .getFileEntrySettings(name);
151: try {
152: Class inputClass = settings.getSettingAsClass(
153: JdbcFileEntrySettings.INDEX_INPUT_TYPE_SETTING,
154: null);
155: indexInput = (IndexInput) inputClass.newInstance();
156: } catch (Exception e) {
157: throw new JdbcStoreException(
158: "Failed to create indexInput instance ["
159: + settings
160: .getSetting(JdbcFileEntrySettings.INDEX_INPUT_TYPE_SETTING)
161: + "]", e);
162: }
163: ((JdbcIndexConfigurable) indexInput).configure(name,
164: jdbcDirectory, settings);
165: return indexInput;
166: }
167:
168: public IndexOutput createOutput(String name) throws IOException {
169: IndexOutput indexOutput;
170: JdbcFileEntrySettings settings = jdbcDirectory.getSettings()
171: .getFileEntrySettings(name);
172: try {
173: Class inputClass = settings.getSettingAsClass(
174: JdbcFileEntrySettings.INDEX_OUTPUT_TYPE_SETTING,
175: null);
176: indexOutput = (IndexOutput) inputClass.newInstance();
177: } catch (Exception e) {
178: throw new JdbcStoreException(
179: "Failed to create indexOutput instance ["
180: + settings
181: .getSetting(JdbcFileEntrySettings.INDEX_OUTPUT_TYPE_SETTING)
182: + "]", e);
183: }
184: ((JdbcIndexConfigurable) indexOutput).configure(name,
185: jdbcDirectory, settings);
186: return indexOutput;
187: }
188:
189: public void close() throws IOException {
190: // do nothing
191: }
192: }
|