Java tutorial
/* * Copyright 2004-2009 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.lucene.store.jdbc.index; import java.io.IOException; import java.sql.Blob; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import org.apache.lucene.store.IndexInput; import org.apache.lucene.store.jdbc.JdbcDirectory; import org.apache.lucene.store.jdbc.JdbcFileEntrySettings; import org.apache.lucene.store.jdbc.JdbcStoreException; import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.PreparedStatementCallback; /** * An <code>IndexInput</code> implementation that will read all the relevant data from the * database when created, and will cache it untill it is closed. * <p/> * Used for small file entries in the database like the segments file. * * @author kimchy */ public class FetchOnOpenJdbcIndexInput extends IndexInput implements JdbcIndexConfigurable { //There is no synchronizaiton since Lucene RAMDirecoty performs no synchronizations. // Need to get to the bottom of it. private long length; private long position = 0; private byte[] data; /** * resourceDescription should be a non-null, opaque string * describing this resource; it's returned from * {@link #toString}. * * @param resourceDescription */ protected FetchOnOpenJdbcIndexInput(String resourceDescription) { super(resourceDescription); } public void configure(final String name, final JdbcDirectory jdbcDirectory, JdbcFileEntrySettings settings) throws IOException { jdbcDirectory.getJdbcTemplate().execute(jdbcDirectory.getTable().sqlSelectSizeValueByName(), new PreparedStatementCallback<Object>() { @Override public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException { ps.setFetchSize(1); ps.setString(1, name); try (ResultSet rs = ps.executeQuery()) { if (!rs.next()) { throw new JdbcStoreException( "No entry for [" + name + "] table " + jdbcDirectory.getTable()); } length = rs.getInt(3); Blob blob = rs.getBlob(2); data = blob.getBytes(1, (int) length); if (data.length != length) { throw new IOException("read past EOF"); } } catch (JdbcStoreException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } }); } public byte readByte() throws IOException { return data[((int) position++)]; } public void readBytes(byte[] b, int offset, int len) throws IOException { System.arraycopy(data, (int) position, b, offset, len); position += len; } public void close() throws IOException { } public long getFilePointer() { return position; } public void seek(long pos) throws IOException { position = (int) pos; } public long length() { return this.length; } @Override public IndexInput slice(String sliceDescription, long offset, long length) throws IOException { FetchOnOpenJdbcIndexInput index = new FetchOnOpenJdbcIndexInput(sliceDescription); index.data = this.data; index.position = offset; index.length = length; return null; } }