001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 2008.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.sail.rdbms.schema;
007:
008: import java.sql.PreparedStatement;
009: import java.sql.ResultSet;
010: import java.sql.SQLException;
011: import java.sql.Types;
012: import java.util.List;
013: import java.util.Map;
014:
015: /**
016: *
017: * @author James Leigh
018: */
019: public class HashTable {
020: private static final int CHUNK_SIZE = 15;
021: private ValueTable table;
022: private PreparedStatement select;
023:
024: public HashTable(ValueTable table) {
025: super ();
026: this .table = table;
027: }
028:
029: public String getName() {
030: return table.getName();
031: }
032:
033: public int getBatchSize() {
034: return table.getBatchSize();
035: }
036:
037: public int getSelectChunkSize() {
038: return CHUNK_SIZE;
039: }
040:
041: public void init() throws SQLException {
042: }
043:
044: public void close() throws SQLException {
045: if (select != null) {
046: select.close();
047: }
048: table.close();
049: }
050:
051: public List<Long> maxIds(int shift, int mod) throws SQLException {
052: return table.maxIds(shift, mod);
053: }
054:
055: public void insert(Number id, long hash) throws SQLException,
056: InterruptedException {
057: synchronized (table) {
058: HashBatch batch = (HashBatch) table.getValueBatch();
059: if (table.isExpired(batch)) {
060: batch = newHashBatch();
061: table.initBatch(batch);
062: }
063: batch.addBatch(id, hash);
064: table.queue(batch);
065: }
066: }
067:
068: public boolean expungeRemovedStatements(int count, String condition)
069: throws SQLException {
070: return table.expungeRemovedStatements(count, condition);
071: }
072:
073: public void optimize() throws SQLException {
074: table.optimize();
075: }
076:
077: public String toString() {
078: return table.toString();
079: }
080:
081: public Map<Long, Number> load(Map<Long, Number> hashes)
082: throws SQLException {
083: assert !hashes.isEmpty();
084: assert hashes.size() <= getSelectChunkSize();
085: if (select == null) {
086: StringBuilder sb = new StringBuilder();
087: sb.append("SELECT id, value\nFROM ").append(getName());
088: sb.append("\nWHERE value IN (");
089: for (int i = 0, n = getSelectChunkSize(); i < n; i++) {
090: sb.append("?,");
091: }
092: sb.setCharAt(sb.length() - 1, ')');
093: select = prepareSelect(sb.toString());
094: }
095: int p = 0;
096: for (Long hash : hashes.keySet()) {
097: select.setLong(++p, hash);
098: }
099: while (p < getSelectChunkSize()) {
100: select.setNull(++p, Types.BIGINT);
101: }
102: ResultSet rs = select.executeQuery();
103: try {
104: while (rs.next()) {
105: long id = rs.getLong(1);
106: long hash = rs.getLong(2);
107: hashes.put(hash, id);
108: }
109: } finally {
110: rs.close();
111: }
112: return hashes;
113: }
114:
115: protected HashBatch newHashBatch() {
116: return new HashBatch();
117: }
118:
119: protected PreparedStatement prepareSelect(String sql)
120: throws SQLException {
121: return table.getRdbmsTable().prepareStatement(sql);
122: }
123:
124: }
|