01: /*
02: * Copyright Aduna (http://www.aduna-software.com/) (c) 2008.
03: *
04: * Licensed under the Aduna BSD-style license.
05: */
06: package org.openrdf.sail.rdbms.schema;
07:
08: import java.sql.PreparedStatement;
09: import java.sql.SQLException;
10:
11: /**
12: *
13: * @author James Leigh
14: */
15: public class Batch {
16:
17: public static Batch CLOSED_SIGNAL = new Batch();
18:
19: private RdbmsTable temporary;
20:
21: private PreparedStatement insertBatch;
22:
23: private PreparedStatement insertSelect;
24:
25: private int maxBatchSize;
26:
27: private int batchCount;
28:
29: public int getMaxBatchSize() {
30: return maxBatchSize;
31: }
32:
33: public void setMaxBatchSize(int batchSize) {
34: this .maxBatchSize = batchSize;
35: }
36:
37: public void setTemporary(RdbmsTable temporary) {
38: assert temporary != null;
39: this .temporary = temporary;
40: }
41:
42: public void setBatchStatement(PreparedStatement insert) {
43: assert insert != null;
44: this .insertBatch = insert;
45: }
46:
47: public void setInsertStatement(PreparedStatement insert) {
48: assert insert != null;
49: this .insertSelect = insert;
50: }
51:
52: public int size() {
53: return batchCount;
54: }
55:
56: public boolean isFull() {
57: return batchCount >= getMaxBatchSize();
58: }
59:
60: public boolean isReady() {
61: return true;
62: }
63:
64: public void setObject(int parameterIndex, Object x)
65: throws SQLException {
66: insertBatch.setObject(parameterIndex, x);
67: }
68:
69: public void addBatch() throws SQLException {
70: insertBatch.addBatch();
71: batchCount++;
72: }
73:
74: public int flush() throws SQLException {
75: try {
76: int count;
77: if (temporary == null) {
78: int[] results = insertBatch.executeBatch();
79: count = results.length;
80: } else {
81: synchronized (temporary) {
82: insertBatch.executeBatch();
83: count = insertSelect.executeUpdate();
84: temporary.clear();
85: }
86: }
87: return count;
88: } finally {
89: insertBatch.close();
90: insertBatch = null;
91: }
92: }
93:
94: }
|