01: /*
02: * Copyright 2004-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.apache.lucene.store.jdbc.index;
18:
19: import java.io.BufferedInputStream;
20: import java.io.File;
21: import java.io.FileInputStream;
22: import java.io.IOException;
23: import java.io.InputStream;
24: import java.io.RandomAccessFile;
25:
26: import org.apache.lucene.store.jdbc.JdbcDirectory;
27: import org.apache.lucene.store.jdbc.JdbcFileEntrySettings;
28:
29: /**
30: * An <code>IndexOutput</code> implemenation that writes all the data to a temporary file, and when closed, flushes
31: * the file to the database.
32: * <p/>
33: * Usefull for large files that are known in advance to be larger then the acceptable threshold configured in
34: * {@link RAMAndFileJdbcIndexOutput}.
35: *
36: * @author kimchy
37: */
38: public class FileJdbcIndexOutput extends AbstractJdbcIndexOutput {
39:
40: private RandomAccessFile file = null;
41:
42: private File tempFile;
43:
44: public void configure(String name, JdbcDirectory jdbcDirectory,
45: JdbcFileEntrySettings settings) throws IOException {
46: super .configure(name, jdbcDirectory, settings);
47: tempFile = File
48: .createTempFile(
49: jdbcDirectory.getTable().getName() + "_" + name
50: + "_" + System.currentTimeMillis(),
51: ".ljt");
52: this .file = new RandomAccessFile(tempFile, "rw");
53: this .jdbcDirectory = jdbcDirectory;
54: this .name = name;
55: }
56:
57: protected void flushBuffer(byte[] b, int offset, int len)
58: throws IOException {
59: file.write(b, offset, len);
60: }
61:
62: /**
63: * Random-access methods
64: */
65: public void seek(long pos) throws IOException {
66: super .seek(pos);
67: file.seek(pos);
68: }
69:
70: public long length() throws IOException {
71: return file.length();
72: }
73:
74: protected InputStream openInputStream() throws IOException {
75: return new BufferedInputStream(
76: new FileInputStream(file.getFD()));
77: }
78:
79: protected void doBeforeClose() throws IOException {
80: file.seek(0);
81: }
82:
83: protected void doAfterClose() throws IOException {
84: file.close();
85: tempFile.delete();
86: tempFile = null;
87: file = null;
88: }
89: }
|