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.oracle;
18:
19: import java.io.IOException;
20: import java.io.InputStream;
21: import java.io.OutputStream;
22: import java.sql.PreparedStatement;
23: import java.sql.ResultSet;
24:
25: import org.apache.lucene.store.jdbc.index.FileJdbcIndexOutput;
26: import org.apache.lucene.store.jdbc.support.JdbcTemplate;
27:
28: /**
29: * A specialized Oracle version that works (through reflection) with
30: * Oracle 9i/8i specific blob API for blobs bigger than 4k.
31: *
32: * @author kimchy
33: */
34: public class OracleFileJdbcIndexOutput extends FileJdbcIndexOutput {
35:
36: public void close() throws IOException {
37: flush();
38: final long length = length();
39: doBeforeClose();
40: String sqlInsert = OracleIndexOutputHelper
41: .sqlInsert(jdbcDirectory.getTable());
42: jdbcDirectory.getJdbcTemplate().executeUpdate(sqlInsert,
43: new JdbcTemplate.PrepateStatementAwareCallback() {
44: public void fillPrepareStatement(
45: PreparedStatement ps) throws Exception {
46: ps.setFetchSize(1);
47: ps.setString(1, name);
48: ps.setLong(2, length);
49: ps.setBoolean(3, false);
50: }
51: });
52:
53: String sqlUpdate = OracleIndexOutputHelper
54: .sqlUpdate(jdbcDirectory.getTable());
55: jdbcDirectory.getJdbcTemplate().executeSelect(sqlUpdate,
56: new JdbcTemplate.ExecuteSelectCallback() {
57: public void fillPrepareStatement(
58: PreparedStatement ps) throws Exception {
59: ps.setFetchSize(1);
60: ps.setString(1, name);
61: }
62:
63: public Object execute(ResultSet rs)
64: throws Exception {
65: OutputStream os = null;
66: try {
67: rs.next();
68: os = OracleIndexOutputHelper
69: .getBlobOutputStream(rs);
70: InputStream is = openInputStream();
71: byte[] buffer = new byte[1000];
72: int bytesRead = 0;
73: while ((bytesRead = is.read(buffer)) != -1) {
74: os.write(buffer, 0, bytesRead);
75: }
76: return null;
77: } finally {
78: if (os != null) {
79: os.close();
80: }
81: }
82: }
83: });
84: doAfterClose();
85: }
86: }
|